Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd-list cleanup #4609

Merged
merged 7 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/commands/npm-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ npm install <tarball url>
npm install <git:// url>
npm install <github username>/<github project>

aliases: i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall, add
aliases: add, i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall
```

<!-- automatically generated, do not edit manually -->
Expand Down
2 changes: 1 addition & 1 deletion docs/content/commands/npm-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description: Search for packages
```bash
npm search [search terms ...]

aliases: s, se, find
aliases: find, s, se
```

<!-- automatically generated, do not edit manually -->
Expand Down
2 changes: 1 addition & 1 deletion docs/content/commands/npm-uninstall.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description: Remove a package
```bash
npm uninstall [<@scope>/]<pkg>...

aliases: un, unlink, remove, rm, r
aliases: unlink, remove, rm, r, un
```

<!-- automatically generated, do not edit manually -->
Expand Down
2 changes: 1 addition & 1 deletion docs/content/commands/npm-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description: View registry info
```bash
npm view [<@scope>/]<pkg>[@<version>] [<field>[.subfield]...]

aliases: v, info, show
aliases: info, show, v
```

<!-- automatically generated, do not edit manually -->
Expand Down
22 changes: 19 additions & 3 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,25 @@ module.exports = async process => {
// so now both broken and unsupported use console, but only broken
// will process.exit. It is important to now perform *both* of these
// checks as early as possible so the user gets the error message.
const { checkForBrokenNode, checkForUnsupportedNode } = require('./utils/unsupported.js')
checkForBrokenNode()
checkForUnsupportedNode()
const semver = require('semver')
const supported = require('../package.json').engines.node
const knownBroken = '<12.5.0'

const nodejsVersion = process.version.replace(/-.*$/, '')
/* eslint-disable no-console */
if (semver.satisfies(nodejsVersion, knownBroken)) {
console.error('ERROR: npm is known not to run on Node.js ' + process.version)
console.error("You'll need to upgrade to a newer Node.js version in order to use this")
console.error('version of npm. You can find the latest version at https://nodejs.org/')
process.exit(1)
}
if (!semver.satisfies(nodejsVersion, supported)) {
console.error('npm does not support Node.js ' + process.version)
console.error('You should probably upgrade to a newer version of node as we')
console.error("can't make any promises that npm will work with this version.")
console.error('You can find the latest version at https://nodejs.org/')
}
/* eslint-enable no-console */

const exitHandler = require('./utils/exit-handler.js')
process.on('uncaughtException', exitHandler)
Expand Down
8 changes: 6 additions & 2 deletions lib/commands/bin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const log = require('../utils/log-shim.js')
const envPath = require('../utils/path.js')
const BaseCommand = require('../base-command.js')
// TODO this may not be needed at all. Ignoring because our tests normalize
// process.env.path already
/* istanbul ignore next */
const path = process.env.path || process.env.Path || process.env.PATH
lukekarrys marked this conversation as resolved.
Show resolved Hide resolved
const { delimiter } = require('path')

class Bin extends BaseCommand {
static description = 'Display npm bin folder'
Expand All @@ -11,7 +15,7 @@ class Bin extends BaseCommand {
async exec (args) {
const b = this.npm.bin
this.npm.output(b)
if (this.npm.config.get('global') && !envPath.includes(b)) {
if (this.npm.config.get('global') && !path.split(delimiter).includes(b)) {
log.error('bin', '(not in PATH env variable)')
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/birthday.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const BaseCommand = require('../base-command.js')

class Birthday extends BaseCommand {
static name = 'birthday'
static description = 'Birthday'
static ignoreImplicitWorkspace = true
static isShellout = true

async exec () {
this.npm.config.set('yes', true)
Expand Down
9 changes: 4 additions & 5 deletions lib/commands/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
//

const { definitions, shorthands } = require('../utils/config/index.js')
const deref = require('../utils/deref-command.js')
const { aliases, cmdList, plumbing } = require('../utils/cmd-list.js')
const aliasNames = Object.keys(aliases)
const fullList = cmdList.concat(aliasNames).filter(c => !plumbing.includes(c))
const nopt = require('nopt')
const configNames = Object.keys(definitions)
const shorthandNames = Object.keys(shorthands)
const allConfs = configNames.concat(shorthandNames)
const isWindowsShell = require('../utils/is-windows-shell.js')
const { isWindowsShell } = require('../utils/is-windows.js')
const fileExists = require('../utils/file-exists.js')

const { promisify } = require('util')
Expand Down Expand Up @@ -152,7 +151,7 @@ class Completion extends BaseCommand {
// check if there's a command already.
const cmd = parsed.argv.remain[1]
if (!cmd) {
return this.wrap(opts, cmdCompl(opts))
return this.wrap(opts, cmdCompl(opts, this.npm))
}

Object.keys(parsed).forEach(k => this.npm.config.set(k, parsed[k]))
Expand Down Expand Up @@ -269,13 +268,13 @@ const isFlag = word => {

// complete against the npm commands
// if they all resolve to the same thing, just return the thing it already is
const cmdCompl = opts => {
const cmdCompl = (opts, npm) => {
const matches = fullList.filter(c => c.startsWith(opts.partialWord))
if (!matches.length) {
return matches
}

const derefs = new Set([...matches.map(c => deref(c))])
const derefs = new Set([...matches.map(c => npm.deref(c))])
if (derefs.size === 1) {
return [...derefs]
}
Expand Down
1 change: 1 addition & 0 deletions lib/commands/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Exec extends BaseCommand {
]

static ignoreImplicitWorkspace = false
static isShellout = true

async exec (_args, { locationMsg, path, runPath } = {}) {
if (!path) {
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/run-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { isServerPackage } = runScript
const rpj = require('read-package-json-fast')
const log = require('../utils/log-shim.js')
const didYouMean = require('../utils/did-you-mean.js')
const isWindowsShell = require('../utils/is-windows-shell.js')
const { isWindowsShell } = require('../utils/is-windows.js')

const cmdList = [
'publish',
Expand Down Expand Up @@ -41,6 +41,7 @@ class RunScript extends BaseCommand {
static name = 'run-script'
static usage = ['<command> [-- <args>]']
static ignoreImplicitWorkspace = false
static isShellout = true

async completion (opts) {
const argv = opts.conf.argv.remain
Expand Down
1 change: 1 addition & 0 deletions lib/lifecycle-cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const BaseCommand = require('./base-command.js')
class LifecycleCmd extends BaseCommand {
static usage = ['[-- <args>]']
static isShellout = true

async exec (args, cb) {
return this.npm.exec('run-script', [this.constructor.name, ...args])
Expand Down
25 changes: 20 additions & 5 deletions lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ const Config = require('@npmcli/config')
require('graceful-fs').gracefulify(require('fs'))

const { definitions, flatten, shorthands } = require('./utils/config/index.js')
const { shellouts } = require('./utils/cmd-list.js')
const usage = require('./utils/npm-usage.js')

const which = require('which')
const fs = require('@npmcli/fs')

const deref = require('./utils/deref-command.js')
const LogFile = require('./utils/log-file.js')
const Timers = require('./utils/timers.js')
const Display = require('./utils/display.js')
const log = require('./utils/log-shim')
const replaceInfo = require('./utils/replace-info.js')
const updateNotifier = require('./utils/update-notifier.js')
const pkg = require('../package.json')
const cmdList = require('./utils/cmd-list.js')

let warnedNonDashArg = false
const _load = Symbol('_load')
Expand All @@ -32,7 +31,6 @@ class Npm extends EventEmitter {
command = null
updateNotification = null
loadErr = null
deref = deref
argv = []

#loadPromise = null
Expand Down Expand Up @@ -62,8 +60,24 @@ class Npm extends EventEmitter {
return this.constructor.version
}

get shelloutCommands () {
return shellouts
deref (c) {
if (!c) {
return
}
if (c.match(/[A-Z]/)) {
c = c.replace(/([A-Z])/g, m => '-' + m.toLowerCase())
}
if (cmdList.plumbing.indexOf(c) !== -1) {
return c
}
// first deref the abbrev, if there is one
// then resolve any aliases
// so `npm install-cl` will resolve to `install-clean` then to `ci`
let a = cmdList.abbrevs[c]
while (cmdList.aliases[a]) {
a = cmdList.aliases[a]
}
return a
}

// Get an instantiated npm command
Expand Down Expand Up @@ -92,6 +106,7 @@ class Npm extends EventEmitter {
if (!this.command) {
process.env.npm_command = command.name
this.command = command.name
this.commandInstance = command
}

// this is async but we dont await it, since its ok if it doesnt
Expand Down
Loading