@effected/changesets 0.1.0 → 0.3.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.
@@ -0,0 +1,226 @@
1
+ import { MissingPackageError } from "./errors.js";
2
+ import { DEPENDENCY_KINDS, WORKSPACE, cleanVersion, increment, matchNames, parseRange, satisfies, shouldUpdate, writtenSets } from "./internal/versions.js";
3
+ import { PreState } from "./PreState.js";
4
+ import { Result, Schema } from "effect";
5
+ import { posix } from "node:path";
6
+ import { InvalidVersionError, SemVer } from "@effected/semver";
7
+
8
+ // src/ReleaseEdits.ts
9
+ /**
10
+ * One dependency range a release rewrites in a manifest.
11
+ *
12
+ * @public
13
+ */
14
+ var RangeEdit = class extends Schema.Class("RangeEdit")({
15
+ /** The manifest field holding the range. */
16
+ kind: Schema.Literals([
17
+ "dependencies",
18
+ "devDependencies",
19
+ "peerDependencies",
20
+ "optionalDependencies"
21
+ ]),
22
+ /** The dependency's package name: the key under `kind`. */
23
+ dependency: Schema.String,
24
+ /** The specifier as the manifest has it now. */
25
+ from: Schema.String,
26
+ /** The specifier the release writes. */
27
+ to: Schema.String
28
+ }) {};
29
+ /**
30
+ * What a release changes in one `package.json`.
31
+ *
32
+ * @public
33
+ */
34
+ var ManifestEdit = class extends Schema.Class("ManifestEdit")({
35
+ /** The package name. */
36
+ name: Schema.String,
37
+ /** The absolute path of the `package.json` to edit. */
38
+ packageJsonPath: Schema.String,
39
+ /**
40
+ * The `version` the release writes. Absent when the version does not move:
41
+ * a `none` entry, or the workspace root, whose ranges alone are edited.
42
+ */
43
+ version: Schema.optionalKey(Schema.String),
44
+ /** The dependency ranges the release rewrites, in the order `changeset version` edits them. */
45
+ ranges: Schema.Array(RangeEdit)
46
+ }) {};
47
+ /**
48
+ * A pending changeset a pre-release consumes: `changeset version` in pre mode
49
+ * moves it under `.changeset/pre/` rather than deleting it.
50
+ *
51
+ * The caller creates `.changeset/pre/` first when it does not exist, as
52
+ * `changeset version` does (`mkdir -p`): a plain rename into a missing
53
+ * directory fails on the first pre-release.
54
+ *
55
+ * @public
56
+ */
57
+ var ChangesetMove = class extends Schema.Class("ChangesetMove")({
58
+ /** The file now, relative to `.changeset/`. */
59
+ from: Schema.String,
60
+ /** Where it goes, relative to `.changeset/`: `pre/<file>`. */
61
+ to: Schema.String
62
+ }) {};
63
+ /**
64
+ * Everything `changeset version` writes for a {@link ReleasePlan}, as data,
65
+ * CHANGELOG files aside. {@link ReleasePlan.edits} computes it; the caller
66
+ * does the IO.
67
+ *
68
+ * @public
69
+ */
70
+ var ReleaseEdits = class extends Schema.Class("ReleaseEdits")({
71
+ /**
72
+ * One entry per `package.json` that changes, in plan order with the
73
+ * workspace root last. A manifest nothing changes in is left out, and so is
74
+ * an edit that would write the value already there.
75
+ */
76
+ manifests: Schema.Array(ManifestEdit),
77
+ /** The changeset files to delete, relative to `.changeset/`. */
78
+ deleteChangesets: Schema.Array(Schema.String),
79
+ /** The changeset files to move under `.changeset/pre/`; only ever non-empty in pre mode. */
80
+ moveChangesets: Schema.Array(ChangesetMove),
81
+ /**
82
+ * `.changeset/pre.json` as the release leaves it. Absent when the release
83
+ * leaves no `pre.json`: the plan had none, or it was in `exit` mode and is
84
+ * not a snapshot, so the file is deleted. When present it equals the
85
+ * plan's {@link PreState}: `changeset version` never rewrites the file.
86
+ */
87
+ preState: Schema.optionalKey(PreState)
88
+ }) {};
89
+ var Halt = class {
90
+ failure;
91
+ constructor(failure) {
92
+ this.failure = failure;
93
+ }
94
+ };
95
+ const unsafeOption = (config, key) => {
96
+ const options = config.extra.___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH;
97
+ return typeof options === "object" && options !== null ? options[key] : void 0;
98
+ };
99
+ const isPrerelease = (version) => {
100
+ const parsed = Result.getOrUndefined(SemVer.parseResult(cleanVersion(version)));
101
+ return parsed !== void 0 && parsed.prerelease.length > 0;
102
+ };
103
+ /** `apply-release-plan`'s `getVersionRangeType`: the operator prefix a rewritten range keeps. */
104
+ const rangePrefix = (range) => range.startsWith("^") ? "^" : range.startsWith("~") ? "~" : range.startsWith(">=") ? ">=" : range.startsWith("<=") ? "<=" : range.startsWith(">") ? ">" : "";
105
+ /**
106
+ * `apply-release-plan`'s `getNewDependencyRange`. A single set of exactly two
107
+ * comparators written with both a `>` and a `<` keeps its upper bound while
108
+ * the new version satisfies it, else takes `<` the new version bumped again
109
+ * by the release type, and its lower bound becomes `>=` the new version, in
110
+ * the order the range was written. Anything else is the range's leading
111
+ * operator before the new version.
112
+ */
113
+ const newRange = (written, sets, newVersion, type) => {
114
+ const comparators = sets.length === 1 ? sets[0] : [];
115
+ if (comparators.length === 2 && written.includes(">") && written.includes("<")) {
116
+ const lower = comparators.find((c) => c.operator.startsWith(">"));
117
+ const upper = comparators.find((c) => c.operator.startsWith("<"));
118
+ if (lower !== void 0 && upper !== void 0) {
119
+ let bound = upper.toString();
120
+ if (!satisfies(newVersion, bound)) {
121
+ const parsed = Result.getOrUndefined(SemVer.parseResult(cleanVersion(newVersion)));
122
+ if (parsed === void 0 || type === "none") throw new Halt(new InvalidVersionError({ input: newVersion }));
123
+ bound = `<${increment(parsed, type).toString()}`;
124
+ }
125
+ return comparators[0] === lower ? `>=${newVersion} ${bound}` : `${bound} >=${newVersion}`;
126
+ }
127
+ }
128
+ return `${rangePrefix(written)}${newVersion}`;
129
+ };
130
+ /** `apply-release-plan`'s `getDependencyVersionEdits` over one manifest. */
131
+ const rangeEdits = (pkg, releases, options, pathOf) => {
132
+ const edits = [];
133
+ for (const kind of DEPENDENCY_KINDS) {
134
+ const declared = pkg[kind];
135
+ for (const release of releases) {
136
+ if (release.type === "none" || !Object.hasOwn(declared, release.name)) continue;
137
+ const from = declared[release.name];
138
+ if (from === "" || from.startsWith("file:") || from.startsWith("link:")) continue;
139
+ if (!shouldUpdate(release, from, kind, options, pathOf(release.name))) continue;
140
+ const isWorkspace = WORKSPACE.test(from);
141
+ if (!isWorkspace && (options.workspaceProtocolOnly || parseRange(from) === void 0)) continue;
142
+ const bare = from.replace(WORKSPACE, "");
143
+ if (isWorkspace && (bare === "*" || bare === "^" || bare === "~")) continue;
144
+ const range = parseRange(bare);
145
+ const sets = writtenSets(bare);
146
+ if (range === void 0 || sets === void 0) continue;
147
+ if (range.isAny() && !isPrerelease(release.newVersion)) continue;
148
+ const next = options.snapshot ? release.newVersion : newRange(bare, sets, release.newVersion, release.type);
149
+ const to = isWorkspace ? `workspace:${next}` : next;
150
+ if (to !== from) edits.push(RangeEdit.make({
151
+ kind,
152
+ dependency: release.name,
153
+ from,
154
+ to
155
+ }));
156
+ }
157
+ }
158
+ return edits;
159
+ };
160
+ /**
161
+ * The computation behind {@link ReleasePlan.edits}; see it for the rules.
162
+ *
163
+ * @internal
164
+ */
165
+ const computeEdits = (plan, input) => {
166
+ try {
167
+ return Result.succeed(editsOrHalt(plan, input));
168
+ } catch (error) {
169
+ if (error instanceof Halt) return Result.fail(error.failure);
170
+ throw error;
171
+ }
172
+ };
173
+ const editsOrHalt = (plan, input) => {
174
+ const { packages, config } = input;
175
+ const byName = new Map(packages.filter((pkg) => !pkg.isRootWorkspace).map((pkg) => [pkg.name, pkg]));
176
+ const root = packages.find((pkg) => pkg.isRootWorkspace);
177
+ const options = {
178
+ minimum: config.updateInternalDependencies,
179
+ onlyPeersOutOfRange: unsafeOption(config, "onlyUpdatePeerDependentsWhenOutOfRange") === true,
180
+ workspaceProtocolOnly: config.extra.bumpVersionsWithWorkspaceProtocolOnly === true,
181
+ snapshot: plan.snapshot
182
+ };
183
+ const pathOf = (name) => byName.get(name)?.relativePath ?? root?.relativePath ?? ".";
184
+ const manifests = [];
185
+ const edited = /* @__PURE__ */ new Set();
186
+ for (const release of plan.releases) {
187
+ const pkg = byName.get(release.name) ?? (root?.name === release.name ? root : void 0);
188
+ if (pkg === void 0) throw new Halt(new MissingPackageError({ packageName: release.name }));
189
+ edited.add(pkg.packageJsonPath);
190
+ const ranges = rangeEdits(pkg, plan.releases, options, pathOf);
191
+ const moves = release.newVersion !== pkg.version;
192
+ if (!moves && ranges.length === 0) continue;
193
+ manifests.push(ManifestEdit.make({
194
+ name: pkg.name,
195
+ packageJsonPath: pkg.packageJsonPath,
196
+ ...moves ? { version: release.newVersion } : {},
197
+ ranges
198
+ }));
199
+ }
200
+ if (root !== void 0 && !edited.has(root.packageJsonPath)) {
201
+ const ranges = rangeEdits(root, plan.releases, options, pathOf);
202
+ if (ranges.length > 0) manifests.push(ManifestEdit.make({
203
+ name: root.name,
204
+ packageJsonPath: root.packageJsonPath,
205
+ ranges
206
+ }));
207
+ }
208
+ const ignored = new Set(matchNames(packages, config.ignore));
209
+ const isSkipped = (name) => {
210
+ const pkg = byName.get(name) ?? (root?.name === name ? root : void 0);
211
+ return pkg === void 0 || ignored.has(name) || pkg.private && !config.privatePackages.version || pkg.version === void 0 || pkg.version === "";
212
+ };
213
+ const consumed = plan.changesets.filter((changeset) => ![...changeset.releases.keys()].some(isSkipped));
214
+ const pre = plan.preState?.mode === "pre";
215
+ return ReleaseEdits.make({
216
+ manifests,
217
+ deleteChangesets: pre ? [] : consumed.map((changeset) => changeset.file),
218
+ moveChangesets: pre ? consumed.map((changeset) => ChangesetMove.make({
219
+ from: changeset.file,
220
+ to: posix.join("pre", changeset.file)
221
+ })) : [],
222
+ ...plan.preState !== void 0 && (pre || plan.snapshot) ? { preState: plan.preState } : {}
223
+ });
224
+ };
225
+
226
+ export { ChangesetMove, ManifestEdit, RangeEdit, ReleaseEdits, computeEdits };