Skip to content

Commit

Permalink
feat: allow option to set npm bin in package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Apr 15, 2022
1 parent 2f7dcfa commit 32f7f7c
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 7 deletions.
20 changes: 17 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { relative, dirname, posix, win32 } = require('path')
const { relative, dirname, join, posix, win32 } = require('path')
const log = require('proc-log')
const { uniq, defaults } = require('lodash')
const parseCIVersions = require('./util/parse-ci-versions.js')
Expand Down Expand Up @@ -50,13 +50,14 @@ const getConfig = async ({
workspaceRepo,
workspaceModule,
version,
...pkgContent
...pkgConfig // this includes config merged in from root
},
}) => {
const isRoot = root === path
const isLatest = version === LATEST_VERSION
const isDogFood = pkg.name === NAME
const isForce = process.argv.includes('--force')
const rawPkgConfig = getPkgConfig(pkg)

// this is written to ci yml files so it needs to always use posix
const pkgRelPath = makePosix(relative(root, path))
Expand Down Expand Up @@ -128,12 +129,25 @@ const getConfig = async ({

// merge the rest of base and pkg content to make the
// full content object
const content = { ...baseContent, ...pkgContent }
const content = { ...baseContent, ...pkgConfig }

// set some defaults on content that can be overwritten unlike
// derived values which are calculated from other config
const contentDefaults = {}

if (content.npmBin && content.npmBin !== baseContent.npmBin) {
// make it relative to each workspace if they did not set the config themselves
if (!rawPkgConfig.npmBin) {
content.npmBin = makePosix(join(relative(path, root), content.npmBin))
}
// a bit of a hack but allow custom node paths or no node path at all
// checks if the first thing has node somewhere in it and if it doesnt
// puts a system node in front of the script
const execPaths = content.npmBin.split(' ')[0].split(posix.sep)
if (execPaths.every(p => p !== 'node')) {
content.npmBin = `node ${content.npmBin}`
}
}
if (Array.isArray(content.ciVersions)) {
const parsed = parseCIVersions(content.ciVersions)
contentDefaults.engines = parsed.engines
Expand Down
1 change: 1 addition & 0 deletions lib/content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module.exports = {
distPaths: ['bin/', 'lib/'],
ciVersions: ['12.13.0', '12.x', '14.15.0', '14.x', '16.0.0', '16.x'],
lockfile: false,
npmBin: 'npm',
unwantedPackages: [
'eslint',
'eslint-plugin-node',
Expand Down
8 changes: 4 additions & 4 deletions lib/content/pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"lint": "eslint \"**/*.js\"",
"postlint": "template-oss-check",
"template-oss-apply": "template-oss-apply --force",
"lintfix": "npm run lint -- --fix",
"preversion": "npm test",
"lintfix": "{{npmBin}} run lint -- --fix",
"preversion": "{{npmBin}} test",
{{#if pkgPrivate}}
"postversion": "git push origin --follow-tags",
{{else}}
"postversion": "npm publish",
"postversion": "{{npmBin}} publish",
"prepublishOnly": "git push origin --follow-tags",
{{/if}}
"snap": "tap",
"test": "tap",
"posttest": "npm run lint",
"posttest": "{{npmBin}} run lint",
"template-copy": {{{del}}},
"lint:fix": {{{del}}}
},
Expand Down
104 changes: 104 additions & 0 deletions test/apply/npm-bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const t = require('tap')
const { join } = require('path')
const setup = require('../setup.js')

t.test('custom node', async (t) => {
const s = await setup(t, {
ok: true,
package: {
templateOSS: {
npmBin: 'node /path/to/npm',
},
},
})
await s.apply()
const { scripts } = await s.readJson('package.json')
t.equal(scripts.posttest, 'node /path/to/npm run lint')
})

t.test('custom node', async (t) => {
const s = await setup(t, {
ok: true,
package: {
templateOSS: {
npmBin: 'node /path/to/npm',
},
},
})
await s.apply()
const { scripts } = await s.readJson('package.json')
t.equal(scripts.posttest, 'node /path/to/npm run lint')
})

t.test('relative npm bin with workspaces', async (t) => {
const s = await setup(t, {
ok: true,
package: {
templateOSS: {
npmBin: 'cli.js',
},
},
workspaces: { a: '@name/aaaa', b: 'bbb' },
})
await s.apply()
const { scripts } = await s.readJson('package.json')
const { scripts: scriptsA } = await s.readJson(join(s.workspaces.a, 'package.json'))
const { scripts: scriptsB } = await s.readJson(join(s.workspaces.b, 'package.json'))
t.equal(scripts.posttest, 'node cli.js run lint')
t.equal(scriptsA.posttest, 'node ../../cli.js run lint')
t.equal(scriptsB.posttest, 'node ../../cli.js run lint')
})

t.test('npm bin workspaces only with root config', async (t) => {
const s = await setup(t, {
ok: true,
package: {
templateOSS: {
rootRepo: false,
rootModule: false,
npmBin: './cli.js',
},
},
workspaces: { a: '@name/aaaa', b: 'bbb' },
})
await s.apply()
const { scripts } = await s.readJson('package.json')
const { scripts: scriptsA } = await s.readJson(join(s.workspaces.a, 'package.json'))
const { scripts: scriptsB } = await s.readJson(join(s.workspaces.b, 'package.json'))
t.equal(scripts, undefined)
t.equal(scriptsA.posttest, 'node ../../cli.js run lint')
t.equal(scriptsB.posttest, 'node ../../cli.js run lint')
})

t.test('separate workspace configs', async (t) => {
const s = await setup(t, {
ok: true,
package: {
templateOSS: {
rootRepo: false,
rootModule: false,
},
},
workspaces: {
a: {
name: 'a',
templateOSS: {
npmBin: 'bin_a.js',
},
},
b: {
name: 'b',
templateOSS: {
npmBin: 'bin_b.js',
},
},
},
})
await s.apply()
const { scripts } = await s.readJson('package.json')
const { scripts: scriptsA } = await s.readJson(join(s.workspaces.a, 'package.json'))
const { scripts: scriptsB } = await s.readJson(join(s.workspaces.b, 'package.json'))
t.equal(scripts, undefined)
t.equal(scriptsA.posttest, 'node bin_a.js run lint')
t.equal(scriptsB.posttest, 'node bin_b.js run lint')
})

0 comments on commit 32f7f7c

Please sign in to comment.