Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Aug 27, 2021
0 parents commit 2a60b5c
Show file tree
Hide file tree
Showing 19 changed files with 10,454 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { readdirSync: readdir } = require('fs')

const localConfigs = readdir(__dirname)
.filter((file) => file.startsWith('.eslintrc.local.'))
.map((file) => `./${file}`)

module.exports = {
extends: [
'@npmcli',
...localConfigs,
],
}
53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: CI

on:
pull_request:
push:
branches:
- main
- latest

jobs:
build:
strategy:
fail-fast: false
matrix:
node-version: [10.0.x, 10.x, 12.0.x, 12.x, 14.0.x, 14.x, 15.x, 16.x]
platform:
- os: ubuntu-latest
shell: bash
- os: macos-latest
shell: bash
- os: windows-latest
shell: bash
- os: windows-latest
shell: cmd
- os: windows-latest
shell: powershell

runs-on: ${{ matrix.platform.os }}
defaults:
run:
shell: ${{ matrix.platform.shell }}

steps:
- name: Checkout Repository
uses: actions/checkout@v1.1.0

- name: Use Nodejs ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: npm

- name: Update npm
run: npm i --prefer-online -g npm@latest

- name: Install dependencies
run: npm ci

- name: Run Tap Tests
run: npm test

- name: List dependencies
run: npm ls -a
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# ignore everything in the root
/*

# keep these
!/.eslintrc*
!/.github
!**/.gitignore
!/package.json
!/package-lock.json
!/bin
!/lib
!/map.js
!/tap-snapshots
!/test
!/README*
!/LICENSE*
18 changes: 18 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ISC License

Copyright npm, Inc.

Permission to use, copy, modify, and/or distribute this
software for any purpose with or without fee is hereby
granted, provided that the above copyright notice and this
permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND NPM DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
EVENT SHALL NPM BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
USE OR PERFORMANCE OF THIS SOFTWARE.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
## @npmcli/template-oss

This module bundles the npm CLI team's basics for package development into a
single devDependency.

**CAUTION: THESE CHANGES WILL OVERWRITE ANY LOCAL FILES AND SETTINGS**

### `package.json` patches

These fields will be set in the project's `package.json`:

```json
{
"author": "GitHub Inc.",
"files": ["bin", "lib"],
"license": "ISC",
"scripts": {
"lint": "eslint '**/*.js'",
"lintfix": "npm run lint -- --fix",
"preversion": "npm test",
"postversion": "npm publish",
"prepublishOnly": "git push origin --follow-tags",
"snap": "tap",
"test": "tap",
"posttest": "npm run lint",
}
}
```

### Static files

Any existing `.eslintrc.*` files will be removed, unless they also match the pattern `.eslintrc.local.*`

These files will be copied, overwriting any existing files:

- `.eslintrc.js`
- `.github/workflows/ci.yml`
- `.gitignore`
- `LICENSE.md`


### Package installation and removal

These packages will be removed:

- `eslint-plugin-import`
- `eslint-plugin-promise`
- `eslint-plugin-standard`
- `@npmcli/lint`


Afterwards, these packages will be installed as devDependencies:

- `eslint`
- `eslint-plugin-node`
- `@npmcli/eslint-config`
- `tap`
26 changes: 26 additions & 0 deletions bin/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env node

const { dirname } = require('path')

const copyContent = require('../lib/content/index.js')
const installPackages = require('../lib/install.js')
const patchPackage = require('../lib/package.js')

const main = async () => {
const pkgPath = process.env.npm_package_json
if (!pkgPath) {
throw new Error('This script must be run as an npm lifecycle event')
}

const root = dirname(pkgPath)

await patchPackage(root)
await copyContent(root)
return installPackages(root)
}

// we export the promise so it can be awaited in tests
module.exports = main().catch((err) => {
console.error(err.stack)
process.exitCode = 1
})
18 changes: 18 additions & 0 deletions lib/content/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ISC License

Copyright npm, Inc.

Permission to use, copy, modify, and/or distribute this
software for any purpose with or without fee is hereby
granted, provided that the above copyright notice and this
permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND NPM DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
EVENT SHALL NPM BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
USE OR PERFORMANCE OF THIS SOFTWARE.
53 changes: 53 additions & 0 deletions lib/content/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: CI

on:
pull_request:
push:
branches:
- main
- latest

jobs:
build:
strategy:
fail-fast: false
matrix:
node-version: [10.0.x, 10.x, 12.0.x, 12.x, 14.0.x, 14.x, 15.x, 16.x]
platform:
- os: ubuntu-latest
shell: bash
- os: macos-latest
shell: bash
- os: windows-latest
shell: bash
- os: windows-latest
shell: cmd
- os: windows-latest
shell: powershell

runs-on: ${{ matrix.platform.os }}
defaults:
run:
shell: ${{ matrix.platform.shell }}

steps:
- name: Checkout Repository
uses: actions/checkout@v1.1.0

- name: Use Nodejs ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: npm

- name: Update npm
run: npm i --prefer-online -g npm@latest

- name: Install dependencies
run: npm ci

- name: Run Tap Tests
run: npm test

- name: List dependencies
run: npm ls -a
12 changes: 12 additions & 0 deletions lib/content/eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { readdirSync: readdir } = require('fs')

const localConfigs = readdir(__dirname)
.filter((file) => file.startsWith('.eslintrc.local.'))
.map((file) => `./${file}`)

module.exports = {
extends: [
'@npmcli',
...localConfigs,
],
}
15 changes: 15 additions & 0 deletions lib/content/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ignore everything in the root
/*

# keep these
!/.github
!**/.gitignore
!/package.json
!/package-lock.json
!/bin
!/lib
!/map.js
!/tap-snapshots
!/test
!/README*
!/LICENSE*
41 changes: 41 additions & 0 deletions lib/content/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { dirname, join } = require('path')
const fs = require('@npmcli/fs')

// keys are destination paths in the target project
// values are paths to contents relative to this file
const content = {
'.eslintrc.js': './eslintrc.js',
'.github/workflows/ci.yml': './ci.yml',
'.gitignore': './gitignore',
'LICENSE.md': './LICENSE.md',
}

// given a root directory, copy all files in the content map
// after purging any non-local .eslintrc config files
const copyContent = async (root) => {
const contents = await fs.readdir(root)

for (const file of contents) {
// remove any eslint config files that aren't local to the project
if (file.startsWith('.eslintrc.') && !file.startsWith('.eslintrc.local.')) {
await fs.rm(join(root, file))
}
}

for (let [target, source] of Object.entries(content)) {
source = join(__dirname, source)
target = join(root, target)
// if the target is a subdirectory of the root, mkdirp it first
if (dirname(target) !== root) {
await fs.mkdir(dirname(target), {
owner: 'inherit',
recursive: true,
force: true,
})
}
await fs.copyFile(source, target, { owner: 'inherit' })
}
}
copyContent.content = content

module.exports = copyContent
31 changes: 31 additions & 0 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const spawn = require('@npmcli/promise-spawn')

const removeDeps = [
'eslint-plugin-import',
'eslint-plugin-promise',
'eslint-plugin-standard',
'@npmcli/lint',
]

const devDeps = [
'eslint',
'eslint-plugin-node',
'@npmcli/eslint-config',
'tap',
]

const npm = (root, args) => {
return spawn('npm', args, {
cwd: root,
stdioString: true,
})
}

const installPackages = async (root) => {
await npm(root, ['uninstall', ...removeDeps])
return npm(root, ['install', '--save-dev', ...devDeps])
}
installPackages.removeDeps = removeDeps
installPackages.devDeps = devDeps

module.exports = installPackages
28 changes: 28 additions & 0 deletions lib/package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const PackageJson = require('@npmcli/package-json')

const changes = {
author: 'GitHub Inc.',
files: ['bin', 'lib'],
license: 'ISC',
scripts: {
lint: `eslint '**/*.js'`,
lintfix: 'npm run lint -- --fix',
preversion: 'npm test',
postversion: 'npm publish',
prepublishOnly: 'git push origin --follow-tags',
snap: 'tap',
test: 'tap',
posttest: 'npm run lint',
},
}

const patchPackage = async (root) => {
const pkg = await PackageJson.load(root)

pkg.update(changes)

return pkg.save()
}
patchPackage.changes = changes

module.exports = patchPackage
Loading

0 comments on commit 2a60b5c

Please sign in to comment.