All files / lib ci.js

96.15% Statements 25/26
100% Branches 2/2
75% Functions 3/4
100% Lines 24/24

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 425x 5x 5x 5x   5x 5x 5x   5x 5x   5x   5x 5x 1x 1x 1x     4x 4x   4x   1x   1x     1x         3x 3x     5x  
const util = require('util')
const Arborist = require('@npmcli/arborist')
const rimraf = util.promisify(require('rimraf'))
const reifyFinish = require('./utils/reify-finish.js')
 
const log = require('npmlog')
const npm = require('./npm.js')
const usageUtil = require('./utils/usage.js')
 
const usage = usageUtil('ci', 'npm ci')
const completion = require('./utils/completion/none.js')
 
const cmd = (args, cb) => ci().then(() => cb()).catch(cb)
 
const ci = async () => {
  if (npm.flatOptions.global) {
    const err = new Error('`npm ci` does not work for global packages')
    err.code = 'ECIGLOBAL'
    throw err
  }
 
  const where = npm.prefix
  const arb = new Arborist({ ...npm.flatOptions, path: where })
 
  await Promise.all([
    arb.loadVirtual().catch(er => {
      log.verbose('loadVirtual', er.stack)
      const msg =
        'The `npm ci` command can only install with an existing package-lock.json or\n' +
        'npm-shrinkwrap.json with lockfileVersion >= 1. Run an install with npm@5 or\n' +
        'later to generate a package-lock.json file, then try again.'
      throw new Error(msg)
    }),
    rimraf(`${where}/node_modules/*`, { glob: { dot: true, nosort: true, silent: true } }),
  ])
  // npm ci should never modify the lockfile or package.json
  await arb.reify({ ...npm.flatOptions, save: false })
  await reifyFinish(arb)
}
 
module.exports = Object.assign(cmd, { completion, usage })