@poli-page/cli 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +185 -58
- package/dist/api-client.d.ts +62 -2
- package/dist/api-client.d.ts.map +1 -1
- package/dist/api-client.js +172 -13
- package/dist/api-client.js.map +1 -1
- package/dist/api-origin.d.ts +28 -0
- package/dist/api-origin.d.ts.map +1 -0
- package/dist/api-origin.js +84 -0
- package/dist/api-origin.js.map +1 -0
- package/dist/auth.d.ts +4 -0
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +19 -13
- package/dist/auth.js.map +1 -1
- package/dist/cloud-context.d.ts +8 -0
- package/dist/cloud-context.d.ts.map +1 -1
- package/dist/cloud-context.js +14 -9
- package/dist/cloud-context.js.map +1 -1
- package/dist/cloud-link.d.ts +41 -0
- package/dist/cloud-link.d.ts.map +1 -0
- package/dist/cloud-link.js +112 -0
- package/dist/cloud-link.js.map +1 -0
- package/dist/commands/auth.d.ts +31 -8
- package/dist/commands/auth.d.ts.map +1 -1
- package/dist/commands/auth.js +72 -23
- package/dist/commands/auth.js.map +1 -1
- package/dist/commands/clone.d.ts +32 -0
- package/dist/commands/clone.d.ts.map +1 -0
- package/dist/commands/clone.js +266 -0
- package/dist/commands/clone.js.map +1 -0
- package/dist/commands/dev.d.ts +58 -3
- package/dist/commands/dev.d.ts.map +1 -1
- package/dist/commands/dev.js +335 -63
- package/dist/commands/dev.js.map +1 -1
- package/dist/commands/documents/delete.d.ts.map +1 -1
- package/dist/commands/documents/delete.js +2 -9
- package/dist/commands/documents/delete.js.map +1 -1
- package/dist/commands/documents/document-id.d.ts +2 -0
- package/dist/commands/documents/document-id.d.ts.map +1 -0
- package/dist/commands/documents/document-id.js +10 -0
- package/dist/commands/documents/document-id.js.map +1 -0
- package/dist/commands/documents/get.d.ts.map +1 -1
- package/dist/commands/documents/get.js +2 -9
- package/dist/commands/documents/get.js.map +1 -1
- package/dist/commands/documents/preview.d.ts +1 -0
- package/dist/commands/documents/preview.d.ts.map +1 -1
- package/dist/commands/documents/preview.js +89 -14
- package/dist/commands/documents/preview.js.map +1 -1
- package/dist/commands/documents/thumbnails.d.ts.map +1 -1
- package/dist/commands/documents/thumbnails.js +35 -20
- package/dist/commands/documents/thumbnails.js.map +1 -1
- package/dist/commands/env.d.ts.map +1 -1
- package/dist/commands/env.js +4 -19
- package/dist/commands/env.js.map +1 -1
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +41 -34
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/link.d.ts +15 -0
- package/dist/commands/link.d.ts.map +1 -1
- package/dist/commands/link.js +88 -21
- package/dist/commands/link.js.map +1 -1
- package/dist/commands/new.d.ts.map +1 -1
- package/dist/commands/new.js +14 -13
- package/dist/commands/new.js.map +1 -1
- package/dist/commands/publish.d.ts +6 -1
- package/dist/commands/publish.d.ts.map +1 -1
- package/dist/commands/publish.js +32 -16
- package/dist/commands/publish.js.map +1 -1
- package/dist/commands/render.d.ts.map +1 -1
- package/dist/commands/render.js +23 -12
- package/dist/commands/render.js.map +1 -1
- package/dist/commands/sync.d.ts +52 -0
- package/dist/commands/sync.d.ts.map +1 -0
- package/dist/commands/sync.js +224 -0
- package/dist/commands/sync.js.map +1 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +2 -0
- package/dist/constants.js.map +1 -1
- package/dist/credentials.d.ts +15 -0
- package/dist/credentials.d.ts.map +1 -1
- package/dist/credentials.js +52 -22
- package/dist/credentials.js.map +1 -1
- package/dist/dev-page.d.ts +7 -0
- package/dist/dev-page.d.ts.map +1 -1
- package/dist/dev-page.js +42 -0
- package/dist/dev-page.js.map +1 -1
- package/dist/dev-server.d.ts +19 -0
- package/dist/dev-server.d.ts.map +1 -1
- package/dist/dev-server.js +70 -8
- package/dist/dev-server.js.map +1 -1
- package/dist/draft-bases.d.ts +54 -0
- package/dist/draft-bases.d.ts.map +1 -0
- package/dist/draft-bases.js +253 -0
- package/dist/draft-bases.js.map +1 -0
- package/dist/draft-files.d.ts +96 -0
- package/dist/draft-files.d.ts.map +1 -0
- package/dist/draft-files.js +447 -0
- package/dist/draft-files.js.map +1 -0
- package/dist/exit-codes.d.ts +3 -1
- package/dist/exit-codes.d.ts.map +1 -1
- package/dist/exit-codes.js +16 -7
- package/dist/exit-codes.js.map +1 -1
- package/dist/file-watcher.d.ts +4 -1
- package/dist/file-watcher.d.ts.map +1 -1
- package/dist/file-watcher.js +26 -11
- package/dist/file-watcher.js.map +1 -1
- package/dist/format-resolver.d.ts +6 -0
- package/dist/format-resolver.d.ts.map +1 -1
- package/dist/format-resolver.js +14 -6
- package/dist/format-resolver.js.map +1 -1
- package/dist/manifest.d.ts +8 -0
- package/dist/manifest.d.ts.map +1 -1
- package/dist/manifest.js +37 -7
- package/dist/manifest.js.map +1 -1
- package/dist/manifest.schema.d.ts +4 -2
- package/dist/manifest.schema.d.ts.map +1 -1
- package/dist/manifest.schema.js +40 -7
- package/dist/manifest.schema.js.map +1 -1
- package/dist/output.d.ts +5 -0
- package/dist/output.d.ts.map +1 -1
- package/dist/output.js +7 -0
- package/dist/output.js.map +1 -1
- package/dist/program.d.ts.map +1 -1
- package/dist/program.js +55 -22
- package/dist/program.js.map +1 -1
- package/dist/project-files.d.ts +26 -0
- package/dist/project-files.d.ts.map +1 -1
- package/dist/project-files.js +46 -7
- package/dist/project-files.js.map +1 -1
- package/dist/project-loader.d.ts +6 -3
- package/dist/project-loader.d.ts.map +1 -1
- package/dist/project-loader.js +36 -61
- package/dist/project-loader.js.map +1 -1
- package/dist/project-sync.d.ts +123 -18
- package/dist/project-sync.d.ts.map +1 -1
- package/dist/project-sync.js +449 -80
- package/dist/project-sync.js.map +1 -1
- package/dist/security/paths.d.ts +29 -0
- package/dist/security/paths.d.ts.map +1 -0
- package/dist/security/paths.js +109 -0
- package/dist/security/paths.js.map +1 -0
- package/dist/slug.d.ts +8 -0
- package/dist/slug.d.ts.map +1 -0
- package/dist/slug.js +15 -0
- package/dist/slug.js.map +1 -0
- package/dist/sync-engine.d.ts +14 -9
- package/dist/sync-engine.d.ts.map +1 -1
- package/dist/sync-engine.js +26 -29
- package/dist/sync-engine.js.map +1 -1
- package/dist/template-importer.d.ts.map +1 -1
- package/dist/template-importer.js +170 -59
- package/dist/template-importer.js.map +1 -1
- package/package.json +1 -1
- package/dist/commands/watch.d.ts +0 -26
- package/dist/commands/watch.d.ts.map +0 -1
- package/dist/commands/watch.js +0 -102
- package/dist/commands/watch.js.map +0 -1
package/dist/commands/dev.js
CHANGED
|
@@ -1,13 +1,57 @@
|
|
|
1
1
|
import { readFile, stat } from 'node:fs/promises';
|
|
2
2
|
import { join, resolve } from 'node:path';
|
|
3
3
|
import { resolveCloudContext } from '../cloud-context.js';
|
|
4
|
+
import { confirmLinkPrompt } from '../cloud-link.js';
|
|
4
5
|
import { createFileWatcher } from '../file-watcher.js';
|
|
5
|
-
import { createProjectSyncer } from '../project-sync.js';
|
|
6
|
+
import { createProjectSyncer, isNotHere, listProjectFiles, SyncConflictError, UnresolvedConflictsError, } from '../project-sync.js';
|
|
7
|
+
import { resolveConflicts } from '../draft-files.js';
|
|
6
8
|
import { findTemplate, loadProject, loadTemplate, unwrapMockJson, } from '../project-loader.js';
|
|
7
|
-
import { errorToExitCode } from '../exit-codes.js';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
9
|
+
import { errorToExitCode, ExitCode, UsageError } from '../exit-codes.js';
|
|
10
|
+
import { oneLine } from '../output.js';
|
|
11
|
+
import { startDevServer, securityHeaders, DEFAULT_DEV_PORT, } from '../dev-server.js';
|
|
12
|
+
import { DEV_PAGE_HTML, DEV_PAGE_SECURITY_HEADERS, DEV_PLACEHOLDER_HTML, DEV_PLACEHOLDER_SECURITY_HEADERS, } from '../dev-page.js';
|
|
10
13
|
const DEFAULT_DEBOUNCE_MS = 300;
|
|
14
|
+
/**
|
|
15
|
+
* The `select` prompt `poli dev` asks a conflict with, in a terminal. The
|
|
16
|
+
* session's signal cancels it (`AbortPromptError`); Ctrl-C rejects it with
|
|
17
|
+
* `ExitPromptError`, as the prompt holds stdin in raw mode. A file this
|
|
18
|
+
* folder does not have is asked with "Skip for now" as the default: keeping
|
|
19
|
+
* "mine" deletes it from the draft.
|
|
20
|
+
*/
|
|
21
|
+
export async function conflictPrompt(conflict, context) {
|
|
22
|
+
const { select } = await import('@inquirer/prompts');
|
|
23
|
+
const path = oneLine(conflict.path);
|
|
24
|
+
if (isNotHere(conflict)) {
|
|
25
|
+
return select({
|
|
26
|
+
message: `${path} is in the cloud draft but not in this folder (deleted here, or never synced).`,
|
|
27
|
+
choices: [
|
|
28
|
+
{ name: 'Keep mine (delete it from the draft)', value: 'mine' },
|
|
29
|
+
{ name: "Take the cloud's", value: 'theirs' },
|
|
30
|
+
{ name: 'Skip for now', value: 'skip' },
|
|
31
|
+
],
|
|
32
|
+
default: 'skip',
|
|
33
|
+
}, { signal: context.signal });
|
|
34
|
+
}
|
|
35
|
+
return select({
|
|
36
|
+
message: conflict.remoteSha256 === null
|
|
37
|
+
? `${path} was deleted in the cloud draft and changed here.`
|
|
38
|
+
: conflict.reconciled
|
|
39
|
+
? `${path} differs from the cloud draft.`
|
|
40
|
+
: `${path} changed both here and in the cloud draft.`,
|
|
41
|
+
choices: [
|
|
42
|
+
{ name: 'Keep mine', value: 'mine' },
|
|
43
|
+
{ name: "Take the cloud's", value: 'theirs' },
|
|
44
|
+
{ name: 'Skip for now', value: 'skip' },
|
|
45
|
+
],
|
|
46
|
+
}, { signal: context.signal });
|
|
47
|
+
}
|
|
48
|
+
/** Ctrl-C at a prompt, or the prompt cancelled by the session: stop, as Ctrl-C does. */
|
|
49
|
+
class StopRequested extends Error {
|
|
50
|
+
}
|
|
51
|
+
function isPromptCancel(err) {
|
|
52
|
+
const name = err instanceof Error ? err.name : undefined;
|
|
53
|
+
return name === 'ExitPromptError' || name === 'AbortPromptError';
|
|
54
|
+
}
|
|
11
55
|
/**
|
|
12
56
|
* A `templates/<name>/…` path belongs to `<name>`. Anything else — a
|
|
13
57
|
* shared partial, `tailwind.css`, an asset, the manifest — belongs to no
|
|
@@ -99,27 +143,29 @@ function toDevError(stage, err) {
|
|
|
99
143
|
*/
|
|
100
144
|
export async function startDevSession(templateName, options = {}) {
|
|
101
145
|
const cwd = options.cwd ?? process.cwd();
|
|
146
|
+
const preview = options.preview !== false;
|
|
102
147
|
const emit = (event) => options.onEvent?.(event);
|
|
103
|
-
// Resolves the manifest, the session token and the org — throws the
|
|
104
|
-
// familiar "not linked" / "not logged in" errors before anything is
|
|
105
|
-
// served, so a misconfigured project fails fast instead of booting a
|
|
106
|
-
// server that can never work.
|
|
107
|
-
const { client, ctx } = await resolveCloudContext({
|
|
108
|
-
cwd,
|
|
109
|
-
apiClient: options.apiClient,
|
|
110
|
-
homeDir: options.homeDir,
|
|
111
|
-
});
|
|
112
148
|
const initial = await loadProject(cwd);
|
|
113
149
|
let manifest = initial.manifest;
|
|
114
150
|
let templates = templateNamesOf(manifest);
|
|
115
|
-
// `projectSlug` is
|
|
116
|
-
//
|
|
117
|
-
//
|
|
118
|
-
// fail fast instead, with the same message (and exit code) as
|
|
119
|
-
// `poli render`.
|
|
151
|
+
// A legacy link (no `projectSlug`) is refused like `poli render` does,
|
|
152
|
+
// and before the link prompt, so the user never confirms a folder that
|
|
153
|
+
// cannot render.
|
|
120
154
|
if (!manifest.cloud?.projectSlug) {
|
|
121
155
|
throw new Error("This folder isn't linked to a cloud project. Run `poli link` first.");
|
|
122
156
|
}
|
|
157
|
+
// An over-limit file set fails before the first request, not after the server starts.
|
|
158
|
+
await listProjectFiles(cwd);
|
|
159
|
+
// Resolves the session token, the org and the confirmed link — throws
|
|
160
|
+
// the familiar "not logged in" errors before anything is served, so a
|
|
161
|
+
// misconfigured project fails fast instead of booting a server that can
|
|
162
|
+
// never work.
|
|
163
|
+
const { client, ctx } = await resolveCloudContext({
|
|
164
|
+
cwd,
|
|
165
|
+
apiClient: options.apiClient,
|
|
166
|
+
homeDir: options.homeDir,
|
|
167
|
+
confirmLink: options.confirmLink,
|
|
168
|
+
});
|
|
123
169
|
if (templateName !== undefined) {
|
|
124
170
|
// Fail fast on an unknown name given on the command line — same
|
|
125
171
|
// message (and exit code) as `poli render`.
|
|
@@ -139,8 +185,9 @@ export async function startDevSession(templateName, options = {}) {
|
|
|
139
185
|
renderVersion: 0,
|
|
140
186
|
error: null,
|
|
141
187
|
lastRenderedAt: null,
|
|
188
|
+
paused: [],
|
|
142
189
|
};
|
|
143
|
-
if (templates.length === 0) {
|
|
190
|
+
if (preview && templates.length === 0) {
|
|
144
191
|
state.error = {
|
|
145
192
|
stage: 'project',
|
|
146
193
|
message: `No templates declared in poli-page.json. Add one with \`poli new <name>\`.`,
|
|
@@ -161,6 +208,23 @@ export async function startDevSession(templateName, options = {}) {
|
|
|
161
208
|
* even for a batch that was already past the `stopped` check.
|
|
162
209
|
*/
|
|
163
210
|
let inflightBatch = Promise.resolve();
|
|
211
|
+
/** Conflicts answered "Skip for now": not asked again while they stay open. */
|
|
212
|
+
const skipped = new Set();
|
|
213
|
+
/** What stopped the session on its own (a conflict nobody can answer). */
|
|
214
|
+
let stopError = null;
|
|
215
|
+
/** A prompt was cancelled at start-up: the session closes before it returns. */
|
|
216
|
+
let stopRequested = false;
|
|
217
|
+
/** Cancels a pending conflict prompt on abort or `close()`. */
|
|
218
|
+
const prompts = new AbortController();
|
|
219
|
+
options.signal?.addEventListener('abort', () => prompts.abort(), { once: true });
|
|
220
|
+
if (options.signal?.aborted)
|
|
221
|
+
prompts.abort();
|
|
222
|
+
let settleDone;
|
|
223
|
+
const done = new Promise((resolveDone, rejectDone) => {
|
|
224
|
+
settleDone = (err) => (err ? rejectDone(err) : resolveDone());
|
|
225
|
+
});
|
|
226
|
+
// Settled even when nobody waits on it (a caller that only uses `close()`).
|
|
227
|
+
done.catch(() => { });
|
|
164
228
|
function broadcast() {
|
|
165
229
|
// During shutdown the SSE responses are being ended — a batch
|
|
166
230
|
// still settling must not write into them.
|
|
@@ -172,7 +236,99 @@ export async function startDevSession(templateName, options = {}) {
|
|
|
172
236
|
state.status = next;
|
|
173
237
|
broadcast();
|
|
174
238
|
}
|
|
175
|
-
const syncer = createProjectSyncer({
|
|
239
|
+
const syncer = createProjectSyncer({
|
|
240
|
+
cwd,
|
|
241
|
+
client,
|
|
242
|
+
ctx,
|
|
243
|
+
signal: options.signal,
|
|
244
|
+
homeDir: options.homeDir,
|
|
245
|
+
});
|
|
246
|
+
/**
|
|
247
|
+
* Skipped paths stay skipped only while they are still in conflict: one
|
|
248
|
+
* settled meanwhile (`poli sync` in another terminal) is asked again if
|
|
249
|
+
* it ever conflicts again.
|
|
250
|
+
*/
|
|
251
|
+
function refreshPaused() {
|
|
252
|
+
const pausedNow = new Set(syncer.paused());
|
|
253
|
+
for (const path of skipped)
|
|
254
|
+
if (!pausedNow.has(path))
|
|
255
|
+
skipped.delete(path);
|
|
256
|
+
state.paused = [...skipped].sort();
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* One sync, and the conflicts it reports: each one not skipped yet is
|
|
260
|
+
* asked (`resolveConflict`), the answers applied (`resolveConflicts`),
|
|
261
|
+
* and the sync runs again, until only skipped conflicts are left. An
|
|
262
|
+
* answer that could not be applied (the file moved again) comes back as
|
|
263
|
+
* a conflict and is asked again. Nobody to ask: `UnresolvedConflictsError`.
|
|
264
|
+
*/
|
|
265
|
+
async function syncResolving(first) {
|
|
266
|
+
const changedPaths = new Set();
|
|
267
|
+
let syncedAt;
|
|
268
|
+
const merge = (outcome) => {
|
|
269
|
+
for (const path of outcome.changedPaths)
|
|
270
|
+
changedPaths.add(path);
|
|
271
|
+
if (outcome.syncedAt)
|
|
272
|
+
syncedAt = outcome.syncedAt;
|
|
273
|
+
};
|
|
274
|
+
const merged = () => ({
|
|
275
|
+
changed: changedPaths.size > 0,
|
|
276
|
+
...(syncedAt ? { syncedAt } : {}),
|
|
277
|
+
changedPaths: [...changedPaths],
|
|
278
|
+
});
|
|
279
|
+
for (let attempt = first;; attempt = () => syncer.sync()) {
|
|
280
|
+
let open;
|
|
281
|
+
try {
|
|
282
|
+
merge(await attempt());
|
|
283
|
+
return merged();
|
|
284
|
+
}
|
|
285
|
+
catch (err) {
|
|
286
|
+
if (!(err instanceof SyncConflictError))
|
|
287
|
+
throw err;
|
|
288
|
+
merge(err.outcome);
|
|
289
|
+
open = err.conflicts.filter((c) => !skipped.has(c.path));
|
|
290
|
+
}
|
|
291
|
+
finally {
|
|
292
|
+
refreshPaused();
|
|
293
|
+
}
|
|
294
|
+
if (open.length === 0)
|
|
295
|
+
return merged();
|
|
296
|
+
if (!options.resolveConflict)
|
|
297
|
+
throw new UnresolvedConflictsError(open);
|
|
298
|
+
const resolutions = [];
|
|
299
|
+
const stopping = () => stopped || prompts.signal.aborted;
|
|
300
|
+
for (const conflict of open) {
|
|
301
|
+
if (stopping())
|
|
302
|
+
throw new StopRequested();
|
|
303
|
+
let choice;
|
|
304
|
+
try {
|
|
305
|
+
choice = await options.resolveConflict(conflict, { signal: prompts.signal });
|
|
306
|
+
}
|
|
307
|
+
catch (err) {
|
|
308
|
+
if (isPromptCancel(err))
|
|
309
|
+
throw new StopRequested();
|
|
310
|
+
throw err;
|
|
311
|
+
}
|
|
312
|
+
if (choice === 'skip')
|
|
313
|
+
skipped.add(conflict.path);
|
|
314
|
+
else
|
|
315
|
+
resolutions.push({ ...conflict, choice });
|
|
316
|
+
}
|
|
317
|
+
// An answer given after the session began to stop is not applied.
|
|
318
|
+
if (stopping())
|
|
319
|
+
throw new StopRequested();
|
|
320
|
+
refreshPaused();
|
|
321
|
+
if (resolutions.length > 0) {
|
|
322
|
+
const left = new Set((await resolveConflicts(ctx, cwd, client, resolutions, options.homeDir)).map((c) => c.path));
|
|
323
|
+
syncer.resume(resolutions.map((r) => r.path).filter((path) => !left.has(path)));
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
/** The status line names the skipped files after every sync that leaves some. */
|
|
328
|
+
function reportPaused() {
|
|
329
|
+
if (state.paused.length > 0)
|
|
330
|
+
emit({ type: 'paused', paths: [...state.paused] });
|
|
331
|
+
}
|
|
176
332
|
/**
|
|
177
333
|
* Re-read the manifest so templates added, renamed or removed during
|
|
178
334
|
* the session show up in the switcher. A broken manifest surfaces in
|
|
@@ -215,7 +371,8 @@ export async function startDevSession(templateName, options = {}) {
|
|
|
215
371
|
}
|
|
216
372
|
function draftPayload(entry, data, locale) {
|
|
217
373
|
return {
|
|
218
|
-
project
|
|
374
|
+
// The verified project sync writes to, not the manifest re-read on each batch.
|
|
375
|
+
project: ctx.projectSlug,
|
|
219
376
|
template: entry.name,
|
|
220
377
|
data,
|
|
221
378
|
format: entry.format,
|
|
@@ -263,8 +420,17 @@ export async function startDevSession(templateName, options = {}) {
|
|
|
263
420
|
try {
|
|
264
421
|
setStatus('syncing');
|
|
265
422
|
emit({ type: 'syncing' });
|
|
266
|
-
const outcome = await syncer.sync();
|
|
423
|
+
const outcome = await syncResolving(() => syncer.sync());
|
|
267
424
|
syncError = null;
|
|
425
|
+
reportPaused();
|
|
426
|
+
if (!preview) {
|
|
427
|
+
if (outcome.changed) {
|
|
428
|
+
emit({ type: 'synced', message: `${outcome.changedPaths.length} file(s)` });
|
|
429
|
+
}
|
|
430
|
+
state.error = null;
|
|
431
|
+
setStatus('idle');
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
268
434
|
if (!(await refreshManifest())) {
|
|
269
435
|
setStatus('idle');
|
|
270
436
|
return;
|
|
@@ -283,8 +449,19 @@ export async function startDevSession(templateName, options = {}) {
|
|
|
283
449
|
await render();
|
|
284
450
|
}
|
|
285
451
|
catch (err) {
|
|
452
|
+
if (err instanceof StopRequested) {
|
|
453
|
+
// Ctrl-C at the prompt never reaches the SIGINT handler: stop here.
|
|
454
|
+
void close();
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
286
457
|
if (options.signal?.aborted)
|
|
287
458
|
return;
|
|
459
|
+
if (err instanceof UnresolvedConflictsError) {
|
|
460
|
+
// Nobody can answer: stop, and let the caller print the commands.
|
|
461
|
+
stopError = err;
|
|
462
|
+
void close();
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
288
465
|
syncError = state.error = toDevError('sync', err);
|
|
289
466
|
emit({ type: 'error', message: state.error.message });
|
|
290
467
|
setStatus('idle');
|
|
@@ -293,12 +470,12 @@ export async function startDevSession(templateName, options = {}) {
|
|
|
293
470
|
async function handleRequest(request) {
|
|
294
471
|
const { method, pathname, query, res } = request;
|
|
295
472
|
if (method === 'GET' && (pathname === '/' || pathname === '/index.html')) {
|
|
296
|
-
sendHtml(res, DEV_PAGE_HTML);
|
|
473
|
+
sendHtml(res, DEV_PAGE_HTML, DEV_PAGE_SECURITY_HEADERS);
|
|
297
474
|
return;
|
|
298
475
|
}
|
|
299
476
|
if (method === 'GET' && pathname === '/pdf') {
|
|
300
477
|
if (!lastGoodPdf) {
|
|
301
|
-
sendHtml(res, DEV_PLACEHOLDER_HTML);
|
|
478
|
+
sendHtml(res, DEV_PLACEHOLDER_HTML, DEV_PLACEHOLDER_SECURITY_HEADERS);
|
|
302
479
|
return;
|
|
303
480
|
}
|
|
304
481
|
res.writeHead(200, {
|
|
@@ -306,6 +483,7 @@ export async function startDevSession(templateName, options = {}) {
|
|
|
306
483
|
'Content-Length': String(lastGoodPdf.buffer.byteLength),
|
|
307
484
|
'Content-Disposition': `inline; filename="${encodeURIComponent(lastGoodPdf.template)}.pdf"`,
|
|
308
485
|
'Cache-Control': 'no-store',
|
|
486
|
+
...securityHeaders({ frameable: true }),
|
|
309
487
|
});
|
|
310
488
|
res.end(lastGoodPdf.buffer);
|
|
311
489
|
return;
|
|
@@ -317,9 +495,11 @@ export async function startDevSession(templateName, options = {}) {
|
|
|
317
495
|
if (method === 'POST' && pathname === '/api/select') {
|
|
318
496
|
const requested = query.get('template');
|
|
319
497
|
if (!requested || !templates.includes(requested)) {
|
|
498
|
+
// The name comes from the request, which may not be the dev
|
|
499
|
+
// page's own: never echo it into the overlay.
|
|
320
500
|
state.error = {
|
|
321
501
|
stage: 'project',
|
|
322
|
-
message:
|
|
502
|
+
message: 'Unknown template: pick one listed in poli-page.json.',
|
|
323
503
|
};
|
|
324
504
|
sendJson(res, 404, state);
|
|
325
505
|
broadcast();
|
|
@@ -347,17 +527,19 @@ export async function startDevSession(templateName, options = {}) {
|
|
|
347
527
|
// The server comes up before the first render so the URL is printable
|
|
348
528
|
// (and the browser openable) immediately: the page shows its
|
|
349
529
|
// placeholder and follows the first sync/render live over SSE.
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
530
|
+
if (preview) {
|
|
531
|
+
server = await startDevServer({
|
|
532
|
+
port: options.port ?? DEFAULT_DEV_PORT,
|
|
533
|
+
maxPortAttempts: options.maxPortAttempts,
|
|
534
|
+
handleRequest,
|
|
535
|
+
});
|
|
536
|
+
}
|
|
355
537
|
emit({
|
|
356
538
|
type: 'ready',
|
|
357
|
-
message: server.url,
|
|
358
|
-
...(state.activeTemplate ? { template: state.activeTemplate } : {}),
|
|
539
|
+
...(server ? { message: server.url } : {}),
|
|
540
|
+
...(preview && state.activeTemplate ? { template: state.activeTemplate } : {}),
|
|
359
541
|
});
|
|
360
|
-
if (options.open !== false) {
|
|
542
|
+
if (server && options.open !== false) {
|
|
361
543
|
const opener = options.openFn ?? defaultOpen;
|
|
362
544
|
try {
|
|
363
545
|
await opener(server.url);
|
|
@@ -367,52 +549,77 @@ export async function startDevSession(templateName, options = {}) {
|
|
|
367
549
|
// is already printed, so this is not worth failing over.
|
|
368
550
|
}
|
|
369
551
|
}
|
|
370
|
-
//
|
|
371
|
-
// the
|
|
372
|
-
//
|
|
373
|
-
await syncer.prime();
|
|
552
|
+
// Reconcile a folder with no bases, then send the files changed since
|
|
553
|
+
// the last sync (while `poli dev` was stopped too), so the first render
|
|
554
|
+
// reflects the local files without overwriting anyone's change.
|
|
374
555
|
try {
|
|
375
556
|
setStatus('syncing');
|
|
376
557
|
emit({ type: 'syncing' });
|
|
377
|
-
await syncer.
|
|
558
|
+
const outcome = await syncResolving(() => syncer.start());
|
|
559
|
+
reportPaused();
|
|
560
|
+
if (!preview && outcome.changed) {
|
|
561
|
+
emit({ type: 'synced', message: `${outcome.changedPaths.length} file(s)` });
|
|
562
|
+
}
|
|
378
563
|
}
|
|
379
564
|
catch (err) {
|
|
380
|
-
|
|
381
|
-
|
|
565
|
+
if (err instanceof StopRequested) {
|
|
566
|
+
stopRequested = true;
|
|
567
|
+
}
|
|
568
|
+
else if (err instanceof UnresolvedConflictsError) {
|
|
569
|
+
// Nobody can answer: nothing was rendered, nothing is watched yet.
|
|
570
|
+
stopped = true;
|
|
571
|
+
await server?.close();
|
|
572
|
+
emit({ type: 'stopped' });
|
|
573
|
+
throw err;
|
|
574
|
+
}
|
|
575
|
+
else {
|
|
576
|
+
syncError = state.error = toDevError('sync', err);
|
|
577
|
+
emit({ type: 'error', message: state.error.message });
|
|
578
|
+
}
|
|
382
579
|
}
|
|
383
580
|
state.status = 'idle';
|
|
384
|
-
if (!stopped) {
|
|
581
|
+
if (preview && !stopped && !stopRequested) {
|
|
385
582
|
await render();
|
|
386
583
|
}
|
|
387
584
|
// Only now start watching: a batch arriving mid-bootstrap would race
|
|
388
|
-
// the
|
|
389
|
-
const watcher =
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
// the
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
585
|
+
// the start-up sync.
|
|
586
|
+
const watcher = stopRequested
|
|
587
|
+
? null
|
|
588
|
+
: (options.watcherFactory ?? createFileWatcher)({
|
|
589
|
+
cwd,
|
|
590
|
+
debounceMs: options.debounceMs ?? DEFAULT_DEBOUNCE_MS,
|
|
591
|
+
// The override is outside the file set, yet a save must re-render.
|
|
592
|
+
extraPaths: preview && options.data ? [resolve(cwd, options.data)] : [],
|
|
593
|
+
onBatch: () => {
|
|
594
|
+
// Chained rather than fire-and-forget so `close()` can await
|
|
595
|
+
// the batch already in flight. `onBatch` handles its own
|
|
596
|
+
// failures; the catch only keeps a surprise rejection from
|
|
597
|
+
// wedging the chain (and with it, `close()`).
|
|
598
|
+
inflightBatch = inflightBatch.then(onBatch).catch(() => { });
|
|
599
|
+
},
|
|
600
|
+
});
|
|
400
601
|
const close = async () => {
|
|
401
602
|
if (closing)
|
|
402
603
|
return closing;
|
|
403
604
|
stopped = true;
|
|
605
|
+
// A pending conflict prompt would hold the batch, and with it the close.
|
|
606
|
+
prompts.abort();
|
|
404
607
|
closing = (async () => {
|
|
405
608
|
// No new batches once the watcher is down…
|
|
406
|
-
await watcher
|
|
609
|
+
await watcher?.close();
|
|
407
610
|
// …and the one already past the `stopped` check finishes
|
|
408
611
|
// before the SSE responses and the port go away.
|
|
409
612
|
await inflightBatch;
|
|
410
613
|
await server?.close();
|
|
411
614
|
emit({ type: 'stopped' });
|
|
615
|
+
settleDone(stopError);
|
|
412
616
|
})();
|
|
413
617
|
return closing;
|
|
414
618
|
};
|
|
415
|
-
if (
|
|
619
|
+
if (stopRequested) {
|
|
620
|
+
await close();
|
|
621
|
+
}
|
|
622
|
+
else if (options.signal) {
|
|
416
623
|
if (options.signal.aborted) {
|
|
417
624
|
await close();
|
|
418
625
|
}
|
|
@@ -421,16 +628,19 @@ export async function startDevSession(templateName, options = {}) {
|
|
|
421
628
|
}
|
|
422
629
|
}
|
|
423
630
|
return {
|
|
424
|
-
url: server
|
|
425
|
-
port: server
|
|
426
|
-
getState: () => ({ ...state, templates: [...state.templates] }),
|
|
631
|
+
url: server?.url ?? null,
|
|
632
|
+
port: server?.port ?? null,
|
|
633
|
+
getState: () => ({ ...state, templates: [...state.templates], paused: [...state.paused] }),
|
|
427
634
|
close,
|
|
635
|
+
done,
|
|
428
636
|
};
|
|
429
637
|
}
|
|
430
638
|
/** Start the session and stay up until the abort signal fires. */
|
|
431
639
|
export async function executeDev(templateName, options = {}) {
|
|
432
640
|
const session = await startDevSession(templateName, options);
|
|
433
641
|
await new Promise((done) => {
|
|
642
|
+
// The session stopped on its own (an unresolved conflict).
|
|
643
|
+
session.done.then(done, done);
|
|
434
644
|
// No signal → the server runs until the process itself is killed.
|
|
435
645
|
// The CLI action always supplies one (SIGINT / SIGTERM).
|
|
436
646
|
if (!options.signal)
|
|
@@ -442,14 +652,17 @@ export async function executeDev(templateName, options = {}) {
|
|
|
442
652
|
options.signal.addEventListener('abort', () => done(), { once: true });
|
|
443
653
|
});
|
|
444
654
|
await session.close();
|
|
655
|
+
// Rejects with what stopped the session, if anything did.
|
|
656
|
+
await session.done;
|
|
445
657
|
}
|
|
446
658
|
function templateNamesOf(manifest) {
|
|
447
659
|
return (manifest.templates ?? []).map((entry) => entry.name);
|
|
448
660
|
}
|
|
449
|
-
function sendHtml(res, html) {
|
|
661
|
+
function sendHtml(res, html, headers) {
|
|
450
662
|
res.writeHead(200, {
|
|
451
663
|
'Content-Type': 'text/html; charset=utf-8',
|
|
452
664
|
'Cache-Control': 'no-store',
|
|
665
|
+
...headers,
|
|
453
666
|
});
|
|
454
667
|
res.end(html);
|
|
455
668
|
}
|
|
@@ -473,6 +686,39 @@ export function parsePort(raw) {
|
|
|
473
686
|
}
|
|
474
687
|
return port;
|
|
475
688
|
}
|
|
689
|
+
/**
|
|
690
|
+
* `--no-preview` starts no preview, so the flags that configure one are
|
|
691
|
+
* mistakes: refused (exit 2) rather than silently ignored.
|
|
692
|
+
*/
|
|
693
|
+
export function checkNoPreviewFlags(template, opts) {
|
|
694
|
+
const named = [];
|
|
695
|
+
if (template !== undefined)
|
|
696
|
+
named.push('a template');
|
|
697
|
+
if (opts.port !== undefined)
|
|
698
|
+
named.push('--port');
|
|
699
|
+
if (opts.data !== undefined)
|
|
700
|
+
named.push('--data');
|
|
701
|
+
if (opts.open === false)
|
|
702
|
+
named.push('--no-open');
|
|
703
|
+
if (named.length === 0)
|
|
704
|
+
return;
|
|
705
|
+
throw new UsageError(`--no-preview starts no preview: ${named.join(', ')} only configure${named.length === 1 ? 's' : ''} one. Drop ${named.length === 1 ? 'it' : 'them'}, or drop --no-preview.`);
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* `poli watch` was removed, not aliased: `poli dev --no-preview` is its
|
|
709
|
+
* loop. Hidden from the help, it only says so and exits 2, whatever it is
|
|
710
|
+
* given, so a script written for 0.11 fails with the replacement named.
|
|
711
|
+
*/
|
|
712
|
+
export function registerRemovedWatchCommand(program) {
|
|
713
|
+
program
|
|
714
|
+
.command('watch', { hidden: true })
|
|
715
|
+
.allowUnknownOption()
|
|
716
|
+
.allowExcessArguments()
|
|
717
|
+
.action(() => {
|
|
718
|
+
console.error('poli watch was removed: use poli dev --no-preview');
|
|
719
|
+
process.exitCode = ExitCode.INVALID_USAGE;
|
|
720
|
+
});
|
|
721
|
+
}
|
|
476
722
|
export function registerDevCommand(program) {
|
|
477
723
|
program
|
|
478
724
|
.command('dev')
|
|
@@ -481,15 +727,26 @@ export function registerDevCommand(program) {
|
|
|
481
727
|
.option('-p, --port <n>', `Port to listen on (default: ${DEFAULT_DEV_PORT})`)
|
|
482
728
|
.option('-d, --data <path>', 'JSON data file (overrides the template mock)')
|
|
483
729
|
.option('--no-open', 'Do not open the browser on start')
|
|
730
|
+
.option('--no-preview', 'Only sync each save to the cloud draft: no server, no browser, no render')
|
|
484
731
|
.action(async (template, opts) => {
|
|
485
732
|
const { default: chalk } = await import('chalk');
|
|
486
733
|
const controller = new AbortController();
|
|
487
734
|
const stop = () => controller.abort();
|
|
488
735
|
process.on('SIGINT', stop);
|
|
489
736
|
process.on('SIGTERM', stop);
|
|
737
|
+
const preview = opts.preview !== false;
|
|
738
|
+
// Piped (concurrently, a Procfile) nobody can answer: refuse instead.
|
|
739
|
+
const interactive = Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
740
|
+
let stoppedSeen = false;
|
|
490
741
|
try {
|
|
742
|
+
if (!preview)
|
|
743
|
+
checkNoPreviewFlags(template, opts);
|
|
491
744
|
await executeDev(template, {
|
|
745
|
+
preview,
|
|
492
746
|
signal: controller.signal,
|
|
747
|
+
confirmLink: interactive ? confirmLinkPrompt : undefined,
|
|
748
|
+
// Without a terminal a conflict stops the session with the `poli sync` commands.
|
|
749
|
+
resolveConflict: interactive ? conflictPrompt : undefined,
|
|
493
750
|
port: opts.port ? parsePort(opts.port) : undefined,
|
|
494
751
|
data: opts.data,
|
|
495
752
|
open: opts.open !== false,
|
|
@@ -500,13 +757,25 @@ export function registerDevCommand(program) {
|
|
|
500
757
|
const ts = new Date().toLocaleTimeString();
|
|
501
758
|
switch (event.type) {
|
|
502
759
|
case 'ready':
|
|
503
|
-
|
|
504
|
-
(
|
|
505
|
-
|
|
760
|
+
if (event.message) {
|
|
761
|
+
console.log(chalk.cyan(`▸ poli dev — ${event.message}`) +
|
|
762
|
+
(event.template ? chalk.dim(` (${event.template})`) : ''));
|
|
763
|
+
console.log(chalk.dim(' Save a file to re-render. Ctrl-C to stop.'));
|
|
764
|
+
}
|
|
765
|
+
else {
|
|
766
|
+
console.log(chalk.cyan('▸ poli dev - syncing to the cloud draft'));
|
|
767
|
+
console.log(chalk.dim(' Save a file to sync it. Ctrl-C to stop.'));
|
|
768
|
+
}
|
|
506
769
|
break;
|
|
507
770
|
case 'syncing':
|
|
508
771
|
console.log(chalk.dim(`[${ts}] syncing...`));
|
|
509
772
|
break;
|
|
773
|
+
case 'synced':
|
|
774
|
+
console.log(chalk.green(`[${ts}] ✓ synced ${event.message ?? ''}`));
|
|
775
|
+
break;
|
|
776
|
+
case 'paused':
|
|
777
|
+
console.log(chalk.yellow(`[${ts}] ⏸ not synced, conflict skipped: ${(event.paths ?? []).map(oneLine).join(', ')}`));
|
|
778
|
+
break;
|
|
510
779
|
case 'rendered':
|
|
511
780
|
console.log(chalk.green(`[${ts}] ✓ rendered ${event.template ?? ''}`));
|
|
512
781
|
break;
|
|
@@ -514,7 +783,8 @@ export function registerDevCommand(program) {
|
|
|
514
783
|
console.error(chalk.red(`[${ts}] ✗ ${event.message ?? 'error'}`));
|
|
515
784
|
break;
|
|
516
785
|
case 'stopped':
|
|
517
|
-
|
|
786
|
+
// Printed last, after any error that stopped it.
|
|
787
|
+
stoppedSeen = true;
|
|
518
788
|
break;
|
|
519
789
|
}
|
|
520
790
|
},
|
|
@@ -529,6 +799,8 @@ export function registerDevCommand(program) {
|
|
|
529
799
|
process.removeListener('SIGINT', stop);
|
|
530
800
|
process.removeListener('SIGTERM', stop);
|
|
531
801
|
}
|
|
802
|
+
if (stoppedSeen)
|
|
803
|
+
console.log(chalk.dim('Dev server stopped.'));
|
|
532
804
|
});
|
|
533
805
|
}
|
|
534
806
|
//# sourceMappingURL=dev.js.map
|