-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathversion.js
150 lines (133 loc) · 3.5 KB
/
version.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const { resolve } = require('node:path')
const { readFile } = require('node:fs/promises')
const { output } = require('proc-log')
const BaseCommand = require('../base-cmd.js')
class Version extends BaseCommand {
static description = 'Bump a package version'
static name = 'version'
static params = [
'allow-same-version',
'commit-hooks',
'git-tag-version',
'json',
'preid',
'sign-git-tag',
'workspace',
'workspaces',
'workspaces-update',
'include-workspace-root',
]
static workspaces = true
static ignoreImplicitWorkspace = false
static usage = ['[<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]']
static async completion (opts) {
const {
conf: {
argv: { remain },
},
} = opts
if (remain.length > 2) {
return []
}
return [
'major',
'minor',
'patch',
'premajor',
'preminor',
'prepatch',
'prerelease',
'from-git',
]
}
async exec (args) {
switch (args.length) {
case 0:
return this.list()
case 1:
return this.change(args)
default:
throw this.usageError()
}
}
async execWorkspaces (args) {
switch (args.length) {
case 0:
return this.listWorkspaces()
case 1:
return this.changeWorkspaces(args)
default:
throw this.usageError()
}
}
async change (args) {
const libnpmversion = require('libnpmversion')
const prefix = this.npm.config.get('tag-version-prefix')
const version = await libnpmversion(args[0], {
...this.npm.flatOptions,
path: this.npm.prefix,
})
return output.standard(`${prefix}${version}`)
}
async changeWorkspaces (args) {
const updateWorkspaces = require('../utils/update-workspaces.js')
const libnpmversion = require('libnpmversion')
const prefix = this.npm.config.get('tag-version-prefix')
const {
config,
flatOptions,
localPrefix,
} = this.npm
await this.setWorkspaces()
const updatedWorkspaces = []
for (const [name, path] of this.workspaces) {
output.standard(name)
const version = await libnpmversion(args[0], {
...flatOptions,
'git-tag-version': false,
path,
})
updatedWorkspaces.push(name)
output.standard(`${prefix}${version}`)
}
return updateWorkspaces({
config,
flatOptions,
localPrefix,
npm: this.npm,
workspaces: updatedWorkspaces,
})
}
async list (results = {}) {
const pj = resolve(this.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 = this.npm.version
for (const [key, version] of Object.entries(process.versions)) {
results[key] = version
}
if (this.npm.config.get('json')) {
output.buffer(results)
} else {
output.standard(results)
}
}
async listWorkspaces () {
const results = {}
await this.setWorkspaces()
for (const path of this.workspacePaths) {
const pj = resolve(path, 'package.json')
// setWorkspaces has already parsed package.json so we know it won't error
const pkg = await readFile(pj, 'utf8').then(data => JSON.parse(data))
if (pkg.name && pkg.version) {
results[pkg.name] = pkg.version
}
}
return this.list(results)
}
}
module.exports = Version