diff options
author | Michael Perrotte <mike@npmjs.com> | 2020-03-17 05:12:20 +0300 |
---|---|---|
committer | Michael Perrotte <mike@npmjs.com> | 2020-03-18 04:06:03 +0300 |
commit | 3c563018ccbef0aba6490efc4d164cc893ff2119 (patch) | |
tree | d613dfbd565f5813513883fd073c76fca3f84c44 | |
parent | bf3370b277c9e738215830fa57a4301c7f8fe59f (diff) |
feat: updated 'npm ci' to use arboristmikemimik/feature/npm-ci-update
-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 + } } |