2021-11-04 20:42:47 +00:00
|
|
|
const reifyFinish = require('../utils/reify-finish.js')
|
2020-12-11 18:54:56 -05:00
|
|
|
const runScript = require('@npmcli/run-script')
|
2023-01-16 22:38:23 -05:00
|
|
|
const fs = require('fs/promises')
|
2021-12-02 22:04:46 +00:00
|
|
|
const log = require('../utils/log-shim.js')
|
2022-02-07 22:15:05 +02:00
|
|
|
const validateLockfile = require('../utils/validate-lockfile.js')
|
2018-04-20 18:26:37 -07:00
|
|
|
|
2021-11-04 20:42:47 +00:00
|
|
|
const ArboristWorkspaceCmd = require('../arborist-cmd.js')
|
2021-02-01 17:26:42 -05:00
|
|
|
|
2021-05-20 15:54:50 -04:00
|
|
|
class CI extends ArboristWorkspaceCmd {
|
2022-05-25 21:26:36 +00:00
|
|
|
static description = 'Clean install a project'
|
2021-11-18 20:58:02 +00:00
|
|
|
static name = 'ci'
|
2022-07-28 11:03:27 -07:00
|
|
|
|
2023-05-07 03:37:34 -07:00
|
|
|
// These are in the order they will show up in when running "-h"
|
|
|
|
static params = [
|
|
|
|
'install-strategy',
|
|
|
|
'legacy-bundling',
|
|
|
|
'global-style',
|
|
|
|
'omit',
|
2023-10-13 06:55:37 -07:00
|
|
|
'include',
|
2023-05-07 03:37:34 -07:00
|
|
|
'strict-peer-deps',
|
|
|
|
'foreground-scripts',
|
|
|
|
'ignore-scripts',
|
|
|
|
'audit',
|
|
|
|
'bin-links',
|
|
|
|
'fund',
|
|
|
|
'dry-run',
|
|
|
|
...super.params,
|
|
|
|
]
|
2021-05-20 15:54:50 -04:00
|
|
|
|
2021-11-04 20:42:47 +00:00
|
|
|
async exec () {
|
2022-05-25 21:26:36 +00:00
|
|
|
if (this.npm.global) {
|
|
|
|
throw Object.assign(new Error('`npm ci` does not work for global packages'), {
|
|
|
|
code: 'ECIGLOBAL',
|
|
|
|
})
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const where = this.npm.prefix
|
2023-05-07 03:37:34 -07:00
|
|
|
const Arborist = require('@npmcli/arborist')
|
2021-03-24 17:25:32 -04:00
|
|
|
const opts = {
|
|
|
|
...this.npm.flatOptions,
|
2022-02-07 22:15:05 +02:00
|
|
|
packageLock: true, // npm ci should never skip lock files
|
2021-03-24 17:25:32 -04:00
|
|
|
path: where,
|
|
|
|
save: false, // npm ci should never modify the lockfile or package.json
|
2021-06-17 18:59:38 +00:00
|
|
|
workspaces: this.workspaceNames,
|
2021-03-24 17:25:32 -04:00
|
|
|
}
|
2019-09-03 17:51:04 -05:00
|
|
|
|
2021-03-24 17:25:32 -04:00
|
|
|
const arb = new Arborist(opts)
|
2022-05-25 21:26:36 +00:00
|
|
|
await 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 this.usageError(msg)
|
|
|
|
})
|
2022-02-07 22:15:05 +02:00
|
|
|
|
|
|
|
// retrieves inventory of packages from loaded virtual tree (lock file)
|
|
|
|
const virtualInventory = new Map(arb.virtualTree.inventory)
|
|
|
|
|
|
|
|
// build ideal tree step needs to come right after retrieving the virtual
|
|
|
|
// inventory since it's going to erase the previous ref to virtualTree
|
|
|
|
await arb.buildIdealTree()
|
|
|
|
|
|
|
|
// verifies that the packages from the ideal tree will match
|
|
|
|
// the same versions that are present in the virtual tree (lock file)
|
|
|
|
// throws a validation error in case of mismatches
|
|
|
|
const errors = validateLockfile(virtualInventory, arb.idealTree.inventory)
|
|
|
|
if (errors.length) {
|
2022-05-25 21:26:36 +00:00
|
|
|
throw this.usageError(
|
2022-02-07 22:15:05 +02:00
|
|
|
'`npm ci` can only install packages when your package.json and ' +
|
|
|
|
'package-lock.json or npm-shrinkwrap.json are in sync. Please ' +
|
|
|
|
'update your lock file with `npm install` ' +
|
|
|
|
'before continuing.\n\n' +
|
2022-05-25 21:26:36 +00:00
|
|
|
errors.join('\n')
|
2022-02-07 22:15:05 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-25 21:26:36 +00:00
|
|
|
// Only remove node_modules after we've successfully loaded the virtual
|
|
|
|
// tree and validated the lockfile
|
|
|
|
await this.npm.time('npm-ci:rm', async () => {
|
|
|
|
const path = `${where}/node_modules`
|
|
|
|
// get the list of entries so we can skip the glob for performance
|
2023-01-16 22:38:23 -05:00
|
|
|
const entries = await fs.readdir(path, null).catch(er => [])
|
2023-01-18 09:48:47 -05:00
|
|
|
return Promise.all(entries.map(f => fs.rm(`${path}/${f}`, { force: true, recursive: true })))
|
2022-05-25 21:26:36 +00:00
|
|
|
})
|
|
|
|
|
2021-03-24 17:25:32 -04:00
|
|
|
await arb.reify(opts)
|
2020-12-11 18:54:56 -05:00
|
|
|
|
2021-03-23 14:58:11 -04:00
|
|
|
const ignoreScripts = this.npm.config.get('ignore-scripts')
|
2021-03-04 17:40:28 -05:00
|
|
|
// run the same set of scripts that `npm install` runs.
|
|
|
|
if (!ignoreScripts) {
|
|
|
|
const scripts = [
|
|
|
|
'preinstall',
|
|
|
|
'install',
|
|
|
|
'postinstall',
|
|
|
|
'prepublish', // XXX should we remove this finally??
|
|
|
|
'preprepare',
|
|
|
|
'prepare',
|
|
|
|
'postprepare',
|
|
|
|
]
|
2021-03-23 14:58:11 -04:00
|
|
|
const scriptShell = this.npm.config.get('script-shell') || undefined
|
2021-03-04 17:40:28 -05:00
|
|
|
for (const event of scripts) {
|
|
|
|
await runScript({
|
|
|
|
path: where,
|
|
|
|
args: [],
|
|
|
|
scriptShell,
|
|
|
|
stdio: 'inherit',
|
2022-02-24 21:41:49 +00:00
|
|
|
banner: !this.npm.silent,
|
2021-03-04 17:40:28 -05:00
|
|
|
event,
|
|
|
|
})
|
|
|
|
}
|
2021-01-07 16:12:19 -05:00
|
|
|
}
|
2021-03-04 17:40:28 -05:00
|
|
|
await reifyFinish(this.npm, arb)
|
2020-12-11 18:54:56 -05:00
|
|
|
}
|
2018-04-20 18:26:37 -07:00
|
|
|
}
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
module.exports = CI
|