yarn-version 8.7.0 → 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/README.md +2 -152
- package/package.json +9 -39
- package/yarn-version.mjs +3 -0
- package/dist/index-node.mjs +0 -1251
- package/dist/index-node.mjs.map +0 -1
- package/rollup.config.mjs +0 -12
- package/src/__fixtures__/basic/package.json +0 -4
- package/src/__fixtures__/basic/yarn.lock +0 -12
- package/src/__fixtures__/monorepo/package.json +0 -7
- package/src/__fixtures__/monorepo/packages/package-1/package.json +0 -4
- package/src/__fixtures__/monorepo/packages/package-2/package.json +0 -7
- package/src/__fixtures__/monorepo/packages/package-3/package.json +0 -7
- package/src/__fixtures__/monorepo/packages/package-4/package.json +0 -7
- package/src/__fixtures__/monorepo/packages/package-5/package.json +0 -8
- package/src/__fixtures__/monorepo/yarn.lock +0 -0
- package/src/__fixtures__/no-version/package.json +0 -3
- package/src/__fixtures__/no-version/yarn.lock +0 -0
- package/src/__fixtures__/with-lifecycle/.yarn/install-state.gz +0 -0
- package/src/__fixtures__/with-lifecycle/package.json +0 -7
- package/src/__fixtures__/with-lifecycle/yarn.lock +0 -11
- package/src/commands/version.test.ts +0 -94
- package/src/commands/version.ts +0 -902
- package/src/index.ts +0 -13
- package/src/missing-types.d.ts +0 -11
- package/src/utils/UsageError.ts +0 -6
- package/src/utils/bumpTypeUtils.ts +0 -113
- package/src/utils/conventionalChangelogUtils.test.ts +0 -761
- package/src/utils/conventionalChangelogUtils.ts +0 -104
- package/src/utils/conventionalCommitConfigUtils.ts +0 -20
- package/src/utils/execCommand.ts +0 -189
- package/src/utils/generatorUtils.ts +0 -11
- package/src/utils/gitUtils.ts +0 -193
- package/src/utils/githubUtils.ts +0 -82
- package/src/utils/mapUtils.ts +0 -7
- package/src/utils/packageDependenciesUtils.test.ts +0 -24
- package/src/utils/packageDependenciesUtils.ts +0 -80
- package/src/utils/packageUtils.ts +0 -97
- package/src/utils/updateChangelog.ts +0 -39
- package/src/utils/workspaceUtils.ts +0 -142
- package/tsconfig.json +0 -23
- package/vitest.config.js +0 -9
package/dist/index-node.mjs
DELETED
|
@@ -1,1251 +0,0 @@
|
|
|
1
|
-
import fs$1 from 'node:fs';
|
|
2
|
-
import { program, Option } from 'commander';
|
|
3
|
-
import { LoggerCLI } from 'nightingale';
|
|
4
|
-
import semver, { satisfies } from 'semver';
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { writeChangelogString } from 'conventional-changelog-writer';
|
|
7
|
-
import { parseCommits } from 'conventional-commits-parser';
|
|
8
|
-
import { loadPreset } from 'conventional-changelog-preset-loader';
|
|
9
|
-
import childProcess from 'node:child_process';
|
|
10
|
-
import { Octokit } from '@octokit/rest';
|
|
11
|
-
import fs from 'node:fs/promises';
|
|
12
|
-
import mapWorkspaces from '@npmcli/map-workspaces';
|
|
13
|
-
import prettyPkg from '@pob/pretty-pkg';
|
|
14
|
-
|
|
15
|
-
class UsageError extends Error {
|
|
16
|
-
constructor(message) {
|
|
17
|
-
super(message);
|
|
18
|
-
this.name = "UsageError";
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function getMapArrayItemForKey(map, key) {
|
|
23
|
-
let value = map.get(key);
|
|
24
|
-
if (value === void 0) {
|
|
25
|
-
map.set(key, value = []);
|
|
26
|
-
}
|
|
27
|
-
return value;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const PackageDescriptorNameUtils = {
|
|
31
|
-
parse: (value) => {
|
|
32
|
-
if (value.startsWith("@")) {
|
|
33
|
-
const [scope, name] = value.slice(1).split("/", 2);
|
|
34
|
-
if (!scope || !name) {
|
|
35
|
-
throw new Error(`Invalid package descriptor name: ${value}`);
|
|
36
|
-
}
|
|
37
|
-
return { scope, name };
|
|
38
|
-
}
|
|
39
|
-
return { name: value };
|
|
40
|
-
},
|
|
41
|
-
stringify: (descriptor) => {
|
|
42
|
-
return descriptor.scope === void 0 ? descriptor.name : `@${descriptor.scope}/${descriptor.name}`;
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
const PackageDependencyDescriptorUtils = {
|
|
46
|
-
make: (descriptor, selector) => {
|
|
47
|
-
return { key: descriptor.key, name: descriptor.name, selector };
|
|
48
|
-
},
|
|
49
|
-
parse: (dependencyKey, dependencyValue) => {
|
|
50
|
-
const [name, selector] = dependencyValue.startsWith("npm:") ? (() => {
|
|
51
|
-
const v = dependencyValue.slice("npm:".length);
|
|
52
|
-
if (!v.startsWith("@")) return v.split("@", 2);
|
|
53
|
-
const [packageNameWithoutFirstChar, selector2] = v.slice(1).split("@", 2);
|
|
54
|
-
if (!packageNameWithoutFirstChar || !selector2) {
|
|
55
|
-
throw new Error(`Invalid package descriptor: ${dependencyValue}`);
|
|
56
|
-
}
|
|
57
|
-
return [`@${packageNameWithoutFirstChar}`, selector2];
|
|
58
|
-
})() : [dependencyKey, dependencyValue];
|
|
59
|
-
return {
|
|
60
|
-
key: dependencyKey,
|
|
61
|
-
name: PackageDescriptorNameUtils.parse(name),
|
|
62
|
-
selector
|
|
63
|
-
};
|
|
64
|
-
},
|
|
65
|
-
stringify: (descriptor) => {
|
|
66
|
-
return [descriptor.key, descriptor.selector];
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
const allDependenciesTypes = [
|
|
71
|
-
"dependencies",
|
|
72
|
-
"devDependencies",
|
|
73
|
-
"peerDependencies"
|
|
74
|
-
];
|
|
75
|
-
const getWorkspaceName = (workspace) => {
|
|
76
|
-
return workspace.pkg.name ?? (path.basename(workspace.cwd) || "unnamed-workspace");
|
|
77
|
-
};
|
|
78
|
-
const buildDependenciesMaps = (project) => {
|
|
79
|
-
const dependenciesMap = /* @__PURE__ */ new Map();
|
|
80
|
-
for (const dependent of project.children.values()) {
|
|
81
|
-
for (const set of allDependenciesTypes) {
|
|
82
|
-
for (const [dependencyKey, dependencyValue] of Object.entries(
|
|
83
|
-
dependent.pkg[set] || {}
|
|
84
|
-
)) {
|
|
85
|
-
if (!dependencyValue) continue;
|
|
86
|
-
const descriptor = PackageDependencyDescriptorUtils.parse(
|
|
87
|
-
dependencyKey,
|
|
88
|
-
dependencyValue
|
|
89
|
-
);
|
|
90
|
-
const workspace = project.children.get(
|
|
91
|
-
PackageDescriptorNameUtils.stringify(descriptor.name)
|
|
92
|
-
);
|
|
93
|
-
if (!workspace) continue;
|
|
94
|
-
const dependencies = getMapArrayItemForKey(dependenciesMap, dependent);
|
|
95
|
-
dependencies.push([workspace, set, descriptor]);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return dependenciesMap;
|
|
100
|
-
};
|
|
101
|
-
const buildTopologicalOrderBatches = (project, dependenciesMap) => {
|
|
102
|
-
const batches = [];
|
|
103
|
-
const added = /* @__PURE__ */ new Set();
|
|
104
|
-
const toAdd = /* @__PURE__ */ new Set([
|
|
105
|
-
project.root,
|
|
106
|
-
...project.children.values()
|
|
107
|
-
]);
|
|
108
|
-
while (toAdd.size > 0) {
|
|
109
|
-
const batch = /* @__PURE__ */ new Set();
|
|
110
|
-
for (const workspace of toAdd) {
|
|
111
|
-
if (workspace === project.root && toAdd.size > 1) {
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
114
|
-
const dependencies = dependenciesMap.get(workspace);
|
|
115
|
-
if (!dependencies || dependencies.every((w) => added.has(w[0]))) {
|
|
116
|
-
batch.add(workspace);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
for (const workspace of batch) {
|
|
120
|
-
added.add(workspace);
|
|
121
|
-
toAdd.delete(workspace);
|
|
122
|
-
}
|
|
123
|
-
if (batch.size === 0) {
|
|
124
|
-
throw new Error("Circular dependency detected");
|
|
125
|
-
}
|
|
126
|
-
batches.push([...batch]);
|
|
127
|
-
}
|
|
128
|
-
return batches;
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
const SUPPORTED_UPGRADE_REGEXP = (
|
|
132
|
-
// eslint-disable-next-line regexp/no-unused-capturing-group
|
|
133
|
-
/^((?:>=|[~^])?)(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-(0|[1-9]\d*|\d*[a-z-][0-9a-z-]*)(\.(0|[1-9]\d*|\d*[a-z-][0-9a-z-]*))*)?(\+[0-9a-z-]+(\.[0-9a-z-]+)*)?$/i
|
|
134
|
-
);
|
|
135
|
-
const yarnWorkspaceProtocol = "workspace:";
|
|
136
|
-
const calcBumpRange = (workspace, range, newVersion) => {
|
|
137
|
-
if (range === "*") {
|
|
138
|
-
return range;
|
|
139
|
-
}
|
|
140
|
-
let useWorkspaceProtocol = false;
|
|
141
|
-
if (range.startsWith(yarnWorkspaceProtocol)) {
|
|
142
|
-
const slicedRange = range.slice(yarnWorkspaceProtocol.length);
|
|
143
|
-
if (slicedRange === workspace.relativeCwd) {
|
|
144
|
-
return range;
|
|
145
|
-
}
|
|
146
|
-
if (slicedRange === "*") {
|
|
147
|
-
return range;
|
|
148
|
-
}
|
|
149
|
-
range = slicedRange;
|
|
150
|
-
useWorkspaceProtocol = true;
|
|
151
|
-
}
|
|
152
|
-
const parsed = SUPPORTED_UPGRADE_REGEXP.exec(range);
|
|
153
|
-
if (!parsed) {
|
|
154
|
-
const workspaceName = getWorkspaceName(workspace);
|
|
155
|
-
throw new Error(`Couldn't bump range ${range} in ${workspaceName}`);
|
|
156
|
-
}
|
|
157
|
-
return `${useWorkspaceProtocol ? yarnWorkspaceProtocol : ""}${parsed[1] ?? ""}${newVersion}`;
|
|
158
|
-
};
|
|
159
|
-
const getHighestBumpType = (bumpTypes) => {
|
|
160
|
-
if (bumpTypes.includes("major")) {
|
|
161
|
-
return "major";
|
|
162
|
-
}
|
|
163
|
-
if (bumpTypes.includes("minor")) {
|
|
164
|
-
return "minor";
|
|
165
|
-
}
|
|
166
|
-
return "patch";
|
|
167
|
-
};
|
|
168
|
-
const calcBumpType = (bumpType, maxBumpType) => {
|
|
169
|
-
if (maxBumpType === "major") {
|
|
170
|
-
return bumpType;
|
|
171
|
-
}
|
|
172
|
-
if (maxBumpType === "minor") {
|
|
173
|
-
return bumpType === "major" || bumpType === "minor" ? "minor" : "patch";
|
|
174
|
-
}
|
|
175
|
-
return "patch";
|
|
176
|
-
};
|
|
177
|
-
const incVersion = (version, bumpType) => {
|
|
178
|
-
if (bumpType === "major" && semver.major(version) === 0) {
|
|
179
|
-
bumpType = "minor";
|
|
180
|
-
}
|
|
181
|
-
return semver.inc(version, bumpType);
|
|
182
|
-
};
|
|
183
|
-
const incrementVersion = (workspace, currentVersion, bumpType) => {
|
|
184
|
-
const newVersion = incVersion(currentVersion, bumpType);
|
|
185
|
-
if (!newVersion) {
|
|
186
|
-
throw new UsageError(
|
|
187
|
-
`Could not determine next version for "${getWorkspaceName(
|
|
188
|
-
workspace
|
|
189
|
-
)}" (currentVersion: ${currentVersion}, bumpType: ${bumpType}})`
|
|
190
|
-
);
|
|
191
|
-
}
|
|
192
|
-
return newVersion;
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
const versions = [
|
|
196
|
-
"major",
|
|
197
|
-
"minor",
|
|
198
|
-
"patch"
|
|
199
|
-
];
|
|
200
|
-
const recommendBump = async (commits, config) => {
|
|
201
|
-
const whatBump = config.whatBump;
|
|
202
|
-
if (!whatBump) {
|
|
203
|
-
throw new Error("whatBump method is missing in config");
|
|
204
|
-
}
|
|
205
|
-
const result = { ...await whatBump(commits) };
|
|
206
|
-
return {
|
|
207
|
-
...result,
|
|
208
|
-
releaseType: result.level != null ? versions[result.level] : void 0
|
|
209
|
-
};
|
|
210
|
-
};
|
|
211
|
-
const generateChangelog = (workspace, pkg, config, previousTag, newTag, commits, date) => {
|
|
212
|
-
if (!newTag) {
|
|
213
|
-
throw new Error(`Missing new tag for package "${pkg.name ?? ""}"`);
|
|
214
|
-
}
|
|
215
|
-
const originUrl = typeof pkg.repository === "string" ? pkg.repository : pkg.repository?.url;
|
|
216
|
-
const match = originUrl && typeof originUrl === "string" && /^(?:git@|https?:\/\/)(?:([^./:]+(?:\.com)?)[/:])?([^/:]+)\/([^./:]+)(?:.git)?/.exec(
|
|
217
|
-
originUrl
|
|
218
|
-
);
|
|
219
|
-
const [, gitHost, gitAccount, repoName] = match || [
|
|
220
|
-
void 0,
|
|
221
|
-
void 0,
|
|
222
|
-
void 0,
|
|
223
|
-
void 0
|
|
224
|
-
];
|
|
225
|
-
return writeChangelogString(
|
|
226
|
-
commits ?? [],
|
|
227
|
-
{
|
|
228
|
-
// @ts-expect-error - missing types
|
|
229
|
-
previousTag,
|
|
230
|
-
currentTag: newTag,
|
|
231
|
-
linkCompare: previousTag != null,
|
|
232
|
-
version: pkg.version,
|
|
233
|
-
host: gitHost ? `https://${gitHost}` : void 0,
|
|
234
|
-
owner: gitAccount,
|
|
235
|
-
repository: repoName,
|
|
236
|
-
date
|
|
237
|
-
},
|
|
238
|
-
// @ts-expect-error - missing types
|
|
239
|
-
config.writer
|
|
240
|
-
);
|
|
241
|
-
};
|
|
242
|
-
const createCommitsParser = (config) => {
|
|
243
|
-
return parseCommits(config.parser);
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
const loadConventionalCommitConfig = async (rootWorkspace, preset) => {
|
|
247
|
-
try {
|
|
248
|
-
return await loadPreset(preset);
|
|
249
|
-
} catch (error) {
|
|
250
|
-
throw new UsageError(
|
|
251
|
-
`Failed to require preset "${preset}": ${error.message}`
|
|
252
|
-
);
|
|
253
|
-
}
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
async function execvp(command, args, {
|
|
257
|
-
cwd = process.cwd(),
|
|
258
|
-
env = process.env,
|
|
259
|
-
encoding,
|
|
260
|
-
strict,
|
|
261
|
-
stdo = "pipe"
|
|
262
|
-
}) {
|
|
263
|
-
const stdoutChunks = [];
|
|
264
|
-
const stderrChunks = [];
|
|
265
|
-
if (env.PWD !== void 0) {
|
|
266
|
-
env = { ...env, PWD: cwd };
|
|
267
|
-
}
|
|
268
|
-
const subprocess = childProcess.spawn(command, args, {
|
|
269
|
-
cwd,
|
|
270
|
-
env,
|
|
271
|
-
stdio: ["ignore", stdo, stdo]
|
|
272
|
-
});
|
|
273
|
-
subprocess.stdout?.on("data", (chunk) => {
|
|
274
|
-
stdoutChunks.push(chunk);
|
|
275
|
-
});
|
|
276
|
-
subprocess.stderr?.on("data", (chunk) => {
|
|
277
|
-
stderrChunks.push(chunk);
|
|
278
|
-
});
|
|
279
|
-
return new Promise((resolve, reject) => {
|
|
280
|
-
subprocess.on("error", (err) => {
|
|
281
|
-
reject(new Error(`Process ${command} failed to spawn`));
|
|
282
|
-
});
|
|
283
|
-
subprocess.on("close", (code, signal) => {
|
|
284
|
-
const chunksToString = (chunks) => stdo === "inherit" ? "" : Buffer.concat(chunks).toString(encoding ?? "utf8");
|
|
285
|
-
const stdout = chunksToString(stdoutChunks);
|
|
286
|
-
const stderr = chunksToString(stderrChunks);
|
|
287
|
-
if (code === 0 || !strict) {
|
|
288
|
-
resolve({
|
|
289
|
-
code,
|
|
290
|
-
signal,
|
|
291
|
-
stdout,
|
|
292
|
-
stderr
|
|
293
|
-
});
|
|
294
|
-
} else {
|
|
295
|
-
reject(
|
|
296
|
-
new Error(
|
|
297
|
-
`Process ${[command, ...args].join(" ")} exited ${code !== null ? `with code ${code}` : `with signal ${signal || ""}`}:
|
|
298
|
-
stdout: ${stdout}
|
|
299
|
-
stderr: ${stderr}`
|
|
300
|
-
)
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
});
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
async function* spawnStreamStdout(command, args, {
|
|
307
|
-
cwd = process.cwd(),
|
|
308
|
-
env = process.env,
|
|
309
|
-
encoding,
|
|
310
|
-
strict,
|
|
311
|
-
separator
|
|
312
|
-
}) {
|
|
313
|
-
const stderrChunks = [];
|
|
314
|
-
if (env.PWD !== void 0) {
|
|
315
|
-
env = { ...env, PWD: cwd };
|
|
316
|
-
}
|
|
317
|
-
const subprocess = childProcess.spawn(command, args, {
|
|
318
|
-
cwd,
|
|
319
|
-
env,
|
|
320
|
-
stdio: ["ignore", "pipe", "pipe"]
|
|
321
|
-
});
|
|
322
|
-
subprocess.stderr.on("data", (chunk2) => {
|
|
323
|
-
stderrChunks.push(chunk2);
|
|
324
|
-
});
|
|
325
|
-
let chunk;
|
|
326
|
-
let payload;
|
|
327
|
-
let buffer = "";
|
|
328
|
-
for await (chunk of subprocess.stdout) {
|
|
329
|
-
buffer += chunk.toString();
|
|
330
|
-
if (buffer.includes(separator)) {
|
|
331
|
-
payload = buffer.split(separator);
|
|
332
|
-
buffer = payload.pop() || "";
|
|
333
|
-
yield* payload;
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
if (buffer) {
|
|
337
|
-
yield buffer;
|
|
338
|
-
}
|
|
339
|
-
return new Promise((resolve, reject) => {
|
|
340
|
-
subprocess.on("error", (err) => {
|
|
341
|
-
reject(new Error(`Process ${command} failed to spawn`));
|
|
342
|
-
});
|
|
343
|
-
subprocess.on("close", (code, signal) => {
|
|
344
|
-
const chunksToString = (chunks) => Buffer.concat(chunks).toString(encoding ?? "utf8");
|
|
345
|
-
const stderr = chunksToString(stderrChunks);
|
|
346
|
-
if (code === 0 || !strict) {
|
|
347
|
-
resolve({
|
|
348
|
-
code,
|
|
349
|
-
signal,
|
|
350
|
-
stderr
|
|
351
|
-
});
|
|
352
|
-
} else {
|
|
353
|
-
reject(
|
|
354
|
-
new Error(
|
|
355
|
-
`Process ${[command, ...args].join(" ")} exited ${code !== null ? `with code ${code}` : `with signal ${signal || ""}`}:
|
|
356
|
-
stderr: ${stderr}`
|
|
357
|
-
)
|
|
358
|
-
);
|
|
359
|
-
}
|
|
360
|
-
});
|
|
361
|
-
});
|
|
362
|
-
}
|
|
363
|
-
const execCommand = (workspace, commandAndArgs = [], stdo = "pipe") => {
|
|
364
|
-
const [command, ...args] = commandAndArgs;
|
|
365
|
-
if (command === void 0) {
|
|
366
|
-
throw new Error("Command is required");
|
|
367
|
-
}
|
|
368
|
-
return execvp(command, args, {
|
|
369
|
-
cwd: workspace.cwd,
|
|
370
|
-
strict: true,
|
|
371
|
-
stdo
|
|
372
|
-
});
|
|
373
|
-
};
|
|
374
|
-
const execCommandStreamStdout = (workspace, commandAndArgs = [], separator = "\n") => {
|
|
375
|
-
const [command, ...args] = commandAndArgs;
|
|
376
|
-
if (command === void 0) {
|
|
377
|
-
throw new Error("Command is required");
|
|
378
|
-
}
|
|
379
|
-
return spawnStreamStdout(command, args, {
|
|
380
|
-
cwd: workspace.cwd,
|
|
381
|
-
strict: true,
|
|
382
|
-
separator
|
|
383
|
-
});
|
|
384
|
-
};
|
|
385
|
-
|
|
386
|
-
async function asyncIterableToArray(asyncIterable) {
|
|
387
|
-
const result = [];
|
|
388
|
-
for await (const value of asyncIterable) {
|
|
389
|
-
result.push(value);
|
|
390
|
-
}
|
|
391
|
-
return result;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
const getGitCurrentBranch = async (workspace) => {
|
|
395
|
-
const { stdout } = await execCommand(workspace, [
|
|
396
|
-
"git",
|
|
397
|
-
"rev-parse",
|
|
398
|
-
"--abbrev-ref",
|
|
399
|
-
"HEAD"
|
|
400
|
-
]);
|
|
401
|
-
const currentBranch = stdout.trim();
|
|
402
|
-
if (currentBranch === "HEAD") {
|
|
403
|
-
throw new UsageError("HEAD is detached. Please checkout a branch.");
|
|
404
|
-
}
|
|
405
|
-
return currentBranch;
|
|
406
|
-
};
|
|
407
|
-
const createGitCommit = async (workspace, commitMessage) => {
|
|
408
|
-
await execCommand(workspace, ["git", "add", "-A"]);
|
|
409
|
-
await execCommand(workspace, [
|
|
410
|
-
"git",
|
|
411
|
-
"commit",
|
|
412
|
-
"--no-verify",
|
|
413
|
-
"-m",
|
|
414
|
-
commitMessage
|
|
415
|
-
]);
|
|
416
|
-
};
|
|
417
|
-
const createGitTag = async (workspace, newTag) => {
|
|
418
|
-
await execCommand(workspace, ["git", "tag", newTag, "-m", newTag]);
|
|
419
|
-
};
|
|
420
|
-
const pushCommitsAndTags = async (workspace, gitRemote, currentBranch) => {
|
|
421
|
-
await execCommand(workspace, [
|
|
422
|
-
"git",
|
|
423
|
-
"push",
|
|
424
|
-
"--follow-tags",
|
|
425
|
-
"--no-verify",
|
|
426
|
-
"--atomic",
|
|
427
|
-
gitRemote,
|
|
428
|
-
currentBranch
|
|
429
|
-
]);
|
|
430
|
-
};
|
|
431
|
-
const isBehindRemote = async (workspace, gitRemote, currentBranch) => {
|
|
432
|
-
await execCommand(workspace, ["git", "remote", "update", gitRemote]);
|
|
433
|
-
const { stdout } = await execCommand(workspace, [
|
|
434
|
-
"git",
|
|
435
|
-
"rev-list",
|
|
436
|
-
"--left-right",
|
|
437
|
-
"--count",
|
|
438
|
-
`${gitRemote}/${currentBranch}..${currentBranch}`
|
|
439
|
-
]);
|
|
440
|
-
const [behind] = stdout.split(" ").map((val) => parseInt(val, 10));
|
|
441
|
-
return behind ? behind > 0 : false;
|
|
442
|
-
};
|
|
443
|
-
const getDirtyFiles = async (workspace) => {
|
|
444
|
-
const { stdout: dirtyFiles } = await execCommand(workspace, [
|
|
445
|
-
"git",
|
|
446
|
-
"status",
|
|
447
|
-
"--porcelain"
|
|
448
|
-
]);
|
|
449
|
-
return dirtyFiles;
|
|
450
|
-
};
|
|
451
|
-
const toArray = (input) => Array.isArray(input) ? input : [input];
|
|
452
|
-
const getGitLatestTagVersion = async (workspace, {
|
|
453
|
-
prefix,
|
|
454
|
-
skipUnstable,
|
|
455
|
-
since,
|
|
456
|
-
from = "",
|
|
457
|
-
to = "HEAD",
|
|
458
|
-
path
|
|
459
|
-
} = {}) => {
|
|
460
|
-
const tagRegex = /tag:\s*(.+?)[,)]/gi;
|
|
461
|
-
for await (const chunk of execCommandStreamStdout(workspace, [
|
|
462
|
-
"git",
|
|
463
|
-
"log",
|
|
464
|
-
"--no-color",
|
|
465
|
-
"--date-order",
|
|
466
|
-
"--format=%d",
|
|
467
|
-
...since ? [`--since=${since instanceof Date ? since.toISOString() : since}`] : [],
|
|
468
|
-
[from, to].filter(Boolean).join(".."),
|
|
469
|
-
...path ? ["--", ...toArray(path)] : []
|
|
470
|
-
])) {
|
|
471
|
-
const trimmed = chunk.trim();
|
|
472
|
-
if (!trimmed) continue;
|
|
473
|
-
const matches = trimmed.matchAll(tagRegex);
|
|
474
|
-
for (const match of matches) {
|
|
475
|
-
const tag = match[1];
|
|
476
|
-
if (!tag) continue;
|
|
477
|
-
if (prefix && !tag.startsWith(prefix)) {
|
|
478
|
-
continue;
|
|
479
|
-
}
|
|
480
|
-
const version = prefix ? tag.slice(prefix.length) : tag;
|
|
481
|
-
if (!semver.valid(version)) {
|
|
482
|
-
continue;
|
|
483
|
-
}
|
|
484
|
-
if (skipUnstable && semver.prerelease(version)) {
|
|
485
|
-
continue;
|
|
486
|
-
}
|
|
487
|
-
return { tag, version };
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
return null;
|
|
491
|
-
};
|
|
492
|
-
const GIT_COMMIT_SEPARATOR = "------------------------ >8 ------------------------";
|
|
493
|
-
const getGitCommits = (workspace, {
|
|
494
|
-
path,
|
|
495
|
-
from = "",
|
|
496
|
-
to = "HEAD",
|
|
497
|
-
format = "%B",
|
|
498
|
-
reverse,
|
|
499
|
-
merges,
|
|
500
|
-
since
|
|
501
|
-
} = {}) => {
|
|
502
|
-
return execCommandStreamStdout(
|
|
503
|
-
workspace,
|
|
504
|
-
[
|
|
505
|
-
"git",
|
|
506
|
-
"log",
|
|
507
|
-
`--format=${format}%n${GIT_COMMIT_SEPARATOR}`,
|
|
508
|
-
...since ? [`--since=${since instanceof Date ? since.toISOString() : since}`] : [],
|
|
509
|
-
...reverse ? ["--reverse"] : [],
|
|
510
|
-
...merges ? ["--merges"] : [],
|
|
511
|
-
...merges === false ? ["--no-merges"] : [],
|
|
512
|
-
[from, to].filter(Boolean).join(".."),
|
|
513
|
-
...path ? ["--", ...toArray(path)] : []
|
|
514
|
-
],
|
|
515
|
-
GIT_COMMIT_SEPARATOR
|
|
516
|
-
);
|
|
517
|
-
};
|
|
518
|
-
|
|
519
|
-
async function createGitHubClient() {
|
|
520
|
-
const { GH_TOKEN, GHE_API_URL, GHE_VERSION } = process.env;
|
|
521
|
-
if (!GH_TOKEN) {
|
|
522
|
-
throw new UsageError(
|
|
523
|
-
'"GH_TOKEN" environment variable required when "createRelease" is set to "github"'
|
|
524
|
-
);
|
|
525
|
-
}
|
|
526
|
-
if (GHE_VERSION) {
|
|
527
|
-
Octokit.plugin(
|
|
528
|
-
await import(`@octokit/plugin-enterprise-rest/ghe-${GHE_VERSION}`)
|
|
529
|
-
);
|
|
530
|
-
}
|
|
531
|
-
const options = {
|
|
532
|
-
auth: `token ${GH_TOKEN}`
|
|
533
|
-
};
|
|
534
|
-
if (GHE_API_URL) {
|
|
535
|
-
options.baseUrl = GHE_API_URL;
|
|
536
|
-
}
|
|
537
|
-
return new Octokit(options);
|
|
538
|
-
}
|
|
539
|
-
const githubRegex = /^https?:\/\/github.com\/([^#/]+)\/([^#/]+?)(?:\.git)?$/;
|
|
540
|
-
const parseGithubRepoUrl = (workspace) => {
|
|
541
|
-
const repository = workspace.pkg.repository;
|
|
542
|
-
const url = typeof repository === "string" ? repository : repository?.url;
|
|
543
|
-
if (!url) {
|
|
544
|
-
throw new Error(
|
|
545
|
-
"No repository URL found in manifest. Please add one and try again. https://docs.npmjs.com/cli/v9/configuring-npm/package-json#repository"
|
|
546
|
-
);
|
|
547
|
-
}
|
|
548
|
-
const match = githubRegex.exec(url);
|
|
549
|
-
if (!match) {
|
|
550
|
-
throw new Error(`Invalid GitHub repository URL: "${url}"`);
|
|
551
|
-
}
|
|
552
|
-
const [, username, reponame] = match;
|
|
553
|
-
if (!username || !reponame) {
|
|
554
|
-
throw new Error(`Invalid GitHub repository URL: ${url}`);
|
|
555
|
-
}
|
|
556
|
-
return { username, reponame };
|
|
557
|
-
};
|
|
558
|
-
const createGitRelease = async (githubClient, parsedRepoUrl, tag, body, prerelease) => {
|
|
559
|
-
await githubClient.repos.createRelease({
|
|
560
|
-
owner: parsedRepoUrl.username,
|
|
561
|
-
repo: parsedRepoUrl.reponame,
|
|
562
|
-
// eslint-disable-next-line camelcase
|
|
563
|
-
tag_name: tag,
|
|
564
|
-
name: tag,
|
|
565
|
-
body,
|
|
566
|
-
draft: false,
|
|
567
|
-
prerelease
|
|
568
|
-
});
|
|
569
|
-
};
|
|
570
|
-
|
|
571
|
-
const createProjectWorkspace = async (root) => {
|
|
572
|
-
const map = root.pkg.workspaces ? await mapWorkspaces({ cwd: root.cwd, pkg: root.pkg }) : /* @__PURE__ */ new Map();
|
|
573
|
-
const children = new Map(
|
|
574
|
-
await Promise.all(
|
|
575
|
-
[...map.entries()].map(
|
|
576
|
-
async ([packageName, packagePath]) => [
|
|
577
|
-
packageName,
|
|
578
|
-
{
|
|
579
|
-
...await createWorkspace(packagePath),
|
|
580
|
-
relativeCwd: path.relative(root.cwd, packagePath)
|
|
581
|
-
}
|
|
582
|
-
]
|
|
583
|
-
)
|
|
584
|
-
)
|
|
585
|
-
);
|
|
586
|
-
return { root, children };
|
|
587
|
-
};
|
|
588
|
-
const isAccessible = (path2) => fs.access(path2).then(
|
|
589
|
-
() => true,
|
|
590
|
-
() => false
|
|
591
|
-
);
|
|
592
|
-
const findRootWorkspace = async (cwd) => {
|
|
593
|
-
let currentPath = cwd;
|
|
594
|
-
do {
|
|
595
|
-
const isRootIfOneOfThesePathsExists = await Promise.all([
|
|
596
|
-
isAccessible(path.join(currentPath, ".yarnrc.yml")),
|
|
597
|
-
isAccessible(path.join(currentPath, "yarn.lock"))
|
|
598
|
-
]);
|
|
599
|
-
if (isRootIfOneOfThesePathsExists.some(Boolean)) {
|
|
600
|
-
return createWorkspace(currentPath);
|
|
601
|
-
}
|
|
602
|
-
currentPath = path.dirname(currentPath);
|
|
603
|
-
} while (currentPath && currentPath !== "/");
|
|
604
|
-
return null;
|
|
605
|
-
};
|
|
606
|
-
const getPackageJsonPath = (cwd) => path.join(cwd, "package.json");
|
|
607
|
-
const createWorkspace = async (path2) => {
|
|
608
|
-
const pkg = await readPkg(path2);
|
|
609
|
-
return { cwd: path2, pkg };
|
|
610
|
-
};
|
|
611
|
-
async function writePkg(workspace, prettierOptions = void 0) {
|
|
612
|
-
const string = await prettyPkg(workspace.pkg, prettierOptions);
|
|
613
|
-
await fs.writeFile(getPackageJsonPath(workspace.cwd), string, "utf8");
|
|
614
|
-
}
|
|
615
|
-
async function readPkg(cwd) {
|
|
616
|
-
const packagePath = getPackageJsonPath(cwd);
|
|
617
|
-
const pkg = await fs.readFile(packagePath, "utf8").catch((error) => {
|
|
618
|
-
throw new Error(
|
|
619
|
-
`Failed to read package.json in "${cwd}": ${error instanceof Error ? error.message : String(error)}`
|
|
620
|
-
);
|
|
621
|
-
});
|
|
622
|
-
try {
|
|
623
|
-
return JSON.parse(pkg);
|
|
624
|
-
} catch (error) {
|
|
625
|
-
throw new Error(
|
|
626
|
-
`Failed to parse package.json in "${cwd}": ${error instanceof Error ? error.message : String(error)}`
|
|
627
|
-
);
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
const COMMIT_GUIDELINE = "See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.";
|
|
632
|
-
const CHANGELOG_HEADER = [
|
|
633
|
-
"# Changelog",
|
|
634
|
-
"",
|
|
635
|
-
"All notable changes to this project will be documented in this file.",
|
|
636
|
-
COMMIT_GUIDELINE,
|
|
637
|
-
""
|
|
638
|
-
].join("\n");
|
|
639
|
-
const updateChangelogFile = async (versionChangelogContent, tagPrefix, file = "CHANGELOG.md") => {
|
|
640
|
-
const START_OF_LAST_RELEASE_PATTERN = new RegExp(
|
|
641
|
-
`(^#+ \\[?(?:${tagPrefix})?\\d+\\.\\d+\\.\\d+|<a name=)`,
|
|
642
|
-
"m"
|
|
643
|
-
);
|
|
644
|
-
let oldContent = "";
|
|
645
|
-
try {
|
|
646
|
-
oldContent = await fs.readFile(file, "utf8");
|
|
647
|
-
const oldContentStart = oldContent.search(START_OF_LAST_RELEASE_PATTERN);
|
|
648
|
-
if (oldContentStart !== -1) {
|
|
649
|
-
oldContent = oldContent.slice(Math.max(0, oldContentStart));
|
|
650
|
-
}
|
|
651
|
-
} catch {
|
|
652
|
-
}
|
|
653
|
-
await fs.writeFile(
|
|
654
|
-
file,
|
|
655
|
-
`${CHANGELOG_HEADER}
|
|
656
|
-
${versionChangelogContent}
|
|
657
|
-
${oldContent}`
|
|
658
|
-
);
|
|
659
|
-
};
|
|
660
|
-
|
|
661
|
-
const todayInYYYYMMDD = () => {
|
|
662
|
-
const dateFormatter = Intl.DateTimeFormat("sv-SE", {
|
|
663
|
-
timeZone: "UTC"
|
|
664
|
-
});
|
|
665
|
-
return dateFormatter.format(/* @__PURE__ */ new Date());
|
|
666
|
-
};
|
|
667
|
-
const versionCommandAction = async (options, { logger = new LoggerCLI("yarn-version", { json: options.json }) } = {}) => {
|
|
668
|
-
const rootWorkspace = await (options.cwdIsRoot ? createWorkspace(options.cwd) : findRootWorkspace(options.cwd));
|
|
669
|
-
if (!rootWorkspace) {
|
|
670
|
-
throw new UsageError("Could not find root workspace from this path.");
|
|
671
|
-
}
|
|
672
|
-
const project = await createProjectWorkspace(rootWorkspace);
|
|
673
|
-
if (!options.dryRun) {
|
|
674
|
-
const dirtyFiles = await getDirtyFiles(project.root);
|
|
675
|
-
if (dirtyFiles) {
|
|
676
|
-
throw new Error(
|
|
677
|
-
`Dirty Files:
|
|
678
|
-
${dirtyFiles}
|
|
679
|
-
|
|
680
|
-
There are uncommitted changes in the git repository. Please commit or stash them first.`
|
|
681
|
-
);
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
const rootWorkspaceChildren = [...project.children.values()];
|
|
685
|
-
let rootNewVersion = "";
|
|
686
|
-
let rootNewTag = "";
|
|
687
|
-
const isMonorepo = rootWorkspaceChildren.length > 0;
|
|
688
|
-
const isMonorepoVersionIndependent = isMonorepo && !rootWorkspace.pkg.version;
|
|
689
|
-
const workspaces = !(isMonorepo && !rootWorkspaceChildren) || options.includesRoot ? [rootWorkspace, ...rootWorkspaceChildren] : rootWorkspaceChildren;
|
|
690
|
-
if (options.prerelease) {
|
|
691
|
-
throw new UsageError("--prerelease is not supported yet.");
|
|
692
|
-
}
|
|
693
|
-
if (options.ignoreChanges) {
|
|
694
|
-
throw new UsageError("--ignore-changes is not supported yet.");
|
|
695
|
-
}
|
|
696
|
-
const [
|
|
697
|
-
conventionalCommitConfig,
|
|
698
|
-
githubClient,
|
|
699
|
-
parsedRepoUrl,
|
|
700
|
-
gitCurrentBranch
|
|
701
|
-
] = await Promise.all([
|
|
702
|
-
loadConventionalCommitConfig(rootWorkspace, options.preset),
|
|
703
|
-
// create client early to fail fast if necessary
|
|
704
|
-
options.createRelease ? createGitHubClient() : void 0,
|
|
705
|
-
options.createRelease ? parseGithubRepoUrl(rootWorkspace) : void 0,
|
|
706
|
-
getGitCurrentBranch(rootWorkspace)
|
|
707
|
-
]);
|
|
708
|
-
const rootPreviousVersionTagPromise = options.force || isMonorepoVersionIndependent ? null : getGitLatestTagVersion(rootWorkspace, {
|
|
709
|
-
prefix: options.tagVersionPrefix,
|
|
710
|
-
skipUnstable: true
|
|
711
|
-
});
|
|
712
|
-
const buildTagName = (workspace, version) => `${isMonorepo && workspace !== rootWorkspace ? `${getWorkspaceName(workspace)}@` : options.tagVersionPrefix}${version}`;
|
|
713
|
-
const changedWorkspaces = /* @__PURE__ */ new Map();
|
|
714
|
-
const dependenciesMap = isMonorepo ? buildDependenciesMaps(project) : null;
|
|
715
|
-
const bumpableWorkspaces = [];
|
|
716
|
-
logger.group(
|
|
717
|
-
options.force ? "Check all workspaces (force option)" : "Finding changed workspaces",
|
|
718
|
-
() => {
|
|
719
|
-
for (const workspace of workspaces) {
|
|
720
|
-
const workspaceName = getWorkspaceName(workspace);
|
|
721
|
-
const isRoot = workspace === rootWorkspace;
|
|
722
|
-
if (isRoot && isMonorepo && isMonorepoVersionIndependent) continue;
|
|
723
|
-
const version = workspace.pkg.version;
|
|
724
|
-
if (!version || version === "0.0.0") {
|
|
725
|
-
if ((isRoot || isMonorepoVersionIndependent) && (!isMonorepo || !isMonorepoVersionIndependent)) {
|
|
726
|
-
throw new UsageError(
|
|
727
|
-
'package.json has no version in its manifest. For the first release, set to "1.0.0-pre" or "0.1.0-pre".'
|
|
728
|
-
);
|
|
729
|
-
}
|
|
730
|
-
logger.info(`${workspaceName}: skipped (no version)`);
|
|
731
|
-
continue;
|
|
732
|
-
}
|
|
733
|
-
bumpableWorkspaces.push({
|
|
734
|
-
workspace,
|
|
735
|
-
workspaceName,
|
|
736
|
-
isRoot,
|
|
737
|
-
version
|
|
738
|
-
});
|
|
739
|
-
}
|
|
740
|
-
if (bumpableWorkspaces.length > 0) {
|
|
741
|
-
logger.info("Found bumpable workspaces", {
|
|
742
|
-
count: bumpableWorkspaces.length
|
|
743
|
-
});
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
);
|
|
747
|
-
const previousTagVersionByWorkspace = new Map(
|
|
748
|
-
await Promise.all(
|
|
749
|
-
bumpableWorkspaces.map(async ({ workspace, workspaceName, isRoot }) => {
|
|
750
|
-
const packageOption = isMonorepo && isMonorepoVersionIndependent ? workspaceName : void 0;
|
|
751
|
-
const tagPrefix = packageOption ? `${packageOption}@` : options.tagVersionPrefix;
|
|
752
|
-
const previousTagAndVersion = await (isRoot || !isMonorepoVersionIndependent ? rootPreviousVersionTagPromise : getGitLatestTagVersion(rootWorkspace, {
|
|
753
|
-
path: workspace.relativeCwd,
|
|
754
|
-
prefix: tagPrefix,
|
|
755
|
-
skipUnstable: true
|
|
756
|
-
}));
|
|
757
|
-
return [workspace, previousTagAndVersion];
|
|
758
|
-
})
|
|
759
|
-
)
|
|
760
|
-
);
|
|
761
|
-
if (options.dryRun) {
|
|
762
|
-
logger.info("Previous tags", {
|
|
763
|
-
previousTagByWorkspace: Object.fromEntries(
|
|
764
|
-
[...previousTagVersionByWorkspace.entries()].map(
|
|
765
|
-
([workspace, previousTagVersion]) => [
|
|
766
|
-
getWorkspaceName(workspace),
|
|
767
|
-
previousTagVersion?.tag
|
|
768
|
-
]
|
|
769
|
-
)
|
|
770
|
-
)
|
|
771
|
-
});
|
|
772
|
-
}
|
|
773
|
-
const parseCommits = createCommitsParser(conventionalCommitConfig);
|
|
774
|
-
const commitsByWorkspace = options.force ? void 0 : new Map(
|
|
775
|
-
await Promise.all(
|
|
776
|
-
bumpableWorkspaces.map(async ({ workspace }) => {
|
|
777
|
-
const previousTagVersion = previousTagVersionByWorkspace.get(workspace);
|
|
778
|
-
const commits = await asyncIterableToArray(
|
|
779
|
-
parseCommits(
|
|
780
|
-
getGitCommits(rootWorkspace, {
|
|
781
|
-
path: workspace.relativeCwd,
|
|
782
|
-
from: previousTagVersion?.tag || void 0
|
|
783
|
-
})
|
|
784
|
-
)
|
|
785
|
-
);
|
|
786
|
-
return [workspace, commits];
|
|
787
|
-
})
|
|
788
|
-
)
|
|
789
|
-
);
|
|
790
|
-
await logger.group("Finding changed workspaces", async () => {
|
|
791
|
-
for (const { workspace, workspaceName } of bumpableWorkspaces) {
|
|
792
|
-
let bumpType = null;
|
|
793
|
-
let bumpReason;
|
|
794
|
-
if (options.force) {
|
|
795
|
-
bumpType = options.force;
|
|
796
|
-
bumpReason = "forced by --force flag";
|
|
797
|
-
} else {
|
|
798
|
-
const commits = commitsByWorkspace?.get(workspace);
|
|
799
|
-
if (!commits || commits.length === 0) {
|
|
800
|
-
logger.info(`${workspaceName}: skipped (no changes)`);
|
|
801
|
-
continue;
|
|
802
|
-
}
|
|
803
|
-
const { releaseType, reason } = await recommendBump(
|
|
804
|
-
commits,
|
|
805
|
-
conventionalCommitConfig
|
|
806
|
-
);
|
|
807
|
-
bumpReason = reason;
|
|
808
|
-
if (releaseType) {
|
|
809
|
-
bumpType = releaseType;
|
|
810
|
-
}
|
|
811
|
-
}
|
|
812
|
-
if (bumpType) {
|
|
813
|
-
if (isMonorepo && !workspace.pkg.name) {
|
|
814
|
-
throw new Error("Workspace name is required");
|
|
815
|
-
}
|
|
816
|
-
const currentVersion = workspace.pkg.version;
|
|
817
|
-
if (!currentVersion) {
|
|
818
|
-
throw new UsageError(
|
|
819
|
-
`Invalid "${getWorkspaceName(workspace)}" version`
|
|
820
|
-
);
|
|
821
|
-
}
|
|
822
|
-
changedWorkspaces.set(workspace, {
|
|
823
|
-
bumpType,
|
|
824
|
-
bumpReason
|
|
825
|
-
});
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
if (changedWorkspaces.size === 0) {
|
|
829
|
-
logger.info("No changed workspaces");
|
|
830
|
-
}
|
|
831
|
-
});
|
|
832
|
-
const bumpedWorkspaces = /* @__PURE__ */ new Map();
|
|
833
|
-
const noVersionToUpdateWorkspaces = /* @__PURE__ */ new Map();
|
|
834
|
-
const batches = dependenciesMap ? buildTopologicalOrderBatches(project, dependenciesMap) : [[rootWorkspace]];
|
|
835
|
-
logger.group("Preparing bumping", () => {
|
|
836
|
-
for (const batch of batches) {
|
|
837
|
-
for (const workspace of batch) {
|
|
838
|
-
const currentVersion = workspace.pkg.version;
|
|
839
|
-
if (!currentVersion && !workspace.pkg.private) {
|
|
840
|
-
throw new UsageError(
|
|
841
|
-
`Invalid "${getWorkspaceName(workspace)}" version`
|
|
842
|
-
);
|
|
843
|
-
}
|
|
844
|
-
const changedWorkspace = changedWorkspaces.get(workspace);
|
|
845
|
-
let bumpType = null;
|
|
846
|
-
const bumpReasons = [];
|
|
847
|
-
const dependenciesToBump = [];
|
|
848
|
-
if (changedWorkspace) {
|
|
849
|
-
bumpType = changedWorkspace.bumpType;
|
|
850
|
-
bumpReasons.push(changedWorkspace.bumpReason || "by commits");
|
|
851
|
-
}
|
|
852
|
-
const dependencies = dependenciesMap?.get(workspace);
|
|
853
|
-
if (dependencies) {
|
|
854
|
-
for (const [
|
|
855
|
-
dependencyWorkspace,
|
|
856
|
-
dependencyType,
|
|
857
|
-
dependencyDescriptor
|
|
858
|
-
] of dependencies) {
|
|
859
|
-
const dependencyBumpedWorkspace = bumpedWorkspaces.get(dependencyWorkspace);
|
|
860
|
-
if (!dependencyBumpedWorkspace) {
|
|
861
|
-
continue;
|
|
862
|
-
}
|
|
863
|
-
if (dependencyType === "peerDependencies" && !options.alwaysBumpPeerDependencies && // skip when peerdependency with a new version satisfied by the existing range.
|
|
864
|
-
satisfies(
|
|
865
|
-
dependencyBumpedWorkspace.newVersion,
|
|
866
|
-
dependencyDescriptor.selector,
|
|
867
|
-
{ includePrerelease: true }
|
|
868
|
-
)) {
|
|
869
|
-
continue;
|
|
870
|
-
}
|
|
871
|
-
const newRange = calcBumpRange(
|
|
872
|
-
workspace,
|
|
873
|
-
dependencyDescriptor.selector,
|
|
874
|
-
dependencyBumpedWorkspace.newVersion
|
|
875
|
-
);
|
|
876
|
-
if (dependencyDescriptor.selector === newRange) {
|
|
877
|
-
continue;
|
|
878
|
-
}
|
|
879
|
-
dependenciesToBump.push([
|
|
880
|
-
dependencyType,
|
|
881
|
-
dependencyDescriptor,
|
|
882
|
-
newRange
|
|
883
|
-
]);
|
|
884
|
-
bumpType = getHighestBumpType([
|
|
885
|
-
bumpType ?? "patch",
|
|
886
|
-
calcBumpType(
|
|
887
|
-
dependencyBumpedWorkspace.bumpType,
|
|
888
|
-
options.bumpDependentsHighestAs
|
|
889
|
-
)
|
|
890
|
-
]);
|
|
891
|
-
bumpReasons.push(
|
|
892
|
-
`Version bump for dependency: ${PackageDescriptorNameUtils.stringify(dependencyDescriptor.name)}`
|
|
893
|
-
);
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
const workspaceName = getWorkspaceName(workspace);
|
|
897
|
-
if (!currentVersion) {
|
|
898
|
-
logger.info(`${workspaceName}: skipped (no version)`);
|
|
899
|
-
if (workspace !== rootWorkspace) {
|
|
900
|
-
noVersionToUpdateWorkspaces.set(workspace, {
|
|
901
|
-
dependenciesToBump
|
|
902
|
-
});
|
|
903
|
-
}
|
|
904
|
-
} else if (!bumpType) {
|
|
905
|
-
logger.info(
|
|
906
|
-
`${workspaceName}: skipped (${changedWorkspace ? `no bump recommended by ${options.preset}` : "no changes"})`
|
|
907
|
-
);
|
|
908
|
-
} else {
|
|
909
|
-
const newVersion = incrementVersion(
|
|
910
|
-
workspace,
|
|
911
|
-
currentVersion,
|
|
912
|
-
bumpType
|
|
913
|
-
);
|
|
914
|
-
const tagName = buildTagName(workspace, newVersion);
|
|
915
|
-
if (workspace === rootWorkspace) {
|
|
916
|
-
rootNewVersion = newVersion;
|
|
917
|
-
rootNewTag = tagName;
|
|
918
|
-
}
|
|
919
|
-
if (workspace !== rootWorkspace || !isMonorepo) {
|
|
920
|
-
const bumpReason = bumpReasons.join("\n");
|
|
921
|
-
bumpedWorkspaces.set(workspace, {
|
|
922
|
-
currentVersion,
|
|
923
|
-
bumpType,
|
|
924
|
-
bumpReason,
|
|
925
|
-
bumpForDependenciesReasons: changedWorkspace ? bumpReasons.slice(1) : bumpReasons,
|
|
926
|
-
newVersion,
|
|
927
|
-
newTag: tagName,
|
|
928
|
-
hasChanged: changedWorkspace !== void 0,
|
|
929
|
-
dependenciesToBump
|
|
930
|
-
});
|
|
931
|
-
logger.info(
|
|
932
|
-
`${workspaceName}: ${currentVersion} -> ${!isMonorepo || isMonorepoVersionIndependent ? newVersion : "bump"} (${bumpReason.replace("\n", " ; ")})`
|
|
933
|
-
);
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
}
|
|
938
|
-
});
|
|
939
|
-
if (isMonorepo && !isMonorepoVersionIndependent) {
|
|
940
|
-
const currentVersion = rootWorkspace.pkg.version;
|
|
941
|
-
const highestBumpType = getHighestBumpType(
|
|
942
|
-
[...bumpedWorkspaces.values()].map(({ bumpType }) => bumpType)
|
|
943
|
-
);
|
|
944
|
-
const newVersion = incrementVersion(
|
|
945
|
-
rootWorkspace,
|
|
946
|
-
currentVersion,
|
|
947
|
-
highestBumpType
|
|
948
|
-
);
|
|
949
|
-
rootNewVersion = newVersion;
|
|
950
|
-
rootNewTag = buildTagName(rootWorkspace, newVersion);
|
|
951
|
-
[...bumpedWorkspaces.entries()].forEach(([workspace, bumpedWorkspace]) => {
|
|
952
|
-
const isRoot = workspace === rootWorkspace;
|
|
953
|
-
if (isRoot) {
|
|
954
|
-
throw new Error("Unexpected root found in bumped workspaces");
|
|
955
|
-
}
|
|
956
|
-
bumpedWorkspace.bumpType = highestBumpType;
|
|
957
|
-
bumpedWorkspace.newVersion = newVersion;
|
|
958
|
-
bumpedWorkspace.newTag = null;
|
|
959
|
-
});
|
|
960
|
-
[
|
|
961
|
-
...bumpedWorkspaces.entries(),
|
|
962
|
-
...noVersionToUpdateWorkspaces.entries()
|
|
963
|
-
].forEach(([workspace, { dependenciesToBump }]) => {
|
|
964
|
-
dependenciesToBump.forEach((dependencyToBump) => {
|
|
965
|
-
dependencyToBump[2] = calcBumpRange(
|
|
966
|
-
workspace,
|
|
967
|
-
dependencyToBump[1].selector,
|
|
968
|
-
newVersion
|
|
969
|
-
);
|
|
970
|
-
});
|
|
971
|
-
});
|
|
972
|
-
bumpedWorkspaces.set(rootWorkspace, {
|
|
973
|
-
currentVersion,
|
|
974
|
-
bumpType: highestBumpType,
|
|
975
|
-
hasChanged: true,
|
|
976
|
-
newVersion: rootNewVersion,
|
|
977
|
-
newTag: rootNewTag,
|
|
978
|
-
dependenciesToBump: []
|
|
979
|
-
});
|
|
980
|
-
logger.info(
|
|
981
|
-
`${currentVersion} -> ${newVersion}`
|
|
982
|
-
// TODO Json only:
|
|
983
|
-
// {
|
|
984
|
-
// oldVersion: currentVersion,
|
|
985
|
-
// newVersion, },
|
|
986
|
-
);
|
|
987
|
-
}
|
|
988
|
-
await logger.group("Applying changes", async () => {
|
|
989
|
-
if (!options.dryRun) {
|
|
990
|
-
logger.info(`${getWorkspaceName(rootWorkspace)}: Running install`);
|
|
991
|
-
await execCommand(rootWorkspace, ["yarn", "install"], "inherit");
|
|
992
|
-
logger.info("Lifecycle script: preversion");
|
|
993
|
-
if (isMonorepoVersionIndependent && rootWorkspace.pkg.scripts?.preversion) {
|
|
994
|
-
await execCommand(
|
|
995
|
-
rootWorkspace,
|
|
996
|
-
["yarn", "run", "preversion"],
|
|
997
|
-
"inherit"
|
|
998
|
-
);
|
|
999
|
-
}
|
|
1000
|
-
for (const workspace of bumpedWorkspaces.keys()) {
|
|
1001
|
-
if (workspace.pkg.scripts?.preversion) {
|
|
1002
|
-
await execCommand(
|
|
1003
|
-
workspace,
|
|
1004
|
-
["yarn", "run", "preversion"],
|
|
1005
|
-
"inherit"
|
|
1006
|
-
);
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1009
|
-
logger.info("Modifying versions in package.json");
|
|
1010
|
-
await Promise.all(
|
|
1011
|
-
[...bumpedWorkspaces.entries()].map(
|
|
1012
|
-
([workspace, { newVersion, dependenciesToBump }]) => {
|
|
1013
|
-
workspace.pkg.version = newVersion;
|
|
1014
|
-
for (const [
|
|
1015
|
-
dependencyType,
|
|
1016
|
-
dependencyDescriptor,
|
|
1017
|
-
dependencyNewRange
|
|
1018
|
-
] of dependenciesToBump) {
|
|
1019
|
-
const newDescriptor = PackageDependencyDescriptorUtils.make(
|
|
1020
|
-
dependencyDescriptor,
|
|
1021
|
-
dependencyNewRange
|
|
1022
|
-
);
|
|
1023
|
-
const [key, newValue] = PackageDependencyDescriptorUtils.stringify(newDescriptor);
|
|
1024
|
-
workspace.pkg[dependencyType][key] = newValue;
|
|
1025
|
-
}
|
|
1026
|
-
return writePkg(workspace);
|
|
1027
|
-
}
|
|
1028
|
-
)
|
|
1029
|
-
);
|
|
1030
|
-
logger.info(`${getWorkspaceName(rootWorkspace)}: Running install`);
|
|
1031
|
-
await execCommand(rootWorkspace, ["yarn", "install"], "inherit");
|
|
1032
|
-
logger.info("Lifecycle script: version");
|
|
1033
|
-
for (const workspace of bumpedWorkspaces.keys()) {
|
|
1034
|
-
if (workspace.pkg.scripts?.version) {
|
|
1035
|
-
await execCommand(workspace, ["yarn", "run", "version"], "inherit");
|
|
1036
|
-
}
|
|
1037
|
-
}
|
|
1038
|
-
}
|
|
1039
|
-
if (isMonorepoVersionIndependent && rootWorkspace.pkg.scripts?.version) {
|
|
1040
|
-
await execCommand(rootWorkspace, ["yarn", "run", "version"], "inherit");
|
|
1041
|
-
}
|
|
1042
|
-
});
|
|
1043
|
-
const changelogs = /* @__PURE__ */ new Map();
|
|
1044
|
-
await Promise.all(
|
|
1045
|
-
[...bumpedWorkspaces.entries()].map(
|
|
1046
|
-
async ([
|
|
1047
|
-
workspace,
|
|
1048
|
-
{ newTag, hasChanged, bumpReason, bumpForDependenciesReasons }
|
|
1049
|
-
]) => {
|
|
1050
|
-
const commits = commitsByWorkspace?.get(workspace);
|
|
1051
|
-
let changelog = await generateChangelog(
|
|
1052
|
-
rootWorkspace,
|
|
1053
|
-
workspace.pkg,
|
|
1054
|
-
conventionalCommitConfig,
|
|
1055
|
-
previousTagVersionByWorkspace.get(workspace)?.tag || null,
|
|
1056
|
-
isMonorepoVersionIndependent ? newTag : rootNewTag,
|
|
1057
|
-
commits,
|
|
1058
|
-
todayInYYYYMMDD()
|
|
1059
|
-
// {
|
|
1060
|
-
// path: workspaceRelativePath,
|
|
1061
|
-
// previousTag: previousTagByWorkspace.get(workspace) || undefined,
|
|
1062
|
-
// verbose: options.verbose,
|
|
1063
|
-
// tagPrefix: options.tagVersionPrefix,
|
|
1064
|
-
// lernaPackage:
|
|
1065
|
-
// rootWorkspace === workspace
|
|
1066
|
-
// ? undefined
|
|
1067
|
-
// : getWorkspaceName(workspace),
|
|
1068
|
-
// },
|
|
1069
|
-
);
|
|
1070
|
-
if (bumpForDependenciesReasons && workspace !== rootWorkspace) {
|
|
1071
|
-
if (bumpForDependenciesReasons.length > 0) {
|
|
1072
|
-
changelog += `${!changelog.endsWith("\n\n") ? "\n" : ""}${bumpForDependenciesReasons.join("\n")}
|
|
1073
|
-
|
|
1074
|
-
`;
|
|
1075
|
-
}
|
|
1076
|
-
}
|
|
1077
|
-
if (changelog.slice(changelog.indexOf("\n")).trim().length === 0) {
|
|
1078
|
-
changelog += `${!changelog.endsWith("\n\n") ? "\n" : ""}Note: no notable changes
|
|
1079
|
-
|
|
1080
|
-
`;
|
|
1081
|
-
}
|
|
1082
|
-
if (!changelog && rootWorkspace === workspace) {
|
|
1083
|
-
throw new Error("No changelog found for root workspace");
|
|
1084
|
-
}
|
|
1085
|
-
changelogs.set(workspace, changelog);
|
|
1086
|
-
if (options.changelog) {
|
|
1087
|
-
if (options.dryRun) {
|
|
1088
|
-
logger.info(
|
|
1089
|
-
`${getWorkspaceName(workspace)}: ${options.changelog}
|
|
1090
|
-
${changelog}`
|
|
1091
|
-
);
|
|
1092
|
-
} else {
|
|
1093
|
-
await updateChangelogFile(
|
|
1094
|
-
changelog,
|
|
1095
|
-
options.tagVersionPrefix,
|
|
1096
|
-
`${workspace.cwd}/${options.changelog}`
|
|
1097
|
-
);
|
|
1098
|
-
}
|
|
1099
|
-
}
|
|
1100
|
-
}
|
|
1101
|
-
)
|
|
1102
|
-
);
|
|
1103
|
-
if (!options.dryRun) {
|
|
1104
|
-
logger.separator();
|
|
1105
|
-
logger.info(`${getWorkspaceName(rootWorkspace)}: Running install`);
|
|
1106
|
-
await execCommand(rootWorkspace, ["yarn", "install"], "inherit");
|
|
1107
|
-
logger.separator();
|
|
1108
|
-
logger.info("Commit, tag and push", {
|
|
1109
|
-
changedFiles: await getDirtyFiles(rootWorkspace)
|
|
1110
|
-
});
|
|
1111
|
-
const tagsSet = new Set(
|
|
1112
|
-
[...bumpedWorkspaces.values()].map(({ newTag }) => newTag).filter((newTag) => newTag !== null)
|
|
1113
|
-
);
|
|
1114
|
-
const tagsInCommitMessage = [...tagsSet].map((tag) => `- ${tag}`).join("\n");
|
|
1115
|
-
const message = options.commitMessage.replace(/\\n/g, "\n").replace(
|
|
1116
|
-
/%a/g,
|
|
1117
|
-
isMonorepoVersionIndependent ? `
|
|
1118
|
-
|
|
1119
|
-
${tagsInCommitMessage}` : rootNewVersion
|
|
1120
|
-
).replace(/%s/g, rootNewTag).replace(/%v/g, rootNewVersion).replace(/%t/g, tagsInCommitMessage);
|
|
1121
|
-
await createGitCommit(rootWorkspace, message);
|
|
1122
|
-
for (const [workspace, { newTag }] of bumpedWorkspaces.entries()) {
|
|
1123
|
-
if (newTag === null) continue;
|
|
1124
|
-
await createGitTag(workspace, newTag);
|
|
1125
|
-
}
|
|
1126
|
-
if (await isBehindRemote(rootWorkspace, options.gitRemote, gitCurrentBranch)) {
|
|
1127
|
-
logger.error("Remote is ahead, aborting");
|
|
1128
|
-
process.exit(1);
|
|
1129
|
-
}
|
|
1130
|
-
await pushCommitsAndTags(
|
|
1131
|
-
rootWorkspace,
|
|
1132
|
-
options.gitRemote,
|
|
1133
|
-
gitCurrentBranch
|
|
1134
|
-
);
|
|
1135
|
-
if (rootWorkspace.pkg.scripts?.postversion) {
|
|
1136
|
-
await execCommand(
|
|
1137
|
-
rootWorkspace,
|
|
1138
|
-
["yarn", "run", "postversion"],
|
|
1139
|
-
"inherit"
|
|
1140
|
-
);
|
|
1141
|
-
}
|
|
1142
|
-
if (options.createRelease && githubClient && parsedRepoUrl) {
|
|
1143
|
-
logger.info("Create git release");
|
|
1144
|
-
await Promise.all(
|
|
1145
|
-
[...bumpedWorkspaces.entries()].map(([workspace, { newTag }]) => {
|
|
1146
|
-
if (newTag === null) return void 0;
|
|
1147
|
-
const changelog = changelogs.get(workspace);
|
|
1148
|
-
if (!changelog) {
|
|
1149
|
-
logger.warn(
|
|
1150
|
-
`No changelog found for workspace: ${getWorkspaceName(
|
|
1151
|
-
workspace
|
|
1152
|
-
)}`
|
|
1153
|
-
);
|
|
1154
|
-
return void 0;
|
|
1155
|
-
}
|
|
1156
|
-
return createGitRelease(
|
|
1157
|
-
githubClient,
|
|
1158
|
-
parsedRepoUrl,
|
|
1159
|
-
newTag,
|
|
1160
|
-
changelog,
|
|
1161
|
-
!!options.prerelease
|
|
1162
|
-
);
|
|
1163
|
-
})
|
|
1164
|
-
);
|
|
1165
|
-
}
|
|
1166
|
-
}
|
|
1167
|
-
};
|
|
1168
|
-
const Defaults = {
|
|
1169
|
-
cwd: process.cwd(),
|
|
1170
|
-
includesRoot: false,
|
|
1171
|
-
dryRun: false,
|
|
1172
|
-
tagVersionPrefix: "v",
|
|
1173
|
-
changelog: "CHANGELOG.md",
|
|
1174
|
-
commitMessage: "chore: release %a",
|
|
1175
|
-
bumpDependentsHighestAs: "major",
|
|
1176
|
-
alwaysBumpPeerDependencies: false,
|
|
1177
|
-
gitRemote: "origin",
|
|
1178
|
-
verbose: false
|
|
1179
|
-
};
|
|
1180
|
-
program.command("version").usage("Bump package version using conventional commit").addOption(
|
|
1181
|
-
new Option("--includes-root", "Release root workspace [untested]").default(
|
|
1182
|
-
Defaults.includesRoot
|
|
1183
|
-
)
|
|
1184
|
-
).addOption(new Option("--cwd", "working directory").default(process.cwd())).addOption(
|
|
1185
|
-
new Option(
|
|
1186
|
-
"--dry-run",
|
|
1187
|
-
"Print the versions without actually generating the package archive"
|
|
1188
|
-
).default(Defaults.dryRun)
|
|
1189
|
-
).addOption(new Option("-v,--verbose").default(Defaults.verbose)).addOption(
|
|
1190
|
-
new Option("--force <type>", "Specify the release type").choices([
|
|
1191
|
-
"major",
|
|
1192
|
-
"minor",
|
|
1193
|
-
"patch"
|
|
1194
|
-
])
|
|
1195
|
-
).addOption(
|
|
1196
|
-
new Option(
|
|
1197
|
-
"--prerelease [releaseType]",
|
|
1198
|
-
"Add a prerelease identifier to new versions"
|
|
1199
|
-
)
|
|
1200
|
-
).addOption(
|
|
1201
|
-
new Option(
|
|
1202
|
-
"--preset <presetName>",
|
|
1203
|
-
"Conventional Changelog preset to require. Defaults to conventional-changelog-conventionalcommits."
|
|
1204
|
-
).default("conventional-changelog-conventionalcommits")
|
|
1205
|
-
).addOption(
|
|
1206
|
-
new Option("--tag-version-prefix <prefix>", "Tag version prefix").default(
|
|
1207
|
-
Defaults.tagVersionPrefix
|
|
1208
|
-
)
|
|
1209
|
-
).addOption(
|
|
1210
|
-
new Option(
|
|
1211
|
-
"--changelog <path>",
|
|
1212
|
-
"Changelog path. Default to CHANGELOG.md."
|
|
1213
|
-
).default(Defaults.changelog)
|
|
1214
|
-
).addOption(
|
|
1215
|
-
new Option(
|
|
1216
|
-
"-m,--commit-message <message>",
|
|
1217
|
-
'Commit message. Default to "chore: release %a". You can use %v for the version, %s for the version with prefix, %t to list tags, %a for auto best display.'
|
|
1218
|
-
).default(Defaults.commitMessage)
|
|
1219
|
-
).addOption(
|
|
1220
|
-
new Option("--create-release <type>", "Create a release").choices([
|
|
1221
|
-
"github"
|
|
1222
|
-
])
|
|
1223
|
-
).addOption(
|
|
1224
|
-
new Option(
|
|
1225
|
-
"--bump-dependents-highest-as <type>",
|
|
1226
|
-
"Bump dependents highest version as major, minor or patch"
|
|
1227
|
-
).choices(["major", "minor", "patch"]).default(Defaults.bumpDependentsHighestAs)
|
|
1228
|
-
).addOption(
|
|
1229
|
-
new Option(
|
|
1230
|
-
"--always-bump-peer-dependencies",
|
|
1231
|
-
"Always bump peer dependencies. Default to bumping only if the version doesn't satisfies the peer dependency range."
|
|
1232
|
-
).default(Defaults.alwaysBumpPeerDependencies)
|
|
1233
|
-
).addOption(
|
|
1234
|
-
new Option(
|
|
1235
|
-
"--git-remote <remote-name>",
|
|
1236
|
-
"Git remote to push commits and tags to"
|
|
1237
|
-
).default(Defaults.gitRemote)
|
|
1238
|
-
).addOption(
|
|
1239
|
-
new Option(
|
|
1240
|
-
"--ignore-changes <glob>",
|
|
1241
|
-
'Ignore changes in files matching the glob. Example: "**/*.test.js"'
|
|
1242
|
-
)
|
|
1243
|
-
).addOption(new Option("--json", "Logger json")).action((options) => versionCommandAction(options));
|
|
1244
|
-
|
|
1245
|
-
const pkg = JSON.parse(
|
|
1246
|
-
// eslint-disable-next-line unicorn/prefer-json-parse-buffer
|
|
1247
|
-
fs$1.readFileSync(new URL("../package.json", import.meta.url), "utf8")
|
|
1248
|
-
);
|
|
1249
|
-
program.name(pkg.name).description(pkg.description).version(pkg.version);
|
|
1250
|
-
program.parse();
|
|
1251
|
-
//# sourceMappingURL=index-node.mjs.map
|