PHP 8.3.31
Preview: ci.js Size: 4.24 KB
/proc/thread-self/root/opt/alt/alt-nodejs24/root/usr/lib/node_modules/npm/lib/commands/ci.js

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'

  // These are in the order they will show up in when running "-h"
  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, // npm ci should never skip lock files
      path: where,
      save: false, // npm ci should never modify the lockfile or package.json
      workspaces: this.workspaceNames,
    }

    // generate an inventory from the virtual tree in the lockfile
    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)

    // Now we make our real Arborist.
    // We need a new one because the virtual tree fromt the lockfile can have extraneous dependencies in it that won't install on this platform
    const arb = new Arborist(opts)
    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).
    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,
      })

      // Only remove node_modules after we've successfully loaded the virtual tree and validated the lockfile
      await time.start('npm-ci:rm', async () => {
        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 })
          }))
        }))
      })
    }

    await arb.reify(opts)

    // 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',
      ]
      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

Directory Contents

Dirs: 1 × Files: 67

Name Size Perms Modified Actions
trust DIR
- drwxr-xr-x 2026-05-14 18:53:29
Edit Download
6.17 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
1.29 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
3.17 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
847 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
11.48 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
4.24 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
10.83 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
11.48 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
1.42 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
2.43 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
7.92 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
5.50 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
449 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
9.99 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
1.72 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
3.39 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
3.55 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
2.11 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
578 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
6.38 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
538 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
5.53 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
3.64 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
7.02 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
308 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
303 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
5.10 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
5.24 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
234 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
1.29 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
1.42 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
18.03 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
3.96 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
7.92 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
5.95 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
2.77 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
873 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
3.58 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
309 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
10.30 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
770 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
9.47 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
3.75 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
2.15 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
1.24 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
303 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
295 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
6.27 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
4.47 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
1.83 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
632 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
2.63 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
1.88 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
1.03 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
293 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
288 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
4.33 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
288 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
7.96 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
302 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
1.49 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
5.24 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
183 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download
1.69 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
3.54 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
13.48 KB lrw-r--r-- 2026-04-24 15:56:02
Edit Download
527 B lrw-r--r-- 2026-04-24 15:56:02
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).