Skip to content

Commit 28019d5

Browse files
committed
fix(cleanup): move cli specific files to separate dir
1 parent 469f788 commit 28019d5

18 files changed

+70
-70
lines changed

.eslintrc.local.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ module.exports = {
2525
overrides: es6Files({
2626
'index.js': ['lib/cli.js'],
2727
'bin/npm-cli.js': ['lib/cli.js'],
28-
'lib/cli.js': ['lib/es6/validate-engines.js'],
29-
'lib/es6/validate-engines.js': ['package.json'],
28+
'lib/cli.js': ['lib/cli/validate-engines.js'],
29+
'lib/cli/validate-engines.js': ['package.json'],
3030
// TODO: This file should also have its requires restricted as well since it
3131
// is an entry point but it currently pulls in config definitions which have
3232
// a large require graph, so that is not currently feasible. A future config

lib/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const validateEngines = require('./es6/validate-engines.js')
2-
const cliEntry = require('node:path').resolve(__dirname, 'cli-entry.js')
1+
const validateEngines = require('./cli/validate-engines.js')
2+
const cliEntry = require('node:path').resolve(__dirname, 'cli/entry.js')
33

44
module.exports = (process) => validateEngines(process, () => require(cliEntry))

lib/cli-entry.js renamed to lib/cli/entry.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ module.exports = async (process, validateEngines) => {
1111
process.argv.splice(1, 1, 'npm', '-g')
1212
}
1313

14+
// Patch the global fs module here at the app level
15+
require('graceful-fs').gracefulify(require('fs'))
16+
1417
const satisfies = require('semver/functions/satisfies')
15-
const exitHandler = require('./utils/exit-handler.js')
16-
const Npm = require('./npm.js')
18+
const exitHandler = require('./exit-handler.js')
19+
const Npm = require('../npm.js')
1720
const npm = new Npm()
1821
exitHandler.setNpm(npm)
1922

@@ -58,11 +61,29 @@ module.exports = async (process, validateEngines) => {
5861
return exitHandler()
5962
}
6063

64+
// this is async but we dont await it, since its ok if it doesnt
65+
// finish before the command finishes running. it uses command and argv
66+
// so it must be initiated here, after the command name is set
67+
const updateNotifier = require('./update-notifier.js')
68+
// eslint-disable-next-line promise/catch-or-return
69+
updateNotifier(npm).then((msg) => (npm.updateNotification = msg))
70+
71+
// Options are prefixed by a hyphen-minus (-, \u2d).
72+
// Other dash-type chars look similar but are invalid.
73+
const nonDashArgs = npm.argv.filter(a => /^[\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]/.test(a))
74+
if (nonDashArgs.length) {
75+
log.error(
76+
'arg',
77+
'Argument starts with non-ascii dash, this is probably invalid:',
78+
nonDashArgs.join(', ')
79+
)
80+
}
81+
6182
await npm.exec(cmd)
6283
return exitHandler()
6384
} catch (err) {
6485
if (err.code === 'EUNKNOWNCOMMAND') {
65-
const didYouMean = require('./utils/did-you-mean.js')
86+
const didYouMean = require('../utils/did-you-mean.js')
6687
const suggestions = await didYouMean(npm.localPrefix, cmd)
6788
output.standard(`Unknown command: "${cmd}"${suggestions}\n`)
6889
output.standard('To see a list of supported npm commands, run:\n npm help')

lib/utils/exit-handler.js renamed to lib/cli/exit-handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { log, output, time } = require('proc-log')
2-
const errorMessage = require('./error-message.js')
2+
const errorMessage = require('../utils/error-message.js')
33
const { redactLog: replaceInfo } = require('@npmcli/redact')
44

55
let npm = null // set by the cli
File renamed without changes.
File renamed without changes.

lib/npm.js

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@ const { resolve, dirname, join } = require('node:path')
22
const Config = require('@npmcli/config')
33
const which = require('which')
44
const fs = require('node:fs/promises')
5-
6-
// Patch the global fs module here at the app level
7-
require('graceful-fs').gracefulify(require('fs'))
8-
95
const { definitions, flatten, shorthands } = require('@npmcli/config/lib/definitions')
106
const usage = require('./utils/npm-usage.js')
117
const LogFile = require('./utils/log-file.js')
128
const Timers = require('./utils/timers.js')
139
const Display = require('./utils/display.js')
1410
const { log, time } = require('proc-log')
1511
const { redactLog: replaceInfo } = require('@npmcli/redact')
16-
const updateNotifier = require('./utils/update-notifier.js')
1712
const pkg = require('../package.json')
1813
const { deref } = require('./utils/cmd-list.js')
1914

@@ -42,7 +37,6 @@ class Npm {
4237
#title = 'npm'
4338
#argvClean = []
4439
#npmRoot = null
45-
#warnedNonDashArg = false
4640

4741
#chalk = null
4842
#logChalk = null
@@ -109,30 +103,7 @@ class Npm {
109103
// passed in directly.
110104
async exec (cmd, args = this.argv) {
111105
const command = this.setCmd(cmd)
112-
113-
const timeEnd = time.start(`command:${cmd}`)
114-
115-
// this is async but we dont await it, since its ok if it doesnt
116-
// finish before the command finishes running. it uses command and argv
117-
// so it must be initiated here, after the command name is set
118-
// eslint-disable-next-line promise/catch-or-return
119-
updateNotifier(this).then((msg) => (this.updateNotification = msg))
120-
121-
// Options are prefixed by a hyphen-minus (-, \u2d).
122-
// Other dash-type chars look similar but are invalid.
123-
if (!this.#warnedNonDashArg) {
124-
const nonDashArgs = args.filter(a => /^[\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]/.test(a))
125-
if (nonDashArgs.length) {
126-
this.#warnedNonDashArg = true
127-
log.error(
128-
'arg',
129-
'Argument starts with non-ascii dash, this is probably invalid:',
130-
nonDashArgs.join(', ')
131-
)
132-
}
133-
}
134-
135-
return command.cmdExec(args).finally(timeEnd)
106+
return time.start(`command:${cmd}`, () => command.cmdExec(args))
136107
}
137108

138109
async load () {

smoke-tests/test/npm-replace-global.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ t.test('fail when updating with lazy require', async t => {
190190
// exit-handler is the last thing called in the code
191191
// so an uncached lazy require within the exit handler will always throw
192192
await fs.writeFile(
193-
join(paths.globalNodeModules, 'npm/lib/utils/exit-handler.js'),
193+
join(paths.globalNodeModules, 'npm/lib/cli/exit-handler.js'),
194194
`module.exports = () => require('./LAZY_REQUIRE_CANARY');module.exports.setNpm = () => {}`,
195195
'utf-8'
196196
)

tap-snapshots/test/lib/utils/update-notifier.js.test.cjs renamed to tap-snapshots/test/lib/cli/update-notifier.js.test.cjs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,95 +5,95 @@
55
* Make sure to inspect the output below. Do not ignore changes!
66
*/
77
'use strict'
8-
exports[`test/lib/utils/update-notifier.js TAP notification situations 122.420.69 - color=always > must match snapshot 1`] = `
8+
exports[`test/lib/cli/update-notifier.js TAP notification situations 122.420.69 - color=always > must match snapshot 1`] = `
99
1010
New major version of npm available! 122.420.69 -> 123.420.69
1111
Changelog: https://github.com/npm/cli/releases/tag/v123.420.69
1212
To update run: npm install -g [email protected]
1313
1414
`
1515

16-
exports[`test/lib/utils/update-notifier.js TAP notification situations 122.420.69 - color=false > must match snapshot 1`] = `
16+
exports[`test/lib/cli/update-notifier.js TAP notification situations 122.420.69 - color=false > must match snapshot 1`] = `
1717
1818
New major version of npm available! 122.420.69 -> 123.420.69
1919
Changelog: https://github.com/npm/cli/releases/tag/v123.420.69
2020
To update run: npm install -g [email protected]
2121
2222
`
2323

24-
exports[`test/lib/utils/update-notifier.js TAP notification situations 123.419.69 - color=always > must match snapshot 1`] = `
24+
exports[`test/lib/cli/update-notifier.js TAP notification situations 123.419.69 - color=always > must match snapshot 1`] = `
2525
2626
New minor version of npm available! 123.419.69 -> 123.420.69
2727
Changelog: https://github.com/npm/cli/releases/tag/v123.420.69
2828
To update run: npm install -g [email protected]
2929
3030
`
3131

32-
exports[`test/lib/utils/update-notifier.js TAP notification situations 123.419.69 - color=false > must match snapshot 1`] = `
32+
exports[`test/lib/cli/update-notifier.js TAP notification situations 123.419.69 - color=false > must match snapshot 1`] = `
3333
3434
New minor version of npm available! 123.419.69 -> 123.420.69
3535
Changelog: https://github.com/npm/cli/releases/tag/v123.420.69
3636
To update run: npm install -g [email protected]
3737
3838
`
3939

40-
exports[`test/lib/utils/update-notifier.js TAP notification situations 123.420.68 - color=always > must match snapshot 1`] = `
40+
exports[`test/lib/cli/update-notifier.js TAP notification situations 123.420.68 - color=always > must match snapshot 1`] = `
4141
4242
New patch version of npm available! 123.420.68 -> 123.420.69
4343
Changelog: https://github.com/npm/cli/releases/tag/v123.420.69
4444
To update run: npm install -g [email protected]
4545
4646
`
4747

48-
exports[`test/lib/utils/update-notifier.js TAP notification situations 123.420.68 - color=false > must match snapshot 1`] = `
48+
exports[`test/lib/cli/update-notifier.js TAP notification situations 123.420.68 - color=false > must match snapshot 1`] = `
4949
5050
New patch version of npm available! 123.420.68 -> 123.420.69
5151
Changelog: https://github.com/npm/cli/releases/tag/v123.420.69
5252
To update run: npm install -g [email protected]
5353
5454
`
5555

56-
exports[`test/lib/utils/update-notifier.js TAP notification situations 123.420.70 - color=always > must match snapshot 1`] = `
56+
exports[`test/lib/cli/update-notifier.js TAP notification situations 123.420.70 - color=always > must match snapshot 1`] = `
5757
5858
New minor version of npm available! 123.420.70 -> 123.421.70
5959
Changelog: https://github.com/npm/cli/releases/tag/v123.421.70
6060
To update run: npm install -g [email protected]
6161
6262
`
6363

64-
exports[`test/lib/utils/update-notifier.js TAP notification situations 123.420.70 - color=false > must match snapshot 1`] = `
64+
exports[`test/lib/cli/update-notifier.js TAP notification situations 123.420.70 - color=false > must match snapshot 1`] = `
6565
6666
New minor version of npm available! 123.420.70 -> 123.421.70
6767
Changelog: https://github.com/npm/cli/releases/tag/v123.421.70
6868
To update run: npm install -g [email protected]
6969
7070
`
7171

72-
exports[`test/lib/utils/update-notifier.js TAP notification situations 123.421.69 - color=always > must match snapshot 1`] = `
72+
exports[`test/lib/cli/update-notifier.js TAP notification situations 123.421.69 - color=always > must match snapshot 1`] = `
7373
7474
New patch version of npm available! 123.421.69 -> 123.421.70
7575
Changelog: https://github.com/npm/cli/releases/tag/v123.421.70
7676
To update run: npm install -g [email protected]
7777
7878
`
7979

80-
exports[`test/lib/utils/update-notifier.js TAP notification situations 123.421.69 - color=false > must match snapshot 1`] = `
80+
exports[`test/lib/cli/update-notifier.js TAP notification situations 123.421.69 - color=false > must match snapshot 1`] = `
8181
8282
New patch version of npm available! 123.421.69 -> 123.421.70
8383
Changelog: https://github.com/npm/cli/releases/tag/v123.421.70
8484
To update run: npm install -g [email protected]
8585
8686
`
8787

88-
exports[`test/lib/utils/update-notifier.js TAP notification situations 124.0.0-beta.0 - color=always > must match snapshot 1`] = `
88+
exports[`test/lib/cli/update-notifier.js TAP notification situations 124.0.0-beta.0 - color=always > must match snapshot 1`] = `
8989
9090
New prerelease version of npm available! 124.0.0-beta.0 -> 124.0.0-beta.99999
9191
Changelog: https://github.com/npm/cli/releases/tag/v124.0.0-beta.99999
9292
To update run: npm install -g [email protected]
9393
9494
`
9595

96-
exports[`test/lib/utils/update-notifier.js TAP notification situations 124.0.0-beta.0 - color=false > must match snapshot 1`] = `
96+
exports[`test/lib/cli/update-notifier.js TAP notification situations 124.0.0-beta.0 - color=false > must match snapshot 1`] = `
9797
9898
New prerelease version of npm available! 124.0.0-beta.0 -> 124.0.0-beta.99999
9999
Changelog: https://github.com/npm/cli/releases/tag/v124.0.0-beta.99999

test/fixtures/mock-npm.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,13 @@ const setGlobalNodeModules = (globalDir) => {
4848
}
4949

5050
const buildMocks = (t, mocks) => {
51-
const allMocks = {
52-
'{LIB}/utils/update-notifier.js': async () => {},
53-
...mocks,
54-
}
51+
const allMocks = { ...mocks }
5552
// The definitions must be mocked since they are a singleton that reads from
5653
// process and environs to build defaults in order to break the requiure
5754
// cache. We also need to mock them with any mocks that were passed in for the
5855
// test in case those mocks are for things like ci-info which is used there.
5956
const definitions = '@npmcli/config/lib/definitions'
6057
allMocks[definitions] = tmock(t, definitions, allMocks)
61-
6258
return allMocks
6359
}
6460

0 commit comments

Comments
 (0)