Skip to content

Commit a5b17fd

Browse files
authored
feat: add npm-template-copy bin (#37)
1 parent 194c434 commit a5b17fd

File tree

6 files changed

+48
-6
lines changed

6 files changed

+48
-6
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ These fields will be set in the project's `package.json`:
4141
lint: `eslint '**/*.js'`,
4242
postlint: 'npm-template-check',
4343
lintfix: 'npm run lint -- --fix',
44+
'template-copy': 'npm-template-copy --force',
4445
preversion: 'npm test',
4546
postversion: 'npm publish',
4647
prepublishOnly: 'git push origin --follow-tags',
@@ -99,6 +100,11 @@ your new file. The object keys are destination paths, and values are source.
99100
`npm-template-check` is run by `postlint` and will error if the `package.json`
100101
is not configured properly, with steps to run to correct any problems.
101102
103+
### Manual copy
104+
105+
Template files will be copied automatically when `template-oss` is updated.
106+
You can force an update with `npm run template-copy`.
107+
102108
#### Extending
103109
104110
Add any unwanted packages to `unwantedPackages` in `lib/check.js`. Currently

lib/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const defaultConfig = {
77
applyRootModuleFiles: true,
88
workspaces: [],
99
paths: [],
10+
force: false,
1011
}
1112

1213
module.exports = async (root) => {
@@ -44,5 +45,7 @@ module.exports = async (root) => {
4445

4546
config.paths.push(root)
4647

48+
config.force = process.argv.indexOf('--force') !== -1
49+
4750
return config
4851
}

lib/postinstall/update-package.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const changes = {
1111
scripts: {
1212
lint: `eslint '**/*.js'`,
1313
postlint: 'npm-template-check',
14+
'template-copy': 'npm-template-copy --force',
1415
lintfix: 'npm run lint -- --fix',
1516
preversion: 'npm test',
1617
postversion: 'npm publish',
@@ -26,6 +27,7 @@ const changes = {
2627

2728
const patchPackage = async (path, root, config) => {
2829
const pkg = await PackageJson.load(path)
30+
config = config || {}
2931

3032
// If we are running this on itself, we always run the script.
3133
// We also don't set templateVersion in package.json because
@@ -38,7 +40,9 @@ const patchPackage = async (path, root, config) => {
3840
// if the target package.json has a templateVersion field matching our own
3941
// current version, we return false here so the postinstall script knows to
4042
// exit early instead of running everything again
41-
if (currentVersion === TEMPLATE_VERSION && !isDogfood) {
43+
if (!config.force
44+
&& currentVersion === TEMPLATE_VERSION
45+
&& !isDogfood) {
4246
return false
4347
}
4448

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "templated files used in npm CLI team oss projects",
55
"main": "lib/index.js",
66
"bin": {
7-
"npm-template-check": "bin/npm-template-check.js"
7+
"npm-template-check": "bin/npm-template-check.js",
8+
"npm-template-copy": "bin/postinstall.js"
89
},
910
"scripts": {
1011
"lint": "eslint '**/*.js'",
@@ -16,7 +17,8 @@
1617
"prepublishOnly": "git push origin --follow-tags",
1718
"preversion": "npm test",
1819
"snap": "tap",
19-
"test": "tap"
20+
"test": "tap",
21+
"template-copy": "npm-template-copy --force"
2022
},
2123
"repository": {
2224
"type": "git",

tap-snapshots/test/postlint/check-package.js.test.cjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Array [
1212
The following package.json fields are incorrect:
1313
Field: "author" Expected: "GitHub Inc." Found: "Bob"
1414
Field: "files" Expected: ["bin","lib"] Found: undefined
15-
Field: "scripts" Expected: {"lint":"eslint '**/*.js'","postlint":"npm-template-check","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"} Found: undefined
15+
Field: "scripts" Expected: {"lint":"eslint '**/*.js'","postlint":"npm-template-check","template-copy":"npm-template-copy --force","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"} Found: undefined
1616
Field: "engines" Expected: {"node":"^12.13.0 || ^14.15.0 || >=16"} Found: undefined
1717
),
1818
"solution": "npm rm @npmcli/template-oss && npm i -D @npmcli/template-oss",
@@ -29,6 +29,7 @@ Array [
2929
Field: "files" Expected: ["bin","lib"] Found: undefined
3030
Field: "scripts.lint" Expected: "eslint '**/*.js'" Found: undefined
3131
Field: "scripts.postlint" Expected: "npm-template-check" Found: undefined
32+
Field: "scripts.template-copy" Expected: "npm-template-copy --force" Found: undefined
3233
Field: "scripts.lintfix" Expected: "npm run lint -- --fix" Found: undefined
3334
Field: "scripts.preversion" Expected: "npm test" Found: undefined
3435
Field: "scripts.postversion" Expected: "npm publish" Found: undefined
@@ -50,7 +51,7 @@ Array [
5051
The following package.json fields are incorrect:
5152
Field: "author" Expected: "GitHub Inc." Found: undefined
5253
Field: "files" Expected: ["bin","lib"] Found: undefined
53-
Field: "scripts" Expected: {"lint":"eslint '**/*.js'","postlint":"npm-template-check","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"} Found: undefined
54+
Field: "scripts" Expected: {"lint":"eslint '**/*.js'","postlint":"npm-template-check","template-copy":"npm-template-copy --force","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"} Found: undefined
5455
Field: "engines" Expected: {"node":"^12.13.0 || ^14.15.0 || >=16"} Found: undefined
5556
),
5657
"solution": "npm rm @npmcli/template-oss && npm i -D @npmcli/template-oss",
@@ -65,7 +66,7 @@ Array [
6566
The following package.json fields are incorrect:
6667
Field: "author" Expected: "GitHub Inc." Found: undefined
6768
Field: "files" Expected: ["bin","lib"] Found: undefined
68-
Field: "scripts" Expected: {"lint":"eslint '**/*.js'","postlint":"npm-template-check","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"} Found: undefined
69+
Field: "scripts" Expected: {"lint":"eslint '**/*.js'","postlint":"npm-template-check","template-copy":"npm-template-copy --force","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"} Found: undefined
6970
Field: "engines" Expected: {"node":"^12.13.0 || ^14.15.0 || >=16"} Found: undefined
7071
),
7172
"solution": "npm rm @npmcli/template-oss && npm i -D @npmcli/template-oss",

test/postinstall/update-package.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,32 @@ t.test('returns false when templateVersion matches own version', async (t) => {
6161
t.notMatch(JSON.parse(contents), patchPackage.changes, 'changes were NOT applied')
6262
})
6363

64+
t.test('returns true when templateVersion matches own version when forced', async (t) => {
65+
const pkg = {
66+
name: '@npmcli/foo',
67+
templateOSS: {
68+
version: TEMPLATE_VERSION,
69+
},
70+
version: '1.0.0',
71+
author: 'someone else',
72+
files: [],
73+
license: 'MIT',
74+
}
75+
76+
const project = t.testdir({
77+
'package.json': JSON.stringify(pkg, null, 2),
78+
})
79+
80+
const needsAction = await patchPackage(project, undefined, { force: true })
81+
t.equal(needsAction, true, 'returned true')
82+
83+
const contents = await fs.readFile(join(project, 'package.json'), {
84+
encoding: 'utf8',
85+
})
86+
const parsed = JSON.parse(contents)
87+
t.match(parsed, patchPackage.changes, 'all changes were applied')
88+
})
89+
6490
t.test('doesnt set templateVersion on own repo', async (t) => {
6591
const pkg = {
6692
name: TEMPLATE_NAME,

0 commit comments

Comments
 (0)