diff options
author | Michael Perrotte <mike@npmjs.com> | 2020-03-17 05:12:20 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2020-05-08 04:12:58 +0300 |
commit | 90c527a1244d6eaa98bc7ee86b790f0110d1d942 (patch) | |
tree | fe2cd70d5f91797ef67e2f04ca511182b6b81df4 /lib | |
parent | fb597cabf4cba91fdbceca60a2fa7b077b8fdaba (diff) |
feat: updated 'npm ci' to use arborist
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ci.js | 74 |
1 files changed, 41 insertions, 33 deletions
@@ -1,47 +1,55 @@ 'use strict' -const npm = require('./npm.js') -const Installer = require('libcipm') -const log = require('npmlog') +const fs = require('fs') const path = require('path') -const pack = require('./pack.js') +const { promisify } = require('util') +const Arborist = require('@npmcli/arborist') +const rimraf = promisify(require('rimraf')) +const lstat = promisify(fs.lstat) + +const npm = require('./npm') +const output = require('./utils/output') ci.usage = 'npm ci' ci.completion = (cb) => cb(null, []) module.exports = ci -function ci (args, cb) { - const opts = { - // Add some non-npm-config opts by hand. - cache: path.join(npm.config.get('cache'), '_cacache'), - // NOTE: npm has some magic logic around color distinct from the config - // value, so we have to override it here - color: !!npm.color, - hashAlgorithm: 'sha1', - includeDeprecated: false, - log, - 'npm-session': npm.session, - 'project-scope': npm.projectScope, - refer: npm.referer, - dmode: npm.modes.exec, - fmode: npm.modes.file, - umask: npm.modes.umask, - npmVersion: npm.version, - tmp: npm.tmp, - dirPacker: pack.packGitDep - } +async function ci (args, cb) { + try { + const where = npm.flatOptions.prefix + if ( + await hasLockfile(where, 'package-lock.json') || + await hasLockfile(where, 'npm-shrinkwrap.json') + ) { + const arb = new Arborist({ ...npm.flatOptions, path: where }) + + const start = Date.now() + await rimraf(`${where}/node_modules/`) + await arb.reify({ ...npm.flatOptions }) + const stop = Date.now() - for (const key in npm.config.list[0]) { - if (!['log', 'cache'].includes(key)) { - opts[key] = npm.config.list[0][key] + const pkgCount = arb.diff.children.length + const added = `added ${pkgCount}` + const time = (stop - start) / 1000 + output(`${added} packages in ${time}s`) + cb() + } else { + 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) } + } catch (err) { + cb(err) } +} - return new Installer(opts).run().then(details => { - log.disableProgress() - console.log(`added ${details.pkgCount} packages in ${ - details.runTime / 1000 - }s`) - }).then(() => cb(), cb) +async function hasLockfile (path, lockfile) { + try { + await lstat(`${path}/${lockfile}`) + return true + } catch (_) { + return false + } } |