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

version.js « lib « npm « deps - github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3a46efae19bf3dc373c7e46d8467643022c586d4 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const libversion = require('libnpmversion')
const npm = require('./npm.js')
const output = require('./utils/output.js')
const usageUtil = require('./utils/usage.js')

const completion = (opts, cb) => {
  const none = require('./utils/completion/none.js')
  const { conf: { argv: { remain } } } = opts
  if (remain.length > 2)
    return none(opts, cb)

  return cb(null, [
    'major',
    'minor',
    'patch',
    'premajor',
    'preminor',
    'prepatch',
    'prerelease',
    'from-git',
  ])
}

const usage = usageUtil('version',
`npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]
(run in package dir)

'npm -v' or 'npm --version' to print npm version (${npm.version})
'npm view <pkg> version' to view a package's published version
'npm ls' to inspect current package/dependency versions`
)

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

const version = async args => {
  switch (args.length) {
    case 0:
      return list()
    case 1:
      return version_(args)
    default:
      throw usage
  }
}

const version_ = async (args) => {
  const prefix = npm.flatOptions.tagVersionPrefix
  const version = await libversion(args[0], {
    ...npm.flatOptions,
    path: npm.prefix,
  })
  return output(`${prefix}${version}`)
}

const list = async () => {
  const results = {}
  const { promisify } = require('util')
  const { resolve } = require('path')
  const readFile = promisify(require('fs').readFile)
  const pj = resolve(npm.prefix, 'package.json')

  const pkg = await readFile(pj, 'utf8')
    .then(data => JSON.parse(data))
    .catch(() => ({}))

  if (pkg.name && pkg.version)
    results[pkg.name] = pkg.version

  results.npm = npm.version
  for (const [key, version] of Object.entries(process.versions))
    results[key] = version

  output(npm.flatOptions.json ? JSON.stringify(results, null, 2) : results)
}

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