Skip to content

Commit fa0b173

Browse files
committed
fix: properly apply file changes and version update
1 parent bbea5f9 commit fa0b173

File tree

8 files changed

+65
-15
lines changed

8 files changed

+65
-15
lines changed

lib/apply/apply-files.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ module.exports = [{
1818
options.config.repoFiles,
1919
options
2020
),
21-
when: ({ config: c }) => c.isForce || (c.needsUpdate && c.applyRepo),
21+
when: ({ config: c }) => c.applyRepo && c.needsUpdate,
2222
name: 'apply-repo',
2323
}, {
2424
run: (options) => run(
2525
options.config.moduleDir,
2626
options.config.moduleFiles,
2727
options
2828
),
29-
when: ({ config: c }) => c.isForce || (c.needsUpdate && c.applyModule),
29+
when: ({ config: c }) => c.applyModule && c.needsUpdate,
3030
name: 'apply-module',
3131
}]

lib/apply/apply-version.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const log = require('proc-log')
2+
const PackageJson = require('@npmcli/package-json')
3+
4+
const run = async ({ config: c }) => {
5+
const {
6+
moduleDir: dir,
7+
__CONFIG_KEY__: key,
8+
__VERSION__: version,
9+
} = c
10+
11+
log.verbose('apply-version', dir)
12+
13+
const pkg = await PackageJson.load(dir)
14+
if (!pkg.content[key]) {
15+
pkg.content[key] = { version }
16+
} else {
17+
pkg.content[key].version = version
18+
}
19+
await pkg.save()
20+
}
21+
22+
module.exports = {
23+
run,
24+
when: ({ config: c }) => c.needsUpdate && !c.isDogFood,
25+
name: 'apply-version',
26+
}

lib/apply/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ const run = require('../index.js')
22

33
module.exports = (root, content) => run(root, content, [
44
require('./apply-files.js'),
5+
require('./apply-version.js'),
56
])

lib/config.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const getConfig = async ({
5656
const isRoot = root === path
5757
const isLatest = version === LATEST_VERSION
5858
const isDogFood = pkg.name === NAME
59+
const isForce = process.argv.includes('--force')
5960

6061
// this is written to ci yml files so it needs to always use posix
6162
const pkgRelPath = makePosix(relative(root, path))
@@ -111,16 +112,17 @@ const getConfig = async ({
111112
pkgName: pkg.name,
112113
pkgNameFs: pkg.name.replace(/\//g, '-').replace(/@/g, ''),
113114
pkgRelPath: pkgRelPath,
114-
// force changes if we are dogfooding this repo or with force argv
115-
// XXX: setup proper cli arg parsing
116-
isForce: isDogFood || process.argv.includes('--force'),
115+
// booleans to control application of updates
116+
isForce,
117+
isDogFood,
117118
isLatest,
118-
needsUpdate: !isLatest,
119+
// needs update if we are dogfooding this repo, with force argv, or its
120+
// behind the current version
121+
needsUpdate: isForce || isDogFood || !isLatest,
119122
// templateoss specific values
120123
__NAME__: NAME,
121124
__CONFIG_KEY__: CONFIG_KEY,
122125
__VERSION__: LATEST_VERSION,
123-
__DOGFOOD__: isDogFood,
124126
}
125127

126128
// merge the rest of base and pkg content to make the

lib/content/pkg.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"node": {{{json engines}}}
2121
},
2222
{{{json __CONFIG_KEY__}}}: {
23-
"version": {{#if __DOGFOOD__}}{{{del}}}{{else}}{{{json __VERSION__}}}{{/if}}
23+
"version": {{#if isDogFood}}{{{del}}}{{else}}{{{json __VERSION__}}}{{/if}}
2424
},
2525
"templateVersion": {{{del}}},
2626
"standard": {{{del}}}

tap-snapshots/test/check/diffs.js.test.cjs

+20-5
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ source
7171
package.json
7272
========================================
7373
{
74-
"name": "testpkg"
74+
"name": "testpkg",
75+
"templateOSS": {
76+
"version": "{{VERSION}}"
77+
}
7578
}
7679
`
7780

@@ -114,7 +117,10 @@ content/source.json
114117
package.json
115118
========================================
116119
{
117-
"name": "testpkg"
120+
"name": "testpkg",
121+
"templateOSS": {
122+
"version": "{{VERSION}}"
123+
}
118124
}
119125
120126
target.json
@@ -163,7 +169,10 @@ content/source.json
163169
package.json
164170
========================================
165171
{
166-
"name": "testpkg"
172+
"name": "testpkg",
173+
"templateOSS": {
174+
"version": "{{VERSION}}"
175+
}
167176
}
168177
169178
target.json
@@ -204,7 +213,10 @@ content/source.json
204213
package.json
205214
========================================
206215
{
207-
"name": "testpkg"
216+
"name": "testpkg",
217+
"templateOSS": {
218+
"version": "{{VERSION}}"
219+
}
208220
}
209221
210222
target.json
@@ -276,7 +288,10 @@ source
276288
package.json
277289
========================================
278290
{
279-
"name": "testpkg"
291+
"name": "testpkg",
292+
"templateOSS": {
293+
"version": "{{VERSION}}"
294+
}
280295
}
281296
282297
target.txt

test/apply/full-content.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ t.test('workspaces + everything', async (t) => {
3737
t.test('with empty content', async (t) => {
3838
const s = await setup(t, { content: {} })
3939
await s.apply()
40-
t.strictSame(await s.readdirSource(), {
41-
'package.json': JSON.stringify({ name: 'testpkg' }, null, 2),
40+
const source = await s.readdirSource()
41+
t.strictSame(Object.keys(source), ['package.json'])
42+
t.strictSame(JSON.parse(source['package.json']), {
43+
name: 'testpkg',
44+
templateOSS: {
45+
version: setup.pkgVersion,
46+
},
4247
})
4348
})

test/setup.js

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ const formatSnapshots = {
176176
module.exports = setup
177177
module.exports.git = setupGit
178178
module.exports.content = CONTENT
179+
module.exports.pkgVersion = VERSION
179180
module.exports.clean = cleanSnapshot
180181
module.exports.format = formatSnapshots
181182
module.exports.okPackage = okPackage

0 commit comments

Comments
 (0)