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')
|
2024-05-30 04:21:05 -07:00
|
|
|
const fs = require('node:fs/promises')
|
|
|
|
const path = require('node:path')
|
2024-04-30 23:53:22 -07:00
|
|
|
const { log, time } = require('proc-log')
|
2022-02-07 22:15:05 +02:00
|
|
|
const validateLockfile = require('../utils/validate-lockfile.js')
|
2021-11-04 20:42:47 +00:00
|
|
|
const ArboristWorkspaceCmd = require('../arborist-cmd.js')
|
2024-05-30 04:21:05 -07:00
|
|
|
const getWorkspaces = require('../utils/get-workspaces.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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-04-07 14:36:14 -07:00
|
|
|
const dryRun = this.npm.config.get('dry-run')
|
|
|
|
if (!dryRun) {
|
2024-05-30 04:21:05 -07:00
|
|
|
const workspacePaths = await getWorkspaces([], {
|
|
|
|
path: this.npm.localPrefix,
|
|
|
|
includeWorkspaceRoot: true,
|
|
|
|
})
|
|
|
|
|
2024-04-07 14:36:14 -07:00
|
|
|
// Only remove node_modules after we've successfully loaded the virtual
|
|
|
|
// tree and validated the lockfile
|
2024-04-30 23:53:22 -07:00
|
|
|
await time.start('npm-ci:rm', async () => {
|
2024-05-30 04:21:05 -07:00
|
|
|
return await Promise.all([...workspacePaths.values()].map(async modulePath => {
|
|
|
|
const fullPath = path.join(modulePath, 'node_modules')
|
|
|
|
// get the list of entries so we can skip the glob for performance
|
|
|
|
const entries = await fs.readdir(fullPath, null).catch(() => [])
|
|
|
|
return Promise.all(entries.map(folder => {
|
|
|
|
return fs.rm(path.join(fullPath, folder), { force: true, recursive: true })
|
|
|
|
}))
|
|
|
|
}))
|
2024-04-07 14:36:14 -07:00
|
|
|
})
|
|
|
|
}
|
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',
|
|
|
|
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
|