diff options
author | Gar <gar+gh@danger.computer> | 2021-02-22 20:26:41 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2021-02-22 23:30:20 +0300 |
commit | 773ae3e7ee87b96aeb241cdde4c0c6b0f122a375 (patch) | |
tree | 3db8989736f8366f30168abe063f5ed4aebd5d79 | |
parent | 4e58274ed0fd2dd29d3c8d6c7c47f37a37dc0f0f (diff) |
chore(refactor): clean up lifecycle-cmds
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: https://github.com/npm/cli/pull/2753
Credit: @wraithgar
Close: #2753
Reviewed-by: @isaacs
-rw-r--r-- | lib/restart.js | 3 | ||||
-rw-r--r-- | lib/start.js | 3 | ||||
-rw-r--r-- | lib/stop.js | 3 | ||||
-rw-r--r-- | lib/test.js | 3 | ||||
-rw-r--r-- | lib/utils/lifecycle-cmd.js | 7 | ||||
-rw-r--r-- | test/lib/test.js | 6 | ||||
-rw-r--r-- | test/lib/utils/lifecycle-cmd.js | 15 |
7 files changed, 20 insertions, 20 deletions
diff --git a/lib/restart.js b/lib/restart.js index 41f9c3a75..1462cf605 100644 --- a/lib/restart.js +++ b/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') diff --git a/lib/start.js b/lib/start.js index e97853650..9fa076d5e 100644 --- a/lib/start.js +++ b/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') diff --git a/lib/stop.js b/lib/stop.js index fd43d08fc..827d414d1 100644 --- a/lib/stop.js +++ b/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') diff --git a/lib/test.js b/lib/test.js index e224aa2de..ea5914ea3 100644 --- a/lib/test.js +++ b/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') { diff --git a/lib/utils/lifecycle-cmd.js b/lib/utils/lifecycle-cmd.js index 40a90aa20..83a712cf4 100644 --- a/lib/utils/lifecycle-cmd.js +++ b/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 }) } diff --git a/test/lib/test.js b/test/lib/test.js index 9a44e4760..6f4a7395d 100644 --- a/test/lib/test.js +++ b/test/lib/test.js @@ -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() }, @@ -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' }) }) diff --git a/test/lib/utils/lifecycle-cmd.js b/test/lib/utils/lifecycle-cmd.js index 0eb342cee..9e45b15e4 100644 --- a/test/lib/utils/lifecycle-cmd.js +++ b/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') |