diff options
author | claudiahdz <cghr1990@gmail.com> | 2020-04-02 20:44:26 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2020-05-08 04:12:58 +0300 |
commit | 28184a4609252dc4c6edcc047b8778935b81d1bb (patch) | |
tree | cf245711ddf766f9adc26d286451f6dba8f6d823 /lib | |
parent | 90c527a1244d6eaa98bc7ee86b790f0110d1d942 (diff) |
ci: use loadVirtual to verify existance of package-lock
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ci.js | 81 |
1 files changed, 38 insertions, 43 deletions
@@ -1,55 +1,50 @@ 'use strict' -const fs = require('fs') -const path = require('path') -const { promisify } = require('util') +const util = require('util') const Arborist = require('@npmcli/arborist') -const rimraf = promisify(require('rimraf')) -const lstat = promisify(fs.lstat) +const rimraf = util.promisify(require('rimraf')) -const npm = require('./npm') -const output = require('./utils/output') +const npm = require('./npm.js') +const output = require('./utils/output.js') -ci.usage = 'npm ci' +cmd.usage = 'npm ci' -ci.completion = (cb) => cb(null, []) +cmd.completion = (cb) => cb(null, []) -module.exports = ci -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() - - 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) - } +module.exports = cmd +function cmd(cb) { + ci() + .then(() => cb()) + .catch(cb) } -async function hasLockfile (path, lockfile) { +async function ci () { + const where = npm.flatOptions.global + ? globalTop + : npm.prefix + + const arb = new Arborist({ ...npm.flatOptions, path: where }) + try { - await lstat(`${path}/${lockfile}`) - return true - } catch (_) { - return false + 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 + } } } |