| const reifyFinish = require('../utils/reify-finish.js')
|
| const runScript = require('@npmcli/run-script')
|
| const fs = require('node:fs/promises')
|
| const path = require('node:path')
|
| const { log, time } = require('proc-log')
|
| const validateLockfile = require('../utils/validate-lockfile.js')
|
| const ArboristWorkspaceCmd = require('../arborist-cmd.js')
|
| const getWorkspaces = require('../utils/get-workspaces.js')
|
|
|
| class CI extends ArboristWorkspaceCmd {
|
| static description = 'Clean install a project'
|
| static name = 'ci'
|
|
|
|
|
| static params = [
|
| 'install-strategy',
|
| 'legacy-bundling',
|
| 'global-style',
|
| 'omit',
|
| 'include',
|
| 'strict-peer-deps',
|
| 'foreground-scripts',
|
| 'ignore-scripts',
|
| 'allow-git',
|
| 'audit',
|
| 'bin-links',
|
| 'fund',
|
| 'dry-run',
|
| ...super.params,
|
| ]
|
|
|
| async exec () {
|
| if (this.npm.global) {
|
| throw Object.assign(new Error('`npm ci` does not work for global packages'), {
|
| code: 'ECIGLOBAL',
|
| })
|
| }
|
|
|
| const dryRun = this.npm.config.get('dry-run')
|
| const ignoreScripts = this.npm.config.get('ignore-scripts')
|
| const where = this.npm.prefix
|
| const Arborist = require('@npmcli/arborist')
|
| const opts = {
|
| ...this.npm.flatOptions,
|
| packageLock: true,
|
| path: where,
|
| save: false,
|
| workspaces: this.workspaceNames,
|
| }
|
|
|
|
|
| const virtualArb = new Arborist(opts)
|
| try {
|
| await virtualArb.loadVirtual()
|
| } catch (err) {
|
| log.verbose('loadVirtual', err.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)
|
| }
|
| const virtualInventory = new Map(virtualArb.virtualTree.inventory)
|
|
|
|
|
|
|
| const arb = new Arborist(opts)
|
| await arb.buildIdealTree()
|
|
|
|
|
| const errors = validateLockfile(virtualInventory, arb.idealTree.inventory)
|
| if (errors.length) {
|
| throw this.usageError(
|
| '`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' +
|
| errors.join('\n')
|
| )
|
| }
|
|
|
| if (!dryRun) {
|
| const workspacePaths = await getWorkspaces([], {
|
| path: this.npm.localPrefix,
|
| includeWorkspaceRoot: true,
|
| })
|
|
|
|
|
| await time.start('npm-ci:rm', async () => {
|
| return await Promise.all([...workspacePaths.values()].map(async modulePath => {
|
| const fullPath = path.join(modulePath, 'node_modules')
|
|
|
| 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 })
|
| }))
|
| }))
|
| })
|
| }
|
|
|
| await arb.reify(opts)
|
|
|
|
|
| if (!ignoreScripts) {
|
| const scripts = [
|
| 'preinstall',
|
| 'install',
|
| 'postinstall',
|
| 'prepublish',
|
| 'preprepare',
|
| 'prepare',
|
| 'postprepare',
|
| ]
|
| const scriptShell = this.npm.config.get('script-shell') || undefined
|
| for (const event of scripts) {
|
| await runScript({
|
| path: where,
|
| args: [],
|
| scriptShell,
|
| stdio: 'inherit',
|
| event,
|
| })
|
| }
|
| }
|
| await reifyFinish(this.npm, arb)
|
| }
|
| }
|
|
|
| module.exports = CI
|
|
|