Skip to content

Commit 1aafd77

Browse files
committed
fixes after applying to the npm cli
1 parent 4f51f43 commit 1aafd77

File tree

6 files changed

+100
-13
lines changed

6 files changed

+100
-13
lines changed

bin/npm-template-check.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ const main = async () => {
1717

1818
const problemSets = []
1919
for (const path of config.paths) {
20-
problemSets.push(await checkPackage(path))
21-
problemSets.push(await checkGitIgnore(path))
20+
if (path !== root || config.applyRootModuleFiles) {
21+
problemSets.push(await checkPackage(path))
22+
}
23+
if (path !== root || config.applyRootRepoFiles) {
24+
problemSets.push(await checkGitIgnore(path))
25+
}
2226
}
2327

2428
const problems = problemSets.flat()

bin/postinstall.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const main = async () => {
1717

1818
const config = await getConfig(root)
1919
for (const path of config.paths) {
20-
if (!await patchPackage(path)) {
20+
if (!await patchPackage(path, root, config)) {
2121
continue
2222
}
2323

lib/postinstall/copy-content.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ const copyContent = async (path, rootPath, config) => {
9898
{ encoding: 'utf-8' }
9999
)
100100

101-
workflowData = workflowData.replace(/%%pkgpath%%/g, path)
101+
const relPath = path.substring(rootPath.length + 1)
102+
workflowData = workflowData.replace(/%%pkgpath%%/g, relPath)
102103
workflowData = workflowData.replace(/%%pkgname%%/g, workspaceName)
103104

104105
await fs.writeFile(join(workflowPath, `ci-${workspaceName}.yml`), workflowData)

lib/postinstall/update-package.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const changes = {
2626
},
2727
}
2828

29-
const patchPackage = async (root) => {
30-
const pkg = await PackageJson.load(root)
29+
const patchPackage = async (path, root, config) => {
30+
const pkg = await PackageJson.load(path)
3131

3232
// If we are running this on itself, we always run the script.
3333
// We also don't set templateVersion in package.json because
@@ -42,13 +42,22 @@ const patchPackage = async (root) => {
4242
return false
4343
}
4444

45-
// we build a new object here so our exported set of changes is not modified
46-
const update = {
47-
...changes,
48-
scripts: {
49-
...pkg.content.scripts,
50-
...changes.scripts,
51-
},
45+
let update
46+
47+
if (path === root && !config.applyRootModuleFiles) {
48+
// only update templateVersion if we're skipping root module files
49+
update = {
50+
templateVersion: TEMPLATE_VERSION,
51+
}
52+
} else {
53+
// we build a new object here so our exported set of changes is not modified
54+
update = {
55+
...changes,
56+
scripts: {
57+
...pkg.content.scripts,
58+
...changes.scripts,
59+
},
60+
}
5261
}
5362

5463
if (isDogfood) {

test/bin/npm-template-check.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,41 @@ t.test('with mocks', (t) => {
8181
t.strictSame(errors, [], 'errors')
8282
})
8383
})
84+
85+
t.test('workspace without root module files', async (t) => {
86+
const pkgWithWorkspaces = {
87+
'package.json': JSON.stringify({
88+
name: 'testpkg',
89+
templateOSS: {
90+
applyRootRepoFiles: false,
91+
applyWorkspaceRepoFiles: true,
92+
applyRootModuleFiles: false,
93+
94+
workspaces: ['amazinga'],
95+
},
96+
}),
97+
workspace: {
98+
a: {
99+
'package.json': JSON.stringify({
100+
name: 'amazinga',
101+
}),
102+
},
103+
},
104+
}
105+
const root = t.testdir(pkgWithWorkspaces)
106+
process.env.npm_config_local_prefix = root
107+
108+
await check({
109+
package: (path, root, config) => [{
110+
message: 'package',
111+
solution: `${path} ${root} ${config.applyRootModuleFiles}`,
112+
}],
113+
gitignore: (path, root, config) => [{
114+
message: 'gitignore',
115+
solution: `${path} ${root} ${config.applyRootRepoFiles}`,
116+
}],
117+
})
118+
119+
t.strictSame(logs, [], 'logs')
120+
t.strictSame(errors, [], 'errors')
121+
})

test/postinstall/update-package.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,38 @@ t.test('doesnt set templateVersion on own repo', async (t) => {
7676
})
7777
t.equal(JSON.parse(contents).templateVersion, undefined, 'did not get template version')
7878
})
79+
80+
t.test('only sets templateVersion on root pkg when configured', async (t) => {
81+
const pkgWithWorkspaces = {
82+
'package.json': JSON.stringify({
83+
name: 'testpkg',
84+
templateOSS: {
85+
applyRootRepoFiles: false,
86+
applyWorkspaceRepoFiles: true,
87+
applyRootModuleFiles: false,
88+
89+
workspaces: ['amazinga'],
90+
},
91+
}),
92+
workspace: {
93+
a: {
94+
'package.json': JSON.stringify({
95+
name: 'amazinga',
96+
}),
97+
},
98+
},
99+
}
100+
const root = t.testdir(pkgWithWorkspaces)
101+
await patchPackage(root, root, {
102+
applyRootRepoFiles: false,
103+
applyWorkspaceRepoFiles: true,
104+
applyRootModuleFiles: false,
105+
})
106+
107+
const contents = JSON.parse(await fs.readFile(join(root, 'package.json'), {
108+
encoding: 'utf8',
109+
}))
110+
111+
t.not(contents.templateVersion, undefined, 'should set templateVersion')
112+
t.equal(contents.author, undefined, 'should not set other fields')
113+
})

0 commit comments

Comments
 (0)