@effected/changesets 0.0.0-stage → 0.2.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/BumpType.js +27 -0
- package/ChangesetConfig.js +102 -0
- package/ChangesetFile.js +68 -0
- package/ChangesetLint.js +108 -0
- package/LICENSE +21 -0
- package/PendingChangesets.js +210 -0
- package/PreState.js +54 -0
- package/README.md +151 -2
- package/ReleasePlan.js +587 -0
- package/errors.js +51 -0
- package/index.d.ts +483 -0
- package/index.js +9 -0
- package/internal/lint/content-structure.js +37 -0
- package/internal/lint/dependency-table-format.js +117 -0
- package/internal/lint/docs.js +72 -0
- package/internal/lint/heading-hierarchy.js +32 -0
- package/internal/lint/required-sections.js +23 -0
- package/internal/lint/uncategorized-content.js +22 -0
- package/lint.d.ts +92 -0
- package/lint.js +3 -0
- package/package.json +65 -5
- package/tsdoc-metadata.json +11 -0
package/ReleasePlan.js
ADDED
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
import { ChangesetConfig } from "./ChangesetConfig.js";
|
|
2
|
+
import { ChangesetConfigError, UnknownPackageError } from "./errors.js";
|
|
3
|
+
import { PendingChangeset, PendingChangesets } from "./PendingChangesets.js";
|
|
4
|
+
import { PreState } from "./PreState.js";
|
|
5
|
+
import { Effect, FileSystem, Option, Path, Result, Schema } from "effect";
|
|
6
|
+
import { posix } from "node:path";
|
|
7
|
+
import { GlobPattern } from "@effected/glob";
|
|
8
|
+
import { InvalidVersionError, Range, SemVer } from "@effected/semver";
|
|
9
|
+
import { WorkspaceDiscovery, WorkspaceDiscoveryError, WorkspacePackage } from "@effected/workspaces";
|
|
10
|
+
|
|
11
|
+
// src/ReleasePlan.ts
|
|
12
|
+
const DEPENDENCY_KINDS = [
|
|
13
|
+
"dependencies",
|
|
14
|
+
"devDependencies",
|
|
15
|
+
"peerDependencies",
|
|
16
|
+
"optionalDependencies"
|
|
17
|
+
];
|
|
18
|
+
const RANK = {
|
|
19
|
+
none: 0,
|
|
20
|
+
patch: 1,
|
|
21
|
+
minor: 2,
|
|
22
|
+
major: 3
|
|
23
|
+
};
|
|
24
|
+
/** Ends assembly with a typed failure from inside the fixpoint loop; never escapes `assemble`. */
|
|
25
|
+
var Halt = class {
|
|
26
|
+
failure;
|
|
27
|
+
constructor(failure) {
|
|
28
|
+
this.failure = failure;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* A package version as node-semver reads one without `loose`: surrounding
|
|
33
|
+
* whitespace and a single leading `v` are dropped, so `v1.0.0` is `1.0.0`. Any
|
|
34
|
+
* other prefix, `=1.2.0` among them, stays and fails to parse.
|
|
35
|
+
*/
|
|
36
|
+
const cleanVersion = (version) => {
|
|
37
|
+
const trimmed = version.trim();
|
|
38
|
+
return trimmed.startsWith("v") ? trimmed.slice(1) : trimmed;
|
|
39
|
+
};
|
|
40
|
+
const parseVersion = (version) => {
|
|
41
|
+
const parsed = SemVer.parseResult(cleanVersion(version));
|
|
42
|
+
if (Result.isFailure(parsed)) throw new Halt(parsed.failure);
|
|
43
|
+
return parsed.success;
|
|
44
|
+
};
|
|
45
|
+
const WORKSPACE = /^workspace:/;
|
|
46
|
+
/** A range, also as node-semver reads a `v` before a version in one: `^v1.0.0` is `^1.0.0`. */
|
|
47
|
+
const parseRange = (specifier) => {
|
|
48
|
+
if (specifier.includes(":")) return void 0;
|
|
49
|
+
const parsed = Range.parseResult(specifier);
|
|
50
|
+
if (Result.isSuccess(parsed)) return parsed.success;
|
|
51
|
+
return Result.getOrUndefined(Range.parseResult(specifier.replace(/(^|[\s^~<>=])v(?=\d)/g, "$1")));
|
|
52
|
+
};
|
|
53
|
+
const pointsAt = (specifier, dependency) => posix.normalize(specifier) === dependency.relativePath;
|
|
54
|
+
/** The specifier that decides an edge: the last kind naming the dependency, link and file devDependencies aside. */
|
|
55
|
+
const edgeSpecifiers = (dependent) => {
|
|
56
|
+
const specifiers = /* @__PURE__ */ new Map();
|
|
57
|
+
for (const kind of DEPENDENCY_KINDS) for (const [name, specifier] of Object.entries(dependent[kind])) {
|
|
58
|
+
if (kind === "devDependencies" && (specifier.startsWith("link:") || specifier.startsWith("file:"))) continue;
|
|
59
|
+
specifiers.set(name, specifier);
|
|
60
|
+
}
|
|
61
|
+
return specifiers;
|
|
62
|
+
};
|
|
63
|
+
const isEdge = (specifier, dependency, workspaceProtocolOnly) => {
|
|
64
|
+
const isWorkspace = WORKSPACE.test(specifier);
|
|
65
|
+
const bare = specifier.replace(WORKSPACE, "");
|
|
66
|
+
if (isWorkspace) {
|
|
67
|
+
if (bare === "*" || bare === "^" || bare === "~" || pointsAt(bare, dependency)) return true;
|
|
68
|
+
} else if (workspaceProtocolOnly) return false;
|
|
69
|
+
const range = parseRange(bare);
|
|
70
|
+
if (range === void 0 || dependency.version === void 0) return false;
|
|
71
|
+
const current = Result.getOrUndefined(SemVer.parseResult(cleanVersion(dependency.version)));
|
|
72
|
+
return current !== void 0 && Range.satisfies(current, range);
|
|
73
|
+
};
|
|
74
|
+
const dependentsByName = (packages, workspaceProtocolOnly) => {
|
|
75
|
+
const byName = new Map(packages.map((pkg) => [pkg.name, pkg]));
|
|
76
|
+
const dependents = /* @__PURE__ */ new Map();
|
|
77
|
+
for (const dependent of packages) for (const [name, specifier] of edgeSpecifiers(dependent)) {
|
|
78
|
+
const dependency = byName.get(name);
|
|
79
|
+
if (dependency === void 0 || !isEdge(specifier, dependency, workspaceProtocolOnly)) continue;
|
|
80
|
+
dependents.set(name, [...dependents.get(name) ?? [], dependent]);
|
|
81
|
+
}
|
|
82
|
+
return dependents;
|
|
83
|
+
};
|
|
84
|
+
/** Every declared range on `dependency`, per kind, resolved against its current version. */
|
|
85
|
+
const declaredRanges = (dependent, dependency, rawOldVersion) => {
|
|
86
|
+
const oldVersion = cleanVersion(rawOldVersion);
|
|
87
|
+
const ranges = [];
|
|
88
|
+
for (const kind of DEPENDENCY_KINDS) {
|
|
89
|
+
const declared = Object.hasOwn(dependent[kind], dependency.name) ? dependent[kind][dependency.name] : "";
|
|
90
|
+
if (declared === "") continue;
|
|
91
|
+
if (!WORKSPACE.test(declared)) {
|
|
92
|
+
ranges.push([kind, declared]);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const bare = declared.replace(WORKSPACE, "");
|
|
96
|
+
if (bare === "*") ranges.push([kind, oldVersion]);
|
|
97
|
+
else if (bare === "^" || bare === "~") ranges.push([kind, `${bare}${oldVersion}`]);
|
|
98
|
+
else if (parseRange(bare) !== void 0) ranges.push([kind, bare]);
|
|
99
|
+
else if (pointsAt(bare, dependency)) ranges.push([kind, oldVersion]);
|
|
100
|
+
}
|
|
101
|
+
return ranges;
|
|
102
|
+
};
|
|
103
|
+
const satisfies = (version, specifier) => {
|
|
104
|
+
const range = parseRange(specifier);
|
|
105
|
+
const parsed = Result.getOrUndefined(SemVer.parseResult(cleanVersion(version)));
|
|
106
|
+
return range !== void 0 && parsed !== void 0 && Range.satisfies(parsed, range);
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* `semver.inc`'s rule, which `SemVerBump` deliberately does not follow: a bump
|
|
110
|
+
* a prerelease is already heading for completes it rather than incrementing.
|
|
111
|
+
*/
|
|
112
|
+
const increment = (version, type) => {
|
|
113
|
+
return version.prerelease.length > 0 && (type === "patch" || version.patch === 0 && (type === "minor" || version.minor === 0)) ? SemVer.truncate(version, "prerelease") : version.bump[type]();
|
|
114
|
+
};
|
|
115
|
+
/** The workspace's names a list of globs selects, `!` negation applying in order, in workspace order. */
|
|
116
|
+
const matchNames = (packages, patterns) => {
|
|
117
|
+
const matchers = patterns.map((source) => {
|
|
118
|
+
const negated = source.startsWith("!");
|
|
119
|
+
const glob = negated ? source.slice(1) : source;
|
|
120
|
+
const compiled = Result.getOrUndefined(GlobPattern.compileResult(glob));
|
|
121
|
+
return {
|
|
122
|
+
negated,
|
|
123
|
+
matches: (name) => compiled?.matches(name) ?? name === glob
|
|
124
|
+
};
|
|
125
|
+
});
|
|
126
|
+
const selected = [];
|
|
127
|
+
for (const { name } of packages) {
|
|
128
|
+
let passed = false;
|
|
129
|
+
for (const { negated, matches } of matchers) if (!passed && !negated && matches(name)) passed = true;
|
|
130
|
+
else if (passed && negated && matches(name)) passed = false;
|
|
131
|
+
if (passed) selected.push(name);
|
|
132
|
+
}
|
|
133
|
+
return selected;
|
|
134
|
+
};
|
|
135
|
+
/** The configuration errors `@changesets/config` rejects a fixed or linked group for. */
|
|
136
|
+
const groupError = (fixed, linked) => {
|
|
137
|
+
for (const [kind, groups] of [["fixed", fixed], ["linked", linked]]) {
|
|
138
|
+
const seen = /* @__PURE__ */ new Set();
|
|
139
|
+
for (const name of groups.flat()) {
|
|
140
|
+
if (seen.has(name)) return new ChangesetConfigError({ reason: `${name} is in more than one ${kind} group` });
|
|
141
|
+
seen.add(name);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const inFixed = new Set(fixed.flat());
|
|
145
|
+
const both = linked.flat().find((name) => inFixed.has(name));
|
|
146
|
+
return both === void 0 ? void 0 : new ChangesetConfigError({ reason: `${both} is in both a fixed and a linked group` });
|
|
147
|
+
};
|
|
148
|
+
/** `n` in a `-<tag>.<n>` prerelease, plus one: the number the next pre-release takes. */
|
|
149
|
+
const nextPreNumber = (version) => {
|
|
150
|
+
const number = parseVersion(version).prerelease[1] ?? -1;
|
|
151
|
+
if (typeof number !== "number") throw new Halt(new InvalidVersionError({ input: version }));
|
|
152
|
+
return number + 1;
|
|
153
|
+
};
|
|
154
|
+
const unsafeOption = (config, key) => {
|
|
155
|
+
const options = config.extra.___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH;
|
|
156
|
+
return typeof options === "object" && options !== null ? options[key] : void 0;
|
|
157
|
+
};
|
|
158
|
+
const LoneManifest = Schema.Struct({
|
|
159
|
+
name: Schema.NonEmptyString,
|
|
160
|
+
version: Schema.optionalKey(Schema.String),
|
|
161
|
+
private: Schema.optionalKey(Schema.Boolean)
|
|
162
|
+
});
|
|
163
|
+
/** The package of a repository with no workspace file, which discovery has no root for. */
|
|
164
|
+
const readLonePackage = Effect.fnUntraced(function* (root, notFound) {
|
|
165
|
+
const fs = yield* FileSystem.FileSystem;
|
|
166
|
+
const packageJsonPath = (yield* Path.Path).join(root, "package.json");
|
|
167
|
+
const text = yield* fs.readFileString(packageJsonPath).pipe(Effect.catch((error) => error.reason._tag === "NotFound" ? Effect.fail(notFound) : Effect.fail(error)));
|
|
168
|
+
const invalid = (kind) => (cause) => new WorkspaceDiscoveryError({
|
|
169
|
+
root,
|
|
170
|
+
path: packageJsonPath,
|
|
171
|
+
kind,
|
|
172
|
+
cause
|
|
173
|
+
});
|
|
174
|
+
const json = yield* Effect.try({
|
|
175
|
+
try: () => JSON.parse(text),
|
|
176
|
+
catch: invalid("invalidJson")
|
|
177
|
+
});
|
|
178
|
+
const manifest = yield* Schema.decodeUnknownEffect(LoneManifest)(json).pipe(Effect.mapError(invalid("invalidShape")));
|
|
179
|
+
return [WorkspacePackage.make({
|
|
180
|
+
...manifest,
|
|
181
|
+
path: root,
|
|
182
|
+
packageJsonPath,
|
|
183
|
+
relativePath: ".",
|
|
184
|
+
workspaceRoot: root
|
|
185
|
+
})];
|
|
186
|
+
});
|
|
187
|
+
/**
|
|
188
|
+
* One package's place in a {@link ReleasePlan}.
|
|
189
|
+
*
|
|
190
|
+
* @public
|
|
191
|
+
*/
|
|
192
|
+
var PlannedRelease = class extends Schema.Class("PlannedRelease")({
|
|
193
|
+
/** The package name. */
|
|
194
|
+
name: Schema.String,
|
|
195
|
+
/** The bump, or `none` for a listed package whose version does not move. */
|
|
196
|
+
type: Schema.Literals([
|
|
197
|
+
"patch",
|
|
198
|
+
"minor",
|
|
199
|
+
"major",
|
|
200
|
+
"none"
|
|
201
|
+
]),
|
|
202
|
+
/**
|
|
203
|
+
* The version the bump starts from: the workspace's version now, or for a
|
|
204
|
+
* member of a fixed or linked group, the group's highest.
|
|
205
|
+
*/
|
|
206
|
+
oldVersion: Schema.String,
|
|
207
|
+
/** The version after the release; equal to `oldVersion` when `type` is `none`. */
|
|
208
|
+
newVersion: Schema.String,
|
|
209
|
+
/** The changeset files naming the package; empty for a package reached only as a dependent. */
|
|
210
|
+
changesets: Schema.Array(Schema.String)
|
|
211
|
+
}) {};
|
|
212
|
+
/**
|
|
213
|
+
* What a set of pending changesets releases: the packages they name and the
|
|
214
|
+
* packages reached as dependents, sorted by name. A package neither named nor
|
|
215
|
+
* reached is absent.
|
|
216
|
+
*
|
|
217
|
+
* @remarks
|
|
218
|
+
* `@changesets/get-release-plan` is the authority on these rules, and
|
|
219
|
+
* `__test__/ReleasePlan.oracle.test.ts` holds this class to it. Its rules are
|
|
220
|
+
* not all the obvious ones:
|
|
221
|
+
*
|
|
222
|
+
* - A dependent bumps `patch` whatever `updateInternalDependencies` says. That
|
|
223
|
+
* option decides which ranges `changeset version` rewrites, not the bump.
|
|
224
|
+
* - An edge exists only for `workspace:*`, `workspace:^`, `workspace:~`, a
|
|
225
|
+
* `workspace:` path to the dependency, or a range the dependency's current
|
|
226
|
+
* version satisfies. `catalog:`, `link:`, `npm:` and an already-missed range
|
|
227
|
+
* draw no edge, so they never ripple. When several kinds name a dependency
|
|
228
|
+
* the last of `dependencies`, `devDependencies`, `peerDependencies`,
|
|
229
|
+
* `optionalDependencies` decides the edge, and a `link:` or `file:`
|
|
230
|
+
* devDependency is not considered.
|
|
231
|
+
* - Once an edge exists every kind naming the dependency is checked: one
|
|
232
|
+
* unsatisfied non-dev range bumps `patch`; only unsatisfied devDependency
|
|
233
|
+
* ranges list the package as `none`.
|
|
234
|
+
* - A changeset may name a package `none`. It is listed as `none` unless another changeset or a ripple gives it a
|
|
235
|
+
* real bump, and it ripples nothing.
|
|
236
|
+
* - A skipped package is ignored, private while `privatePackages.version` is
|
|
237
|
+
* off (its default), or version-less. Naming one in a changeset does nothing, and it never
|
|
238
|
+
* bumps, but it is still listed as `none` when it depends on anything in the
|
|
239
|
+
* plan, whether or not the range holds, and it carries that listing onward
|
|
240
|
+
* to its own skipped dependents. A `none` entry bumps nothing: its version
|
|
241
|
+
* does not move, so no range on it is checked, even with
|
|
242
|
+
* `updateInternalDependents` set to `always`.
|
|
243
|
+
* - `ignore` entries are globs over the workspace's names, with `!` negation
|
|
244
|
+
* applying in order. They are matched by `@effected/glob`, where the oracle
|
|
245
|
+
* uses picomatch; the two agree on every pattern a package name plausibly
|
|
246
|
+
* needs (`*`, `?`, `[ab]`, `{a,b}`, `@scope/*`, a leading `!`) and differ
|
|
247
|
+
* only on exotic ones: `(a|b)` is no group here, `!(a)` is a negation of the
|
|
248
|
+
* literal `(a)` rather than an extglob, and `x/**` does not match `x`.
|
|
249
|
+
* - A version is read as node-semver reads one: surrounding whitespace and a
|
|
250
|
+
* single leading `v` are dropped, so `v1.0.0` bumps to `1.0.1` and is still
|
|
251
|
+
* reported as `v1.0.0`, and a `v` before a version in a range is accepted.
|
|
252
|
+
* - A bump a prerelease is already heading for completes it, as `semver.inc`
|
|
253
|
+
* does: a `major` of `2.0.0-rc.1` is `2.0.0`, where `SemVerBump` answers
|
|
254
|
+
* `3.0.0`.
|
|
255
|
+
* - The options `bumpVersionsWithWorkspaceProtocolOnly` and
|
|
256
|
+
* `___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH.updateInternalDependents`
|
|
257
|
+
* are honoured from the config's `extra`.
|
|
258
|
+
* - A `fixed` group with any member releasing brings every member that is not
|
|
259
|
+
* skipped to the group's highest bump, from the group's highest current
|
|
260
|
+
* version: that is the `oldVersion` each member reports, not its own. A
|
|
261
|
+
* skipped member never releases, but its version still counts towards the
|
|
262
|
+
* highest. A `linked` group does the same for only the members that
|
|
263
|
+
* release. Ripples, `fixed` and `linked` repeat until none changes
|
|
264
|
+
* anything, so a dependent bumped by a ripple pulls its group along. Group
|
|
265
|
+
* entries are globs, as `ignore` is. A package in two groups of one kind,
|
|
266
|
+
* or in both a fixed and a linked group, fails with
|
|
267
|
+
* {@link ChangesetConfigError}, as the oracle refuses it.
|
|
268
|
+
* - In pre-release mode (a {@link PreState} with `mode: "pre"`) the
|
|
269
|
+
* changesets under `.changeset/pre/` were consumed by an earlier pre-release
|
|
270
|
+
* and are left out. Each bumped version gains `-<tag>.<n>`, where `n` is one
|
|
271
|
+
* more than the number in the package's current `-<tag>.<n>`, or `0` when it
|
|
272
|
+
* has none, and every member of a fixed or linked group takes the group's
|
|
273
|
+
* highest `n`. Dependents are checked against that prerelease version, which
|
|
274
|
+
* a plain range does not admit. In `exit` mode the consumed changesets count
|
|
275
|
+
* again, no suffix is added, and every package whose version carries a
|
|
276
|
+
* numbered prerelease bumps at least `patch`; those added bumps reach no
|
|
277
|
+
* dependent.
|
|
278
|
+
*
|
|
279
|
+
* Deliberate differences, each ruled:
|
|
280
|
+
*
|
|
281
|
+
* - A version-less package is not listed at all, where the oracle lists it
|
|
282
|
+
* with no versions, because a {@link PlannedRelease} states versions. In a
|
|
283
|
+
* fixed or linked group it is passed over when the group's highest version
|
|
284
|
+
* and pre-release number are found; the oracle fails on it.
|
|
285
|
+
* - A prerelease number that is not a number, such as `1.0.0-next.x`, fails
|
|
286
|
+
* with `InvalidVersionError` in pre-release mode; the oracle throws an
|
|
287
|
+
* internal error.
|
|
288
|
+
* - A version changesets cannot bump, such as `=1.2.0`, which npm's loose
|
|
289
|
+
* parse reads but strict `semver.inc` does not, fails with
|
|
290
|
+
* `InvalidVersionError` wherever it must be read. The oracle crashes on it
|
|
291
|
+
* in pre mode and in a releasing group, and plans a bump of it with no new
|
|
292
|
+
* version at all.
|
|
293
|
+
* - A `pre.json` with no `tag`, or with a `mode` other than `pre` or `exit`,
|
|
294
|
+
* fails {@link PreState.read} with {@link ChangesetConfigError}. The oracle
|
|
295
|
+
* plans the first with `-undefined.0` suffixes and treats the second as
|
|
296
|
+
* `pre`, both defects rather than behaviour.
|
|
297
|
+
* - An older-layout `pre.json` that lists consumed changesets by id is
|
|
298
|
+
* planned as if they were under `.changeset/pre/`, and nothing is written.
|
|
299
|
+
* The oracle moves them there while it plans, after reading them, so its
|
|
300
|
+
* first plan counts them in pre mode and every later one does not; versions,
|
|
301
|
+
* types and changeset ids here are its later plans', so in exit mode a
|
|
302
|
+
* consumed changeset is named `pre/<file>`, where the oracle moved it,
|
|
303
|
+
* though it was never moved.
|
|
304
|
+
* - Snapshot versions are not planned. `@changesets/get-release-plan` takes no
|
|
305
|
+
* snapshot input, so it cannot be the oracle for one, and a snapshot is a
|
|
306
|
+
* flag on `changeset version`, not a state a repository is in.
|
|
307
|
+
*
|
|
308
|
+
* @public
|
|
309
|
+
*/
|
|
310
|
+
var ReleasePlan = class ReleasePlan extends Schema.Class("ReleasePlan")({
|
|
311
|
+
/** The planned releases, sorted by package name. */
|
|
312
|
+
releases: Schema.Array(PlannedRelease),
|
|
313
|
+
/**
|
|
314
|
+
* The changesets the plan was made from: every pending changeset, less, in
|
|
315
|
+
* pre mode, those an earlier pre-release consumed. A changelog written from
|
|
316
|
+
* the plan reads these, so it never re-derives which a pre-release consumed.
|
|
317
|
+
*/
|
|
318
|
+
changesets: Schema.Array(PendingChangeset),
|
|
319
|
+
/** The pre-release state the plan was made in, when the repository has one. */
|
|
320
|
+
preState: Schema.optionalKey(PreState)
|
|
321
|
+
}) {
|
|
322
|
+
/**
|
|
323
|
+
* Assemble the plan from packages, changesets and a config, with no IO.
|
|
324
|
+
*
|
|
325
|
+
* Fails with {@link UnknownPackageError} when a changeset names a package
|
|
326
|
+
* `packages` lacks; with `InvalidVersionError` when a version it must read
|
|
327
|
+
* is not one node-semver reads, which is the version of a package that
|
|
328
|
+
* bumps, the versions of a releasing fixed or linked group's members, and,
|
|
329
|
+
* in pre-release mode, the version of every package not skipped and of every
|
|
330
|
+
* fixed or linked member; and with
|
|
331
|
+
* {@link ChangesetConfigError}
|
|
332
|
+
* when a package is in two fixed groups, two linked groups, or a group of
|
|
333
|
+
* each. What the oracle rejects outright and this does not check is planned,
|
|
334
|
+
* not refused: a changeset mixing skipped and released packages, a public
|
|
335
|
+
* package depending on a skipped one, and `privatePackages` with `tag` on
|
|
336
|
+
* and `version` off.
|
|
337
|
+
*
|
|
338
|
+
* @param input - The packages to plan, the pending changesets, the config
|
|
339
|
+
* and, in pre-release mode, the {@link PreState}. {@link ReleasePlan.read}
|
|
340
|
+
* decides whether the workspace root is one of the packages.
|
|
341
|
+
*/
|
|
342
|
+
static assemble(input) {
|
|
343
|
+
try {
|
|
344
|
+
return Result.succeed(assembleOrHalt(input));
|
|
345
|
+
} catch (error) {
|
|
346
|
+
if (error instanceof Halt) return Result.fail(error.failure);
|
|
347
|
+
throw error;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Discover the workspace's packages, read its config, pending changesets and
|
|
352
|
+
* `.changeset/pre.json`, and {@link ReleasePlan.assemble | assemble} the
|
|
353
|
+
* plan.
|
|
354
|
+
*
|
|
355
|
+
* `root` decides all three, whatever directory the `WorkspaceDiscovery` layer
|
|
356
|
+
* was built from. The workspace root package is left out while other packages
|
|
357
|
+
* exist. When there are none it is the package and is planned, including when
|
|
358
|
+
* `root` has only a `package.json` and no workspace file. The oracle refuses
|
|
359
|
+
* `packages: []` and patterns that match nothing; here they plan the root too,
|
|
360
|
+
* because discovery reports them the same way. A root listed in `packages:`
|
|
361
|
+
* alongside others stays out, where the oracle plans it. A root with a
|
|
362
|
+
* catalog-only workspace file answers for itself, where the oracle climbs to
|
|
363
|
+
* any enclosing workspace.
|
|
364
|
+
*
|
|
365
|
+
* @param root - The repository root.
|
|
366
|
+
*/
|
|
367
|
+
static read = Effect.fn("ReleasePlan.read")(function* (root) {
|
|
368
|
+
const discovered = yield* (yield* WorkspaceDiscovery).listPackagesIn(root).pipe(Effect.catchTag("WorkspaceRootNotFoundError", (error) => readLonePackage(root, error)));
|
|
369
|
+
const config = yield* ChangesetConfig.read(root);
|
|
370
|
+
const changesets = yield* PendingChangesets.read(root);
|
|
371
|
+
const pre = yield* PreState.read(root);
|
|
372
|
+
const members = discovered.filter((pkg) => !pkg.isRootWorkspace);
|
|
373
|
+
const packages = members.length > 0 ? members : discovered;
|
|
374
|
+
return yield* Effect.fromResult(ReleasePlan.assemble({
|
|
375
|
+
packages,
|
|
376
|
+
changesets,
|
|
377
|
+
config,
|
|
378
|
+
...Option.isSome(pre) ? { pre: pre.value } : {}
|
|
379
|
+
}));
|
|
380
|
+
});
|
|
381
|
+
};
|
|
382
|
+
/** The highest of `types`, none of which is `none`. */
|
|
383
|
+
const highestType = (types) => types.reduce((high, type) => RANK[type] > RANK[high] ? type : high, "none");
|
|
384
|
+
/**
|
|
385
|
+
* `@changesets/assemble-release-plan`'s algorithm: flatten the relevant
|
|
386
|
+
* changesets, then ripple to dependents, hold fixed groups together and keep
|
|
387
|
+
* linked groups in step until none of the three changes anything.
|
|
388
|
+
*/
|
|
389
|
+
const assembleOrHalt = (input) => {
|
|
390
|
+
const { packages, changesets, config, pre } = input;
|
|
391
|
+
const byName = new Map(packages.map((pkg) => [pkg.name, pkg]));
|
|
392
|
+
const ignored = new Set(matchNames(packages, config.ignore));
|
|
393
|
+
const isSkipped = (pkg) => ignored.has(pkg.name) || pkg.private && !config.privatePackages.version || pkg.version === void 0;
|
|
394
|
+
const fixed = config.fixed.map((group) => matchNames(packages, group));
|
|
395
|
+
const linked = config.linked.map((group) => matchNames(packages, group));
|
|
396
|
+
const invalid = groupError(fixed, linked);
|
|
397
|
+
if (invalid !== void 0) throw new Halt(invalid);
|
|
398
|
+
for (const changeset of changesets) for (const name of changeset.releases.keys()) if (!byName.has(name)) throw new Halt(new UnknownPackageError({
|
|
399
|
+
packageName: name,
|
|
400
|
+
changeset: changeset.file
|
|
401
|
+
}));
|
|
402
|
+
const consumedIds = new Set(pre?.changesets ?? []);
|
|
403
|
+
const listedConsumed = (file) => !file.startsWith("pre/") && consumedIds.has(file.replace(/\.md$/, ""));
|
|
404
|
+
const relevant = pre === void 0 ? changesets : pre.mode === "pre" ? changesets.filter((c) => !c.file.startsWith("pre/") && !listedConsumed(c.file)) : changesets.map((c) => listedConsumed(c.file) ? PendingChangeset.make({
|
|
405
|
+
...c,
|
|
406
|
+
file: `pre/${c.file}`
|
|
407
|
+
}) : c);
|
|
408
|
+
const preNumbers = /* @__PURE__ */ new Map();
|
|
409
|
+
if (pre !== void 0) {
|
|
410
|
+
for (const pkg of packages) if (!isSkipped(pkg) && pkg.version !== void 0) preNumbers.set(pkg.name, nextPreNumber(pkg.version));
|
|
411
|
+
for (const group of [...fixed, ...linked]) {
|
|
412
|
+
let highest = 0;
|
|
413
|
+
for (const name of group) {
|
|
414
|
+
const version = byName.get(name)?.version;
|
|
415
|
+
if (version !== void 0) highest = Math.max(highest, nextPreNumber(version));
|
|
416
|
+
}
|
|
417
|
+
for (const name of group) preNumbers.set(name, highest);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
const newVersion = (release) => {
|
|
421
|
+
if (release.type === "none" || release.oldVersion === void 0) return release.oldVersion;
|
|
422
|
+
const next = increment(parseVersion(release.oldVersion), release.type).toString();
|
|
423
|
+
return pre !== void 0 && pre.mode === "pre" ? `${next}-${pre.tag}.${preNumbers.get(release.name) ?? 0}` : next;
|
|
424
|
+
};
|
|
425
|
+
const releases = /* @__PURE__ */ new Map();
|
|
426
|
+
for (const changeset of relevant) for (const [name, type] of changeset.releases) {
|
|
427
|
+
const pkg = byName.get(name);
|
|
428
|
+
if (isSkipped(pkg)) continue;
|
|
429
|
+
const release = releases.get(name);
|
|
430
|
+
if (release === void 0) {
|
|
431
|
+
releases.set(name, {
|
|
432
|
+
name,
|
|
433
|
+
type,
|
|
434
|
+
oldVersion: pkg.version,
|
|
435
|
+
changesets: [changeset.file]
|
|
436
|
+
});
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
if (RANK[type] > RANK[release.type]) release.type = type;
|
|
440
|
+
release.changesets.push(changeset.file);
|
|
441
|
+
}
|
|
442
|
+
const alwaysRipple = unsafeOption(config, "updateInternalDependents") === "always";
|
|
443
|
+
const dependents = dependentsByName(packages, config.extra.bumpVersionsWithWorkspaceProtocolOnly === true);
|
|
444
|
+
const rippleDependents = () => {
|
|
445
|
+
let updated = false;
|
|
446
|
+
const search = [...releases.values()];
|
|
447
|
+
for (let next = search.shift(); next !== void 0; next = search.shift()) {
|
|
448
|
+
const dependency = byName.get(next.name);
|
|
449
|
+
const found = [];
|
|
450
|
+
for (const dependent of dependents.get(next.name) ?? []) {
|
|
451
|
+
let type;
|
|
452
|
+
if (isSkipped(dependent)) type = "none";
|
|
453
|
+
else if (next.type !== "none" && next.oldVersion !== void 0) {
|
|
454
|
+
const existing = releases.get(dependent.name);
|
|
455
|
+
if (existing === void 0 || existing.type === "none") {
|
|
456
|
+
const bumped = newVersion(next);
|
|
457
|
+
for (const [kind, range] of declaredRanges(dependent, dependency, next.oldVersion)) {
|
|
458
|
+
if (!alwaysRipple && satisfies(bumped, range)) continue;
|
|
459
|
+
if (kind !== "devDependencies") type = "patch";
|
|
460
|
+
else if (type === void 0) type = "none";
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
if (type === void 0 || releases.get(dependent.name)?.type === type) continue;
|
|
465
|
+
found.push([dependent, type]);
|
|
466
|
+
}
|
|
467
|
+
for (const [dependent, type] of found) {
|
|
468
|
+
updated = true;
|
|
469
|
+
const release = {
|
|
470
|
+
name: dependent.name,
|
|
471
|
+
type,
|
|
472
|
+
oldVersion: dependent.version,
|
|
473
|
+
changesets: []
|
|
474
|
+
};
|
|
475
|
+
releases.set(dependent.name, release);
|
|
476
|
+
search.push(release);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
return updated;
|
|
480
|
+
};
|
|
481
|
+
/**
|
|
482
|
+
* The group's highest current version, as its member writes it (`v1.3.0`
|
|
483
|
+
* stays `v1.3.0`), over every member with a version; the first of equals wins.
|
|
484
|
+
*/
|
|
485
|
+
const highestVersion = (group) => {
|
|
486
|
+
let highest;
|
|
487
|
+
for (const name of group) {
|
|
488
|
+
const version = byName.get(name)?.version;
|
|
489
|
+
if (version === void 0) continue;
|
|
490
|
+
const parsed = parseVersion(version);
|
|
491
|
+
if (highest === void 0 || parsed.gt(highest.parsed)) highest = {
|
|
492
|
+
parsed,
|
|
493
|
+
written: version
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
return highest?.written;
|
|
497
|
+
};
|
|
498
|
+
const releasingIn = (group) => [...releases.values()].filter((release) => group.includes(release.name) && release.type !== "none");
|
|
499
|
+
const holdFixed = () => {
|
|
500
|
+
let updated = false;
|
|
501
|
+
for (const group of fixed) {
|
|
502
|
+
const releasing = releasingIn(group);
|
|
503
|
+
if (releasing.length === 0) continue;
|
|
504
|
+
const type = highestType(releasing.map((release) => release.type));
|
|
505
|
+
const oldVersion = highestVersion(group);
|
|
506
|
+
for (const name of group) {
|
|
507
|
+
if (isSkipped(byName.get(name))) continue;
|
|
508
|
+
const release = releases.get(name);
|
|
509
|
+
if (release === void 0) {
|
|
510
|
+
updated = true;
|
|
511
|
+
releases.set(name, {
|
|
512
|
+
name,
|
|
513
|
+
type,
|
|
514
|
+
oldVersion,
|
|
515
|
+
changesets: []
|
|
516
|
+
});
|
|
517
|
+
continue;
|
|
518
|
+
}
|
|
519
|
+
if (release.type !== type) {
|
|
520
|
+
updated = true;
|
|
521
|
+
release.type = type;
|
|
522
|
+
}
|
|
523
|
+
if (release.oldVersion !== oldVersion) {
|
|
524
|
+
updated = true;
|
|
525
|
+
release.oldVersion = oldVersion;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return updated;
|
|
530
|
+
};
|
|
531
|
+
const keepLinked = () => {
|
|
532
|
+
let updated = false;
|
|
533
|
+
for (const group of linked) {
|
|
534
|
+
const releasing = releasingIn(group);
|
|
535
|
+
if (releasing.length === 0) continue;
|
|
536
|
+
const type = highestType(releasing.map((release) => release.type));
|
|
537
|
+
const oldVersion = highestVersion(group);
|
|
538
|
+
for (const release of releasing) {
|
|
539
|
+
if (release.type !== type) {
|
|
540
|
+
updated = true;
|
|
541
|
+
release.type = type;
|
|
542
|
+
}
|
|
543
|
+
if (release.oldVersion !== oldVersion) {
|
|
544
|
+
updated = true;
|
|
545
|
+
release.oldVersion = oldVersion;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
return updated;
|
|
550
|
+
};
|
|
551
|
+
for (let settled = false; !settled;) {
|
|
552
|
+
const rippled = rippleDependents();
|
|
553
|
+
const held = holdFixed();
|
|
554
|
+
settled = !keepLinked() && !rippled && !held;
|
|
555
|
+
}
|
|
556
|
+
if (pre?.mode === "exit") for (const pkg of packages) {
|
|
557
|
+
if (isSkipped(pkg) || (preNumbers.get(pkg.name) ?? 0) === 0) continue;
|
|
558
|
+
const release = releases.get(pkg.name);
|
|
559
|
+
if (release === void 0) releases.set(pkg.name, {
|
|
560
|
+
name: pkg.name,
|
|
561
|
+
type: "patch",
|
|
562
|
+
oldVersion: pkg.version,
|
|
563
|
+
changesets: []
|
|
564
|
+
});
|
|
565
|
+
else if (release.type === "none") release.type = "patch";
|
|
566
|
+
}
|
|
567
|
+
const planned = [];
|
|
568
|
+
for (const release of releases.values()) {
|
|
569
|
+
const next = newVersion(release);
|
|
570
|
+
if (release.oldVersion === void 0 || next === void 0) continue;
|
|
571
|
+
planned.push(PlannedRelease.make({
|
|
572
|
+
name: release.name,
|
|
573
|
+
type: release.type,
|
|
574
|
+
oldVersion: release.oldVersion,
|
|
575
|
+
newVersion: next,
|
|
576
|
+
changesets: [...release.changesets]
|
|
577
|
+
}));
|
|
578
|
+
}
|
|
579
|
+
planned.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
580
|
+
return ReleasePlan.make({
|
|
581
|
+
releases: planned,
|
|
582
|
+
changesets: relevant,
|
|
583
|
+
...pre === void 0 ? {} : { preState: pre }
|
|
584
|
+
});
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
export { PlannedRelease, ReleasePlan };
|
package/errors.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
// src/errors.ts
|
|
4
|
+
/**
|
|
5
|
+
* A changeset file could not be parsed: no front matter, a malformed YAML
|
|
6
|
+
* block, or a bump outside `patch | minor | major`.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
var ChangesetParseError = class extends Schema.TaggedError()("ChangesetParseError", {
|
|
11
|
+
/** What was wrong with the file. */
|
|
12
|
+
reason: Schema.String,
|
|
13
|
+
/** The 1-based line the problem was found on, when known. */
|
|
14
|
+
line: Schema.optionalKey(Schema.Number),
|
|
15
|
+
/** The changeset file name, when the file was read from `.changeset/`. */
|
|
16
|
+
file: Schema.optionalKey(Schema.String)
|
|
17
|
+
}) {
|
|
18
|
+
get message() {
|
|
19
|
+
return `Invalid changeset${this.file === void 0 ? "" : ` (${this.file}${this.line === void 0 ? "" : `:${this.line}`})`}: ${this.reason}`;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* `.changeset/config.json` is missing, is not valid JSON, or does not match
|
|
24
|
+
* the documented shape.
|
|
25
|
+
*
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
var ChangesetConfigError = class extends Schema.TaggedError()("ChangesetConfigError", {
|
|
29
|
+
/** `"missing"` when the file does not exist, otherwise what was wrong. */
|
|
30
|
+
reason: Schema.String }) {
|
|
31
|
+
get message() {
|
|
32
|
+
return `Invalid changeset config: ${this.reason}`;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* A changeset names a package the workspace does not contain.
|
|
37
|
+
*
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
var UnknownPackageError = class extends Schema.TaggedError()("UnknownPackageError", {
|
|
41
|
+
/** The package name the changeset gave. */
|
|
42
|
+
packageName: Schema.String,
|
|
43
|
+
/** The changeset file that named it. */
|
|
44
|
+
changeset: Schema.String
|
|
45
|
+
}) {
|
|
46
|
+
get message() {
|
|
47
|
+
return `Changeset ${this.changeset} names ${this.packageName}, which is not in the workspace`;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export { ChangesetConfigError, ChangesetParseError, UnknownPackageError };
|