PHP 8.2.30
Preview: ci.js Size: 4.22 KB
//proc/self/root/opt/alt/alt-nodejs22/root/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',
    '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 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,
    }

    const arb = new Arborist(opts)
    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)
    })

    // 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) {
      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')
      )
    }

    const dryRun = this.npm.config.get('dry-run')
    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)

    const ignoreScripts = this.npm.config.get('ignore-scripts')
    // 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: 0 × Files: 67

Name Size Perms Modified Actions
6.04 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
1.29 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
3.15 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
847 B lrw-r--r-- 2026-02-23 06:53:58
Edit Download
7.12 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
4.22 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
8.90 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
11.18 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
1.41 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
2.13 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
8.00 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
5.52 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
449 B lrw-r--r-- 2026-02-23 06:53:59
Edit Download
10.10 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
1.76 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
3.42 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
3.58 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
2.16 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
634 B lrw-r--r-- 2026-02-23 06:53:59
Edit Download
6.46 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
577 B lrw-r--r-- 2026-02-23 06:53:59
Edit Download
5.52 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
3.66 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
3.37 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
6.85 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
373 B lrw-r--r-- 2026-02-23 06:53:59
Edit Download
370 B lrw-r--r-- 2026-02-23 06:53:59
Edit Download
5.15 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
5.25 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
234 B lrw-r--r-- 2026-02-23 06:53:59
Edit Download
1.29 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
1.42 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
16.81 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
4.02 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
7.70 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
5.85 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
2.60 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
873 B lrw-r--r-- 2026-02-23 06:53:58
Edit Download
3.56 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
335 B lrw-r--r-- 2026-02-23 06:53:59
Edit Download
10.57 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
799 B lrw-r--r-- 2026-02-23 06:53:59
Edit Download
7.38 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
3.51 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
2.19 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
1.25 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
310 B lrw-r--r-- 2026-02-23 06:53:59
Edit Download
295 B lrw-r--r-- 2026-02-23 06:53:58
Edit Download
6.08 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
4.51 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
1.83 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
671 B lrw-r--r-- 2026-02-23 06:53:59
Edit Download
2.65 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
1.87 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
1.03 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
300 B lrw-r--r-- 2026-02-23 06:53:58
Edit Download
295 B lrw-r--r-- 2026-02-23 06:53:59
Edit Download
4.36 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
295 B lrw-r--r-- 2026-02-23 06:53:59
Edit Download
6.02 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
1.52 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
5.27 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
183 B lrw-r--r-- 2026-02-23 06:53:59
Edit Download
1.72 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
3.54 KB lrw-r--r-- 2026-02-23 06:53:58
Edit Download
12.80 KB lrw-r--r-- 2026-02-23 06:53:59
Edit Download
527 B lrw-r--r-- 2026-02-23 06:53:58
Edit Download

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