blob: e64bca5d626905ddf2dc9fd87214c5a748ea8e25 (
plain)
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
|
'use strict'
const util = require('util')
const Arborist = require('@npmcli/arborist')
const rimraf = util.promisify(require('rimraf'))
const npm = require('./npm.js')
const output = require('./utils/output.js')
cmd.usage = 'npm ci'
cmd.completion = (cb) => cb(null, [])
module.exports = cmd
function cmd(cb) {
ci()
.then(() => cb())
.catch(cb)
}
async function ci () {
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 })
try {
await arb.loadVirtual()
const start = Date.now()
await rimraf(`${where}/node_modules/`)
await arb.reify()
const stop = Date.now()
const time = (stop - start) / 1000
const pkgCount = arb.diff.children.length
const added = `added ${pkgCount}`
output(`${added} packages in ${time}s`)
} catch (err) {
if (err.message.match(/shrinkwrap/)) {
const msg = 'The \`npm ci\` command can only install packages with an existing ' +
'package-lock.json or npm-shrinkwrap.json with lockfileVersion >= 1. Run an install ' +
'with npm@5 or later to generate a package-lock.json file, then try again.'
throw new Error(msg)
} else {
throw err
}
}
}
|