Skip to content

Commit

Permalink
chore(refactor): clean up lifecycle-cmds
Browse files Browse the repository at this point in the history
This is a small incremental step in removing some of the complexity
surrounding the `npm` object in our code.

It moves that object one level up the chain of code, with the end goal
being that we will only require it once in the codebase, ultimately allowing
us to improve our tests.

I also changed the command that it calls to `run-script` because that is the
base command. `run` was an alias, and that is one more layer of hoops a
developer would have to jump through to find what file in `./lib` is even
being referenced here.

PR-URL: #2753
Credit: @wraithgar
Close: #2753
Reviewed-by: @isaacs
  • Loading branch information
wraithgar authored and isaacs committed Feb 22, 2021
1 parent 4e58274 commit 773ae3e
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
3 changes: 2 additions & 1 deletion lib/restart.js
@@ -1 +1,2 @@
module.exports = require('./utils/lifecycle-cmd.js')('restart')
const npm = require('./npm.js')
module.exports = require('./utils/lifecycle-cmd.js')(npm, 'restart')
3 changes: 2 additions & 1 deletion lib/start.js
@@ -1 +1,2 @@
module.exports = require('./utils/lifecycle-cmd.js')('start')
const npm = require('./npm.js')
module.exports = require('./utils/lifecycle-cmd.js')(npm, 'start')
3 changes: 2 additions & 1 deletion lib/stop.js
@@ -1 +1,2 @@
module.exports = require('./utils/lifecycle-cmd.js')('stop')
const npm = require('./npm.js')
module.exports = require('./utils/lifecycle-cmd.js')(npm, 'stop')
3 changes: 2 additions & 1 deletion lib/test.js
@@ -1,4 +1,5 @@
const testCmd = require('./utils/lifecycle-cmd.js')('test')
const npm = require('./npm.js')
const testCmd = require('./utils/lifecycle-cmd.js')(npm, 'test')
const { completion, usage } = testCmd
const cmd = (args, cb) => testCmd(args, er => {
if (er && er.code === 'ELIFECYCLE') {
Expand Down
7 changes: 3 additions & 4 deletions lib/utils/lifecycle-cmd.js
@@ -1,12 +1,11 @@
// The implementation of commands that are just "run a script"
// test, start, stop, restart

const npm = require('../npm.js')
const usageUtil = require('./usage.js')
const completion = require('./completion/none.js')

module.exports = stage => {
const cmd = (args, cb) => npm.commands.run([stage, ...args], cb)
module.exports = (npm, stage) => {
const cmd = (args, cb) => npm.commands['run-script']([stage, ...args], cb)
const usage = usageUtil(stage, `npm ${stage} [-- <args>]`)
const completion = require('./completion/none.js')
return Object.assign(cmd, { usage, completion })
}
6 changes: 3 additions & 3 deletions test/lib/test.js
Expand Up @@ -3,7 +3,7 @@ const requireInject = require('require-inject')
let RUN_ARGS = null
const npmock = {
commands: {
run: (args, cb) => {
'run-script': (args, cb) => {
RUN_ARGS = args
cb()
},
Expand All @@ -26,12 +26,12 @@ t.test('run a test', t => {
})
const otherErr = new Error('should see this')

npmock.commands.run = (args, cb) => cb(lcErr)
npmock.commands['run-script'] = (args, cb) => cb(lcErr)
test([], (er) => {
t.equal(er, 'Test failed. See above for more details.')
})

npmock.commands.run = (args, cb) => cb(otherErr)
npmock.commands['run-script'] = (args, cb) => cb(otherErr)
test([], (er) => {
t.match(er, { message: 'should see this' })
})
Expand Down
15 changes: 6 additions & 9 deletions test/lib/utils/lifecycle-cmd.js
@@ -1,15 +1,12 @@
const t = require('tap')
const requireInject = require('require-inject')
const lifecycleCmd = requireInject('../../../lib/utils/lifecycle-cmd.js', {
'../../../lib/npm.js': {
commands: {
run: (args, cb) => cb(null, 'called npm.commands.run'),
},
const lifecycleCmd = require('../../../lib/utils/lifecycle-cmd.js')
const npm = {
commands: {
'run-script': (args, cb) => cb(null, 'called npm.commands.run'),
},
})

}
t.test('create a lifecycle command', t => {
const cmd = lifecycleCmd('asdf')
const cmd = lifecycleCmd(npm, 'asdf')
t.equal(cmd.completion, require('../../../lib/utils/completion/none.js'), 'empty completion')
cmd(['some', 'args'], (er, result) => {
t.strictSame(result, 'called npm.commands.run')
Expand Down

0 comments on commit 773ae3e

Please sign in to comment.