From 773ae3e7ee87b96aeb241cdde4c0c6b0f122a375 Mon Sep 17 00:00:00 2001 From: Gar Date: Mon, 22 Feb 2021 09:26:41 -0800 Subject: [PATCH] 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 --- lib/restart.js | 3 ++- lib/start.js | 3 ++- lib/stop.js | 3 ++- lib/test.js | 3 ++- lib/utils/lifecycle-cmd.js | 7 +++---- test/lib/test.js | 6 +++--- 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 41f9c3a756862..1462cf6051d0f 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 e978536500777..9fa076d5e35f5 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 fd43d08fc12ed..827d414d1384c 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 e224aa2de62c7..ea5914ea38a1a 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 40a90aa20b199..83a712cf40946 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} [-- ]`) - 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 9a44e4760a2a5..6f4a7395d732a 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 0eb342cee5012..9e45b15e40401 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')