@elaraai/e3-cloud-cli 1.1.18 → 1.1.20

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.
@@ -9,26 +9,52 @@
9
9
  * a workspace as its own deployable. `build` makes a bundle of an app's
10
10
  * source (the app, and the package it was built against); `deploy` takes
11
11
  * bundles, or sources, imports their package into the repository when it is
12
- * not there, uploads each app object and deploys them together, printing
12
+ * not there (for the platform's next e3, `--for-platform`, hands it to the
13
+ * platform to hold for its move instead: the e3 the platform runs
14
+ * would refuse an import of a package the next e3 exported), uploads each
15
+ * app object and deploys them together, printing
13
16
  * every problem when the deploy is refused; `list`, `versions` and `remove`
14
17
  * read and remove what is deployed (a protected workspace, one with a
15
18
  * staging, refuses a deploy and a removal unless an admin forces it with
16
19
  * `--force`); `list-all` lists every app of the deployment, and which needs
17
20
  * a bundle built with the platform's e3 (admin). An app is developed in the
18
21
  * cloud: deployed to a workspace of one's own and opened there.
22
+ *
23
+ * `--against-workspace` builds an app against the package a workspace runs,
24
+ * as its repository holds it, rather than the package its source exports
25
+ * again: the bundle names that package and carries none. It binds only an
26
+ * app with no page reading a record through indexes, built from a source of
27
+ * that package and version whose pages fit the held package by the
28
+ * platform's own checks, the project's release of them (`package-compare.ts`).
29
+ * A bundle that carries no package is deployed only when the workspace runs
30
+ * exactly its package as read seconds before the deploy. A workspace deploy
31
+ * that completes within those seconds leads the platform to deploy the held
32
+ * package back over it: nothing on the platform side can refuse that yet, so
33
+ * the CLI says it loudly and exits non-zero. So an app whose package the next
34
+ * e3 exports to other bytes is still staged for it on the package its
35
+ * workspace runs, on any release of the platform. A deployment that holds a
36
+ * build's own package for its move takes it with that package
37
+ * instead, where binding refuses it (a page reading through indexes, or a
38
+ * fit the checks refuse): the CLI's refusals then say so, and never to
39
+ * remove the app across the move; on one that does not, they say what they
40
+ * said before.
19
41
  */
20
42
  import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
21
43
  import { dirname, resolve } from 'node:path';
22
- import { decodeBeast2For, encodeBeast2For, none, some } from '@elaraai/east';
23
- import { packageImport, packageList, workspaceList } from '@elaraai/e3-api-client';
24
- import { ApiError, adminAppList, appDeploy, appDeployWait, appGet, appList, repositoryAppList, appDeployRefusedText, appRemove, appsDeploy, appUpload, appVersions, } from '@elaraai/e3-cloud-client';
44
+ import { StringType, decodeBeast2For, encodeBeast2For, equalFor, none, some } from '@elaraai/east';
45
+ import { objectGet, packageImport, packageList, workspaceGet, workspaceList } from '@elaraai/e3-api-client';
46
+ import { ApiError, adminAppList, appDeploy, appDeployWait, appGet, appList, repositoryAppList, appDeployRefusedText, appHoldPackage, appRemove, appRemoveStaged, appsDeploy, appUpload, appVersions, heldPackagesServed, platformVersions, } from '@elaraai/e3-cloud-client';
25
47
  import { AppBundleType, AppType } from '@elaraai/e3-cloud-ui/types';
26
48
  import { getValidToken } from '../credentials.js';
27
- import { buildAppFromSource } from '../app-source.js';
28
- import { exitError, formatError, parseRepoUrl, printDateTime } from '../utils.js';
49
+ import { samePackage } from '@elaraai/e3-cloud-types';
50
+ import { packageZipCheck, packageZipRef } from '@elaraai/e3-cloud-ui/check';
51
+ import { FIT_PROBLEMS, HOLDS_FOR_THE_MOVE, bindToWorkspacePackage, bindingChecks, bindingRefusalLines, boundRefusalLines, buildAppFromSource, carriesPackage, checksFor, holdsUnknownWords, movedDuringDeployLines, newerRelease, problemLines, taskPageLines } from '../app-source.js';
52
+ import { bindingDecision, zipObjects } from '../package-compare.js';
53
+ import { exitError, formatError, parseRepoUrl, parseWorkspaceUrl, printDateTime } from '../utils.js';
29
54
  const decodeBundle = decodeBeast2For(AppBundleType);
30
55
  const encodeBundle = encodeBeast2For(AppBundleType);
31
56
  const encodeApp = encodeBeast2For(AppType);
57
+ const sameString = equalFor(StringType);
32
58
  /** An error, in words: an API error's code and the server's message. */
33
59
  function describe(err) {
34
60
  if (err instanceof ApiError)
@@ -55,29 +81,366 @@ function readBundle(file) {
55
81
  exitError(`${file} is no app bundle (build one with e3-cloud app build): ${formatError(err)}`);
56
82
  }
57
83
  }
58
- /** What a deploy did, in words. */
59
- function deployedWords(response, workspace, pkg) {
84
+ /** A package with the start of its hash, which tells two of one name and version apart: `acme@1.5.0 (a1b2c3d4e5f6)`. */
85
+ function packageWithHash(pkg) {
86
+ return `${packageWords(pkg)} (${pkg.hash.slice(0, 12)})`;
87
+ }
88
+ /**
89
+ * The package a workspace runs, as its repository holds it: what
90
+ * `--against-workspace` builds against.
91
+ *
92
+ * @param baseUrl - The deployment
93
+ * @param repo - The repository
94
+ * @param workspace - The workspace
95
+ * @param token - The caller's token
96
+ * @returns Its name, version and hash, or null when it runs none
97
+ */
98
+ async function workspacePackage(baseUrl, repo, workspace, token) {
99
+ try {
100
+ const state = await workspaceGet(baseUrl, repo, workspace, { token });
101
+ return { name: state.packageName, version: state.packageVersion, hash: state.packageHash };
102
+ }
103
+ catch (err) {
104
+ if (err instanceof ApiError && err.code === 'workspace_not_deployed')
105
+ return null;
106
+ throw err;
107
+ }
108
+ }
109
+ /** What `--against-workspace` says of a workspace that runs no package. */
110
+ const RUNS_NO_PACKAGE = (workspace) => `${workspace} runs no package: deploy one first, or build without --against-workspace`;
111
+ /**
112
+ * Whether the deployment holds a build's own package for its move, for the
113
+ * words of a refusal: what it says of the way across the move. Only the
114
+ * definite sign that the hold route is absent (a 404 that is no JSON error)
115
+ * is "holds none"; any other error is said as it is, and no way is advised.
116
+ */
117
+ async function platformHolds(baseUrl, repo, workspace, app, token) {
118
+ return heldPackagesServed(baseUrl, repo, workspace, app, { token }).catch((err) => ({ unknown: describe(err) }));
119
+ }
120
+ /**
121
+ * What a deploy for the platform's next e3 says on a deployment that holds no
122
+ * package for its move (a release from before the hold): a build prepared there
123
+ * runs on the package its workspace runs.
124
+ */
125
+ async function holdsNoPackageWords(baseUrl, token, names, pkg, repo) {
126
+ const platform = await platformVersions(baseUrl, { token }).catch(() => null);
127
+ const release = platform !== null && platform.e3Cloud.type === 'some' ? `e3 cloud ${platform.e3Cloud.value},` : 'a release of e3 cloud';
128
+ return `${names} built against ${pkg}, which ${repo} doesn't hold. This deployment runs ${release} whose builds prepared for the next e3 run on the package their workspace runs: build against it (--against-workspace), or remove the app before the platform moves and deploy it after`;
129
+ }
130
+ /**
131
+ * What a refused hold of a build's own package says, after the platform's
132
+ * words: what the person does about it. Nothing is held, and nothing deployed.
133
+ */
134
+ function holdRefusalWords(err, names, pkg, repo, url, workspace) {
135
+ const said = describe(err);
136
+ const nothing = `Nothing is held for ${names}, and nothing is deployed`;
137
+ if (!(err instanceof ApiError))
138
+ return said;
139
+ switch (err.code) {
140
+ case 'package_version_conflict':
141
+ return `${said}\n${nothing}: give ${pkg.name} a new version in its source (it is ${pkg.version} now), build it again and deploy it. Only an admin may replace a zip held for the move instead, with --replace, and only while no build is prepared on it`;
142
+ case 'held_package_staged':
143
+ return `${said}\n${nothing}: give ${pkg.name} a new version in its source (it is ${pkg.version} now), build it again and deploy it; or remove each build prepared on the zip held, then hold it again with --replace (an admin's). ${REMOVE_PREPARED_WORDS(url)}`;
144
+ case 'held_packages_full': {
145
+ // The platform says what keeps each zip, and the steps that apply: the command's form, for a zip in use
146
+ const inUse = typeof err.details === 'string' && err.details.includes('remove-prepared');
147
+ return `${said}\n${nothing}${inUse ? `. ${REMOVE_PREPARED_WORDS(url)}: its zip is let go at once` : ''}`;
148
+ }
149
+ case 'bad_request':
150
+ return `${said}\n${nothing}: the bundle's package zip is not one e3 exports whole, or names a release e3 does not write. Build it again (e3-cloud app build, or deploy --from-source)`;
151
+ case 'invalid_name':
152
+ return `${said}\n${nothing}: give ${pkg.name} a shorter name or version in the source, build it again and deploy it`;
153
+ case 'workspace_protected':
154
+ return `${said}\n${nothing}: prepare the build on ${workspace}'s staging, and a release carries it here`;
155
+ default:
156
+ return said;
157
+ }
158
+ }
159
+ /**
160
+ * What a refused deploy for the platform's next e3 says after the platform's
161
+ * words, of a build with a package of its own: what the person does about it.
162
+ * Nothing for any other refusal.
163
+ */
164
+ function preparedRefusalWords(err, url, workspace, forPlatform, pkg) {
165
+ if (!(err instanceof ApiError))
166
+ return null;
167
+ // A version names one package: the repository holds another build of it
168
+ if (err.code === 'package_version_conflict')
169
+ return `Nothing is deployed: give ${pkg.name} a new version in its source (it is ${pkg.version} now) and deploy again; for the platform's next e3, the platform then holds that package for its move`;
170
+ if (forPlatform === null)
171
+ return null;
172
+ switch (err.code) {
173
+ case 'staged_package_differs':
174
+ return `Nothing is prepared. ${REMOVE_PREPARED_WORDS(url)}, leaving its live version as it is; then deploy ${workspace}'s apps again together`;
175
+ case 'package_e3_differs':
176
+ return `Nothing is prepared. Build the apps with e3 ${forPlatform}, giving the package a new version, and deploy them again with --for-platform ${forPlatform}`;
177
+ case 'package_unreadable':
178
+ return `Nothing is prepared. Build the apps again with e3 ${forPlatform}, giving the package a new version, and deploy them again with --for-platform ${forPlatform}`;
179
+ case 'held_package_gone':
180
+ return `${packageWords(pkg)} is no longer held, and this deploy's build is taken back (as the platform says above): deploy again, which holds it again`;
181
+ case 'held_package_gone_prepared':
182
+ return `The build may be left prepared on ${packageWords(pkg)}, which is no longer held, and the platform's move refuses it so: deploy again, which holds it again and repairs that`;
183
+ case 'package_files_differ':
184
+ return `Nothing is prepared. The move deploys the package with no client to send files: fit ${workspace}'s Files declarations to it, or build against a package that takes them as ${workspace}'s does`;
185
+ default:
186
+ return null;
187
+ }
188
+ }
189
+ /**
190
+ * How a prepared build is removed, and who may: on a workspace with a
191
+ * staging (protected), on its staging, which a release carries over; an
192
+ * admin alone forces it on the protected workspace.
193
+ */
194
+ const REMOVE_PREPARED_WORDS = (url) => `Remove a prepared build no longer needed with e3-cloud app remove-prepared ${url} <workspace> <app> (owners and admins; on a workspace with a staging, remove it on the staging and release, or an admin forces it with --force)`;
195
+ /** What the CLI says after a refusal that follows a hold: the zip stays held, and how it goes. */
196
+ const STAYS_HELD = (pkg) => `${packageWords(pkg)} stays held for the platform's move: the hold that next needs room lets it go by itself once no prepared build names it`;
197
+ /**
198
+ * A package zip as e3 exports it whole, here: its ref, and every object of it
199
+ * hashed to its name, the package object among them, as the platform's hold
200
+ * checks it. Exits in words, holding and sending nothing, for one that does
201
+ * not read or verify.
202
+ */
203
+ async function verifiedZip(zip, names) {
204
+ try {
205
+ const ref = packageZipRef(zip);
206
+ await packageZipCheck(zip, ref);
207
+ return ref;
208
+ }
209
+ catch (err) {
210
+ exitError(`The package zip ${names} ${names.includes(',') ? 'carry' : 'carries'} is not one e3 exports whole: ${formatError(err)}. Nothing is held, and nothing is deployed: build it again (e3-cloud app build, or deploy --from-source)`);
211
+ }
212
+ }
213
+ /**
214
+ * The checks of a build that brings its own package, here, against that
215
+ * package as its zip holds it, before anything is held: the project's, as its
216
+ * build ran them, for a source; this CLI's own for a bundle file. Where the
217
+ * platform's e3 cannot read a package of the next e3, these are the only
218
+ * checks before the platform's move, whose preflight checks the build again.
219
+ *
220
+ * @param bundles - The builds, each with its source (null for a bundle file)
221
+ * @param zip - Their package's zip
222
+ * @param pkg - Their package
223
+ * @returns Each app's problems, and the checks run
224
+ */
225
+ async function checkedHere(bundles, zip, pkg) {
226
+ const checks = await checksFor(bundles.find((one) => one.source !== null)?.source ?? null);
227
+ const view = await checks.checks.readPackageView(await zipObjects(zip), pkg);
228
+ const problems = bundles
229
+ .map(({ bundle }) => ({ app: bundle.app.name, problems: checks.checks.checkApp(bundle.app, view, { e3Version: null }) }))
230
+ .filter((entry) => entry.problems.length > 0);
231
+ return { problems, checks };
232
+ }
233
+ /** The checks run here, in words: `the project's checks (@elaraai/e3-cloud-ui 1.1.19)`. */
234
+ function checksWords(checks) {
235
+ const release = checks.version === null ? '' : ` (@elaraai/e3-cloud-ui ${checks.version})`;
236
+ return checks.from === 'project' ? `the project's checks${release}` : `this CLI's checks${release}, since ${checks.fallback ?? "the project's can't be loaded"}`;
237
+ }
238
+ /**
239
+ * The live apps of a workspace with no build for the next e3 prepared, after
240
+ * a deploy prepared some: what the platform's move waits for, since its
241
+ * preflight refuses while an app built with another e3 has none.
242
+ */
243
+ function stillToPrepare(apps, next) {
244
+ return apps
245
+ .filter((app) => !sameString(app.current.builtWith, next) && !(app.staged.type === 'some' && sameString(app.staged.value.forPlatform, next)))
246
+ .map((app) => app.name);
247
+ }
248
+ /** Names, in words: `ops`, `ops and exec`, `ops, exec and planner`. */
249
+ function namesInWords(names) {
250
+ return names.length <= 1 ? names.join('') : `${names.slice(0, -1).join(', ')} and ${names.at(-1)}`;
251
+ }
252
+ /**
253
+ * What a hold did, in words: held, kept as held already, or held in place of
254
+ * another zip (an admin's replace); and, when `--replace` was asked for and
255
+ * nothing was replaced, that it was not.
256
+ */
257
+ function holdWords(answer, repo, workspace, next, replace) {
258
+ const pkg = `${answer.name}@${answer.version}`;
259
+ const exported = `exported with e3 ${answer.release.type === 'some' ? answer.release.value : 'of no release'}`;
260
+ const move = `the move imports it and deploys it to ${workspace}`;
261
+ const replacedNothing = replace ? ` (--replace replaced nothing: no other zip was held for ${pkg})` : '';
262
+ switch (answer.outcome.type) {
263
+ case 'held':
264
+ return `Holding ${pkg} (${exported}) in ${repo} for the platform's move to e3 ${next}: ${move}${replacedNothing}`;
265
+ case 'kept':
266
+ return `${pkg} (${exported}) is held in ${repo} for the platform's move to e3 ${next} already: ${move}${replacedNothing}`;
267
+ case 'replaced':
268
+ return `Holding ${pkg} (${exported}) in ${repo} for the platform's move to e3 ${next}, in place of the zip held for it before (${answer.outcome.value.slice(0, 12)}): ${move}`;
269
+ case 'in_repository':
270
+ return `${repo} holds ${pkg} already: nothing is held for the move`;
271
+ }
272
+ }
273
+ /**
274
+ * Whether builds whose package is the version their workspace runs, exported
275
+ * to other bytes, fit the workspace's package: the binding `--against-workspace`
276
+ * makes, by the project's checks (this CLI's for a bundle file).
277
+ *
278
+ * @returns The bound builds; why not, in lines (none when they fit); and
279
+ * what differs where no page binds it, or only in code, in the lines
280
+ * `--against-workspace` prints
281
+ */
282
+ async function fitsRunning(built, sourceOf, runs, workspace, baseUrl, repo, token) {
283
+ const why = [];
284
+ const differs = [];
285
+ // What differs that e3 shows (a name, a type, a signature, an initial value, a file source): the owner's change, not an export's
286
+ const changed = [];
287
+ // Among them, an input's file source: bound by the workspace's Files declarations
288
+ let files = false;
289
+ const bound = [];
290
+ const held = `${packageWords(runs)}, which ${workspace} runs`;
291
+ for (const bundle of built) {
292
+ const source = sourceOf.get(bundle) ?? null;
293
+ const checks = await checksFor(source);
294
+ const decision = await bindingDecision(bundle.app, { hash: bundle.app.package.hash, read: await zipObjects(bundle.package) }, { hash: runs.hash, read: (hash) => objectGet(baseUrl, repo, hash, { token }) }, runs, held, checks.checks);
295
+ const name = bundle.app.name;
296
+ if (decision.indexed.length > 0)
297
+ why.push(` ${name}: ${decision.indexed.map((read) => `page ${read.page} reads ${read.records.join(', ')} through indexes, whose programs it carries from its own source`).join('; ')}`);
298
+ if (decision.unknown.length > 0)
299
+ why.push(` ${name}: ${decision.unknown.map((entry) => `page ${entry.page} calls ${entry.calls.join(', ')}, binds this CLI doesn't know`).join('; ')}`);
300
+ if (decision.problems.length > 0)
301
+ why.push(` ${name}:`, ...problemLines(decision.problems, { app: name, package: packageWords(runs) }).map((line) => ` ${line}`));
302
+ // What differs, as --against-workspace says it
303
+ if (decision.differences.length > 0)
304
+ differs.push(`${name}'s pages fit ${held}; these differ, where no page of ${name} binds them, or only in code:`, ...decision.differences.map((line) => ` ${line}`));
305
+ // Its kind as the comparison found it, never read back from its words
306
+ for (const difference of decision.kinds) {
307
+ if (difference.kind === 'code')
308
+ continue;
309
+ if (difference.kind === 'files')
310
+ files = true;
311
+ if (!changed.includes(difference.line))
312
+ changed.push(difference.line);
313
+ }
314
+ differs.push(...taskPageLines(name, packageWords(runs), decision.taskPages));
315
+ const one = bindToWorkspacePackage(bundle, runs, workspace);
316
+ if (source !== null)
317
+ sourceOf.set(one, source);
318
+ bound.push(one);
319
+ }
320
+ return { bound, why, differs, changed, files };
321
+ }
322
+ /**
323
+ * A workspace's URL, as `app build --against-workspace` takes it: refused
324
+ * with anything past the workspace's name.
325
+ *
326
+ * @param url - The URL
327
+ * @returns The deployment, the repository and the workspace
328
+ */
329
+ function againstWorkspaceUrl(url) {
330
+ const place = parseWorkspaceUrl(url);
331
+ if (!/^\/repos\/[^/]+\/workspaces\/[^/]+\/?$/.test(new URL(url).pathname)) {
332
+ exitError(`--against-workspace takes a workspace's URL, …/repos/<repo>/workspaces/<workspace>, with nothing past its name: ${url}`);
333
+ }
334
+ return place;
335
+ }
336
+ /**
337
+ * Binds a bundle built from source to the package its workspace runs, once
338
+ * the package the source exports is found to differ from it only where no
339
+ * page reads (`comparePackages`), saying what differs; it exits naming
340
+ * anything else that differs.
341
+ *
342
+ * @param bundle - The bundle, as the build made it
343
+ * @param running - The package the workspace runs
344
+ * @param workspace - The workspace
345
+ * @param baseUrl - The deployment
346
+ * @param repo - The repository, whose objects the held package is read from
347
+ * @param token - The caller's token
348
+ * @returns The bound bundle
349
+ */
350
+ async function boundAgainst(source, bundle, running, workspace, baseUrl, repo, token) {
351
+ const bound = bindToWorkspacePackage(bundle, running, workspace);
352
+ const name = bundle.app.name;
353
+ const held = `${packageWords(running)}, which ${workspace} runs`;
354
+ // The checks the project's build ran: another release of them may read its pages otherwise
355
+ const checks = await bindingChecks(source);
356
+ if (checks.fallback !== null)
357
+ console.warn(`${source}: ${checks.fallback}, so ${name} is checked with this CLI's own checks${checks.version === null ? '' : ` (@elaraai/e3-cloud-ui ${checks.version})`}, which may read its pages otherwise than its build did`);
358
+ const decision = await bindingDecision(bundle.app, { hash: bundle.app.package.hash, read: await zipObjects(bundle.package) }, { hash: running.hash, read: (hash) => objectGet(baseUrl, repo, hash, { token }) }, running, held, checks.checks);
359
+ // An app with an indexed read is never bound: its pages carry the record's
360
+ // index programs from its own source, which the platform's checks don't read
361
+ if (decision.indexed.length > 0) {
362
+ const holds = await platformHolds(baseUrl, repo, workspace, name, token);
363
+ exitError([
364
+ `${name} can't be built against ${held}: ${decision.indexed.length === 1 ? 'a page of it reads' : 'pages of it read'} records through indexes`,
365
+ ...decision.indexed.map((read) => ` page ${read.page}: ${read.records.join(', ')}`),
366
+ holds === true
367
+ ? `Such a page carries the record's index programs from its own source, which can't be shown to be the workspace's. Deploy ${name} with a package of its own instead, without --against-workspace, giving ${running.name} a new version in its source: ${HOLDS_FOR_THE_MOVE}`
368
+ : holds === false
369
+ ? `Such a page carries the record's index programs from its own source, which can't be shown to be the workspace's. Stage it with a package of its own once the platform takes one; until then, remove ${name} before the platform moves and deploy it after`
370
+ : `Such a page carries the record's index programs from its own source, which can't be shown to be the workspace's. ${holdsUnknownWords(holds)}`,
371
+ ].join('\n'));
372
+ }
373
+ // A bind this CLI doesn't know may carry programs as the indexed read does: refused, failing closed
374
+ if (decision.unknown.length > 0) {
375
+ const holds = await platformHolds(baseUrl, repo, workspace, name, token);
376
+ exitError([
377
+ `${name} can't be built against ${held}: ${decision.unknown.length === 1 ? 'a page of it calls' : 'pages of it call'} binds this e3-cloud CLI doesn't know`,
378
+ ...decision.unknown.map((entry) => ` page ${entry.page}: ${entry.calls.join(', ')}`),
379
+ `This release of the e3-cloud CLI doesn't know such a bind of the workspace, so it can't show that what the page carries from its own source is the workspace's. Deploy ${name} as built, with the package its source exports (without --against-workspace), where the repository holds no other of that version${holds === true ? `; ${HOLDS_FOR_THE_MOVE}` : holds === false ? `; or remove ${name} before the platform moves and deploy it after` : `. ${holdsUnknownWords(holds)}`}`,
380
+ ].join('\n'));
381
+ }
382
+ // What its pages bind, held to the held package by the platform's own checks, in their words
383
+ if (decision.problems.length > 0) {
384
+ const holds = decision.problems.some((problem) => FIT_PROBLEMS.has(problem.type)) ? await platformHolds(baseUrl, repo, workspace, name, token) : false;
385
+ exitError(bindingRefusalLines(name, packageWords(running), `which ${workspace} runs`, decision.problems, checks, holds).join('\n'));
386
+ }
387
+ if (decision.differences.length > 0) {
388
+ console.log([`${name}'s pages fit ${held}; these differ, where no page of ${name} binds them, or only in code:`, ...decision.differences.map((line) => ` ${line}`)].join('\n'));
389
+ }
390
+ // A task page shows its task's output, which the checks don't hold to a type
391
+ for (const line of taskPageLines(name, packageWords(running), decision.taskPages))
392
+ console.log(line);
393
+ return bound;
394
+ }
395
+ /**
396
+ * What a deploy did, in words. A build for the next e3 prepared with a package
397
+ * of its own, other than the one its workspace runs, says what the
398
+ * platform's move does: it deploys that package, and the workspace then needs
399
+ * a run.
400
+ *
401
+ * @param response - The deploy's answer
402
+ * @param workspace - The workspace
403
+ * @param pkg - The package the apps were built against
404
+ * @param running - The package the workspace runs, read once for a deploy for
405
+ * the next e3; null for any other deploy, or a workspace that runs none
406
+ */
407
+ function deployedWords(response, workspace, pkg, running) {
408
+ const own = running !== null && !samePackage(running, pkg);
60
409
  switch (response.type) {
61
410
  case 'refused':
62
411
  return '';
63
412
  case 'checked': {
64
413
  const apps = response.value.apps.join(', ');
65
- const what = response.value.deploy.type === 'ui_only' ? `a deploy would change only ${response.value.apps.length === 1 ? 'the app' : 'the apps'}: ${workspace} runs ${pkg}`
66
- : response.value.deploy.type === 'for_platform' ? `a deploy would stage ${response.value.apps.length === 1 ? 'it' : 'them'} for e3 ${response.value.deploy.value}`
67
- : `a deploy would deploy ${pkg} first`;
414
+ const them = response.value.apps.length === 1 ? 'it' : 'them';
415
+ const what = response.value.deploy.type === 'ui_only' ? `a deploy would change only ${response.value.apps.length === 1 ? 'the app' : 'the apps'}: ${workspace} runs ${packageWords(pkg)}`
416
+ : response.value.deploy.type === 'for_platform'
417
+ ? own
418
+ ? `a deploy would prepare ${them} for e3 ${response.value.deploy.value} with ${packageWords(pkg)}, which the platform's move deploys`
419
+ : `a deploy would prepare a build of ${them} for e3 ${response.value.deploy.value}`
420
+ : `a deploy would deploy ${packageWords(pkg)} first`;
68
421
  return `${apps} ${response.value.apps.length === 1 ? 'fits' : 'fit'} ${workspace}: ${what}`;
69
422
  }
70
423
  case 'live':
71
- return response.value.map((app) => `${app.name} v${app.current.number} is live on ${workspace} (UI only: ${workspace} runs ${pkg})`).join('\n');
424
+ return response.value.map((app) => `${app.name} v${app.current.number} is live on ${workspace} (UI only: ${workspace} runs ${packageWords(pkg)})`).join('\n');
72
425
  case 'deploying':
73
- return `Deploying ${pkg} to ${workspace}, keeping the inputs people set · ${response.value.apps.join(', ')} ${response.value.apps.length === 1 ? 'goes' : 'go'} live as the deploy completes (job ${response.value.job})`;
426
+ return `Deploying ${packageWords(pkg)} to ${workspace}, keeping the inputs people set · ${response.value.apps.join(', ')} ${response.value.apps.length === 1 ? 'goes' : 'go'} live as the deploy completes (job ${response.value.job})`;
74
427
  case 'staged':
75
- return response.value.map((app) => `${app.name} is staged for e3 ${app.staged.type === 'some' ? app.staged.value.forPlatform : '?'} on ${workspace}: it goes live with the platform; v${app.current.number} stays live until then`).join('\n');
428
+ return response.value.map((app) => {
429
+ const next = app.staged.type === 'some' ? app.staged.value.forPlatform : '?';
430
+ if (own && app.staged.type === 'some' && !samePackage(app.staged.value.package, running)) {
431
+ const brings = packageWords(app.staged.value.package);
432
+ return `A build of ${app.name} for e3 ${next} is prepared on ${workspace}, with ${brings}: the platform's move deploys ${brings} to ${workspace}, and to each workspace a release carries the build to, keeping the inputs people set and migrating records. Each then needs a run, which nothing starts: the deploy keeps only the outputs of tasks it leaves unchanged, and the next e3's export usually changes every task. v${app.current.number} stays live on ${packageWords(running)} until then`;
433
+ }
434
+ return `A build of ${app.name} for e3 ${next} is prepared on ${workspace}: it goes live when the platform moves; v${app.current.number} stays live until then`;
435
+ }).join('\n');
76
436
  }
77
437
  }
78
438
  /** An app's row in a listing. */
79
439
  function appRow(app) {
80
- const staged = app.staged.type === 'some' ? ` · staged for ${app.staged.value.forPlatform}` : '';
440
+ // A build prepared with a package of its own names it: the package the platform's move deploys
441
+ const staged = app.staged.type === 'none' ? ''
442
+ : samePackage(app.staged.value.package, app.current.package) ? ` · build for ${app.staged.value.forPlatform} prepared`
443
+ : ` · build for ${app.staged.value.forPlatform} prepared, with ${packageWords(app.staged.value.package)}`;
81
444
  return [
82
445
  app.workspace.padEnd(16),
83
446
  app.name.padEnd(16),
@@ -93,20 +456,40 @@ function appRow(app) {
93
456
  */
94
457
  const FORCE_WARNING = (workspace, what) => `Warning: --force sends this ${what} past ${workspace}'s protection, if it has a staging: it changes ${workspace} without a release, only an admin may, and its history records it as forced. Its next release from its staging undoes it.`;
95
458
  export const appCommand = {
96
- /** e3-cloud app build <source> [-o <file>] */
459
+ /** e3-cloud app build <source> [-o <file>] [--against-workspace <workspace-url>] */
97
460
  async build(source, options) {
98
461
  try {
99
- const bundle = await buildAppFromSource(source);
462
+ // The sign-in and the workspace first, so a wrong URL or an expired sign-in fails before the build
463
+ const against = options.againstWorkspace === undefined ? null : againstWorkspaceUrl(options.againstWorkspace);
464
+ const token = against === null ? null : await getValidToken(against.baseUrl);
465
+ const running = against === null ? null : await workspacePackage(against.baseUrl, against.repo, against.workspace, token);
466
+ if (against !== null && running === null)
467
+ exitError(RUNS_NO_PACKAGE(against.workspace));
468
+ const built = await buildAppFromSource(source);
469
+ // Against the package a workspace runs: the bundle names it, and carries no package
470
+ const bundle = against === null ? built : await boundAgainst(source, built, running, against.workspace, against.baseUrl, against.repo, token);
100
471
  const output = resolve(options.output ?? `dist/${bundle.app.name}.app`);
101
472
  mkdirSync(dirname(output), { recursive: true });
102
473
  writeFileSync(output, encodeBundle(bundle));
103
- console.log(`${bundle.app.name}: ${bundle.app.pages.length} pages, built against ${packageWords(bundle.app.package)} with e3 ${bundle.app.builtWith} → ${output}`);
474
+ if (against === null) {
475
+ console.log(`${bundle.app.name}: ${bundle.app.pages.length} pages, built against ${packageWords(bundle.app.package)} with e3 ${bundle.app.builtWith} → ${output}`);
476
+ }
477
+ else {
478
+ const pkg = bundle.app.package;
479
+ console.log(`${bundle.app.name}: ${bundle.app.pages.length} pages, built with e3 ${bundle.app.builtWith} against ${packageWithHash(pkg)}, which ${against.workspace} runs: the bundle carries no package → ${output}`);
480
+ // A deployment on an older e3 than the app is built with takes it staged for its move, with --for-platform;
481
+ // the bundle is written already, so a deployment that doesn't answer leaves the hint without it
482
+ const platform = await platformVersions(against.baseUrl, { token: token }).catch(() => null);
483
+ const staging = platform !== null && newerRelease(bundle.app.builtWith, platform.e3) ? ` --for-platform ${bundle.app.builtWith}` : '';
484
+ const why = staging === '' ? '' : ` (the deployment runs e3 ${platform.e3}: the app is prepared for its move to ${bundle.app.builtWith})`;
485
+ console.log(`Its pages were checked against ${packageWords(pkg)} by the platform's checks, here; e3-cloud app deploy ${against.baseUrl}/repos/${against.repo} ${against.workspace} ${output}${staging} --dry-run checks them by the deployment's own${why}`);
486
+ }
104
487
  }
105
488
  catch (err) {
106
489
  exitError(describe(err));
107
490
  }
108
491
  },
109
- /** e3-cloud app deploy <repoUrl> <workspace> [bundles...] [--from-source <source...>] [--dry-run] [--for-platform <version>] [--force] */
492
+ /** e3-cloud app deploy <repoUrl> <workspace> [bundles...] [--from-source <source...>] [--against-workspace] [--dry-run] [--for-platform <version>] [--force] [--replace] */
110
493
  async deploy(url, workspace, bundles, options) {
111
494
  try {
112
495
  const force = options.force === true;
@@ -115,37 +498,263 @@ export const appCommand = {
115
498
  const sources = options.fromSource ?? [];
116
499
  if (bundles.length === 0 && sources.length === 0)
117
500
  exitError('Give the bundles to deploy, or their sources with --from-source');
501
+ const against = options.againstWorkspace === true;
502
+ if (against && sources.length === 0)
503
+ exitError('--against-workspace builds sources: give them with --from-source, or build the bundle with e3-cloud app build --against-workspace');
118
504
  const { baseUrl, repo } = parseRepoUrl(url);
119
505
  const token = await getValidToken(baseUrl);
120
- const built = [...bundles.map(readBundle)];
121
- for (const source of sources)
122
- built.push(await buildAppFromSource(source));
123
- const pkg = built[0].app.package;
124
- const other = built.find((b) => b.app.package.hash !== pkg.hash);
506
+ let built = [...bundles.map(readBundle)];
507
+ // Each build's source, for the checks run here; none for a bundle file
508
+ const sourceOf = new Map();
509
+ // The package the workspace runs, read before any build: what sources are built against
510
+ const running = against ? await workspacePackage(baseUrl, repo, workspace, token) : null;
511
+ if (against && running === null)
512
+ exitError(RUNS_NO_PACKAGE(workspace));
513
+ // Each source, built; against the package the workspace runs, bound to it
514
+ for (const source of sources) {
515
+ const bundle = await buildAppFromSource(source);
516
+ const one = against ? await boundAgainst(source, bundle, running, workspace, baseUrl, repo, token) : bundle;
517
+ sourceOf.set(one, source);
518
+ built.push(one);
519
+ }
520
+ let pkg = built[0].app.package;
521
+ const other = built.find((b) => !samePackage(b.app.package, pkg));
125
522
  if (other !== undefined) {
126
- exitError(`Apps deployed together are built against one package: ${built[0].app.name} against ${packageWords(pkg)}, ${other.app.name} against ${packageWords(other.app.package)}`);
523
+ // Two of one name and version are told apart by their hashes
524
+ const alike = sameString(other.app.package.name, pkg.name) && sameString(other.app.package.version, pkg.version);
525
+ const words = (p) => (alike ? packageWithHash(p) : packageWords(p));
526
+ exitError(`Apps deployed together are built against one package: ${built[0].app.name} against ${words(pkg)}, ${other.app.name} against ${words(other.app.package)}`);
527
+ }
528
+ const names = built.map((bundle) => bundle.app.name);
529
+ const one = names.length === 1;
530
+ const namesWere = `${names.join(', ')} ${one ? 'was' : 'were'}`;
531
+ const dryRun = options.dryRun === true;
532
+ // The platform's e3 against the build's, read before anything is sent: a
533
+ // build of a newer e3 is prepared for the platform's move by itself, as
534
+ // if --for-platform named its e3, which stays accepted and means the same
535
+ let forPlatform = options.forPlatform;
536
+ // The platform's e3 not read: said, and never a newer build imported unseen (below)
537
+ let platformUnread = null;
538
+ if (forPlatform === undefined) {
539
+ const platform = await platformVersions(baseUrl, { token }).catch((err) => {
540
+ // A deployment from before the route (a 404) runs an e3 no build is newer than here: as before
541
+ if (!(err instanceof ApiError && (err.code === 'not_found' || err.code === 'http_404')))
542
+ platformUnread = describe(err);
543
+ return null;
544
+ });
545
+ const withs = [...new Set(built.map((bundle) => bundle.app.builtWith))];
546
+ if (platform !== null && withs.some((w) => newerRelease(w, platform.e3))) {
547
+ if (withs.length > 1)
548
+ exitError(`${names.join(', ')} are built with e3 ${withs.join(' and ')}: build them with one, the platform's next`);
549
+ forPlatform = withs[0];
550
+ console.log(`This platform runs e3 ${platform.e3}, and ${names.join(', ')} ${one ? 'is' : 'are'} built with e3 ${forPlatform}: preparing ${one ? 'it' : 'them'} for the platform's move to e3 ${forPlatform} on ${workspace}. ${one ? 'It goes' : 'They go'} live when the platform moves; until then ${workspace}'s live ${one ? 'version stays' : 'versions stay'}`);
551
+ }
552
+ }
553
+ // Choosing the package of a build for the next e3: the one the workspace
554
+ // runs; the same version exported to other bytes, when the apps fit the
555
+ // workspace's (the binding --against-workspace makes); else their own,
556
+ // held for the move, which needs a version no other build of holds
557
+ if (forPlatform !== undefined && built.some(carriesPackage)) {
558
+ const runs = await workspacePackage(baseUrl, repo, workspace, token);
559
+ if (runs !== null && !sameString(runs.hash, pkg.hash) && sameString(runs.name, pkg.name) && sameString(runs.version, pkg.version)) {
560
+ const fit = await fitsRunning(built, sourceOf, runs, workspace, baseUrl, repo, token);
561
+ if (fit.why.length > 0) {
562
+ exitError([
563
+ `${names.join(', ')} ${one ? 'is' : 'are'} built against ${packageWords(pkg)}, which ${workspace} runs exported to other bytes (${pkg.hash.slice(0, 12)}, not ${runs.hash.slice(0, 12)}), and ${one ? "doesn't" : "don't"} fit ${workspace}'s:`,
564
+ ...fit.why,
565
+ `A version names one package: give ${pkg.name} a new version in its source (it is ${pkg.version} now) and deploy again. Nothing is sent; the platform then holds that package for its move, and the move deploys it`,
566
+ ].join('\n'));
567
+ }
568
+ // A difference e3 shows is the owner's change to the package, never
569
+ // an export's: refused before anything is sent, with the one step
570
+ if (fit.changed.length > 0) {
571
+ exitError([
572
+ `${names.join(', ')} ${one ? 'is' : 'are'} built against ${packageWords(pkg)}, and its package differs from ${packageWords(runs)}, which ${workspace} runs, where e3 shows it: a change to the package, not the next e3's export of it:`,
573
+ ...fit.changed.map((line) => ` ${line}`),
574
+ `A version names one package: give ${pkg.name} a new version in its source (it is ${pkg.version} now) and deploy again. Nothing is sent; the platform then holds that package for its move, and the move deploys it with ${one ? 'the app' : 'the apps'}`,
575
+ // An input's file source differs: the new version is then held to the workspace's Files declarations too
576
+ ...(fit.files ? [`An input taken from another file is bound by ${workspace}'s Files declarations, which must fit the new version as well: until they do, its deploy is refused package_files_differ, which says how to fit them`] : []),
577
+ ].join('\n'));
578
+ }
579
+ built = fit.bound;
580
+ pkg = runs;
581
+ // First, before what differs: a change of code made here is not deployed by this
582
+ console.log(`This prepares ${namesInWords(names)} on the package ${workspace} runs, ${packageWords(runs)}, not on the package built here: a change you made to the package's code is not deployed. To send it with ${one ? 'the app' : 'the apps'}, give ${pkg.name} a new version in its source and deploy again`);
583
+ console.log(`${names.join(', ')}'s package, exported with e3 ${forPlatform}, differs from ${packageWords(runs)}, which ${workspace} runs, in code alone, which an export by the next e3 also changes; ${one ? 'it fits' : 'they fit'} ${packageWords(runs)}, so ${one ? 'its build is' : 'their builds are'} prepared on it, and the move deploys no package to ${workspace}`);
584
+ for (const line of fit.differs)
585
+ console.log(line);
586
+ }
587
+ }
588
+ // A bundle carrying no package runs on exactly the package the workspace runs.
589
+ // Read again now, after the builds, so a workspace that moved meanwhile
590
+ // refuses it; one that moves in the seconds after is said, below. As the
591
+ // platform decides it: a build for the next e3 by name, version and hash
592
+ // (a move to another version of one content shares the hash, and is
593
+ // still another package, which the platform refuses package_differs); a
594
+ // live one by its content, which the platform deploys UI only
595
+ const bareNow = built.filter((b) => !carriesPackage(b));
596
+ const now = bareNow.length === 0 ? null : await workspacePackage(baseUrl, repo, workspace, token);
597
+ const runsIt = (p) => now !== null && (forPlatform === undefined ? sameString(now.hash, p.hash) : samePackage(now, p));
598
+ for (const bundle of bareNow) {
599
+ if (!runsIt(bundle.app.package)) {
600
+ exitError(`${bundle.app.name} was built against ${packageWithHash(bundle.app.package)} and carries no package, and ${workspace} runs ${now === null ? 'no package' : packageWithHash(now)}: build it again with --against-workspace, against the package ${workspace} runs now`);
601
+ }
602
+ }
603
+ const listed = await packageList(baseUrl, repo, { token });
604
+ const inRepository = listed.some((p) => sameString(p.name, pkg.name) && sameString(p.version, pkg.version));
605
+ const carrier = built.find(carriesPackage);
606
+ if (!inRepository && carrier === undefined) {
607
+ exitError(`${built[0].app.name} was built against ${packageWords(pkg)} as a repository held it when it was built, and ${repo} holds no ${packageWords(pkg)}: build it from source with --against-workspace here`);
608
+ }
609
+ // A package the repository lacks, checked here first: its zip verified as e3
610
+ // exports one, and the apps against it, by the project's checks
611
+ let fitsHere = null;
612
+ if (!inRepository) {
613
+ await verifiedZip(carrier.package, names.join(', '));
614
+ const here = await checkedHere(built.map((bundle) => ({ bundle, source: sourceOf.get(bundle) ?? null })), carrier.package, pkg);
615
+ if (here.problems.length > 0) {
616
+ exitError([
617
+ `${here.problems.map((entry) => entry.app).join(', ')} ${here.problems.length === 1 ? "doesn't" : "don't"} fit ${packageWords(pkg)}, which ${here.problems.length === 1 ? 'it brings' : 'they bring'}, by ${checksWords(here.checks)}, here: nothing is held, and nothing is deployed`,
618
+ ...here.problems.flatMap((entry) => problemLines(entry.problems, { app: entry.app, package: packageWords(pkg) })),
619
+ ].join('\n'));
620
+ }
621
+ fitsHere = `Checked here, by ${checksWords(here.checks)}: the zip of ${packageWords(pkg)} is whole, and ${names.join(', ')} ${one ? 'fits' : 'fit'} it`;
622
+ }
623
+ // A dry run keeps nothing: a package the repository lacks is neither
624
+ // imported nor held, and the platform drops the build it is sent to check. Not for the next e3, the platform's checks need it
625
+ // imported, so nothing is sent, and the checks run here answer; for the
626
+ // next e3, the platform checks against a zip held already, and the
627
+ // checks here answer when none is
628
+ const them = one ? 'it' : 'them';
629
+ // A package the repository lacks, with the platform's e3 unknown: the
630
+ // build may be of a newer e3, whose package the platform refuses to import
631
+ // The platform's e3 unknown: whether the build is to be prepared or deployed live is not known either,
632
+ // and a newer build sent as a live one meets words to build it again with the older e3: stop, either way
633
+ if (forPlatform === undefined && platformUnread !== null) {
634
+ exitError(`The platform's e3 could not be read (${platformUnread}), so whether ${names.join(', ')} ${one ? 'is' : 'are'} built with a newer e3 than it runs is not known${inRepository ? '' : ', and the platform refuses to import a package a newer e3 exported'}. Nothing is ${inRepository ? '' : 'imported or '}deployed: run again, or name the e3 the build is for with --for-platform ${built[0].app.builtWith} to prepare ${them} for the platform's move to it`);
635
+ }
636
+ if (dryRun && !inRepository && forPlatform === undefined) {
637
+ console.log(`${fitsHere}. A dry run writes nothing, and ${packageWords(pkg)} is not in ${repo}: the platform's own checks were not made, since they need the package imported. A deploy without --dry-run imports ${packageWords(pkg)}, then the platform checks ${them} against it`);
638
+ return;
639
+ }
640
+ // What can be known before a hold is checked before it, so nothing is
641
+ // held for a deploy the platform would refuse
642
+ if (!inRepository && forPlatform !== undefined) {
643
+ const zipRef = packageZipRef(carrier.package);
644
+ if (zipRef.release !== forPlatform) {
645
+ exitError(`${packageWords(pkg)} was exported ${zipRef.release === null ? 'by an e3 its zip does not name' : `with e3 ${zipRef.release}`}, and ${names.join(', ')} ${one ? 'is' : 'are'} prepared for e3 ${forPlatform}, whose move imports only a zip that e3 exported. Nothing is held: build with e3 ${forPlatform}, giving ${pkg.name} a new version, and deploy again`);
646
+ }
647
+ const apps = await appList(baseUrl, repo, workspace, { token });
648
+ const missing = names.filter((name) => !apps.some((app) => sameString(app.name, name)));
649
+ if (missing.length > 0) {
650
+ exitError(`${namesInWords(missing)} ${missing.length === 1 ? 'has' : 'have'} no live version on ${workspace}, and a build for the next e3 is prepared beside a live version. Nothing is held: deploy ${missing.length === 1 ? 'it' : 'them'} once the platform runs e3 ${forPlatform}`);
651
+ }
652
+ const elsewhere = apps.filter((app) => !names.includes(app.name) && app.staged.type === 'some' && sameString(app.staged.value.forPlatform, forPlatform) && !samePackage(app.staged.value.package, pkg));
653
+ if (elsewhere.length > 0) {
654
+ const on = elsewhere[0].staged.type === 'some' ? elsewhere[0].staged.value.package : pkg;
655
+ const prepared = elsewhere.filter((app) => app.staged.type === 'some' && samePackage(app.staged.value.package, on)).map((app) => app.name);
656
+ exitError(`${workspace}'s builds for e3 ${forPlatform} are prepared on one package: ${namesInWords(prepared)} ${prepared.length === 1 ? 'is' : 'are'} prepared on ${packageWords(on)}, and ${names.join(', ')} ${one ? 'is' : 'are'} built against ${packageWords(pkg)}. Nothing is held. Prepare ${workspace}'s apps on one package together, in one deploy; or ${REMOVE_PREPARED_WORDS(url).replace(/^Remove/, 'remove')}`);
657
+ }
658
+ }
659
+ // The package, imported when the repository does not hold its version; for
660
+ // the next e3, on a deployment that holds packages for its move
661
+ let heldNow = false;
662
+ if (!inRepository) {
663
+ if (forPlatform === undefined) {
664
+ console.log(`Importing ${packageWords(pkg)} into ${repo}…`);
665
+ await packageImport(baseUrl, repo, carrier.package, { token });
666
+ }
667
+ else {
668
+ // A deployment from before the hold holds none, said only on its definite sign
669
+ if (!await heldPackagesServed(baseUrl, repo, workspace, names[0], { token })) {
670
+ exitError(await holdsNoPackageWords(baseUrl, token, namesWere, packageWords(pkg), repo));
671
+ }
672
+ }
673
+ }
674
+ // Held for the platform's move, which imports it and deploys it; a dry run holds nothing
675
+ if (!inRepository && forPlatform !== undefined && !dryRun) {
676
+ let answer;
677
+ try {
678
+ answer = await appHoldPackage(baseUrl, repo, workspace, names[0], carrier.package, { token }, { force, replace: options.replace === true });
679
+ }
680
+ catch (err) {
681
+ exitError(holdRefusalWords(err, names.join(', '), pkg, repo, url, workspace));
682
+ }
683
+ heldNow = answer.held;
684
+ console.log(holdWords(answer, repo, workspace, forPlatform, options.replace === true));
127
685
  }
128
- // The package, imported when the repository does not hold its version
129
- const held = await packageList(baseUrl, repo, { token });
130
- if (!held.some((p) => p.name === pkg.name && p.version === pkg.version)) {
131
- console.log(`Importing ${packageWords(pkg)} into ${repo}…`);
132
- await packageImport(baseUrl, repo, built[0].package, { token });
686
+ if (options.replace === true && (inRepository || forPlatform === undefined)) {
687
+ console.log(`--replace replaces a zip held for the platform's move, and this deploy holds none (${inRepository ? `${repo} holds ${packageWords(pkg)}` : 'it is for no next e3'}): nothing is replaced`);
133
688
  }
689
+ /** A refusal after a hold: the zip stays held, said, unless the refusal is that it is gone */
690
+ const refuse = (lines, gone = false) => exitError(heldNow && !gone ? `${lines}\n${STAYS_HELD(pkg)}` : lines);
691
+ // The package the workspace runs, for the words of a build for the next e3: read once
692
+ const workspaceRuns = forPlatform === undefined ? null : bareNow.length > 0 ? now : await workspacePackage(baseUrl, repo, workspace, token);
134
693
  // Each app object, uploaded; then one deploy of them all
135
694
  const items = [];
136
695
  for (const bundle of built)
137
696
  items.push({ name: bundle.app.name, source: await appUpload(baseUrl, repo, workspace, bundle.app.name, encodeApp(bundle.app), { token }) });
138
- const forPlatform = options.forPlatform === undefined ? none : some(options.forPlatform);
139
- const dryRun = options.dryRun === true;
140
- const response = items.length === 1
141
- ? await appDeploy(baseUrl, repo, workspace, items[0].name, { source: items[0].source, forPlatform, dryRun, force }, { token })
142
- : await appsDeploy(baseUrl, repo, workspace, { apps: items, forPlatform, dryRun, force }, { token });
697
+ const preparing = forPlatform === undefined ? none : some(forPlatform);
698
+ let response;
699
+ try {
700
+ response = items.length === 1
701
+ ? await appDeploy(baseUrl, repo, workspace, items[0].name, { source: items[0].source, forPlatform: preparing, dryRun, force }, { token })
702
+ : await appsDeploy(baseUrl, repo, workspace, { apps: items, forPlatform: preparing, dryRun, force }, { token });
703
+ }
704
+ catch (err) {
705
+ // A dry run for the next e3 holds nothing: one with a package not held
706
+ // yet is answered by the checks run here, and says what a deploy would do
707
+ if (dryRun && !inRepository && fitsHere !== null && err instanceof ApiError && err.code === 'package_missing') {
708
+ console.log(`${fitsHere}. A dry run keeps nothing, and ${packageWords(pkg)} is neither in ${repo} nor held for the platform's move: the platform's own checks were not made, since they need the package held. A deploy without --dry-run holds ${packageWords(pkg)} for the move and prepares ${them} on ${workspace} for e3 ${forPlatform}, once the platform's checks pass`);
709
+ return;
710
+ }
711
+ const then = preparedRefusalWords(err, url, workspace, forPlatform ?? null, pkg);
712
+ // A zip let go since its hold (another build on it removed meanwhile): not held, so never "stays held"
713
+ const gone = err instanceof ApiError && (err.code === 'package_missing' || err.code === 'held_package_gone' || err.code === 'held_package_gone_prepared');
714
+ return refuse(then === null ? describe(err) : `${describe(err)}\n${then}`, gone);
715
+ }
143
716
  // The apps refused: those deployed, and any other of the workspace the package would break
144
717
  if (response.type === 'refused') {
145
- console.error(appDeployRefusedText(response.value, packageWords(pkg)));
718
+ const lines = [appDeployRefusedText(response.value, packageWords(pkg))];
719
+ // An app built against the package its workspace runs: what its problems mean for it
720
+ const bound = new Set(built.filter((bundle) => !carriesPackage(bundle)).map((bundle) => bundle.app.name));
721
+ // How such an app crosses the platform's move, where the package doesn't fit it
722
+ const unfit = response.value.some((entry) => bound.has(entry.app) && entry.problems.some((problem) => FIT_PROBLEMS.has(problem.type)));
723
+ const holds = unfit ? await platformHolds(baseUrl, repo, workspace, [...bound][0], token) : false;
724
+ lines.push(...boundRefusalLines(response.value, bound, packageWords(pkg), `which ${workspace} runs`, forPlatform ?? null, holds));
725
+ if (heldNow)
726
+ lines.push(STAYS_HELD(pkg));
727
+ console.error(lines.join('\n'));
728
+ process.exit(1);
729
+ }
730
+ // A bundle carrying no package, answered with a deploy of its package: the
731
+ // workspace moved in the seconds since it was read, and the platform is
732
+ // deploying the held package back over it. Said loudly, never passed over
733
+ if (response.type === 'deploying' && bareNow.length > 0) {
734
+ // What it had moved to, while the job has yet to deploy the held package back
735
+ const movedTo = await workspacePackage(baseUrl, repo, workspace, token).catch(() => null);
736
+ const known = movedTo !== null && !sameString(movedTo.hash, now.hash) ? packageWithHash(movedTo) : null;
737
+ console.error(movedDuringDeployLines(url, workspace, packageWithHash(now), known, response.value.job, bareNow.map((bundle) => bundle.app.name)).join('\n'));
146
738
  process.exit(1);
147
739
  }
148
- console.log(deployedWords(response, workspace, packageWords(pkg)));
740
+ console.log(deployedWords(response, workspace, pkg, workspaceRuns));
741
+ if (response.type === 'staged' && fitsHere !== null) {
742
+ // The platform may not read a package of a newer e3 than it runs: then the build is checked at the move alone
743
+ const platform = await platformVersions(baseUrl, { token }).catch(() => null);
744
+ if (platform !== null && newerRelease(forPlatform, platform.e3)) {
745
+ console.log(`This platform runs e3 ${platform.e3} and may not read a package of e3 ${forPlatform}: where it cannot, the build is checked at the platform's move, before it deploys. ${fitsHere}`);
746
+ }
747
+ else {
748
+ console.log(fitsHere);
749
+ }
750
+ }
751
+ // Prepared: the workspace's live apps the move still waits for a build of
752
+ if (response.type === 'staged') {
753
+ const left = stillToPrepare(await appList(baseUrl, repo, workspace, { token }), forPlatform);
754
+ console.log(left.length === 0
755
+ ? `Every app of ${workspace} has a build for e3 ${forPlatform} prepared, or is built with it`
756
+ : `${workspace}'s apps with no build for e3 ${forPlatform} prepared yet: ${left.join(', ')}. The platform's move to it waits for a build of each, prepared as these are`);
757
+ }
149
758
  // With its package: the deploy job, waited for, then the apps as it left them
150
759
  if (response.type === 'deploying') {
151
760
  let said = '';
@@ -229,7 +838,7 @@ export const appCommand = {
229
838
  // Those a platform deploy to another e3 needs a bundle for, and those it has
230
839
  const rebuild = apps.filter((app) => app.current.builtWith !== platform
231
840
  && !(app.staged.type === 'some' && app.staged.value.forPlatform === platform));
232
- console.log(`\nThe platform runs e3 ${platform}: ${rebuild.length === 0 ? 'every app is built with it, or has a bundle staged for it' : `${rebuild.length} ${rebuild.length === 1 ? 'app needs' : 'apps need'} a bundle built with it`}.`);
841
+ console.log(`\nThe platform runs e3 ${platform}: ${rebuild.length === 0 ? 'every app is built with it, or has a build prepared for it' : `${rebuild.length} ${rebuild.length === 1 ? 'app needs' : 'apps need'} a build of it prepared`}.`);
233
842
  }
234
843
  catch (err) {
235
844
  exitError(describe(err));
@@ -260,6 +869,33 @@ export const appCommand = {
260
869
  exitError(describe(err));
261
870
  }
262
871
  },
872
+ /** e3-cloud app remove-prepared <repoUrl> <workspace> <app> [--force] */
873
+ async removePrepared(url, workspace, name, options = {}) {
874
+ try {
875
+ const force = options.force === true;
876
+ if (force)
877
+ console.warn(FORCE_WARNING(workspace, 'removal'));
878
+ const { baseUrl, repo } = parseRepoUrl(url);
879
+ const token = await getValidToken(baseUrl);
880
+ const before = await appGet(baseUrl, repo, workspace, name, { token });
881
+ let removed;
882
+ try {
883
+ removed = await appRemoveStaged(baseUrl, repo, workspace, name, { token }, { force });
884
+ }
885
+ catch (err) {
886
+ // A protected workspace's prepared builds change by a release from its staging
887
+ if (err instanceof ApiError && err.code === 'workspace_protected') {
888
+ exitError(`${describe(err)}\nRemove ${name}'s prepared build on ${workspace}'s staging (e3-cloud app remove-prepared ${url} <staging> ${name}), then release the staging to ${workspace}, which clears it here; an admin alone forces it here with --force`);
889
+ }
890
+ throw err;
891
+ }
892
+ const was = before.staged.type === 'some' ? ` for e3 ${before.staged.value.forPlatform}, with ${packageWords(before.staged.value.package)},` : '';
893
+ console.log(`${removed.name}'s build${was} prepared on ${workspace} is removed: v${removed.current.number} stays live on ${packageWords(removed.current.package)}, and the platform's move waits for a build of it, prepared again`);
894
+ }
895
+ catch (err) {
896
+ exitError(describe(err));
897
+ }
898
+ },
263
899
  /** e3-cloud app remove <repoUrl> <workspace> <app> */
264
900
  async remove(url, workspace, name, options = {}) {
265
901
  try {