|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| const fs = require('node:fs/promises')
|
| const nopt = require('nopt')
|
| const { resolve } = require('node:path')
|
| const { output } = require('proc-log')
|
| const Npm = require('../npm.js')
|
| const { definitions, shorthands } = require('@npmcli/config/lib/definitions')
|
| const { commands, aliases, deref } = require('../utils/cmd-list.js')
|
| const { isWindowsShell } = require('../utils/is-windows.js')
|
| const BaseCommand = require('../base-cmd.js')
|
|
|
| const fileExists = (file) => fs.stat(file).then(s => s.isFile()).catch(() => false)
|
|
|
| class Completion extends BaseCommand {
|
| static description = 'Tab Completion for npm'
|
| static name = 'completion'
|
|
|
| static definitions = []
|
|
|
|
|
| static async completion (opts) {
|
| if (opts.w > 2) {
|
| return
|
| }
|
|
|
| const [bashExists, zshExists] = await Promise.all([
|
| fileExists(resolve(process.env.HOME, '.bashrc')),
|
| fileExists(resolve(process.env.HOME, '.zshrc')),
|
| ])
|
| const out = []
|
| if (zshExists) {
|
| out.push(['>>', '~/.zshrc'])
|
| }
|
|
|
| if (bashExists) {
|
| out.push(['>>', '~/.bashrc'])
|
| }
|
|
|
| return out
|
| }
|
|
|
| async exec (args) {
|
| if (isWindowsShell) {
|
| const msg = 'npm completion supported only in MINGW / Git bash on Windows'
|
| throw Object.assign(new Error(msg), {
|
| code: 'ENOTSUP',
|
| })
|
| }
|
|
|
| const { COMP_CWORD, COMP_LINE, COMP_POINT, COMP_FISH } = process.env
|
|
|
|
|
| if (COMP_CWORD === undefined || COMP_LINE === undefined || COMP_POINT === undefined) {
|
| return dumpScript(resolve(this.npm.npmRoot, 'lib', 'utils', 'completion.sh'))
|
| }
|
|
|
|
|
|
|
| const w = +COMP_CWORD
|
| const line = COMP_LINE
|
|
|
| const hasFlags = line.includes(' -') && !args.some(arg => arg.startsWith('-'))
|
| const words = (hasFlags ? line.split(/\s+/) : args).map(unescape)
|
| const word = words[w] || ''
|
| const point = +COMP_POINT
|
| const partialLine = line.slice(0, point)
|
| const partialWords = words.slice(0, w)
|
|
|
|
|
| const partialWordRaw = args[w] || ''
|
| let i = partialWordRaw.length
|
| while (partialWordRaw.slice(0, i) !== partialLine.slice(-1 * i) && i > 0) {
|
| i--
|
| }
|
|
|
| const partialWord = unescape(partialWordRaw.slice(0, i))
|
| partialWords.push(partialWord)
|
|
|
| const opts = {
|
| isFish: COMP_FISH === 'true',
|
| words,
|
| w,
|
| word,
|
| line,
|
| lineLength: line.length,
|
| point,
|
| partialLine,
|
| partialWords,
|
| partialWord,
|
| raw: args,
|
| }
|
|
|
|
|
| const types = Object.entries(definitions).reduce((acc, [key, def]) => {
|
| acc[key] = def.type
|
| return acc
|
| }, {})
|
| const parsed = opts.conf =
|
| nopt(types, shorthands, partialWords.slice(0, -1), 0)
|
| const cmd = parsed.argv.remain[1]
|
| const subCmd = parsed.argv.remain[2]
|
|
|
| if (partialWords.slice(0, -1).indexOf('--') === -1) {
|
| if (word && word.charAt(0) === '-') {
|
| return this.wrap(opts, configCompl(opts, cmd, subCmd, this.npm))
|
| }
|
|
|
| if (words[w - 1] &&
|
| words[w - 1].charAt(0) === '-' &&
|
| !isFlag(words[w - 1], cmd, subCmd, this.npm)) {
|
|
|
|
|
| return this.wrap(opts, configValueCompl(opts))
|
| }
|
| }
|
|
|
|
|
| if (!cmd) {
|
| return this.wrap(opts, cmdCompl(opts, this.npm))
|
| }
|
|
|
| Object.keys(parsed).forEach(k => this.npm.config.set(k, parsed[k]))
|
|
|
|
|
|
|
| try {
|
| const { completion } = Npm.cmd(cmd)
|
| if (completion) {
|
| const comps = await completion(opts, this.npm)
|
| return this.wrap(opts, comps)
|
| }
|
| } catch {
|
|
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
| wrap (opts, compls) {
|
| if (opts.partialWord) {
|
| compls = compls.filter(c => c.startsWith(opts.partialWord))
|
| }
|
|
|
| if (compls.length > 0) {
|
| output.standard(compls.join('\n'))
|
| }
|
| }
|
| }
|
|
|
| const dumpScript = async (p) => {
|
| const d = (await fs.readFile(p, 'utf8')).replace(/^#!.*?\n/, '')
|
| await new Promise((res, rej) => {
|
| let done = false
|
| process.stdout.on('error', er => {
|
| if (done) {
|
| return
|
| }
|
|
|
| done = true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if (er.errno === 'EPIPE') {
|
| res()
|
| } else {
|
| rej(er)
|
| }
|
| })
|
|
|
| process.stdout.write(d, () => {
|
| if (done) {
|
| return
|
| }
|
|
|
| done = true
|
| res()
|
| })
|
| })
|
| }
|
|
|
| const unescape = w => w.charAt(0) === '\'' ? w.replace(/^'|'$/g, '')
|
| : w.replace(/\\ /g, ' ')
|
|
|
|
|
| const getCustomDefinitions = (cmd, subCmd) => {
|
| if (!cmd) {
|
| return []
|
| }
|
|
|
| try {
|
| const command = Npm.cmd(cmd)
|
|
|
|
|
| if (subCmd && command.subcommands && command.subcommands[subCmd]) {
|
| const subcommand = command.subcommands[subCmd]
|
|
|
| return subcommand.definitions
|
| }
|
|
|
|
|
| if (command.definitions) {
|
| return command.definitions
|
| }
|
| } catch {
|
|
|
| }
|
|
|
| return []
|
| }
|
|
|
|
|
| const getCustomConfigNames = (customDefs) => {
|
| const names = new Set()
|
| for (const def of customDefs) {
|
| names.add(def.key)
|
| if (def.alias && Array.isArray(def.alias)) {
|
| def.alias.forEach(a => names.add(a))
|
| }
|
| }
|
| return [...names]
|
| }
|
|
|
|
|
|
|
| const configCompl = (opts, cmd, subCmd, npm) => {
|
| const word = opts.word
|
| const split = word.match(/^(-+)((?:no-)*)(.*)$/)
|
| const dashes = split[1]
|
| const no = split[2]
|
|
|
|
|
| const customDefs = getCustomDefinitions(cmd, subCmd, npm)
|
| const customNames = getCustomConfigNames(customDefs)
|
|
|
|
|
|
|
| if (customNames.length > 0) {
|
| const flags = customNames.filter(name => isFlag(name, cmd, subCmd, npm))
|
| return customNames.map(c => dashes + c)
|
| .concat(flags.map(f => dashes + (no || 'no-') + f))
|
| }
|
|
|
| return []
|
| }
|
|
|
|
|
|
|
| const configValueCompl = () => []
|
|
|
|
|
| const isFlag = (word, cmd, subCmd, npm) => {
|
|
|
| const split = word.match(/^(-*)((?:no-)+)?(.*)$/)
|
| const no = split[2]
|
| const conf = split[3]
|
|
|
|
|
| const customDefs = getCustomDefinitions(cmd, subCmd, npm)
|
|
|
|
|
| let customDef = customDefs.find(d => d.key === conf)
|
| if (!customDef) {
|
|
|
| for (const def of customDefs) {
|
| if (def.alias && Array.isArray(def.alias) && def.alias.includes(conf)) {
|
| customDef = def
|
| break
|
| }
|
| }
|
| }
|
|
|
| if (customDef) {
|
| const { type } = customDef
|
| return no ||
|
| type === Boolean ||
|
| (Array.isArray(type) && type.includes(Boolean))
|
| }
|
|
|
|
|
| return false
|
| }
|
|
|
|
|
|
|
| const cmdCompl = (opts) => {
|
| const allCommands = commands.concat(Object.keys(aliases))
|
| const matches = allCommands.filter(c => c.startsWith(opts.partialWord))
|
| if (!matches.length) {
|
| return matches
|
| }
|
|
|
| const derefs = new Set([...matches.map(c => deref(c))])
|
| if (derefs.size === 1) {
|
| return [...derefs]
|
| }
|
|
|
| return allCommands
|
| }
|
|
|
| module.exports = Completion
|
|
|