@wtf/release-npm 1.2.0 → 2.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/dist/release.js CHANGED
@@ -1,81 +1,65 @@
1
1
  /**
2
- * A release of a train published, in three steps, each by the system it is given, none of which
3
- * writes a file of the repo: the version of the release is the tag's, and each package.json in git
4
- * keeps the version it has, a placeholder, as `0.0.0-development`.
2
+ * The npm packages of a release published, of the plan its caller gives it, as `Plan` of
3
+ * `@wtf/release/contract` has it: the packages that publish, in the order to publish them in, and
4
+ * the version of each package of the repo a range of theirs resolves to. It knows no train and no
5
+ * release tool, and writes no file of the repo: the version of the release is given, and each
6
+ * package.json in git keeps the version it has, a placeholder, as `0.0.0-development`. Each step of
7
+ * the contract is a function of this module, which the ecosystem of `./ecosystem` runs:
5
8
  *
6
- * 1. {@link resolveManifests}: the packages of the train that publish, every one under its paths
7
- * that is not private, in the order to publish them in, each manifest as it publishes: of the
8
- * version of the release, each `workspace:` range resolved, of a package of the train to the
9
- * release's version and of another train's to the last release of that train, by its tags, and
10
- * each export of the source stripped; each checked. It writes nothing.
11
- * 2. {@link packTrain}: each package copied into a folder of the system's temporary files, its
12
- * manifest as it publishes written into the copy, and packed there by a publisher that packs,
13
- * all before the first publish, so a fault stops the run before anything is on the registry.
14
- * 3. {@link publishTrain}: each published in order, under the dist-tag of the channel, or of the
15
- * version of none ({@link distTagOf}), `release-1.x` of a channel npm takes for a range, with
16
- * the provenance of the build where the CI gives an ID token, and each the registry has at the
17
- * version already skipped, so a publish run again publishes what is missing. `cleanup` of what
18
- * {@link packTrain} gave removes the copies, and the tarballs but of a `tarballDir` named.
9
+ * - {@link verifyRelease}: the version of semver, the options ({@link npmOptionsSchema}), and the
10
+ * packages as they are in the repo ({@link checkPlan});
11
+ * - {@link prepareRelease}: each manifest as it publishes, of the version of the release, each
12
+ * `workspace:` range resolved to the version of the plan, each export of the source stripped, and
13
+ * checked ({@link resolvePlan}); each package packed from a copy in a temporary folder of the
14
+ * platform ({@link packRelease}); and the registry asked, so a release that would move a dist-tag
15
+ * back fails before anything is published ({@link publishedOf}). It writes no file of the repo,
16
+ * and gives none;
17
+ * - {@link smokeRelease}: each package packed tried as a consumer installs it, by `smokePacked` of
18
+ * `./smoke`;
19
+ * - {@link publishRelease}: each package published in order, under the first dist-tag of `moving`,
20
+ * each other pointed at it, with the provenance of the build where the CI gives an ID token, and
21
+ * each the registry has at the version already skipped, so a publish run again publishes what is
22
+ * missing ({@link publishPacked});
23
+ * - {@link moveRelease}: each dist-tag of `moving` pointed at a version published already.
19
24
  *
20
- * {@link releaseTrain} runs the three in one call, as the job of the pipeline of a tag publishes
21
- * its release. {@link tagTrain} moves a release published already to another channel: the dist-tag
22
- * of each package of the train pointed at its version.
25
+ * What `prepare` packed, `smoke` and `publish` take in the same process, of the same folder,
26
+ * version and packages; a step that finds nothing packed packs again, as the same tree packs the
27
+ * same tarballs, and {@link discardPacked} removes what is packed of a release that goes no
28
+ * further.
29
+ *
30
+ * ```ts
31
+ * import type { Release } from '@wtf/release/contract'
32
+ * import npm from '@wtf/release-npm'
33
+ * import type { NpmPackage } from '@wtf/release-npm/manifests'
34
+ *
35
+ * declare const release: Release
36
+ * declare const own: readonly NpmPackage[]
37
+ *
38
+ * const plan = { own, versions: new Map(own.map(({ name }) => [name, release.version])) }
39
+ * const options = { moving: ['latest'] } as const
40
+ * await npm.verify(release, plan, options)
41
+ * await npm.prepare(release, plan, options)
42
+ * const released = await npm.publish(release, plan, options)
43
+ * ```
23
44
  *
24
45
  * @packageDocumentation
25
46
  * @module @wtf/release-npm/release
26
47
  */
27
48
  import semver from 'semver';
28
- import { throwAll } from '@wtf/release-system/errors';
29
- import { pathMatcher } from '@wtf/release-system/paths';
30
- import { joinPath, relativePath, resolvePath } from '@wtf/release-system/posix';
31
- import { TrainsError } from '@wtf/release-trains/errors';
32
- import { ownersOf, packagesOf, publishOrder, releasedVersions, unsatisfiedErrors } from '@wtf/release-trains/packages';
33
- import { selectTrain } from '@wtf/release-trains/trains';
49
+ import { removeAt, tempFolder } from '@wtf/platform-fs/files';
50
+ import { isReleaseError, throwAll } from '@wtf/release/errors';
51
+ import { copy, readDir, writeFile } from '@wtf/release/files';
52
+ import { publishOrder } from '@wtf/release/order';
53
+ import { pathMatcher } from '@wtf/release/paths';
54
+ import { joinPath, relativePath, resolvePath } from '@wtf/release/posix';
55
+ import { anyObject, anyOf, arrayOf, booleanOf, errorsOf, issueOf, objectOf, oneOf, refined, stringOf } from '@wtf/release/validator';
34
56
  import { checkPackages, formatIssue } from "./check.js";
35
- import { npmEcosystem } from "./ecosystem.js";
57
+ import { NpmError } from "./errors.js";
36
58
  import { packagesOfManifests } from "./manifests.js";
37
59
  import { prepareManifest } from "./prepare.js";
38
- import { npmPublisher, registryOf } from "./publishers.js";
60
+ import { isPublisher, npmPublisher, registryOf } from "./publishers.js";
61
+ import { smokePacked } from "./smoke.js";
39
62
  import { readWorkspace } from "./workspace.js";
40
- /**
41
- * The dist-tag of a version: `latest` of a release, and the first identifier of a prerelease, `beta`
42
- * of `2.1.0-beta.3`, the channel of the branch semantic-release named it after; `prerelease` of one
43
- * whose first identifier is a number, as `2.1.0-0`, which npm takes for a range and not a dist-tag,
44
- * and is the channel of no branch. A prerelease never takes `latest`, which every install with no
45
- * version gets.
46
- *
47
- * @param version - The version of the release, as `2.1.0` or `2.1.0-beta.3`.
48
- * @returns The dist-tag of the version.
49
- */
50
- export const distTagOf = (version) => {
51
- const prerelease = /^\d+\.\d+\.\d+-([0-9A-Za-z-]+)/.exec(version)?.[1];
52
- if (prerelease === undefined)
53
- return 'latest';
54
- return /^\d+$/.test(prerelease) ? 'prerelease' : prerelease;
55
- };
56
- /**
57
- * The dist-tag of a release on `channel`, or of its version where it has none ({@link distTagOf}):
58
- * `release-1.x` of a channel npm takes for a range, as `1.x` of a branch of maintenance, which npm
59
- * refuses as a dist-tag, as `@semantic-release/npm` names it. An empty channel is none, as it is
60
- * to `@semantic-release/npm`.
61
- */
62
- const distTagOfChannel = (channel, version) => {
63
- const tag = channel === null || channel === undefined || channel === '' ? distTagOf(version) : channel;
64
- return semver.validRange(tag) === null ? tag : `release-${tag}`;
65
- };
66
- /**
67
- * The train `train` selects, the workspace of `cwd`, its packages of a name, the packages of the
68
- * train, and those of them that publish, in the order to publish them in, with the faults of that
69
- * order.
70
- */
71
- async function publishingOf(trains, train, { cwd, system, first, last }) {
72
- const [name] = selectTrain(trains, train);
73
- const workspace = await readWorkspace(cwd, { system });
74
- const all = packagesOfManifests(workspace);
75
- const packages = packagesOf(trains, name, all);
76
- const publishable = packages.filter(({ published }) => published);
77
- return { name, workspace, all, packages, ...publishOrder(publishable, { first, last }) };
78
- }
79
63
  /**
80
64
  * The variables of the environment of an ID token the CI gives, of which npm signs provenance:
81
65
  * Sigstore's on GitLab, GitHub's OIDC on GitHub Actions. Not `NPM_ID_TOKEN`, of npm's trusted
@@ -91,163 +75,254 @@ export const RUNNER_ENVIRONMENTS = [
91
75
  'CI_RUNNER_ENVIRONMENT',
92
76
  'RUNNER_ENVIRONMENT'
93
77
  ];
78
+ /** What is wrong with the order of the plan: its own, or what `publishOrder` finds of `own`. */
79
+ const faultsOf = (plan) => plan.faults ?? publishOrder(plan.own).faults;
80
+ /** A list of dist-tags, non-empty, each one npm takes, of no range of semver, which npm refuses. */
81
+ const distTagsOf = () => refined(arrayOf(stringOf({ minLength: 1 }), { minItems: 1 }), (tags, path) => tags.flatMap((tag, index) => semver.validRange(tag) === null
82
+ ? []
83
+ : [
84
+ issueOf([...path, index], tag, 'a dist-tag, which npm takes of no range of semver', {
85
+ details: `npm reads ${tag} as a range of versions, and refuses it as a dist-tag: name it as \`release-${tag}\`.`
86
+ })
87
+ ]), 'Each a dist-tag npm takes, of no range of semver.');
88
+ /** The validator of the options of the release of npm packages, each step's. */
89
+ export const npmOptionsSchema = objectOf({
90
+ moving: { of: distTagsOf() },
91
+ publisher: {
92
+ of: refined(anyObject(), (publisher, path) => isPublisher(publisher)
93
+ ? []
94
+ : [
95
+ issueOf(path, undefined, 'a publisher', {
96
+ code: 'EPUBLISHER',
97
+ message: () => 'The publisher is no publisher.',
98
+ details: 'A publisher has a `name`, and `lookup` and `publish` functions, and `pack`, `tag` and `page`, where it has them, functions too: see the Publisher type.'
99
+ })
100
+ ], 'A Publisher of the types of the package.'),
101
+ optional: true
102
+ },
103
+ access: { of: oneOf(['public', 'restricted']), optional: true },
104
+ provenance: {
105
+ of: anyOf([booleanOf(), oneOf(['auto'])], "true, false or 'auto'"),
106
+ optional: true
107
+ },
108
+ registry: { of: stringOf({ minLength: 1 }), optional: true },
109
+ conditions: { of: arrayOf(stringOf({ minLength: 1 })), optional: true },
110
+ check: { of: booleanOf(), optional: true },
111
+ aside: {
112
+ of: objectOf({
113
+ packages: { of: arrayOf(stringOf({ minLength: 1 })), optional: true },
114
+ moving: { of: distTagsOf(), optional: true },
115
+ none: { of: stringOf({ minLength: 1 }), optional: true }
116
+ }),
117
+ optional: true
118
+ },
119
+ tarballDir: { of: stringOf({ minLength: 1 }), optional: true },
120
+ pack: { of: arrayOf(stringOf({ minLength: 1 })), optional: true },
121
+ forbid: { of: arrayOf(stringOf({ minLength: 1 })), optional: true }
122
+ });
123
+ /** The error of the faults of the packages of a release, of how to go on after them. */
124
+ const checkError = (issues, details) => new NpmError('EPUBLISHCHECK', `The packages would not publish as they should:\n${issues.map((issue) => `- ${formatIssue(issue)}`).join('\n')}`, details);
125
+ /**
126
+ * The faults of the packages of `aside`: `EPUBLISHCHECK` of a name of no package of the workspace,
127
+ * which puts nothing aside. One of no package of the plan is none, as of `aside` at the root of a
128
+ * config of several parts of the repo, each of its own release.
129
+ */
130
+ const asideFaults = (aside, workspace) => {
131
+ const names = new Set(workspace.map(({ name }) => name));
132
+ const unknown = (aside?.packages ?? []).filter((name) => !names.has(name));
133
+ return unknown.length === 0
134
+ ? []
135
+ : [
136
+ new NpmError('EPUBLISHCHECK', `\`aside\` names ${unknown.join(', ')}, of no package of the workspace.`, 'Name a package of the workspace in `aside`, by the name of its package.json.')
137
+ ];
138
+ };
94
139
  /**
95
- * The manifest of each package of the train that publishes, as it publishes, in the order to
96
- * publish them in, and checked: of the version of the release; each `workspace:` range of a package
97
- * of the train resolved to that version, as pnpm resolves `workspace:*`, `workspace:^` and
98
- * `workspace:~`; of a package of another train, to the last release of that train, by its tags
99
- * merged into `HEAD` (`releasedVersions` of `@wtf/release-trains/packages`), or of `released`; and
100
- * each export of the source stripped; and of each private package of the train of `pack`, the same,
101
- * of no check. It writes nothing. Throws `EDEPENDENCYUNSATISFIED` of a range no release of the other
102
- * train satisfies, `EDEPENDENCYNOTRAIN` and `EDEPENDENCYNORELEASE` of a range of a package of no
103
- * train, or of a train with no release, that reaches the registry, and `EPUBLISHCHECK` of what is
104
- * wrong with a manifest.
140
+ * Checks the packages of a plan as they are in the repo, before a release makes them ready: each
141
+ * scoped one public, of a license and a repository, none running with a private package, and an
142
+ * order of publishing that holds, unless `check` is `false`; and each name of `aside` of a package
143
+ * of the workspace. It reads the workspace of the folder of the repo, and writes nothing. Gives the
144
+ * name of each package that publishes, in order; throws `EPUBLISHCHECK` of what is wrong.
105
145
  *
106
- * @param release - The trains, the train, the version and the channel of the release, and its
107
- * options.
108
- * @returns The packages of the train, each manifest as it publishes, in the order to publish.
146
+ * @param root - The folder of the repo, a handle of the File System standard.
147
+ * @param plan - The packages that publish, in order, and what is wrong with their order.
148
+ * @param options - Whether the packages are checked, the access of each, and the packages of
149
+ * `aside`.
150
+ * @returns The name of each package that publishes, in the order of publishing.
109
151
  */
110
- export async function resolveManifests(release) {
111
- const { system, version } = release;
112
- const cwd = release.cwd ?? system.cwd;
113
- const git = { cwd, env: release.env, system };
114
- const distTag = release.distTag === undefined || release.distTag === ''
115
- ? distTagOfChannel(release.channel, version)
116
- : release.distTag;
117
- const { name, workspace, all, packages, order, faults } = await publishingOf(release.trains, release.train, { cwd, system, first: release.first, last: release.last });
118
- const repo = [{ ecosystem: npmEcosystem, packages: all }];
119
- // A name of `aside` of no package of the workspace fails here, before anything is packed; and a
120
- // range of the trains of `released` is not checked: their version is the one given.
121
- const { released: given } = release;
152
+ export async function checkPlan(root, plan, options = {}) {
153
+ const workspace = await readWorkspace(root);
154
+ const issues = options.check === false
155
+ ? []
156
+ : checkPackages(plan.own, {
157
+ prepared: false,
158
+ faults: faultsOf(plan),
159
+ workspace,
160
+ access: options.access
161
+ });
162
+ // A name of `aside` of no package fails here, before anything is packed, with each fault of the
163
+ // packages, together.
122
164
  throwAll([
123
- ...asideFaults(release.aside, workspace),
124
- ...(given === undefined ? await unsatisfiedErrors(release.trains, name, repo, git) : [])
165
+ ...asideFaults(options.aside, workspace),
166
+ ...(issues.length === 0
167
+ ? []
168
+ : [checkError(issues, 'Fix each before the release: nothing is published until each is.')])
125
169
  ]);
126
- // The last release of each other train, by its tags, and the train of each package.
127
- const versions = given === undefined
128
- ? await releasedVersions({ trains: release.trains, train: name, repo, ...git })
129
- : new Map(Object.entries(given));
130
- const owners = ownersOf(release.trains, all);
131
- const own = new Set(packages.map(({ name: each }) => each));
132
- // A private package never publishes, of this train or another: it has no version to resolve to,
133
- // so a devDependency on it is dropped, and any other range of it is left for the check to find.
134
- const publishable = new Set(all.flatMap(({ name: each, published }) => (published ? [each] : [])));
170
+ return plan.own.map(({ name }) => name);
171
+ }
172
+ /**
173
+ * Checks a release before anything of it is written: its version, one of semver
174
+ * (`EPUBLISHVERSION`), as npm publishes no other; its options, by {@link npmOptionsSchema}
175
+ * (`EOPTIONS`, and `EPUBLISHER` of a publisher that is none); and the packages of its plan as they
176
+ * are in the repo, by {@link checkPlan} (`EPUBLISHCHECK`). The faults of the version and of the
177
+ * options are thrown together, before the packages are read.
178
+ *
179
+ * @param release - The version of the release, the folder of the repo and the platform.
180
+ * @param plan - The packages that publish, in order.
181
+ * @param options - The options of the release.
182
+ */
183
+ export async function verifyRelease(release, plan, options) {
184
+ const { version } = release;
185
+ throwAll([
186
+ ...(semver.valid(version) === version.split('+')[0]
187
+ ? []
188
+ : [
189
+ new NpmError('EPUBLISHVERSION', `${version} is no version of semver, which npm publishes.`, 'Give the release a version of semver, as `1.2.0` or `1.2.0-beta.1`, of no `v`.')
190
+ ]),
191
+ ...errorsOf(npmOptionsSchema, options, NpmError)
192
+ ]);
193
+ await checkPlan(release.root, plan, options);
194
+ }
195
+ /**
196
+ * The manifest of each package of the plan that publishes, as it publishes, in the order of the
197
+ * plan, and checked: of the version of the release; each `workspace:` range of a package of the
198
+ * plan resolved to that version, as pnpm resolves `workspace:*`, `workspace:^` and `workspace:~`,
199
+ * and of another package of the repo to its version of the plan's `versions`; each export of the
200
+ * source stripped; and of each private package of `pack`, the same, of no check. A devDependency of
201
+ * a package of the workspace of no version is dropped, as no consumer installs it; any other range
202
+ * of no version is left, for the check to find. It writes nothing. Throws `EPUBLISHCHECK` of what
203
+ * is wrong with a manifest, and of a name of `aside` of no package of the workspace.
204
+ *
205
+ * @param release - The version of the release, and the folder of the repo, whose workspace it
206
+ * reads.
207
+ * @param plan - The packages that publish, in order, the versions of the repo, and the packages of
208
+ * `pack`.
209
+ * @param options - The access, the conditions of the source, whether to check, the packages of
210
+ * `aside`, and of `pack`.
211
+ * @returns The packages of the release, each manifest as it publishes, in the order to publish.
212
+ */
213
+ export async function resolvePlan(release, plan, options = {}) {
214
+ const { version } = release;
215
+ const workspace = await readWorkspace(release.root);
216
+ throwAll(asideFaults(options.aside, workspace));
217
+ const own = new Set(plan.own.map(({ name }) => name));
218
+ // A private package never publishes: it has no version to resolve to, so a devDependency on it is
219
+ // dropped, and any other range of it is left for the check to find.
220
+ const publishable = new Set(workspace.flatMap(({ name, json }) => name !== undefined && json['private'] !== true ? [name] : []));
135
221
  const versionOf = (each) => {
136
222
  if (!publishable.has(each))
137
223
  return undefined;
138
- if (own.has(each))
139
- return version;
140
- const owner = owners.get(each);
141
- return owner === undefined ? undefined : versions.get(owner);
224
+ return own.has(each) ? version : plan.versions.get(each);
142
225
  };
143
- const names = new Set(workspace.flatMap(({ name: each }) => (each === undefined ? [] : [each])));
144
- const prepared = order.map((pkg) => Object.assign({}, pkg, {
145
- json: prepareManifest(pkg.json, {
226
+ const names = new Set(workspace.flatMap(({ name }) => (name === undefined ? [] : [name])));
227
+ const prepared = plan.own.map(({ name, dir, json }) => ({
228
+ name,
229
+ dir,
230
+ json: prepareManifest(json, {
146
231
  version,
147
232
  versionOf,
148
233
  workspace: names,
149
- conditions: release.conditions
234
+ conditions: options.conditions
150
235
  }).manifest
151
236
  }));
152
- if (release.check !== false) {
237
+ if (options.check !== false) {
153
238
  const issues = checkPackages(prepared, {
154
- faults,
239
+ faults: faultsOf(plan),
155
240
  workspace,
156
- access: release.access,
157
- conditions: release.conditions
241
+ access: options.access,
242
+ conditions: options.conditions
158
243
  });
159
244
  if (issues.length > 0)
160
- throw new TrainsError('EPUBLISHCHECK', `Train ${name} would not publish as it should:\n${issues.map((issue) => `- ${formatIssue(issue)}`).join('\n')}`, 'Fix each, and run the release again: nothing was published.');
245
+ throw checkError(issues, 'Fix each, and run the release again: nothing was published.');
161
246
  }
162
247
  // Each private package of `pack`, of the version of the release, as each private package of the
163
248
  // workspace it lists is: none of them has a release of its own to resolve to.
164
- const packs = pathMatcher(release.pack ?? []);
165
- const packOnly = packages.filter(({ dir, json }) => json['private'] === true && packs(dir));
249
+ const packs = pathMatcher(options.pack ?? []);
250
+ const packOnly = (plan.packages ?? packagesOfManifests(workspace)).filter(({ dir, json }) => json['private'] === true && packs(dir));
166
251
  return {
167
- train: name,
168
252
  version,
169
- distTag,
170
- packages: prepared.map(({ dir, json }) => ({
171
- name: json['name'],
172
- dir,
173
- manifest: json
174
- })),
253
+ packages: prepared.map(({ name, dir, json }) => ({ name, dir, manifest: json })),
175
254
  packOnly: packOnly
176
255
  .toSorted((a, b) => a.dir.localeCompare(b.dir))
177
- .map(({ dir, json }) => ({
178
- name: json['name'] ?? dir,
256
+ .map(({ name, dir, json }) => ({
257
+ name,
179
258
  dir,
180
259
  manifest: prepareManifest(json, {
181
260
  version,
182
261
  versionOf: (each) => versionOf(each) ?? (names.has(each) ? version : undefined),
183
262
  workspace: names,
184
- conditions: release.conditions
263
+ conditions: options.conditions
185
264
  }).manifest
186
265
  }))
187
266
  };
188
267
  }
189
268
  /**
190
- * Packs the packages of a release, of the manifests {@link resolveManifests} gave: each package's
191
- * folder copied, but its `node_modules`, into a folder of the system's temporary files, its manifest
192
- * as it publishes written into the copy, and each packed there by a publisher that packs, all
193
- * before the first publish. The repo is left as it is. The copies and the tarballs stay until
194
- * `cleanup`, and the tarballs of a `tarballDir` the caller names after it too; of a pack that fails,
195
- * they are removed.
269
+ * Packs the packages of a release, of the manifests {@link resolvePlan} gave: each package's folder
270
+ * copied, but its `node_modules`, into a temporary folder of the platform, its manifest as it
271
+ * publishes written into the copy, and each packed there by a publisher that packs, all before the
272
+ * first publish. The repo is left as it is. The copies and the tarballs stay until `cleanup`, and
273
+ * the tarballs of a `tarballDir` the caller names after it too; of a pack that fails, they are
274
+ * removed.
196
275
  *
197
- * @param resolved - The packages of the release, of {@link resolveManifests}.
198
- * @param options - The publisher, the folder of the repo, the system, the folder of the tarballs,
199
- * and the log.
276
+ * @param resolved - The packages of the release, of {@link resolvePlan}.
277
+ * @param options - The publisher, the folder of the repo, the platform, the folder of the
278
+ * tarballs, and the log.
200
279
  * @returns The packages packed, their copies and their tarballs, and how each goes.
201
280
  */
202
- export async function packTrain(resolved, options) {
281
+ export async function packRelease(resolved, options) {
203
282
  const context = contextOf(options);
204
- const { system, cwd, log } = context;
283
+ const { root, platform, log } = context;
205
284
  const publisher = options.publisher ?? npmPublisher;
206
- const { train, version, distTag } = resolved;
207
- const staging = joinPath(system.tmpdir, `release-trains-${train}-${version}-${system.now().getTime()}`);
285
+ const { version } = resolved;
286
+ const folder = await tempFolder(platform, { prefix: `release-npm-${version}-` });
287
+ const staging = folder.path;
208
288
  const tarballDir = options.tarballDir === undefined
209
289
  ? joinPath(staging, '.tarballs')
210
- : resolvePath(cwd, options.tarballDir);
290
+ : resolvePath(platform.dirs.cwd, options.tarballDir);
211
291
  const tarballs = new Map();
212
292
  // The folder of the tarballs by default is in the staging, which goes. One the caller names keeps
213
293
  // them, as for a later plugin to attach to the release of the host.
214
- const cleanup = () => system.fs.rm(staging);
294
+ const cleanup = () => folder.remove();
215
295
  const discard = async () => {
216
296
  await Promise.allSettled([
217
297
  cleanup(),
218
- ...[...tarballs.values()].map((tarball) => system.fs.rm(tarball))
298
+ ...[...tarballs.values()].map((tarball) => removeAt(platform, tarball, { recursive: true }))
219
299
  ]);
220
300
  };
221
301
  try {
222
- await system.fs.rm(staging);
223
302
  const packages = [];
224
303
  const packOnly = [];
225
304
  const copies = [
226
305
  ...resolved.packages.map((each) => [each, packages]),
227
- ...(resolved.packOnly ?? []).map((each) => [each, packOnly])
306
+ ...resolved.packOnly.map((each) => [each, packOnly])
228
307
  ];
229
308
  for (const [{ name, dir, manifest }, into] of copies) {
230
- const from = joinPath(cwd, dir);
231
- const to = joinPath(staging, dir);
232
309
  // oxlint-disable-next-line no-await-in-loop -- one package after another
233
- const entries = await system.fs.readdir(from);
310
+ const entries = await readDir(root, dir);
234
311
  for (const entry of entries.filter(({ name: each }) => each !== 'node_modules'))
235
312
  // oxlint-disable-next-line no-await-in-loop -- one file after another
236
- await system.fs.copy(joinPath(from, entry.name), joinPath(to, entry.name));
313
+ await copy(root, joinPath(dir, entry.name), folder.handle, joinPath(dir, entry.name));
237
314
  // oxlint-disable-next-line no-await-in-loop -- one package after another
238
- await system.fs.writeFile(joinPath(to, 'package.json'), `${JSON.stringify(manifest, null, 2)}\n`);
239
- into.push({ name, version, dir: to, manifest });
315
+ await writeFile(folder.handle, joinPath(dir, 'package.json'), `${JSON.stringify(manifest, null, 2)}\n`);
316
+ into.push({ name, version, dir: joinPath(staging, dir), manifest });
240
317
  }
241
318
  if (publisher.pack !== undefined) {
242
319
  for (const pkg of [...packages, ...packOnly])
243
320
  // oxlint-disable-next-line no-await-in-loop -- in order, one after the other
244
321
  tarballs.set(pkg.name, await publisher.pack(pkg, tarballDir, context));
245
- log(`Packed ${tarballs.size} package(s) of train ${train}.`);
322
+ log(`Packed ${tarballs.size} package(s) at ${version}.`);
246
323
  }
247
324
  return {
248
- train,
249
325
  version,
250
- distTag,
251
326
  publisher,
252
327
  packages,
253
328
  packOnly,
@@ -264,114 +339,60 @@ export async function packTrain(resolved, options) {
264
339
  throw error;
265
340
  }
266
341
  }
267
- /**
268
- * The dist-tag a release of `version` is published under beside a newer one of `distTag`: of its
269
- * major's line, `release-<major>.x` of `latest`, as semantic-release names a channel of maintenance,
270
- * and `<dist-tag>-<major>.x` of another.
271
- */
272
- const asideOf = (version, distTag, maintenance) => {
273
- const major = semver.major(version);
274
- // A release of a branch of maintenance, or a dist-tag of a line already, has none aside of it: a
275
- // package of it whose dist-tag points above the release is behind in its line. A line's is
276
- // `release-<range>` of the names semantic-release takes for a range of maintenance, as
277
- // `release-1.x` or `release-1.2.x`, not any `release-` name a range may read; or `-<major>.x`.
278
- const ofRange = /^release-\d+(?:\.(?:\d+|x))?\.x$/.test(distTag);
279
- if (maintenance || ofRange || distTag.endsWith(`-${major}.x`))
280
- return undefined;
281
- return distTag === 'latest' ? `release-${major}.x` : `${distTag}-${major}.x`;
282
- };
283
- /**
284
- * The faults of `aside`: `EPUBLISHCHECK` of a name of no package of the workspace, which puts nothing
285
- * aside. One of another train's is none, as of `aside` at the root of a config of several trains.
286
- */
287
- const asideFaults = (aside, workspace) => {
288
- const names = new Set(workspace.map(({ name }) => name));
289
- const unknown = (aside ?? []).filter((name) => !names.has(name));
290
- return unknown.length === 0
291
- ? []
292
- : [
293
- new TrainsError('EPUBLISHCHECK', `\`aside\` names ${unknown.join(', ')}, of no package of the workspace.`, 'Name a package of the workspace in `aside`, by the name of its package.json.')
294
- ];
295
- };
296
342
  /**
297
343
  * The packages of a packed release that the registry has at its version already, by name, asked by
298
- * the publisher's {@link Publisher.lookup}, a few at a time; and, of a publisher that reads
299
- * dist-tags, `EPUBLISHBEHIND` of a package it has not whose dist-tag points above the release, as
300
- * of an older release published after a newer one, which would move the dist-tag back, but of a
301
- * package of `aside`, which goes under the dist-tag of its line, unless that points above it too. A
302
- * package it has is no fault: an older release run again, all of it on the registry, publishes
303
- * nothing. The plugin asks it in `prepare`, before the tag, and {@link publishTrain} before the
304
- * first publish.
344
+ * the publisher's {@link Publisher.lookup}, a few at a time, of the first dist-tag of `moving`;
345
+ * and, of a publisher that reads dist-tags, `EPUBLISHBEHIND` of a package it has not whose dist-tag
346
+ * points above the release, as of an older release published after a newer one, which would move
347
+ * the dist-tag back, but of a package of `aside`, which goes under the dist-tags of `aside.moving`,
348
+ * unless the first of them points above it too. A package it has is no fault: an older release run
349
+ * again, all of it on the registry, publishes nothing. {@link prepareRelease} asks it, before
350
+ * anything is published, and {@link publishPacked} before the first publish.
305
351
  *
306
- * @param packed - The release asked of: its packages, its version, its dist-tag and its publisher.
307
- * @param run - The registry, the packages of `aside`, the system and the log the run asks by.
352
+ * @param packed - The release asked of: its packages, its version and its publisher.
353
+ * @param run - The dist-tags, the registry, the packages of `aside`, the platform and the log the
354
+ * run asks by.
308
355
  * @returns The packages the registry has at the version, and those that go aside.
309
356
  */
310
357
  export function publishedOf(packed, run) {
311
358
  return publishedIn(packed, run, contextOf(run));
312
359
  }
313
- /**
314
- * {@link publishedOf} of a release {@link resolveManifests} resolved, packed of nothing: each
315
- * package of its folder in the repo, by the publisher of `run`, npm's by default. As the job of a
316
- * release asks it of the next version before the tag: what it would find, with no pack.
317
- *
318
- * @param resolved - The packages of the release, of {@link resolveManifests}.
319
- * @param run - The publisher, the registry, the packages of `aside`, the system and the log the run
320
- * asks by.
321
- * @returns The packages the registry has at the version, and those that go aside.
322
- */
323
- export function publishedOfResolved(resolved, run) {
324
- const cwd = run.cwd ?? run.system.cwd;
325
- return publishedOf({
326
- train: resolved.train,
327
- version: resolved.version,
328
- distTag: resolved.distTag,
329
- publisher: run.publisher ?? npmPublisher,
330
- packages: resolved.packages.map(({ name, dir, manifest }) => ({
331
- name,
332
- version: resolved.version,
333
- dir: joinPath(cwd, dir),
334
- manifest
335
- }))
336
- }, run);
337
- }
338
360
  /**
339
361
  * {@link publishedOf} in the context `context`, so a caller that publishes after it keeps one store
340
362
  * of the registry's answers, of which a publish forgets what it changes.
341
363
  */
342
364
  async function publishedIn(packed, run, context) {
343
- const { version, distTag, publisher, packages } = packed;
344
- // Its names are checked of the workspace where it is read, as `resolveManifests` and
345
- // `checkTrain` do: a name of no package of the train here is another train's.
346
- const asidePackages = new Set(run.aside ?? []);
365
+ const { version, publisher, packages } = packed;
366
+ const [distTag] = run.moving;
367
+ // Its names are checked of the workspace where it is read, as `resolvePlan` and `checkPlan` do: a
368
+ // name of no package of the release here is another release's.
369
+ const asidePackages = new Set(run.aside?.packages ?? []);
347
370
  const asked = await lookupAll(publisher, packages.map((pkg) => ({ pkg })), version, distTag, context, run.registry);
348
371
  const behind = asked.filter(({ published, tagged }) => !published && isAhead(tagged, version, distTag));
349
- const aside = asideOf(version, distTag, run.maintenance ?? false);
372
+ const aside = run.aside?.moving?.[0];
350
373
  const blocking = behind.filter(({ pkg }) => aside === undefined || !asidePackages.has(pkg.name));
351
374
  const faults = blocking.length === 0
352
375
  ? []
353
376
  : [
354
- new TrainsError('EPUBLISHBEHIND', `${version} is below what the dist-tag ${distTag} points at, ${blocking.map(({ pkg, tagged }) => `${pkg.name}@${tagged}`).join(', ')}: publishing it would move ${distTag} back.`, aside === undefined
355
- ? run.maintenance === true
356
- ? `This run published nothing. Release forward: a release of a branch of maintenance puts nothing aside, as its channel, ${distTag}, is its line.`
357
- : `This run published nothing. Release forward: ${distTag} is of a line already, which has no other aside of it.`
377
+ new NpmError('EPUBLISHBEHIND', `${version} is below what the dist-tag ${distTag} points at, ${blocking.map(({ pkg, tagged }) => `${pkg.name}@${tagged}`).join(', ')}: publishing it would move ${distTag} back.`, aside === undefined
378
+ ? `This run published nothing. Release forward${run.aside?.none === undefined ? '.' : `: ${run.aside.none}`}`
358
379
  : `This run published nothing. Release forward; or, of a package that joined the train at a higher version of its own, name it in \`aside\`, which publishes it under ${aside}, and its ${distTag} stays.`)
359
380
  ];
360
381
  const published = new Set(asked.flatMap(({ pkg, published: has }) => (has ? [pkg.name] : [])));
361
382
  if (aside === undefined) {
362
383
  throwAll(faults);
363
- return { published, aside: new Set(), asideTag: undefined };
384
+ return { published, aside: new Set() };
364
385
  }
365
- // Each to publish that goes aside: of `aside`, behind the channel's dist-tag.
386
+ // Each to publish that goes aside: of `aside`, behind the release's dist-tag.
366
387
  const goes = behind.filter(({ pkg }) => asidePackages.has(pkg.name));
367
388
  // A package of `aside` the registry has already, whose dist-tag points above the release, and
368
- // whose dist-tag of its line points at it, went aside of an earlier run of it, as of a retry: so
369
- // it is recorded. One the dist-tag of its line points elsewhere at went out on the channel's.
389
+ // whose dist-tag aside points at it, went aside of an earlier run of it, as of a retry: so it is
390
+ // recorded. One the dist-tag aside points elsewhere at went out on the release's.
370
391
  const maybe = asked.filter(({ pkg, published: has, tagged }) => has && asidePackages.has(pkg.name) && isAhead(tagged, version, distTag));
371
- // The dist-tag of its line is no more to move back than the channel's: each to publish is asked
372
- // of it too, in the same round as those of an earlier run, each in its place, and of a package
373
- // that blocks the release too, so every fault is thrown together.
374
- // Each asked of the dist-tag of its line, each marked of what it is asked for.
392
+ // The dist-tag aside is no more to move back than the release's: each to publish is asked of it
393
+ // too, in the same round as those of an earlier run, each in its place, and of a package that
394
+ // blocks the release too, so every fault is thrown together. Each is marked of what it is asked
395
+ // for.
375
396
  const ofLine = maybe.length + goes.length === 0
376
397
  ? []
377
398
  : await lookupAll(publisher, [
@@ -385,36 +406,55 @@ async function publishedIn(packed, run, context) {
385
406
  ...(asideAhead.length === 0
386
407
  ? []
387
408
  : [
388
- new TrainsError('EPUBLISHBEHIND', `${version} is below what the dist-tag ${aside} points at, ${asideAhead.map(({ pkg, tagged }) => `${pkg.name}@${tagged}`).join(', ')}: publishing it aside would move ${aside} back.`, 'This run published nothing. Release forward.')
409
+ new NpmError('EPUBLISHBEHIND', `${version} is below what the dist-tag ${aside} points at, ${asideAhead.map(({ pkg, tagged }) => `${pkg.name}@${tagged}`).join(', ')}: publishing it aside would move ${aside} back.`, 'This run published nothing. Release forward.')
389
410
  ])
390
411
  ]);
391
- return {
392
- published,
393
- aside: new Set([...goes, ...went].map(({ pkg }) => pkg.name)),
394
- asideTag: aside
395
- };
412
+ return { published, aside: new Set([...goes, ...went].map(({ pkg }) => pkg.name)) };
396
413
  }
397
414
  /**
398
- * Publishes the packages {@link packTrain} packed, each in order, under the dist-tag of the
399
- * channel, with the provenance of the build where the CI gives an ID token, and skips each the
400
- * registry has at the version already. It leaves the copies and the tarballs, for `cleanup`.
401
- * Before the first, it asks the registry by {@link publishedOf}: `EPUBLISHBEHIND` of a release that
402
- * would move a dist-tag back, and nothing published.
415
+ * Publishes the packages {@link packRelease} packed, each in order, under the first dist-tag of
416
+ * `moving`, with the provenance of the build where the CI gives an ID token, and skips each the
417
+ * registry has at the version already; then points each other dist-tag of `moving` at the version,
418
+ * of each package, as {@link moveRelease} does, and of a package aside each other of
419
+ * `aside.moving`. Before the first, it asks the registry by {@link publishedOf}: `EPUBLISHBEHIND`
420
+ * of a release that would move a dist-tag back, and nothing published. It removes what it packed
421
+ * after: every tarball, of a `tarballDir` named too, of a publish that fails, whose error is the
422
+ * one thrown, as a release that failed has no assets; the copies, and the tarballs but of a
423
+ * `tarballDir` named, of one that passes. A copy that cannot be removed then fails nothing: the log
424
+ * says where it stays.
403
425
  *
404
- * @param packed - The packages of the release, of {@link packTrain}.
405
- * @param run - The access, the provenance, the registry, the system and the log of the publish.
426
+ * @param packed - The packages of the release, of {@link packRelease}.
427
+ * @param run - The dist-tags, the access, the provenance, the registry, the packages of `aside`,
428
+ * the platform and the log of the publish.
406
429
  * @returns What was published, and what was skipped, of each package.
407
430
  */
408
- export async function publishTrain(packed, run) {
431
+ export async function publishPacked(packed, run) {
432
+ let published;
433
+ try {
434
+ published = await publishAll(packed, run);
435
+ }
436
+ catch (error) {
437
+ await packed.discard();
438
+ throw error;
439
+ }
440
+ await packed.cleanup().catch((error) => {
441
+ run.log?.(`Left ${packed.staging}: ${error.message}`);
442
+ });
443
+ return published;
444
+ }
445
+ /** {@link publishPacked}, of no removal of what was packed. */
446
+ async function publishAll(packed, run) {
409
447
  const context = contextOf(run);
410
- const { env, dryRun, log } = context;
411
- const { version, distTag, publisher, packages } = packed;
412
- const provenance = provenanceOf(run.provenance, env);
413
- const { published, aside, asideTag } = await publishedIn(packed, run, context);
448
+ const { platform, dryRun, log } = context;
449
+ const { version, publisher, packages } = packed;
450
+ const [distTag, ...more] = run.moving;
451
+ const provenance = provenanceOf(run.provenance, platform.env);
452
+ const { published, aside } = await publishedIn(packed, run, context);
453
+ const [asideTag, ...asideMore] = run.aside?.moving ?? [];
414
454
  const done = [];
415
455
  for (const pkg of packages) {
416
456
  const skip = published.has(pkg.name);
417
- // A package of `aside` whose dist-tag points above the release, under the dist-tag of its line.
457
+ // A package of `aside` whose dist-tag points above the release, under the dist-tag aside.
418
458
  const tag = aside.has(pkg.name) && asideTag !== undefined ? asideTag : distTag;
419
459
  if (!skip) {
420
460
  const access = run.access ??
@@ -442,6 +482,18 @@ export async function publishTrain(packed, run) {
442
482
  registry: registryOf(pkg, run.registry)
443
483
  });
444
484
  }
485
+ // Each other dist-tag of the release, or aside, pointed at the version too, each as a move does.
486
+ const of = (goesAside) => packages
487
+ .filter(({ name }) => aside.has(name) === goesAside)
488
+ .map((pkg) => ({ pkg, dir: relativePath(packed.staging, pkg.dir) || '.' }));
489
+ for (const [tags, items] of [
490
+ [more, of(false)],
491
+ [asideMore, of(true)]
492
+ ])
493
+ for (const tag of tags)
494
+ // oxlint-disable-next-line no-await-in-loop -- one dist-tag after another
495
+ if (items.length > 0)
496
+ await moveTag(publisher, items, version, tag, context, run.registry);
445
497
  return { version, distTag, publisher: publisher.name, packages: done };
446
498
  }
447
499
  /** How many questions {@link lookupAll} asks the registry at a time: as many processes of npm. */
@@ -449,9 +501,9 @@ const LOOKUPS = 4;
449
501
  /**
450
502
  * What the registry has of the package of each of `items`, in their order, each item given back
451
503
  * with the {@link Lookup} of its package, by the publisher's {@link Publisher.lookup},
452
- * {@link LOOKUPS} at a time. Each settles before a fault is thrown, so no question runs on in a copy
453
- * the caller then removes; a fault of the code is thrown as it is, and every question that failed,
454
- * together, each by `advise`.
504
+ * {@link LOOKUPS} at a time. Each settles before a fault is thrown, so no question runs on in a
505
+ * copy the caller then removes; a fault of the code is thrown as it is, and every question that
506
+ * failed, together, each by `advise`.
455
507
  */
456
508
  async function lookupAll(publisher, items, version, distTag, context, registry, advise = (error) => error) {
457
509
  // At most LOOKUPS questions at a time, each package's answer in its place.
@@ -476,7 +528,7 @@ async function lookupAll(publisher, items, version, distTag, context, registry,
476
528
  await Promise.all(Array.from({ length: Math.min(LOOKUPS, items.length) }, worker));
477
529
  const failed = settled.flatMap((each) => each.status === 'rejected' ? [each.reason] : []);
478
530
  // By its index, so a rejection of no reason, as `undefined`, is thrown as it is too.
479
- const unexpected = failed.findIndex((error) => !(error instanceof TrainsError));
531
+ const unexpected = failed.findIndex((error) => !isReleaseError(error));
480
532
  if (unexpected !== -1)
481
533
  throw failed[unexpected];
482
534
  // An answer that is no Lookup, as a boolean of a publisher's own, would read as not published,
@@ -499,7 +551,7 @@ async function lookupAll(publisher, items, version, distTag, context, registry,
499
551
  ...(malformed.length === 0
500
552
  ? []
501
553
  : [
502
- new TrainsError('EPUBLISHER', `The publisher ${publisher.name} looked up ${malformed.join(', ')} with no Lookup.`, 'A lookup answers `{ published, tagged }`: `published` a boolean, `tagged` a version, or none, as `undefined` or `null`. See the Publisher type.')
554
+ new NpmError('EPUBLISHER', `The publisher ${publisher.name} looked up ${malformed.join(', ')} with no Lookup.`, 'A lookup answers `{ published, tagged }`: `published` a boolean, `tagged` a version, or none, as `undefined` or `null`. See the Publisher type.')
503
555
  ])
504
556
  ]);
505
557
  // The two answers alone, so no other key of a publisher's own answer, as `pkg`, overwrites the
@@ -510,11 +562,10 @@ async function lookupAll(publisher, items, version, distTag, context, registry,
510
562
  return { ...item, published, tagged: tagged ?? undefined };
511
563
  });
512
564
  }
513
- /** The context a publisher runs in, of a run's options: the system's, where they name none. */
565
+ /** The context a publisher runs in, of a run's options: the folder of the repo, the platform. */
514
566
  const contextOf = (run) => ({
515
- system: run.system,
516
- cwd: run.cwd ?? run.system.cwd,
517
- env: run.env ?? run.system.env,
567
+ root: run.root,
568
+ platform: run.platform,
518
569
  dryRun: run.dryRun ?? false,
519
570
  log: run.log ?? (() => { }),
520
571
  // One store of the registry's answers a step: each step asks afresh.
@@ -532,115 +583,83 @@ const isAhead = (at, version, distTag) => at !== undefined &&
532
583
  semver.gt(at, version) &&
533
584
  (distTag !== 'latest' || semver.prerelease(at) === null || semver.prerelease(version) !== null);
534
585
  /**
535
- * Publishes a release of a train in one call, as a job of the pipeline of its tag does: its
536
- * manifests resolved ({@link resolveManifests}), its packages packed ({@link packTrain}) and
537
- * published ({@link publishTrain}). The copies go after, and the tarballs but of a `tarballDir`
538
- * named; every tarball goes of a publish that fails, whose error is the one thrown. A copy that
539
- * cannot be removed after the publish fails nothing: the log says where it stays.
540
- *
541
- * @param release - The release, and what each of its three steps takes.
542
- * @returns What was published, and what was skipped, of each package.
586
+ * The dist-tag `tag` of each package of `items` pointed at `version`, in order, as
587
+ * {@link moveRelease} moves each.
543
588
  */
544
- export async function releaseTrain(release) {
545
- return publishPacked(await packTrain(await resolveManifests(release), release), release);
546
- }
547
- /**
548
- * Publishes a release {@link packTrain} packed, by {@link publishTrain}, and removes what it packed:
549
- * every tarball, of a `tarballDir` named too, of a publish that fails, whose error is the one
550
- * thrown, as a release that failed has no assets; the copies, and the tarballs but of a
551
- * `tarballDir` named, of one that passes. A copy that cannot be removed then fails nothing: the log
552
- * says where it stays.
553
- *
554
- * @param packed - The packages of the release, of {@link packTrain}.
555
- * @param run - The access, the provenance, the registry, the system and the log of the publish.
556
- * @returns What was published, and what was skipped, of each package.
557
- */
558
- export async function publishPacked(packed, run) {
559
- let published;
560
- try {
561
- published = await publishTrain(packed, run);
562
- }
563
- catch (error) {
564
- await packed.discard();
565
- throw error;
566
- }
567
- await packed.cleanup().catch((error) => {
568
- run.log?.(`Left ${packed.staging}: ${error.message}`);
569
- });
570
- return published;
571
- }
572
- /**
573
- * Moves a release of a train published already to the channel of `channel`: the dist-tag of each
574
- * package of the train that publishes pointed at the version, in the order of publishing, by the
575
- * publisher's {@link Publisher.tag}; each the registry does not have at the version, as a package
576
- * that joined the train after the release, skipped, and each whose dist-tag points above the
577
- * version, of a publisher that reads dist-tags, as of an older release moved after a newer one,
578
- * which would move it back; one whose dist-tag points at the version already is on the channel,
579
- * and not moved again. The registry is asked of every package, a few at a time, by the
580
- * publisher's {@link Publisher.lookup}, before the first dist-tag moves. A publisher that moves no
581
- * dist-tag moves none, and says so: the release stays on its channel.
582
- *
583
- * @param release - The trains, the train, the version and the channel of the release, the
584
- * publisher, the system and the log.
585
- * @returns What was moved, and what was skipped, of each package.
586
- */
587
- export async function tagTrain(release) {
588
- const { version } = release;
589
- const context = contextOf(release);
590
- const { system, cwd, log } = context;
591
- const publisher = release.publisher ?? npmPublisher;
592
- const distTag = distTagOfChannel(release.channel, version);
593
- // The train is selected first, so a run of no train fails of a publisher that moves nothing too.
594
- const { order } = await publishingOf(release.trains, release.train, {
595
- cwd,
596
- system,
597
- first: release.first,
598
- last: release.last
599
- });
600
- if (publisher.tag === undefined) {
601
- log(`The publisher ${publisher.name} moves no dist-tag: ${version} stays on its channel.`);
602
- return { version, distTag, publisher: publisher.name, packages: [] };
603
- }
604
- // Each package with its folder from the root of the repo, as the order gives it.
605
- const listed = order.map(({ name: each, dir, json }) => ({
606
- dir,
607
- pkg: { name: each, version, dir: joinPath(cwd, dir), manifest: json }
608
- }));
589
+ async function moveTag(publisher, items, version, tag, context, registry) {
590
+ const { log } = context;
591
+ if (publisher.tag === undefined)
592
+ return [];
609
593
  // The question to the registry of each package, a few at a time, before the first dist-tag moves:
610
- // a question that fails moves none. The advice of a publish, to delete the tag and run again,
611
- // moves no channel: a run of semantic-release again finds the channel recorded, and asks for no
612
- // `addChannel`.
613
- const asked = await lookupAll(publisher, listed, version, distTag, context, release.registry, (error) => new TrainsError(error.code, error.message, `Once the cause is fixed, point the dist-tag ${distTag} at ${version} by hand, of each package of the train the registry has at ${version} whose ${distTag} is not above it, as one above it would move back: a run of semantic-release again moves no dist-tag, as it records the channel of a release before it asks for \`addChannel\`.`));
594
+ // a question that fails moves none.
595
+ const asked = await lookupAll(publisher, items, version, tag, context, registry, (error) => new NpmError(error.code, error.message, `Once the cause is fixed, move the release to ${tag} again: a dist-tag that points at ${version} already is not moved again, and one above it stays.`));
614
596
  const done = [];
615
597
  for (const { pkg, dir, published: has, tagged: at } of asked) {
616
598
  const each = pkg.name;
617
599
  // A dist-tag that points above the release stays, as a retry of an older release would move it
618
600
  // back.
619
- const behind = has && isAhead(at, version, distTag);
601
+ const behind = has && isAhead(at, version, tag);
620
602
  // A dist-tag that points at the version already is on the channel: it is not moved again.
621
603
  const there = has && at === version;
622
604
  const move = has && !behind && !there;
623
605
  // oxlint-disable-next-line no-await-in-loop -- in order, one after the other
624
606
  if (move)
625
- await publisher.tag(pkg, version, distTag, context, release.registry);
607
+ await publisher.tag(pkg, version, tag, context, registry);
626
608
  log(move
627
- ? `${context.dryRun ? 'WOULD TAG' : 'TAGGED'} ${each}@${version} ${distTag} by ${publisher.name}.`
609
+ ? `${context.dryRun ? 'WOULD TAG' : 'TAGGED'} ${each}@${version} ${tag} by ${publisher.name}.`
628
610
  : there
629
- ? `TAGGED ${each}@${version} ${distTag} already.`
611
+ ? `TAGGED ${each}@${version} ${tag} already.`
630
612
  : behind
631
- ? `SKIP ${each}@${version}: ${distTag} points at ${at}, above it, and stays.`
613
+ ? `SKIP ${each}@${version}: ${tag} points at ${at}, above it, and stays.`
632
614
  : `SKIP ${each}@${version}: not on the registry, so no dist-tag to move.`);
633
615
  done.push({
634
616
  name: each,
635
617
  version,
636
618
  dir,
619
+ tag,
637
620
  status: move || there ? 'tagged' : 'skipped',
638
621
  // A version the registry has not has no page.
639
- page: has ? publisher.page?.(pkg, version, release.registry) : undefined,
640
- registry: registryOf(pkg, release.registry)
622
+ page: has ? publisher.page?.(pkg, version, registry) : undefined,
623
+ registry: registryOf(pkg, registry)
641
624
  });
642
625
  }
643
- return { version, distTag, publisher: publisher.name, packages: done };
626
+ return done;
627
+ }
628
+ /**
629
+ * Moves a release published already to the dist-tags of `moving`: each pointed at the version, of
630
+ * each package of the plan, in the order of publishing, by the publisher's {@link Publisher.tag};
631
+ * each the registry does not have at the version, as a package that joined the release after it was
632
+ * published, skipped, and each whose dist-tag points above the version, of a publisher that reads
633
+ * dist-tags, as of an older release moved after a newer one, which would move it back; one whose
634
+ * dist-tag points at the version already is there, and not moved again. The registry is asked of
635
+ * every package, a few at a time, by the publisher's {@link Publisher.lookup}, before the first
636
+ * dist-tag moves. A publisher that moves no dist-tag moves none, and says so: the release stays
637
+ * where it was published.
638
+ *
639
+ * @param release - The version of the release, the folder of the repo, the platform and the log.
640
+ * @param plan - The packages that publish, in order.
641
+ * @param options - The dist-tags, the publisher and the registry.
642
+ * @returns What was moved, and what was skipped, of each package and each dist-tag.
643
+ */
644
+ export async function moveRelease(release, plan, options) {
645
+ const { version } = release;
646
+ const context = contextOf(release);
647
+ const cwd = context.platform.dirs.cwd;
648
+ const publisher = options.publisher ?? npmPublisher;
649
+ if (publisher.tag === undefined) {
650
+ context.log(`The publisher ${publisher.name} moves no dist-tag: ${version} stays on its channel.`);
651
+ return { version, publisher: publisher.name, packages: [] };
652
+ }
653
+ // Each package with its folder from the root of the repo, as the plan gives it.
654
+ const items = plan.own.map(({ name, dir, json }) => ({
655
+ dir,
656
+ pkg: { name, version, dir: joinPath(cwd, dir), manifest: json }
657
+ }));
658
+ const packages = [];
659
+ for (const tag of options.moving)
660
+ // oxlint-disable-next-line no-await-in-loop -- one dist-tag after another
661
+ packages.push(...(await moveTag(publisher, items, version, tag, context, options.registry)));
662
+ return { version, publisher: publisher.name, packages };
644
663
  }
645
664
  /**
646
665
  * Whether a release is published with provenance: of the option, or of an ID token of the CI on a
@@ -650,39 +669,143 @@ const provenanceOf = (option, env) => option === undefined || option === 'auto'
650
669
  ? ID_TOKENS.some((name) => Boolean(env[name])) &&
651
670
  !RUNNER_ENVIRONMENTS.some((name) => env[name] === 'self-hosted')
652
671
  : option;
672
+ /** What `prepare` packed, by the folder of the repo, the version and the packages. */
673
+ const packed = new Map();
674
+ /** The key of what is packed of a release and its plan. */
675
+ const keyOf = ({ platform, version }, plan) => `${platform.dirs.cwd}\0${version}\0${plan.own.map(({ name }) => name).join('\0')}`;
676
+ /** The options of a pack and a publish of a step, of the release and the options of the release. */
677
+ const runOf = (release, options) => ({
678
+ ...options,
679
+ root: release.root,
680
+ platform: release.platform,
681
+ dryRun: release.dryRun,
682
+ log: release.log
683
+ });
653
684
  /**
654
- * Checks the packages of a train as they are in the repo, before a release makes them ready: each
655
- * scoped one public, of a license and a repository, none running with a private package, and an
656
- * order of publishing that holds, unless `check` is `false`; and each name of `aside` of a package
657
- * of the workspace. Gives the name of each package that publishes, in order; throws `EPUBLISHCHECK`
658
- * of what is wrong.
685
+ * The packages of a release packed: what `prepare` packed of it in this process, which a later step
686
+ * takes, or packed anew of its plan.
687
+ */
688
+ async function packedOf(release, plan, options) {
689
+ const kept = packed.get(keyOf(release, plan));
690
+ if (kept !== undefined)
691
+ return { release: kept, kept: true };
692
+ const run = runOf(release, options);
693
+ return { release: await packRelease(await resolvePlan(release, plan, options), run), kept: false };
694
+ }
695
+ /**
696
+ * Prepares a release: each manifest of its plan resolved as it publishes and checked
697
+ * ({@link resolvePlan}), each package packed from a copy in a temporary folder of the platform
698
+ * ({@link packRelease}), and the registry asked ({@link publishedOf}), so a range of no version, a
699
+ * package that cannot be packed, or a release below the version its dist-tag points at on the
700
+ * registry (`EPUBLISHBEHIND`) fails before anything is published. What it packed is kept, of the
701
+ * folder of the repo, the version and the packages, for `smoke` and `publish` of the same process;
702
+ * of a step that fails, it is removed. Nothing of npm is committed: it writes no file of the repo,
703
+ * and gives none.
659
704
  *
660
- * @param release - The trains, the train of the release, the folder of the repo and the options of
661
- * the publishing.
662
- * @returns The name of each package that publishes, in the order of publishing.
705
+ * @param release - The version of the release, the folder of the repo, the platform and the log.
706
+ * @param plan - The packages that publish, in order, and the versions of the repo.
707
+ * @param options - The options of the release.
708
+ * @returns No file: none of npm is committed, as the version is the release's alone.
663
709
  */
664
- export async function checkTrain(release) {
665
- const { system } = release;
666
- const cwd = release.cwd ?? system.cwd;
667
- const { name, workspace, order, faults } = await publishingOf(release.trains, release.train, {
668
- cwd,
669
- system,
670
- first: release.first,
671
- last: release.last
672
- });
673
- const issues = release.check === false
674
- ? []
675
- : checkPackages(order, { prepared: false, faults, workspace, access: release.access });
676
- // A name of `aside` of no package fails here, before anything is packed, with each fault of the
677
- // packages, together.
678
- throwAll([
679
- ...asideFaults(release.aside, workspace),
680
- ...(issues.length === 0
681
- ? []
682
- : [
683
- new TrainsError('EPUBLISHCHECK', `Train ${name} would not publish as it should:\n${issues.map((issue) => `- ${formatIssue(issue)}`).join('\n')}`, 'Fix each before the release: nothing is published until each is.')
684
- ])
685
- ]);
686
- return order.map(({ name: each }) => each);
710
+ export async function prepareRelease(release, plan, options) {
711
+ const run = runOf(release, options);
712
+ const packing = await packRelease(await resolvePlan(release, plan, options), run);
713
+ // A release that would move a dist-tag back fails here, before anything is published.
714
+ try {
715
+ await publishedOf(packing, run);
716
+ }
717
+ catch (error) {
718
+ await packing.discard();
719
+ throw error;
720
+ }
721
+ const key = keyOf(release, plan);
722
+ await packed.get(key)?.cleanup();
723
+ packed.set(key, packing);
724
+ return [];
725
+ }
726
+ /**
727
+ * Tries each package of a release as a consumer installs it, and each private package of `pack`, by
728
+ * `smokePacked` of `./smoke`, of the globs of `forbid` and the conditions of the source: of what
729
+ * `prepare` packed in this process, or of a pack of its own, which it removes after. Throws
730
+ * `EPUBLISHSMOKE` of every fault together; what `prepare` packed is removed then too.
731
+ *
732
+ * @param release - The version of the release, the folder of the repo, the platform and the log.
733
+ * @param plan - The packages that publish, in order, and the versions of the repo.
734
+ * @param options - The options of the release.
735
+ */
736
+ export async function smokeRelease(release, plan, options) {
737
+ const of = await packedOf(release, plan, options);
738
+ try {
739
+ await smokePacked({
740
+ packed: [of.release],
741
+ forbid: options.forbid,
742
+ conditions: options.conditions,
743
+ platform: release.platform,
744
+ log: release.log
745
+ });
746
+ }
747
+ catch (error) {
748
+ packed.delete(keyOf(release, plan));
749
+ await of.release.discard();
750
+ throw error;
751
+ }
752
+ if (!of.kept)
753
+ await of.release.discard();
754
+ }
755
+ /**
756
+ * Each package of a published release, for its links: of each package published or skipped, its
757
+ * page, the registry it was published to, and its tarball; of each package of `pack`, its tarball
758
+ * alone. A tarball is there of a `tarballDir` alone, as the publish removes the others.
759
+ */
760
+ function releasedOf(published, of, kept) {
761
+ const file = (name) => (kept ? of.tarballs.get(name) : undefined);
762
+ return [
763
+ ...published.packages.map(({ name, version, page, registry }) => ({
764
+ ecosystem: 'npm',
765
+ name,
766
+ version,
767
+ page,
768
+ registry: registry === undefined ? undefined : { url: registry, type: 'npm', name },
769
+ file: file(name)
770
+ })),
771
+ ...of.packOnly.map(({ name }) => ({
772
+ ecosystem: 'npm',
773
+ name,
774
+ version: of.version,
775
+ file: file(name)
776
+ }))
777
+ ];
778
+ }
779
+ /**
780
+ * Publishes a release: what `prepare` packed of it in this process, or its plan resolved and packed
781
+ * anew ({@link resolvePlan}, {@link packRelease}), each package published in order by
782
+ * {@link publishPacked}, each the registry has already skipped, so a publish run again finishes a
783
+ * release; and what it packed removed, the tarballs of a `tarballDir` kept. Of a dry run, the
784
+ * publisher says what it would publish, and publishes nothing.
785
+ *
786
+ * @param release - The version of the release, the folder of the repo, the platform and the log.
787
+ * @param plan - The packages that publish, in order, and the versions of the repo.
788
+ * @param options - The options of the release.
789
+ * @returns Each package published, or found published, and each of `pack`: its page, its registry
790
+ * and, of a `tarballDir`, its tarball.
791
+ */
792
+ export async function publishRelease(release, plan, options) {
793
+ const of = await packedOf(release, plan, options);
794
+ packed.delete(keyOf(release, plan));
795
+ const published = await publishPacked(of.release, runOf(release, options));
796
+ return releasedOf(published, of.release, options.tarballDir !== undefined);
797
+ }
798
+ /**
799
+ * Removes what `prepare` packed of a release and its plan in this process, the copies and every
800
+ * tarball, of a `tarballDir` too, as of a release that goes no further: none of none.
801
+ *
802
+ * @param release - The version of the release and the platform, of the folder of the repo.
803
+ * @param plan - The packages that publish.
804
+ */
805
+ export async function discardPacked(release, plan) {
806
+ const key = keyOf(release, plan);
807
+ const kept = packed.get(key);
808
+ packed.delete(key);
809
+ await kept?.discard();
687
810
  }
688
811
  //# sourceMappingURL=release.js.map