Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rebuild.js « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a2beecc50c21b2e1193b33d2edb1bb637e4711aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const Arborist = require('@npmcli/arborist')
const npm = require('./npm.js')
const usageUtil = require('./utils/usage.js')
const { resolve } = require('path')
const output = require('./utils/output.js')
const npa = require('npm-package-arg')
const semver = require('semver')

const cmd = (args, cb) => rebuild(args).then(() => cb()).catch(cb)

const usage = usageUtil('rebuild', 'npm rebuild [[<@scope>/]<name>[@<version>] ...]')

const completion = require('./utils/completion/installed-deep.js')

const rebuild = async args => {
  const globalTop = resolve(npm.globalDir, '..')
  const where = npm.flatOptions.global ? globalTop : npm.prefix
  const arb = new Arborist({
    ...npm.flatOptions,
    path: where
  })

  if (args.length) {
    // get the set of nodes matching the name that we want rebuilt
    const tree = await arb.loadActual()
    const filter = getFilterFn(args)
    await arb.rebuild({
      nodes: tree.inventory.filter(filter)
    })
  } else {
    await arb.rebuild()
  }

  output('rebuilt dependencies successfully')
}

const getFilterFn = args => {
  const specs = args.map(arg => {
    const spec = npa(arg)
    if (spec.type === 'tag' && spec.rawSpec === '') { return spec }
    if (spec.type !== 'range' && spec.type !== 'version') { throw new Error('`npm rebuild` only supports SemVer version/range specifiers') }
    return spec
  })
  return node => specs.some(spec => {
    const { version } = node.package
    if (spec.name !== node.name) { return false }
    if (spec.rawSpec === '' || spec.rawSpec === '*') { return true }
    return semver.satisfies(version, spec.fetchSpec)
  })
}

module.exports = Object.assign(cmd, { usage, completion })