@foliwake/cli 0.10.2 → 0.11.1

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.
Files changed (94) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +196 -14
  3. package/config/docker/compose.varnish.yaml +44 -0
  4. package/config/docker/compose.yaml +165 -0
  5. package/dist/commands/dev.d.ts +3 -0
  6. package/dist/commands/dev.js +58 -21
  7. package/dist/commands/docker/backup.d.ts +12 -0
  8. package/dist/commands/docker/backup.js +29 -0
  9. package/dist/commands/docker/configure.d.ts +23 -0
  10. package/dist/commands/docker/configure.js +73 -0
  11. package/dist/commands/docker/exec.d.ts +8 -0
  12. package/dist/commands/docker/exec.js +26 -0
  13. package/dist/commands/docker/install.d.ts +33 -0
  14. package/dist/commands/docker/install.js +126 -0
  15. package/dist/commands/docker/link.d.ts +12 -0
  16. package/dist/commands/docker/link.js +24 -0
  17. package/dist/commands/docker/list.d.ts +7 -0
  18. package/dist/commands/docker/list.js +16 -0
  19. package/dist/commands/docker/logs.d.ts +15 -0
  20. package/dist/commands/docker/logs.js +33 -0
  21. package/dist/commands/docker/remove.d.ts +13 -0
  22. package/dist/commands/docker/remove.js +35 -0
  23. package/dist/commands/docker/restart.d.ts +9 -0
  24. package/dist/commands/docker/restart.js +18 -0
  25. package/dist/commands/docker/restore.d.ts +14 -0
  26. package/dist/commands/docker/restore.js +34 -0
  27. package/dist/commands/docker/start.d.ts +9 -0
  28. package/dist/commands/docker/start.js +18 -0
  29. package/dist/commands/docker/status.d.ts +10 -0
  30. package/dist/commands/docker/status.js +22 -0
  31. package/dist/commands/docker/stop.d.ts +9 -0
  32. package/dist/commands/docker/stop.js +18 -0
  33. package/dist/commands/docker/update.d.ts +14 -0
  34. package/dist/commands/docker/update.js +42 -0
  35. package/dist/commands/project/publish.d.ts +1 -0
  36. package/dist/commands/project/publish.js +9 -2
  37. package/dist/core-command.d.ts +4 -1
  38. package/dist/core-command.js +6 -2
  39. package/dist/core-types.d.ts +1 -0
  40. package/dist/dev/container.d.ts +45 -0
  41. package/dist/dev/container.js +128 -0
  42. package/dist/dev/remote-html.js +2 -2
  43. package/dist/docker/asker.d.ts +4 -0
  44. package/dist/docker/asker.js +34 -0
  45. package/dist/docker/backup.d.ts +21 -0
  46. package/dist/docker/backup.js +114 -0
  47. package/dist/docker/cache-plan.d.ts +17 -0
  48. package/dist/docker/cache-plan.js +58 -0
  49. package/dist/docker/compose-file.d.ts +18 -0
  50. package/dist/docker/compose-file.js +58 -0
  51. package/dist/docker/context.d.ts +55 -0
  52. package/dist/docker/context.js +12 -0
  53. package/dist/docker/engine.d.ts +8 -0
  54. package/dist/docker/engine.js +51 -0
  55. package/dist/docker/env-block.d.ts +19 -0
  56. package/dist/docker/env-block.js +117 -0
  57. package/dist/docker/env-keys.d.ts +4 -0
  58. package/dist/docker/env-keys.js +33 -0
  59. package/dist/docker/env-verify.d.ts +17 -0
  60. package/dist/docker/env-verify.js +62 -0
  61. package/dist/docker/install-plan.d.ts +81 -0
  62. package/dist/docker/install-plan.js +332 -0
  63. package/dist/docker/install.d.ts +22 -0
  64. package/dist/docker/install.js +118 -0
  65. package/dist/docker/instance.d.ts +105 -0
  66. package/dist/docker/instance.js +184 -0
  67. package/dist/docker/lifecycle.d.ts +19 -0
  68. package/dist/docker/lifecycle.js +70 -0
  69. package/dist/docker/ports.d.ts +14 -0
  70. package/dist/docker/ports.js +50 -0
  71. package/dist/docker/process-context.d.ts +8 -0
  72. package/dist/docker/process-context.js +26 -0
  73. package/dist/docker/remove.d.ts +7 -0
  74. package/dist/docker/remove.js +82 -0
  75. package/dist/docker/report.d.ts +32 -0
  76. package/dist/docker/report.js +109 -0
  77. package/dist/docker/runner.d.ts +28 -0
  78. package/dist/docker/runner.js +55 -0
  79. package/dist/docker/settings-files.d.ts +40 -0
  80. package/dist/docker/settings-files.js +125 -0
  81. package/dist/docker/trace-url.d.ts +5 -0
  82. package/dist/docker/trace-url.js +36 -0
  83. package/dist/docker/update.d.ts +29 -0
  84. package/dist/docker/update.js +317 -0
  85. package/dist/docker-command.d.ts +17 -0
  86. package/dist/docker-command.js +37 -0
  87. package/dist/project-preview.d.ts +8 -0
  88. package/dist/project-preview.js +27 -5
  89. package/dist/project-publish.d.ts +8 -7
  90. package/dist/project-publish.js +124 -57
  91. package/dist/publish-code.d.ts +75 -0
  92. package/dist/publish-code.js +223 -0
  93. package/dist/render/site-status.js +2 -2
  94. package/package.json +13 -8
@@ -3,13 +3,16 @@
3
3
  // `foliwake preview` of this folder changed, with `expect` filled from it; paths of
4
4
  // files or folders under content/ choose pages by their front matter id. The API
5
5
  // always requires expect; this only fills it in. A terminal asks before anything
6
- // goes live; scripts pass --yes.
6
+ // goes live; scripts pass --yes. A preview built with new site code (the last commit's
7
+ // code, not live yet) goes live with that code: one question for both, or --yes --with-code
8
+ // (publish-code.ts).
7
9
  import { lstat, readFile, readdir, realpath } from 'node:fs/promises';
8
10
  import path from 'node:path';
9
11
  import { COMMAND } from './settings.js';
10
12
  import { frontMatterId } from './lib/front-matter.js';
11
13
  import { ConnectRefused, signedInTo } from './connect.js';
12
- import { lastPreviewFile, readConnection } from './project-preview.js';
14
+ import { lastPreviewFile, readConnection, saveLastPreview, } from './project-preview.js';
15
+ import { byHandLines, codeChoice, codeLine, codeQuestion, newCodeOf, pageCount, publishPair, refusalAdvice, } from './publish-code.js';
13
16
  const C = COMMAND;
14
17
  const WAIT_MS = 10 * 60 * 1000;
15
18
  /**
@@ -62,75 +65,135 @@ async function confirmed(request, question) {
62
65
  throw new ConnectRefused(`Not a terminal: pass --yes to ${question.toLowerCase().replace(/\?$/, '')} without being asked.`);
63
66
  return request.confirm(question);
64
67
  }
65
- async function finish(operation, io, verb) {
68
+ async function finish(operation, io, verbs) {
66
69
  const result = operation.result;
67
70
  if (operation.state !== 'succeeded') {
68
- io.err(`${verb} did not succeed ` +
71
+ io.err(`${verbs.failed} ` +
69
72
  `(${operation.state}${operation.error ? `: ${operation.error.code}: ${operation.error.message}` : ''}).`);
70
- if (operation.error?.code === 'PUBLISH_PREVIEW_CHANGED')
71
- io.err(`The preview changed since your last \`${C} preview\`; run it again.`);
73
+ const advice = refusalAdvice(operation.error);
74
+ if (advice)
75
+ io.err(advice);
72
76
  return 1;
73
77
  }
74
- io.out(`${verb}: live is release ${result?.generation ?? '?'} (${result?.siteRelease ?? '?'}).`);
78
+ io.out(`${verbs.done}: live is release ${result?.generation ?? '?'} (${result?.siteRelease ?? '?'}).`);
75
79
  if (operation.delivery?.state === 'pending')
76
80
  io.out(`The cache refresh is still running; \`${C} operation recover\` on the server finishes it.`);
77
81
  return 0;
78
82
  }
83
+ const nothingDone = (exit) => ({ exit, pages: [], operation: null, code: null });
84
+ async function linkedConnection(cwd) {
85
+ const connection = await readConnection(cwd);
86
+ if (!connection)
87
+ throw new ConnectRefused(`This folder is not linked to a site: run \`${C} site create <id>\` or \`${C} site link <id>\` first.`);
88
+ return connection;
89
+ }
90
+ /** The last `foliwake preview` of this folder, when it was of the linked site. */
91
+ async function lastPreviewOf(cwd, connection) {
92
+ const last = await readFile(lastPreviewFile(cwd), 'utf8').then((text) => JSON.parse(text), () => null);
93
+ return last && last.site === connection.site && last.server === connection.server ? last : null;
94
+ }
95
+ function reviewedPreview(request, last) {
96
+ if (request.expect)
97
+ return request.expect;
98
+ if (last)
99
+ return last.siteRelease;
100
+ throw new ConnectRefused(`There is no preview of this folder to publish: run \`${C} preview\` first (or name one with --expect sr_…).`);
101
+ }
102
+ async function changedPagesOf(core, site, preview) {
103
+ const changes = await core.channels.changes({ site, preview }).catch((error) => {
104
+ if (error.coreError?.code === 'PREVIEW_CHANGED')
105
+ throw new ConnectRefused(`The preview changed since your last \`${C} preview\`; run it again, review it, then publish.`);
106
+ throw error;
107
+ });
108
+ return new Map(changes.pages.map((page) => [page.id, page]));
109
+ }
110
+ /** With no paths, every changed page; with paths, their pages that the preview changes. */
111
+ async function chosenPages(request, changed, io) {
112
+ if (request.paths.length === 0)
113
+ return [...changed.keys()];
114
+ const pages = [];
115
+ for (const { file, id } of await pagesOf(request.cwd, request.paths)) {
116
+ if (changed.has(id))
117
+ pages.push(id);
118
+ else
119
+ io.err(`${file}: nothing to publish (not in this preview, or live has it as it is).`);
120
+ }
121
+ return [...new Set(pages)];
122
+ }
123
+ function listPages(io, preview, pages, changed) {
124
+ io.out(`From preview ${preview}:`);
125
+ for (const id of pages)
126
+ io.out(` ${changed.get(id)?.change === 'new' ? 'new ' : 'changed'} ${id} (${changed.get(id)?.path ?? ''})`);
127
+ }
128
+ /** The reviewed preview has new site code: one question for code and pages, then the three steps. */
129
+ async function publishWithCode(request, publication, newCode, io) {
130
+ const { core, site, preview, pages, changed, last } = publication;
131
+ listPages(io, preview, pages, changed);
132
+ io.out(codeLine(newCode));
133
+ const terminal = request.confirm !== null;
134
+ const choice = codeChoice(newCode, { yes: request.yes, withCode: request.withCode === true, terminal });
135
+ if (typeof choice === 'object')
136
+ throw new ConnectRefused(choice.refused);
137
+ if (choice === 'ask' && !(await request.confirm?.(codeQuestion(pages.length)))) {
138
+ io.out('Nothing was published.');
139
+ for (const line of byHandLines(newCode))
140
+ io.out(line);
141
+ return nothingDone(0);
142
+ }
143
+ const onRebuilt = async (siteRelease) => {
144
+ if (last?.siteRelease === preview)
145
+ await saveLastPreview(request.cwd, { ...last, siteRelease });
146
+ };
147
+ const pair = { site, reviewed: preview, newCode, pages, paths: request.paths, onRebuilt };
148
+ const outcome = await publishPair(core, pair, io);
149
+ return {
150
+ exit: outcome.exit,
151
+ pages: outcome.operation ? pages : [],
152
+ operation: outcome.operation,
153
+ code: outcome.released ? newCode.code : null,
154
+ };
155
+ }
156
+ /** The pages of the reviewed preview on the live code. */
157
+ async function publishPages(request, publication, io) {
158
+ const { core, site, preview, pages, changed } = publication;
159
+ if (pages.length === 0) {
160
+ io.out('Nothing to publish: the preview changes no page on live.');
161
+ return nothingDone(0);
162
+ }
163
+ listPages(io, preview, pages, changed);
164
+ if (!(await confirmed(request, `Publish these ${pageCount(pages.length)} to live?`))) {
165
+ io.out('Nothing was published.');
166
+ return nothingDone(0);
167
+ }
168
+ const operation = await waitFor(core, await core.channels.publish({ site, pages, expect: { preview } }));
169
+ const verbs = {
170
+ done: `Published ${pageCount(pages.length)}`,
171
+ failed: `Publishing ${pageCount(pages.length)} failed`,
172
+ };
173
+ return { exit: await finish(operation, io, verbs), pages, operation, code: null };
174
+ }
79
175
  export async function publishProject(request, io) {
80
176
  try {
81
- const connection = await readConnection(request.cwd);
82
- if (!connection)
83
- throw new ConnectRefused(`This folder is not linked to a site: run \`${C} site create <id>\` or \`${C} site link <id>\` first.`);
84
- let expect = request.expect ?? null;
85
- if (!expect) {
86
- const last = await readFile(lastPreviewFile(request.cwd), 'utf8').then((text) => JSON.parse(text), () => null);
87
- if (!last || last.site !== connection.site || last.server !== connection.server)
88
- throw new ConnectRefused(`There is no preview of this folder to publish: run \`${C} preview\` first (or name one with --expect sr_…).`);
89
- expect = last.siteRelease;
90
- }
177
+ const connection = await linkedConnection(request.cwd);
178
+ const last = await lastPreviewOf(request.cwd, connection);
179
+ const preview = reviewedPreview(request, last);
91
180
  const { core } = await signedInTo({ server: connection.server, env: request.env }, io);
92
- const changes = await core.channels.changes({ site: connection.site, preview: expect }).catch((error) => {
93
- if (error.coreError?.code === 'PREVIEW_CHANGED')
94
- throw new ConnectRefused(`The preview changed since your last \`${C} preview\`; run it again, review it, then publish.`);
95
- throw error;
96
- });
97
- const changed = new Map(changes.pages.map((page) => [page.id, page]));
98
- let pages;
99
- if (request.paths.length === 0)
100
- pages = [...changed.keys()];
101
- else {
102
- pages = [];
103
- for (const { file, id } of await pagesOf(request.cwd, request.paths)) {
104
- if (changed.has(id))
105
- pages.push(id);
106
- else
107
- io.err(`${file}: nothing to publish (not in this preview, or live has it as it is).`);
108
- }
109
- }
110
- pages = [...new Set(pages)];
111
- if (pages.length === 0) {
112
- io.out('Nothing to publish: the preview changes no page on live.');
113
- return { exit: 0, pages, operation: null };
114
- }
115
- io.out(`From preview ${expect}:`);
116
- for (const id of pages)
117
- io.out(` ${changed.get(id)?.change === 'new' ? 'new ' : 'changed'} ${id} (${changed.get(id)?.path ?? ''})`);
118
- if (!(await confirmed(request, `Publish these ${pages.length} page(s) to live?`))) {
119
- io.out('Nothing was published.');
120
- return { exit: 0, pages: [], operation: null };
121
- }
122
- const operation = await waitFor(core, await core.channels.publish({ site: connection.site, pages, expect: { preview: expect } }));
123
- return { exit: await finish(operation, io, `Published ${pages.length} page(s)`), pages, operation };
181
+ const changed = await changedPagesOf(core, connection.site, preview);
182
+ const pages = await chosenPages(request, changed, io);
183
+ const publication = { core, site: connection.site, preview, pages, changed, last };
184
+ const commit = last?.siteRelease === preview ? last.commit : null;
185
+ const newCode = newCodeOf(await core.sites.status({ site: connection.site }), preview, commit);
186
+ if (newCode)
187
+ return await publishWithCode(request, publication, newCode, io);
188
+ return await publishPages(request, publication, io);
124
189
  }
125
190
  catch (error) {
126
- return { exit: failure(error, io), pages: [], operation: null };
191
+ return nothingDone(failure(error, io));
127
192
  }
128
193
  }
129
194
  export async function unpublishProject(request, io) {
130
195
  try {
131
- const connection = await readConnection(request.cwd);
132
- if (!connection)
133
- throw new ConnectRefused(`This folder is not linked to a site: run \`${C} site create <id>\` or \`${C} site link <id>\` first.`);
196
+ const connection = await linkedConnection(request.cwd);
134
197
  if (request.paths.length === 0)
135
198
  throw new ConnectRefused('Name the pages to take off live: files or folders under content/.');
136
199
  const pages = [...new Set((await pagesOf(request.cwd, request.paths)).map((page) => page.id))];
@@ -139,15 +202,19 @@ export async function unpublishProject(request, io) {
139
202
  if (!live)
140
203
  throw new ConnectRefused('This site has nothing live.');
141
204
  io.out(`From live ${live}: ${pages.join(', ')}`);
142
- if (!(await confirmed(request, `Take these ${pages.length} page(s) off live?`))) {
205
+ if (!(await confirmed(request, `Take these ${pageCount(pages.length)} off live?`))) {
143
206
  io.out('Nothing was changed.');
144
- return { exit: 0, pages: [], operation: null };
207
+ return nothingDone(0);
145
208
  }
146
209
  const operation = await waitFor(core, await core.channels.unpublish({ site: connection.site, pages, expect: { live } }));
147
- return { exit: await finish(operation, io, `Took ${pages.length} page(s) off live`), pages, operation };
210
+ const verbs = {
211
+ done: `Took ${pageCount(pages.length)} off live`,
212
+ failed: `Taking ${pageCount(pages.length)} off live failed`,
213
+ };
214
+ return { exit: await finish(operation, io, verbs), pages, operation, code: null };
148
215
  }
149
216
  catch (error) {
150
- return { exit: failure(error, io), pages: [], operation: null };
217
+ return nothingDone(failure(error, io));
151
218
  }
152
219
  }
153
220
  function failure(error, io) {
@@ -0,0 +1,75 @@
1
+ import type { CoreCaller, Operation, SiteStatus } from './core-types.js';
2
+ import { type ConnectIO } from './connect.js';
3
+ type PreviewVersion = NonNullable<SiteStatus['previewVersion']>;
4
+ type SourceInputs = PreviewVersion['inputs'];
5
+ /** New site code waiting in the reviewed preview, and the live release it would replace. */
6
+ export interface NewCode {
7
+ /** The code release the preview was prepared for (cd_…). */
8
+ code: string;
9
+ /** The commit it was prepared from, when this folder's last preview made it. */
10
+ commit: string | null;
11
+ /** The live release and its code, as read before asking. */
12
+ live: {
13
+ siteRelease: string;
14
+ code: string;
15
+ };
16
+ /** The source snapshots the reviewed preview was built from. */
17
+ inputs: SourceInputs;
18
+ }
19
+ /** The new code of the reviewed preview `reviewed`, or null when publishing it needs no code release. */
20
+ export declare function newCodeOf(status: SiteStatus, reviewed: string, commit: string | null): NewCode | null;
21
+ /** 1 page, 2 pages. */
22
+ export declare const pageCount: (count: number) => string;
23
+ /** What `publish` does about new code: ask in the terminal, release it without asking, or refuse. */
24
+ export type CodeChoice = 'ask' | 'release' | {
25
+ refused: string;
26
+ };
27
+ export declare function codeChoice(newCode: NewCode, { yes, withCode, terminal }: {
28
+ yes: boolean;
29
+ withCode: boolean;
30
+ terminal: boolean;
31
+ }): CodeChoice;
32
+ /** The one question of `publish` when the preview has new code. */
33
+ export declare function codeQuestion(pages: number): string;
34
+ /** The line under the page list that names the code. */
35
+ export declare function codeLine(newCode: NewCode): string;
36
+ /** The commands that do it by hand, printed when the author answers no. */
37
+ export declare function byHandLines(newCode: NewCode): string[];
38
+ /** What a refused publication tells the author to do next, from the refusal's code and message. */
39
+ export declare function refusalAdvice(error: {
40
+ code: string;
41
+ message: string;
42
+ } | null | undefined): string | null;
43
+ /** The publication of the reviewed pair, step by step; `pages` are the pages chosen from the reviewed preview. */
44
+ export interface PairRequest {
45
+ site: string;
46
+ reviewed: string;
47
+ newCode: NewCode;
48
+ pages: string[];
49
+ /** The paths given to `publish`, repeated in the command that finishes a stopped publication. */
50
+ paths: string[];
51
+ /** Called with the rebuilt preview, before its pages are published (to remember it as the last preview). */
52
+ onRebuilt?: (siteRelease: string) => Promise<void>;
53
+ }
54
+ export interface PairOutcome {
55
+ exit: number;
56
+ /** True once the code release succeeded. */
57
+ released: boolean;
58
+ /** The rebuilt preview, once it exists. */
59
+ rebuilt: string | null;
60
+ /** The publication of the pages, when it was requested. */
61
+ operation: Operation | null;
62
+ }
63
+ /**
64
+ * Before anything changes: the sources still select the snapshots the reviewed preview was built from, so the
65
+ * rebuilt preview (step 2) reads the reviewed content. Refused otherwise, with nothing released.
66
+ */
67
+ export declare function sourcesMoved(selected: Array<{
68
+ id: string;
69
+ snapshot: string | null;
70
+ }>, inputs: SourceInputs): string | null;
71
+ /** Why the rebuilt preview is not the reviewed pair, or null when it is. */
72
+ export declare function rebuiltDifference(version: PreviewVersion | null, rebuilt: string, newCode: NewCode): string | null;
73
+ /** The three steps; a refusal before the code release goes to the caller, a stop after it is reported here. */
74
+ export declare function publishPair(core: CoreCaller, request: PairRequest, io: ConnectIO): Promise<PairOutcome>;
75
+ export {};
@@ -0,0 +1,223 @@
1
+ import { COMMAND } from './settings.js';
2
+ import { ConnectRefused } from './connect.js';
3
+ const C = COMMAND;
4
+ const WAIT_MS = 10 * 60 * 1000;
5
+ /** The new code of the reviewed preview `reviewed`, or null when publishing it needs no code release. */
6
+ export function newCodeOf(status, reviewed, commit) {
7
+ const version = status.previewVersion;
8
+ if (!version || version.site_release !== reviewed)
9
+ return null;
10
+ const code = version.prepared_for_code;
11
+ if (!code || !status.live || code === status.live.code_release)
12
+ return null;
13
+ return {
14
+ code,
15
+ commit,
16
+ live: { siteRelease: status.live.site_release, code: status.live.code_release },
17
+ inputs: version.inputs,
18
+ };
19
+ }
20
+ /** 1 page, 2 pages. */
21
+ export const pageCount = (count) => `${count} page${count === 1 ? '' : 's'}`;
22
+ export function codeChoice(newCode, { yes, withCode, terminal }) {
23
+ if (yes && withCode)
24
+ return 'release';
25
+ if (yes)
26
+ return { refused: yesWithoutCode(newCode) };
27
+ if (terminal)
28
+ return 'ask';
29
+ return {
30
+ refused: `Not a terminal: pass --yes --with-code to release the new site code ${newCode.code} together with the ` +
31
+ 'pages without being asked. Nothing was published.',
32
+ };
33
+ }
34
+ function yesWithoutCode(newCode) {
35
+ return (`This preview has new site code ${newCode.code}; --yes alone never releases code. ` +
36
+ `Pass --yes --with-code to release it together with the pages. Nothing was published.`);
37
+ }
38
+ /** The one question of `publish` when the preview has new code. */
39
+ export function codeQuestion(pages) {
40
+ const what = pages === 0 ? 'Release it (it changes no page content)?' : `Release it together with ${pageCount(pages)}?`;
41
+ return `The preview has new site code (styles, scripts, blocks). ${what} It changes every page of the live site.`;
42
+ }
43
+ /** The line under the page list that names the code. */
44
+ export function codeLine(newCode) {
45
+ const commit = newCode.commit ? ` (commit ${newCode.commit.slice(0, 7)})` : '';
46
+ return ` code ${newCode.code}${commit}: new site code; live runs ${newCode.live.code}`;
47
+ }
48
+ /** The commands that do it by hand, printed when the author answers no. */
49
+ export function byHandLines(newCode) {
50
+ return [
51
+ 'To do it by hand:',
52
+ ` ${C} code release ${newCode.code} --expect ${newCode.live.siteRelease} # live code; pages stay as they are`,
53
+ ` ${C} preview # then review it, and \`${C} publish\``,
54
+ ];
55
+ }
56
+ /** What a refused publication tells the author to do next, from the refusal's code and message. */
57
+ export function refusalAdvice(error) {
58
+ if (!error)
59
+ return null;
60
+ const preparedFor = /prepared for code (cd_[0-9a-f]+)/.exec(error.message)?.[1];
61
+ if (error.code === 'PUBLISH_PREVIEW_CHANGED' && preparedFor)
62
+ return (`The preview has new site code ${preparedFor} that is not live: run \`${C} publish\` in a terminal to ` +
63
+ `release it together with the pages, or \`${C} publish --yes --with-code\`.`);
64
+ if (error.code === 'PUBLISH_CODE_CHANGED')
65
+ return `Live code changed since this preview: run \`${C} preview\` again, review it, then publish.`;
66
+ if (error.code === 'PUBLISH_PREVIEW_CHANGED')
67
+ return `The preview changed since your last \`${C} preview\`; run it again.`;
68
+ return null;
69
+ }
70
+ /** A stop after the code went live: the reason, and the preview to finish from (null: none was rebuilt). */
71
+ class StoppedAfterRelease extends Error {
72
+ rebuilt;
73
+ constructor(message, rebuilt) {
74
+ super(message);
75
+ this.rebuilt = rebuilt;
76
+ }
77
+ }
78
+ async function waitFor(core, operation) {
79
+ const { waitInSlices } = await import('./remote/wait-slices.js');
80
+ return waitInSlices((sliceMs) => core.operations.wait({ site: operation.site, id: operation.id, until: 'commit', timeoutMs: sliceMs }), { until: 'commit', timeoutMs: WAIT_MS });
81
+ }
82
+ const failureText = (operation) => `${operation.state}${operation.error ? `: ${operation.error.code}: ${operation.error.message}` : ''}`;
83
+ /**
84
+ * Before anything changes: the sources still select the snapshots the reviewed preview was built from, so the
85
+ * rebuilt preview (step 2) reads the reviewed content. Refused otherwise, with nothing released.
86
+ */
87
+ export function sourcesMoved(selected, inputs) {
88
+ if (!inputs)
89
+ return 'the source snapshots it was built from are not recorded on the server';
90
+ for (const [source, input] of Object.entries(inputs)) {
91
+ const now = selected.find((candidate) => candidate.id === source)?.snapshot ?? null;
92
+ if (now !== input.snapshot)
93
+ return `source ${source} selects ${now ?? 'no snapshot'} now, not ${input.snapshot}`;
94
+ }
95
+ return null;
96
+ }
97
+ async function checkSources(core, request) {
98
+ const { sources } = await core.sources.list({ site: request.site });
99
+ const selected = sources.map((source) => ({ id: source.id, snapshot: source.selected?.snapshot ?? null }));
100
+ const moved = sourcesMoved(selected, request.newCode.inputs);
101
+ if (moved)
102
+ throw new ConnectRefused(`The pages of preview ${request.reviewed} changed on the server since it was built (${moved}): ` +
103
+ `run \`${C} preview\` again and review it. Nothing was released or published.`);
104
+ }
105
+ /** Step 1: the reviewed code goes live, refused when live moved since it was read. */
106
+ async function releaseCode(core, request, io) {
107
+ const { newCode } = request;
108
+ io.out(`Releasing code ${newCode.code} as live code…`);
109
+ const requested = await core.code.release({
110
+ site: request.site,
111
+ code: newCode.code,
112
+ expect: { live: newCode.live.siteRelease },
113
+ ...(newCode.commit ? { sourceRevision: newCode.commit } : {}),
114
+ });
115
+ const operation = await waitFor(core, requested);
116
+ if (operation.state === 'succeeded')
117
+ return;
118
+ throw new ConnectRefused(`Releasing code ${newCode.code} did not succeed (${failureText(operation)}). ` +
119
+ 'Live is unchanged: neither the code nor the pages were released.');
120
+ }
121
+ /** Step 2: the preview again, from the same source snapshots, on the code that is live now. */
122
+ async function rebuildPreview(core, request, io) {
123
+ io.out('Rebuilding the reviewed preview on the released code…');
124
+ const requested = await core.channels.preview({
125
+ site: request.site,
126
+ pages: null,
127
+ expect: { preview: request.reviewed },
128
+ });
129
+ const operation = await waitFor(core, requested);
130
+ const siteRelease = operation.result?.siteRelease ?? null;
131
+ if (operation.state !== 'succeeded' || !siteRelease)
132
+ throw new StoppedAfterRelease(`rebuilding the preview did not succeed (${failureText(operation)})`, null);
133
+ return siteRelease;
134
+ }
135
+ const sameInputs = (a, b) => {
136
+ const sources = new Set([...Object.keys(a ?? {}), ...Object.keys(b ?? {})]);
137
+ return [...sources].every((source) => a?.[source]?.snapshot === b?.[source]?.snapshot);
138
+ };
139
+ /** Why the rebuilt preview is not the reviewed pair, or null when it is. */
140
+ export function rebuiltDifference(version, rebuilt, newCode) {
141
+ if (version?.site_release !== rebuilt)
142
+ return `the current preview is no longer ${rebuilt}`;
143
+ if (version.code_release !== newCode.code)
144
+ return `the rebuilt preview has code ${version.code_release}`;
145
+ if (version.prepared_for_code)
146
+ return `the rebuilt preview was prepared for code ${version.prepared_for_code}`;
147
+ if (!sameInputs(version.inputs, newCode.inputs))
148
+ return 'the rebuilt preview was built from other source snapshots than the reviewed one';
149
+ return null;
150
+ }
151
+ /** Step 2, checked: the rebuilt preview holds the reviewed content on the reviewed code, and every chosen page. */
152
+ async function checkRebuilt(core, request, rebuilt) {
153
+ const status = await core.sites.status({ site: request.site });
154
+ const difference = rebuiltDifference(status.previewVersion, rebuilt, request.newCode);
155
+ if (difference)
156
+ throw new StoppedAfterRelease(difference, rebuilt);
157
+ const changes = await core.channels.changes({ site: request.site, preview: rebuilt });
158
+ const changed = new Set(changes.pages.map((page) => page.id));
159
+ const missing = request.pages.filter((page) => !changed.has(page));
160
+ if (missing.length > 0)
161
+ throw new StoppedAfterRelease(`the rebuilt preview does not change ${missing.join(', ')}`, rebuilt);
162
+ }
163
+ /** Step 3: the chosen pages of the rebuilt preview go live. */
164
+ async function publishPages(core, request, rebuilt) {
165
+ const requested = await core.channels.publish({
166
+ site: request.site,
167
+ pages: request.pages,
168
+ expect: { preview: rebuilt },
169
+ });
170
+ const operation = await waitFor(core, requested);
171
+ if (operation.state !== 'succeeded')
172
+ throw new StoppedAfterRelease(`publishing them did not succeed (${failureText(operation)})`, rebuilt);
173
+ return operation;
174
+ }
175
+ /** How to finish or undo a publication that stopped after its code went live. */
176
+ function stoppedLines(request, stop) {
177
+ const { newCode } = request;
178
+ const paths = request.paths.length > 0 ? ` ${request.paths.join(' ')}` : '';
179
+ const finish = stop.rebuilt
180
+ ? `\`${C} publish --expect ${stop.rebuilt}${paths}\``
181
+ : `\`${C} preview\`, review it, then \`${C} publish${paths}\``;
182
+ return [
183
+ `Code ${newCode.code} is live now, but the pages were not published: ${stop.message}.`,
184
+ `Finish with ${finish},`,
185
+ `or roll the code back with \`${C} code rollback ${newCode.live.code} --latest-live\`.`,
186
+ ];
187
+ }
188
+ function liveLine(operation, pages) {
189
+ const result = operation.result;
190
+ const release = `release ${result?.generation ?? '?'} (${result?.siteRelease ?? '?'})`;
191
+ return `Published ${pageCount(pages)} with the new code: live is ${release}.`;
192
+ }
193
+ /** The three steps; a refusal before the code release goes to the caller, a stop after it is reported here. */
194
+ export async function publishPair(core, request, io) {
195
+ await checkSources(core, request);
196
+ await releaseCode(core, request, io);
197
+ let rebuilt = null;
198
+ try {
199
+ rebuilt = await rebuildPreview(core, request, io);
200
+ await request.onRebuilt?.(rebuilt);
201
+ await checkRebuilt(core, request, rebuilt);
202
+ if (request.pages.length === 0) {
203
+ io.out(`Released the new code; preview ${rebuilt} now shows it as live does.`);
204
+ return { exit: 0, released: true, rebuilt, operation: null };
205
+ }
206
+ const operation = await publishPages(core, request, rebuilt);
207
+ io.out(liveLine(operation, request.pages.length));
208
+ return { exit: 0, released: true, rebuilt, operation };
209
+ }
210
+ catch (error) {
211
+ const stop = error instanceof StoppedAfterRelease ? error : new StoppedAfterRelease(reasonOf(error), rebuilt);
212
+ for (const line of stoppedLines(request, stop))
213
+ io.err(line);
214
+ return { exit: 1, released: true, rebuilt: stop.rebuilt, operation: null };
215
+ }
216
+ }
217
+ /** A thrown refusal or core error after the code release, as one clause. */
218
+ function reasonOf(error) {
219
+ const coreError = error.coreError;
220
+ if (coreError)
221
+ return `${coreError.code}: ${coreError.message}`;
222
+ return error instanceof Error ? error.message : String(error);
223
+ }
@@ -18,8 +18,8 @@ function versionLines(version) {
18
18
  `${version.site_release})`,
19
19
  ];
20
20
  if (version.prepared_for_code) {
21
- lines.push(` prepared for code ${version.prepared_for_code} (preview --code): this preview cannot be published ` +
22
- `(PUBLISH_PREVIEW_CHANGED); make that code live, run \`${C} preview\` again and review it.`);
21
+ lines.push(` prepared for code ${version.prepared_for_code} (preview --code): its pages go live only with ` +
22
+ `that code; in the linked folder \`${C} publish\` releases both (scripts: --yes --with-code).`);
23
23
  }
24
24
  if (version.preview_update === 'migration' &&
25
25
  version.live_code_release &&
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@foliwake/cli",
3
- "version": "0.10.2",
4
- "description": "The foliwake command without the engine: sign in to a Foliwake server and run its commands there (site status, sync, preview, publish, code deploy, branches, releases, sources, credentials, CI keys), and develop blocks against a remote preview.",
3
+ "version": "0.11.1",
4
+ "description": "The foliwake command without the engine: install a Foliwake server in Docker, sign in to it and run its commands there (site status, sync, preview, publish, code deploy, branches, releases, sources, credentials, CI keys), and develop a site locally (in Docker) or against a remote preview.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
7
7
  "sideEffects": false,
@@ -67,6 +67,9 @@
67
67
  "credential": {
68
68
  "description": "Credential values the content sources name (stored on the server, never shown)"
69
69
  },
70
+ "docker": {
71
+ "description": "Foliwake instances in Docker on this computer: install, list, start, update, remove"
72
+ },
70
73
  "next": {
71
74
  "description": "Next.js apps that show Foliwake pages"
72
75
  },
@@ -104,16 +107,18 @@
104
107
  "@oclif/core": "4.14.0",
105
108
  "chokidar": "6.0.0",
106
109
  "parse5": "7.3.0",
107
- "@foliwake/blocks-contract": "0.10.2",
108
- "@foliwake/server": "0.10.2",
109
- "@foliwake/core-contract": "0.10.2",
110
- "create-foliwake": "0.10.2",
111
- "@foliwake/site-check": "0.10.2",
112
- "@foliwake/block-kit": "0.10.2"
110
+ "yaml": "2.9.1",
111
+ "@foliwake/block-kit": "0.11.1",
112
+ "@foliwake/blocks-contract": "0.11.1",
113
+ "@foliwake/core-contract": "0.11.1",
114
+ "@foliwake/server": "0.11.1",
115
+ "create-foliwake": "0.11.1",
116
+ "@foliwake/site-check": "0.11.1"
113
117
  },
114
118
  "files": [
115
119
  "dist/",
116
120
  "!dist/**/*.map",
121
+ "config/",
117
122
  "LICENSE",
118
123
  "README.md",
119
124
  "CHANGELOG.md"