All files / lib install.js

98.33% Statements 59/60
100% Branches 25/25
87.5% Functions 7/8
100% Lines 56/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145      7x 7x 7x 7x 7x 7x 7x 7x 7x 7x   7x   7x   3x 3x 3x     3x     3x 1x     3x 2x   3x         3x       3x 1x 1x                 1x 7x                   3x     7x                               7x 6x       6x   1x     5x         4x 4x 4x   4x 3x 3x 1x   2x 2x 1x         1x       4x 4x 3x   3x     3x 3x   1x     2x     1x           1x     7x  
'use strict'
/* eslint-disable camelcase */
/* eslint-disable standard/no-callback-literal */
const fs = require('fs')
const util = require('util')
const readdir = util.promisify(fs.readdir)
const npm = require('./npm.js')
const usageUtil = require('./utils/usage.js')
const reifyFinish = require('./utils/reify-finish.js')
const log = require('npmlog')
const { resolve, join } = require('path')
const Arborist = require('@npmcli/arborist')
const runScript = require('@npmcli/run-script')
 
const cmd = async (args, cb) => install(args).then(() => cb()).catch(cb)
 
const install = async args => {
  // the /path/to/node_modules/..
  const globalTop = resolve(npm.globalDir, '..')
  const { ignoreScripts, global: isGlobalInstall } = npm.flatOptions
  const where = isGlobalInstall ? globalTop : npm.prefix
 
  // don't try to install the prefix into itself
  args = args.filter(a => resolve(a) !== npm.prefix)
 
  // `npm i -g` => "install this package globally"
  if (where === globalTop && !args.length)
    args = ['.']
 
  // TODO: Add warnings for other deprecated flags?  or remove this one?
  if (npm.config.get('dev'))
    log.warn('install', 'Usage of the `--dev` option is deprecated. Use `--include=dev` instead.')
 
  const arb = new Arborist({
    ...npm.flatOptions,
    path: where,
  })
 
  await arb.reify({
    ...npm.flatOptions,
    add: args,
  })
  if (!args.length && !isGlobalInstall && !ignoreScripts) {
    const { scriptShell } = npm.flatOptions
    const scripts = [
      'preinstall',
      'install',
      'postinstall',
      'prepublish', // XXX should we remove this finally??
      'preprepare',
      'prepare',
      'postprepare',
    ]
    for (const event of scripts) {
      await runScript({
        path: where,
        args: [],
        scriptShell,
        stdio: 'inherit',
        stdioString: true,
        event,
      })
    }
  }
  await reifyFinish(arb)
}
 
const usage = usageUtil(
  'install',
  'npm install (with no args, in package dir)' +
  '\nnpm install [<@scope>/]<pkg>' +
  '\nnpm install [<@scope>/]<pkg>@<tag>' +
  '\nnpm install [<@scope>/]<pkg>@<version>' +
  '\nnpm install [<@scope>/]<pkg>@<version range>' +
  '\nnpm install <alias>@npm:<name>' +
  '\nnpm install <folder>' +
  '\nnpm install <tarball file>' +
  '\nnpm install <tarball url>' +
  '\nnpm install <git:// url>' +
  '\nnpm install <github username>/<github project>',
  '[--save-prod|--save-dev|--save-optional|--save-peer] [--save-exact] [--no-save]'
)
 
const completion = async (opts, cb) => {
  const { partialWord } = opts
  // install can complete to a folder with a package.json, or any package.
  // if it has a slash, then it's gotta be a folder
  // if it starts with https?://, then just give up, because it's a url
  if (/^https?:\/\//.test(partialWord)) {
    // do not complete to URLs
    return cb(null, [])
  }
 
  if (/\//.test(partialWord)) {
    // Complete fully to folder if there is exactly one match and it
    // is a folder containing a package.json file.  If that is not the
    // case we return 0 matches, which will trigger the default bash
    // complete.
    const lastSlashIdx = partialWord.lastIndexOf('/')
    const partialName = partialWord.slice(lastSlashIdx + 1)
    const partialPath = partialWord.slice(0, lastSlashIdx) || '/'
 
    const annotatePackageDirMatch = async (sibling) => {
      const fullPath = join(partialPath, sibling)
      if (sibling.slice(0, partialName.length) !== partialName)
        return null // not name match
 
      try {
        const contents = await readdir(fullPath)
        return {
          fullPath,
          isPackage: contents.indexOf('package.json') !== -1,
        }
      } catch (er) {
        return { isPackage: false }
      }
    }
 
    try {
      const siblings = await readdir(partialPath)
      const matches = await Promise.all(
        siblings.map(async sibling => {
          return await annotatePackageDirMatch(sibling)
        })
      )
      const match = matches.filter(el => !el || el.isPackage).pop()
      if (match) {
        // Success - only one match and it is a package dir
        return cb(null, [match.fullPath])
      } else {
        // no matches
        return cb(null, [])
      }
    } catch (er) {
      return cb(null, []) // invalid dir: no matching
    }
  }
  // Note: there used to be registry completion here,
  // but it stopped making sense somewhere around
  // 50,000 packages on the registry
  cb()
}
 
module.exports = Object.assign(cmd, { usage, completion })