Skip to content

Commit

Permalink
fix: properly apply file changes and version update
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Mar 29, 2022
1 parent bbea5f9 commit fa0b173
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/apply/apply-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ module.exports = [{
options.config.repoFiles,
options
),
when: ({ config: c }) => c.isForce || (c.needsUpdate && c.applyRepo),
when: ({ config: c }) => c.applyRepo && c.needsUpdate,
name: 'apply-repo',
}, {
run: (options) => run(
options.config.moduleDir,
options.config.moduleFiles,
options
),
when: ({ config: c }) => c.isForce || (c.needsUpdate && c.applyModule),
when: ({ config: c }) => c.applyModule && c.needsUpdate,
name: 'apply-module',
}]
26 changes: 26 additions & 0 deletions lib/apply/apply-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const log = require('proc-log')
const PackageJson = require('@npmcli/package-json')

const run = async ({ config: c }) => {
const {
moduleDir: dir,
__CONFIG_KEY__: key,
__VERSION__: version,
} = c

log.verbose('apply-version', dir)

const pkg = await PackageJson.load(dir)
if (!pkg.content[key]) {
pkg.content[key] = { version }
} else {
pkg.content[key].version = version
}
await pkg.save()
}

module.exports = {
run,
when: ({ config: c }) => c.needsUpdate && !c.isDogFood,
name: 'apply-version',
}
1 change: 1 addition & 0 deletions lib/apply/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ const run = require('../index.js')

module.exports = (root, content) => run(root, content, [
require('./apply-files.js'),
require('./apply-version.js'),
])
12 changes: 7 additions & 5 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const getConfig = async ({
const isRoot = root === path
const isLatest = version === LATEST_VERSION
const isDogFood = pkg.name === NAME
const isForce = process.argv.includes('--force')

// this is written to ci yml files so it needs to always use posix
const pkgRelPath = makePosix(relative(root, path))
Expand Down Expand Up @@ -111,16 +112,17 @@ const getConfig = async ({
pkgName: pkg.name,
pkgNameFs: pkg.name.replace(/\//g, '-').replace(/@/g, ''),
pkgRelPath: pkgRelPath,
// force changes if we are dogfooding this repo or with force argv
// XXX: setup proper cli arg parsing
isForce: isDogFood || process.argv.includes('--force'),
// booleans to control application of updates
isForce,
isDogFood,
isLatest,
needsUpdate: !isLatest,
// needs update if we are dogfooding this repo, with force argv, or its
// behind the current version
needsUpdate: isForce || isDogFood || !isLatest,
// templateoss specific values
__NAME__: NAME,
__CONFIG_KEY__: CONFIG_KEY,
__VERSION__: LATEST_VERSION,
__DOGFOOD__: isDogFood,
}

// merge the rest of base and pkg content to make the
Expand Down
2 changes: 1 addition & 1 deletion lib/content/pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"node": {{{json engines}}}
},
{{{json __CONFIG_KEY__}}}: {
"version": {{#if __DOGFOOD__}}{{{del}}}{{else}}{{{json __VERSION__}}}{{/if}}
"version": {{#if isDogFood}}{{{del}}}{{else}}{{{json __VERSION__}}}{{/if}}
},
"templateVersion": {{{del}}},
"standard": {{{del}}}
Expand Down
25 changes: 20 additions & 5 deletions tap-snapshots/test/check/diffs.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ source
package.json
========================================
{
"name": "testpkg"
"name": "testpkg",
"templateOSS": {
"version": "{{VERSION}}"
}
}
`

Expand Down Expand Up @@ -114,7 +117,10 @@ content/source.json
package.json
========================================
{
"name": "testpkg"
"name": "testpkg",
"templateOSS": {
"version": "{{VERSION}}"
}
}
target.json
Expand Down Expand Up @@ -163,7 +169,10 @@ content/source.json
package.json
========================================
{
"name": "testpkg"
"name": "testpkg",
"templateOSS": {
"version": "{{VERSION}}"
}
}
target.json
Expand Down Expand Up @@ -204,7 +213,10 @@ content/source.json
package.json
========================================
{
"name": "testpkg"
"name": "testpkg",
"templateOSS": {
"version": "{{VERSION}}"
}
}
target.json
Expand Down Expand Up @@ -276,7 +288,10 @@ source
package.json
========================================
{
"name": "testpkg"
"name": "testpkg",
"templateOSS": {
"version": "{{VERSION}}"
}
}
target.txt
Expand Down
9 changes: 7 additions & 2 deletions test/apply/full-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ t.test('workspaces + everything', async (t) => {
t.test('with empty content', async (t) => {
const s = await setup(t, { content: {} })
await s.apply()
t.strictSame(await s.readdirSource(), {
'package.json': JSON.stringify({ name: 'testpkg' }, null, 2),
const source = await s.readdirSource()
t.strictSame(Object.keys(source), ['package.json'])
t.strictSame(JSON.parse(source['package.json']), {
name: 'testpkg',
templateOSS: {
version: setup.pkgVersion,
},
})
})
1 change: 1 addition & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ const formatSnapshots = {
module.exports = setup
module.exports.git = setupGit
module.exports.content = CONTENT
module.exports.pkgVersion = VERSION
module.exports.clean = cleanSnapshot
module.exports.format = formatSnapshots
module.exports.okPackage = okPackage
Expand Down

0 comments on commit fa0b173

Please sign in to comment.