@astryxdesign/cli 0.7.14 → 0.7.15-canary.g79b95eb
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/api/doctor/_adapter.d.mts +1 -0
- package/api/doctor/_adapter.mjs +8 -0
- package/api/doctor/_authoring-audit.d.mts +42 -0
- package/api/doctor/_authoring-audit.mjs +106 -0
- package/api/doctor/doctor.d.mts +7 -0
- package/api/doctor/doctor.mjs +24 -39
- package/api/search/_adapter.mjs +124 -3
- package/api/search/_candidate-score.mjs +63 -9
- package/api/search/_entries.d.mts +10 -0
- package/api/search/_entries.mjs +68 -1
- package/api/search/_gather-templates.d.mts +11 -0
- package/api/search/_gather-templates.mjs +22 -2
- package/api/search/_words.mjs +26 -6
- package/api/swizzle/copy/copy.mjs +1 -1
- package/api/theme/_adapter.mjs +20 -9
- package/api/theme/build/build.d.mts +15 -1
- package/api/theme/build/build.mjs +132 -32
- package/api/theme/targets/targets.d.mts +8 -0
- package/api/theme/targets/targets.mjs +14 -1
- package/api/theme/themeBuild.doc.mjs +6 -3
- package/api/theme/themeEject.doc.mjs +3 -3
- package/api/theme/themeTargets.doc.mjs +1 -1
- package/api/theme-edit/_adapter.d.mts +46 -3
- package/api/theme-edit/_adapter.mjs +372 -109
- package/api/theme-edit/_check.d.mts +6 -3
- package/api/theme-edit/_check.mjs +665 -87
- package/api/theme-edit/_color.mjs +4 -2
- package/api/theme-edit/_compiler.d.mts +2 -1
- package/api/theme-edit/_compiler.mjs +27 -13
- package/api/theme-edit/_context.d.mts +2 -1
- package/api/theme-edit/_context.mjs +47 -8
- package/api/theme-edit/_edit.d.mts +9 -3
- package/api/theme-edit/_edit.mjs +484 -66
- package/api/theme-edit/_how-commands.d.mts +33 -7
- package/api/theme-edit/_how-commands.mjs +57 -45
- package/api/theme-edit/_how-question.d.mts +4 -0
- package/api/theme-edit/_how-question.mjs +1004 -223
- package/api/theme-edit/_how.mjs +96 -9
- package/api/theme-edit/_palette.d.mts +38 -13
- package/api/theme-edit/_palette.mjs +620 -223
- package/api/theme-edit/_parts.mjs +63 -7
- package/api/theme-edit/_png.d.mts +4 -4
- package/api/theme-edit/_png.mjs +4 -2
- package/api/theme-edit/_preview-app.d.mts +6 -0
- package/api/theme-edit/_preview-app.mjs +1194 -207
- package/api/theme-edit/_preview.mjs +2 -2
- package/api/theme-edit/_project.d.mts +12 -1
- package/api/theme-edit/_project.mjs +37 -6
- package/api/theme-edit/_settings.d.mts +104 -33
- package/api/theme-edit/_settings.mjs +818 -118
- package/api/theme-edit/_tables.d.mts +7 -0
- package/api/theme-edit/_tables.mjs +61 -0
- package/api/theme-edit/theme-edit.type.d.mts +11 -1
- package/api/theme-edit/theme-edit.type.mjs +4 -3
- package/api/theme-edit/themeEditHow.doc.mjs +6 -0
- package/api/theme-edit/themeEditPreviewAdd.doc.mjs +1 -1
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +1 -1
- package/api/theme-edit/themeEditPreviewList.doc.mjs +1 -1
- package/api/theme-edit/themeEditPreviewRemove.doc.mjs +5 -1
- package/api/theme-edit/themeEditUndo.doc.mjs +1 -1
- package/api/upgrade/_adapter.d.mts +1 -0
- package/assets/codemods/run-codemod.mjs +61 -0
- package/assets/codemods/runner.mjs +5 -4
- package/assets/codemods/transforms/v0.0.14/rename-status-variants.mjs +3 -0
- package/assets/codemods/transforms/v0.1.8/rename-avatar-size-scale.mjs +5 -1
- package/assets/codemods/transforms/v0.6.4/migrate-native-picker-to-presentation.mjs +3 -0
- package/assets/codemods/transforms/v0.7.0/migrate-deprecated-theme-surface.mjs +4 -0
- package/assets/codemods/transforms/v0.7.0/rename-empty-search-results-text.mjs +17 -12
- package/assets/codemods/transforms/v0.7.2/migrate-list-edge-compensation-to-edge.mjs +3 -0
- package/assets/codemods/transforms/v0.7.5/migrate-codeblock-tokenizer-to-language.mjs +3 -0
- package/assets/codemods/transforms/v0.7.8/promote-tokenizer-presentation.mjs +3 -0
- package/assets/docs/tree/calendars.doc.mjs +1 -3
- package/assets/docs.generated.json +2 -1
- package/assets/self-audit.generated.json +1 -0
- package/assets/templates/blocks/components/Theme/useThemeNameHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Theme/useThemeNameHookUsage.tsx +31 -0
- package/assets/templates/components.generated.json +1 -1
- package/clients/cli/bin/astryx.mjs +19 -15
- package/clients/cli/build-id.generated.json +1 -0
- package/clients/cli/commands/ensure-core-built.mjs +12 -5
- package/clients/cli/commands/experimental/theme.mjs +178 -78
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +1 -2
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-eject.doc.mjs +3 -3
- package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-list.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-remove.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-template.doc.mjs +4 -3
- package/clients/cli/commands/manifest.mjs +5 -2
- package/clients/cli/commands/theme.mjs +118 -22
- package/clients/cli/index.mjs +9 -3
- package/clients/cli/lib/manifest.mjs +37 -0
- package/clients/cli/lib/resolve-theme.mjs +2 -1
- package/clients/cli/manifest.generated.json +1 -0
- package/foundation/discovery/module-exports.mjs +16 -2
- package/foundation/discovery/stored-self-audit.d.mts +31 -0
- package/foundation/discovery/stored-self-audit.mjs +119 -0
- package/foundation/discovery/template-adapter.d.mts +48 -2
- package/foundation/discovery/template-adapter.mjs +51 -19
- package/foundation/discovery/template-needs.d.mts +6 -0
- package/foundation/discovery/template-needs.mjs +20 -1
- package/foundation/discovery/template-source-tree.d.mts +7 -0
- package/foundation/discovery/template-source-tree.mjs +15 -0
- package/foundation/discovery/theme-discovery.d.mts +2 -1
- package/foundation/discovery/theme-discovery.mjs +2 -8
- package/foundation/discovery/theme-module-extensions.d.mts +18 -0
- package/foundation/discovery/theme-module-extensions.mjs +30 -0
- package/foundation/doc-compiler/import.mjs +12 -11
- package/foundation/doc-compiler/stored-docs.d.mts +18 -12
- package/foundation/doc-compiler/stored-docs.mjs +52 -19
- package/foundation/env/private-dir.d.mts +17 -0
- package/foundation/env/private-dir.mjs +42 -0
- package/foundation/fs/gather-cache.d.mts +108 -0
- package/foundation/fs/gather-cache.mjs +716 -0
- package/foundation/fs/module-loader.mjs +7 -1
- package/foundation/fs/read-record.d.mts +31 -1
- package/foundation/fs/read-record.mjs +93 -6
- package/foundation/integrations/validate-contributions.mjs +24 -11
- package/foundation/release/cache.d.mts +11 -2
- package/foundation/release/cache.mjs +59 -10
- package/foundation/release/folder-lock.d.mts +21 -0
- package/foundation/release/folder-lock.mjs +171 -0
- package/package.json +8 -6
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
import {createHash} from 'node:crypto';
|
|
14
|
+
import {parse as parseModule} from '@babel/parser';
|
|
14
15
|
import * as fs from 'node:fs';
|
|
15
16
|
import * as path from 'node:path';
|
|
16
17
|
import {fileURLToPath} from 'node:url';
|
|
@@ -20,15 +21,18 @@ import {
|
|
|
20
21
|
assertWithin,
|
|
21
22
|
PathSafetyError,
|
|
22
23
|
} from '../../foundation/fs/path-safety.mjs';
|
|
23
|
-
import {baseResolved, openProject} from './_project.mjs';
|
|
24
|
+
import {baseResolved, installLine, openProject} from './_project.mjs';
|
|
24
25
|
import {
|
|
26
|
+
buildTheme,
|
|
27
|
+
editedThemes,
|
|
28
|
+
fontPackages,
|
|
25
29
|
loadTheme,
|
|
30
|
+
modesOf,
|
|
26
31
|
nearest,
|
|
27
32
|
refuse,
|
|
28
33
|
shown,
|
|
34
|
+
staleBuild,
|
|
29
35
|
themeFiles,
|
|
30
|
-
buildTheme,
|
|
31
|
-
fontPackages,
|
|
32
36
|
} from './_settings.mjs';
|
|
33
37
|
import {
|
|
34
38
|
colorsPageSource,
|
|
@@ -53,6 +57,7 @@ import {
|
|
|
53
57
|
import {detectStylingSystem} from '../../foundation/agent-docs/agent-docs.mjs';
|
|
54
58
|
import {requireProject, context} from './_context.mjs';
|
|
55
59
|
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
|
|
60
|
+
import {quoteCommandArgument} from '../../foundation/env/package-manager.mjs';
|
|
56
61
|
|
|
57
62
|
const E = ERROR_CODES;
|
|
58
63
|
|
|
@@ -85,16 +90,6 @@ const MAX_WORDS = 40;
|
|
|
85
90
|
const shownDir = (/** @type {string} */ cwd, /** @type {string} */ dir) =>
|
|
86
91
|
shown(cwd, dir) || '.';
|
|
87
92
|
|
|
88
|
-
/**
|
|
89
|
-
* A value as one shell word: single quotes whenever a character could mean
|
|
90
|
-
* something to a shell, so a suggested command is safe to paste.
|
|
91
|
-
* @param {unknown} v
|
|
92
|
-
*/
|
|
93
|
-
const shq = v =>
|
|
94
|
-
/^[\w./@:=+-]+$/.test(String(v))
|
|
95
|
-
? String(v)
|
|
96
|
-
: `'${String(v).replace(/'/g, `'\\''`)}'`;
|
|
97
|
-
|
|
98
93
|
/**
|
|
99
94
|
* Refuse a folder name that this platform would take literally in a way the
|
|
100
95
|
* caller did not mean: Windows separators or a drive letter on POSIX, a `~`
|
|
@@ -148,6 +143,14 @@ function reservedFolder(cwd, dir) {
|
|
|
148
143
|
return 'themes/ holds the themes, and everything in a theme folder is copied with the theme';
|
|
149
144
|
if (segs.includes('node_modules'))
|
|
150
145
|
return 'node_modules/ belongs to the package manager, which replaces it';
|
|
146
|
+
if (segs[0] === 'public')
|
|
147
|
+
return 'Next serves every file in public/ as it is, at the site root, so anyone could download it';
|
|
148
|
+
const odd = segs.find(
|
|
149
|
+
seg =>
|
|
150
|
+
/^(con|prn|aux|nul|com\d|lpt\d)(\..*)?$/i.test(seg) || /^\.+$/.test(seg),
|
|
151
|
+
);
|
|
152
|
+
if (odd)
|
|
153
|
+
return `${odd} is a name Windows does not allow for a folder, so the project could not be checked out there`;
|
|
151
154
|
if (segs[0] === 'pages' || (segs[0] === 'src' && segs[1] === 'pages'))
|
|
152
155
|
return 'Next serves every file under the Pages Router folder (pages/ or src/pages/) as a public page; this preview needs the App Router (app/ or src/app/)';
|
|
153
156
|
return null;
|
|
@@ -170,11 +173,240 @@ function unroutedSegment(app, dir) {
|
|
|
170
173
|
: null;
|
|
171
174
|
}
|
|
172
175
|
|
|
176
|
+
/**
|
|
177
|
+
* The URL a folder of the routes folder is served at, with route groups
|
|
178
|
+
* dropped, as path segments.
|
|
179
|
+
* @param {string} app
|
|
180
|
+
* @param {string} dir
|
|
181
|
+
*/
|
|
182
|
+
const urlSegments = (app, dir) =>
|
|
183
|
+
path
|
|
184
|
+
.relative(app, dir)
|
|
185
|
+
.split(path.sep)
|
|
186
|
+
.filter(seg => seg && !/^\(.*\)$/.test(seg));
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The `pageExtensions` next.config sets, read as text (best effort), or
|
|
190
|
+
* null when it sets none.
|
|
191
|
+
* @param {string} cwd
|
|
192
|
+
* @returns {string[] | null}
|
|
193
|
+
*/
|
|
194
|
+
function pageExtensions(cwd) {
|
|
195
|
+
for (const name of [
|
|
196
|
+
'next.config.mjs',
|
|
197
|
+
'next.config.js',
|
|
198
|
+
'next.config.ts',
|
|
199
|
+
'next.config.cjs',
|
|
200
|
+
]) {
|
|
201
|
+
/** @type {string} */
|
|
202
|
+
let text;
|
|
203
|
+
try {
|
|
204
|
+
text = fs.readFileSync(path.join(cwd, name), 'utf8');
|
|
205
|
+
} catch {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const m = /pageExtensions\s*:\s*\[([^\]]*)\]/.exec(text);
|
|
209
|
+
return m ? [...m[1].matchAll(/['"`]([^'"`]+)['"`]/g)].map(x => x[1]) : null;
|
|
210
|
+
}
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* A page of the app that Next serves at the same URL as `dir`, which would
|
|
216
|
+
* stop the whole app from building, or null.
|
|
217
|
+
* @param {string} cwd
|
|
218
|
+
* @param {string} app the routes folder
|
|
219
|
+
* @param {string} dir
|
|
220
|
+
*/
|
|
221
|
+
function sameUrlPage(cwd, app, dir) {
|
|
222
|
+
const url = urlSegments(app, dir).join('/');
|
|
223
|
+
/** @param {string} folder @returns {string | null} */
|
|
224
|
+
const walk = folder => {
|
|
225
|
+
/** @type {fs.Dirent[]} */
|
|
226
|
+
let entries;
|
|
227
|
+
try {
|
|
228
|
+
entries = fs.readdirSync(folder, {withFileTypes: true});
|
|
229
|
+
} catch {
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
for (const e of entries) {
|
|
233
|
+
const full = path.join(folder, e.name);
|
|
234
|
+
if (e.isDirectory()) {
|
|
235
|
+
if (/^[_@]/.test(e.name) || e.name === 'node_modules' || full === dir)
|
|
236
|
+
continue;
|
|
237
|
+
const hit = walk(full);
|
|
238
|
+
if (hit) return hit;
|
|
239
|
+
} else if (
|
|
240
|
+
folder !== dir &&
|
|
241
|
+
/^page\.(tsx|ts|jsx|js|mdx)$/.test(e.name) &&
|
|
242
|
+
urlSegments(app, folder).join('/') === url
|
|
243
|
+
)
|
|
244
|
+
return full;
|
|
245
|
+
}
|
|
246
|
+
return null;
|
|
247
|
+
};
|
|
248
|
+
const inApp = walk(app);
|
|
249
|
+
if (inApp) return inApp;
|
|
250
|
+
for (const pages of [path.join(cwd, 'pages'), path.join(cwd, 'src', 'pages')])
|
|
251
|
+
for (const ext of ['tsx', 'ts', 'jsx', 'js']) {
|
|
252
|
+
for (const f of [`${url}.${ext}`, `${url}/index.${ext}`]) {
|
|
253
|
+
const full = path.join(pages, f);
|
|
254
|
+
if (url && fs.existsSync(full)) return full;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* The folders that hold a root layout when the routes folder has none of
|
|
262
|
+
* its own: each route group with a layout (`(shop)/`, `(marketing)/`), or
|
|
263
|
+
* a `[locale]/` in an app with the locale in every URL. Empty when the
|
|
264
|
+
* routes folder has its own root layout.
|
|
265
|
+
* @param {string} app
|
|
266
|
+
* @returns {string[]}
|
|
267
|
+
*/
|
|
268
|
+
function nestedRootLayouts(app) {
|
|
269
|
+
const LAYOUT = /^layout\.(tsx|ts|jsx|js)$/;
|
|
270
|
+
if (fs.readdirSync(app).some(f => LAYOUT.test(f))) return [];
|
|
271
|
+
return fs
|
|
272
|
+
.readdirSync(app, {withFileTypes: true})
|
|
273
|
+
.filter(
|
|
274
|
+
e =>
|
|
275
|
+
e.isDirectory() &&
|
|
276
|
+
fs.readdirSync(path.join(app, e.name)).some(f => LAYOUT.test(f)),
|
|
277
|
+
)
|
|
278
|
+
.map(e => path.join(app, e.name))
|
|
279
|
+
.sort();
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Refuse a folder where the preview would not be served as reported, or
|
|
284
|
+
* would break the app: one that holds the app itself, a reserved or
|
|
285
|
+
* unrouted folder, src/app/ while app/ is what Next serves, an app whose
|
|
286
|
+
* next.config names other page extensions, a folder outside the folder of
|
|
287
|
+
* the app's root layout, and a URL another page already has.
|
|
288
|
+
* @param {string} cwd
|
|
289
|
+
* @param {string} dir
|
|
290
|
+
* @param {string} where how the folder was given, for messages
|
|
291
|
+
* @param {Retry} retry
|
|
292
|
+
*/
|
|
293
|
+
function checkPlacement(cwd, dir, where, retry) {
|
|
294
|
+
const served = [path.join(cwd, 'app'), path.join(cwd, 'src', 'app')].find(
|
|
295
|
+
r => fs.existsSync(r) && fs.statSync(r).isDirectory(),
|
|
296
|
+
);
|
|
297
|
+
const fallback = retry(
|
|
298
|
+
served
|
|
299
|
+
? shown(cwd, path.join(served, 'theme-preview'))
|
|
300
|
+
: 'app/theme-preview',
|
|
301
|
+
);
|
|
302
|
+
if (fs.existsSync(dir) && !fs.statSync(dir).isDirectory())
|
|
303
|
+
refuse(
|
|
304
|
+
E.ERR_INVALID_ARGUMENT,
|
|
305
|
+
`${where} is a file; the preview goes in a folder of its own.`,
|
|
306
|
+
{run: [retry(null)]},
|
|
307
|
+
);
|
|
308
|
+
if (holdsApp(cwd, dir)) {
|
|
309
|
+
const record = path.join(dir, PREVIEW_DATA);
|
|
310
|
+
/** @type {string | null} */
|
|
311
|
+
let damaged = null;
|
|
312
|
+
if (fs.existsSync(record))
|
|
313
|
+
try {
|
|
314
|
+
readPreview(cwd, dir);
|
|
315
|
+
} catch {
|
|
316
|
+
damaged = shown(cwd, record);
|
|
317
|
+
}
|
|
318
|
+
refuse(
|
|
319
|
+
E.ERR_INVALID_ARGUMENT,
|
|
320
|
+
damaged
|
|
321
|
+
? `${where} holds the app itself, and an older, damaged ${PREVIEW_DATA}.`
|
|
322
|
+
: fs.existsSync(record)
|
|
323
|
+
? `${where} holds an older theme preview, in a folder that holds the app itself; move its files into a folder of their own, then add the preview there.`
|
|
324
|
+
: `${where} is no place for the preview: the folder holds the app itself (its root, src/, a routes folder or a package.json), and the app does not serve a preview there.`,
|
|
325
|
+
damaged
|
|
326
|
+
? {
|
|
327
|
+
fix: `give ${damaged} another name (for example ${PREVIEW_DATA}.broken), then run ${PREVIEW} add`,
|
|
328
|
+
}
|
|
329
|
+
: {
|
|
330
|
+
run: fs.existsSync(record)
|
|
331
|
+
? [
|
|
332
|
+
`${PREVIEW} remove --path ${quoteCommandArgument(shownDir(cwd, dir))}`,
|
|
333
|
+
]
|
|
334
|
+
: [fallback],
|
|
335
|
+
},
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
const app = [path.join(cwd, 'app'), path.join(cwd, 'src', 'app')].find(r =>
|
|
339
|
+
dir.startsWith(r + path.sep),
|
|
340
|
+
);
|
|
341
|
+
const rootLayouts = served ? nestedRootLayouts(served) : [];
|
|
342
|
+
// The root layout above the folder, when the app has several (one per
|
|
343
|
+
// route group) or one in [locale]/.
|
|
344
|
+
const rootLayout =
|
|
345
|
+
rootLayouts.find(r => dir.startsWith(r + path.sep)) ?? null;
|
|
346
|
+
// A [param] folder that holds the root layout (an app with the locale in
|
|
347
|
+
// every URL) is where its pages go.
|
|
348
|
+
const unrouted =
|
|
349
|
+
app &&
|
|
350
|
+
unroutedSegment(
|
|
351
|
+
app,
|
|
352
|
+
rootLayout && dir.startsWith(rootLayout + path.sep)
|
|
353
|
+
? path.join(app, path.relative(rootLayout, dir))
|
|
354
|
+
: dir,
|
|
355
|
+
);
|
|
356
|
+
const why = reservedFolder(cwd, dir) ?? unrouted;
|
|
357
|
+
if (why)
|
|
358
|
+
refuse(
|
|
359
|
+
E.ERR_INVALID_ARGUMENT,
|
|
360
|
+
`${where} is no place for the preview: ${why}.`,
|
|
361
|
+
{run: [fallback]},
|
|
362
|
+
);
|
|
363
|
+
if (!app) return;
|
|
364
|
+
if (served && app !== served)
|
|
365
|
+
refuse(
|
|
366
|
+
E.ERR_INVALID_ARGUMENT,
|
|
367
|
+
`${where} is in ${shown(cwd, app)}/, and Next serves ${shown(cwd, served)}/ instead when both are there.`,
|
|
368
|
+
{
|
|
369
|
+
run: fs.existsSync(path.join(dir, PREVIEW_DATA))
|
|
370
|
+
? [
|
|
371
|
+
`${PREVIEW} remove --path ${quoteCommandArgument(shownDir(cwd, dir))}`,
|
|
372
|
+
]
|
|
373
|
+
: [fallback],
|
|
374
|
+
},
|
|
375
|
+
);
|
|
376
|
+
const exts = pageExtensions(cwd);
|
|
377
|
+
if (exts && !exts.includes('tsx'))
|
|
378
|
+
refuse(
|
|
379
|
+
E.ERR_INVALID_ARGUMENT,
|
|
380
|
+
`next.config sets pageExtensions to ${exts.join(', ')}, so Next does not serve page.tsx, the route file the preview writes.`,
|
|
381
|
+
{fix: 'add "tsx" to pageExtensions to use the preview in this app'},
|
|
382
|
+
);
|
|
383
|
+
if (rootLayouts.length && !rootLayout)
|
|
384
|
+
refuse(
|
|
385
|
+
E.ERR_INVALID_ARGUMENT,
|
|
386
|
+
`${where} has no root layout above it: the app's root ${rootLayouts.length > 1 ? 'layouts are' : 'layout is'} in ${rootLayouts.map(r => `${shown(cwd, r)}/`).join(', ')}, so a page there would have no html, global CSS or providers.`,
|
|
387
|
+
{
|
|
388
|
+
run: rootLayouts.map(r =>
|
|
389
|
+
retry(shown(cwd, path.join(r, 'theme-preview'))),
|
|
390
|
+
),
|
|
391
|
+
},
|
|
392
|
+
);
|
|
393
|
+
const clash = sameUrlPage(cwd, app, dir);
|
|
394
|
+
if (clash) {
|
|
395
|
+
let free = `${dir}-2`;
|
|
396
|
+
for (let n = 3; fs.existsSync(free); n++) free = `${dir}-${n}`;
|
|
397
|
+
refuse(
|
|
398
|
+
E.ERR_FILE_EXISTS,
|
|
399
|
+
`${shown(cwd, clash)} already serves /${urlSegments(app, dir).join('/')}, so a preview in ${where} would stop the app from building.`,
|
|
400
|
+
{run: [retry(shown(cwd, free))]},
|
|
401
|
+
);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
173
405
|
/**
|
|
174
406
|
* Where the preview route lives. `--path` names it (inside the project);
|
|
175
407
|
* else an existing preview in the app's routes folder; else the one preview
|
|
176
|
-
* a theme recorded elsewhere (`retry` given); else the routes folder
|
|
177
|
-
*
|
|
408
|
+
* a theme recorded elsewhere (`retry` given); else the routes folder Next
|
|
409
|
+
* serves (`app/`, else `src/app/`).
|
|
178
410
|
* @param {string} cwd
|
|
179
411
|
* @param {string | undefined} where
|
|
180
412
|
* @param {{optional?: boolean, write?: boolean, retry?: Retry}} [opts]
|
|
@@ -186,7 +418,8 @@ function unroutedSegment(app, dir) {
|
|
|
186
418
|
function previewDir(cwd, where, opts = {}) {
|
|
187
419
|
const retry =
|
|
188
420
|
opts.retry ??
|
|
189
|
-
(p =>
|
|
421
|
+
(p =>
|
|
422
|
+
`${PREVIEW} list${p == null ? '' : ` --path ${quoteCommandArgument(p)}`}`);
|
|
190
423
|
const routes = ['app', path.join('src', 'app')]
|
|
191
424
|
.map(r => path.join(cwd, r))
|
|
192
425
|
.filter(r => fs.existsSync(r) && fs.statSync(r).isDirectory());
|
|
@@ -210,51 +443,7 @@ function previewDir(cwd, where, opts = {}) {
|
|
|
210
443
|
);
|
|
211
444
|
throw e;
|
|
212
445
|
}
|
|
213
|
-
if (opts.write) {
|
|
214
|
-
if (fs.existsSync(dir) && !fs.statSync(dir).isDirectory())
|
|
215
|
-
refuse(
|
|
216
|
-
E.ERR_INVALID_ARGUMENT,
|
|
217
|
-
`--path ${where} is a file; the preview goes in a folder of its own.`,
|
|
218
|
-
{run: [retry(null)]},
|
|
219
|
-
);
|
|
220
|
-
// The project root, src/ and the routes folder hold the app itself;
|
|
221
|
-
// nothing the preview writes there is served.
|
|
222
|
-
if (dir === cwd || dir === path.join(cwd, 'src') || routes.includes(dir))
|
|
223
|
-
refuse(
|
|
224
|
-
E.ERR_INVALID_ARGUMENT,
|
|
225
|
-
fs.existsSync(path.join(dir, PREVIEW_DATA))
|
|
226
|
-
? `--path ${where} holds an older theme preview, in a folder that holds the app itself; remove it, then add the preview in its own folder.`
|
|
227
|
-
: `--path ${where} is no place for the preview: the folder holds the app itself, and the app does not serve a preview there.`,
|
|
228
|
-
{
|
|
229
|
-
run: fs.existsSync(path.join(dir, PREVIEW_DATA))
|
|
230
|
-
? [`${PREVIEW} remove --path ${shq(shownDir(cwd, dir))}`]
|
|
231
|
-
: [
|
|
232
|
-
retry(
|
|
233
|
-
routes.length
|
|
234
|
-
? shown(cwd, path.join(routes[0], 'theme-preview'))
|
|
235
|
-
: 'theme-preview',
|
|
236
|
-
),
|
|
237
|
-
],
|
|
238
|
-
},
|
|
239
|
-
);
|
|
240
|
-
const app = appOf(dir);
|
|
241
|
-
const why =
|
|
242
|
-
reservedFolder(cwd, dir) ?? (app && unroutedSegment(app, dir));
|
|
243
|
-
if (why)
|
|
244
|
-
refuse(
|
|
245
|
-
E.ERR_INVALID_ARGUMENT,
|
|
246
|
-
`--path ${where} is no place for the preview: ${why}.`,
|
|
247
|
-
{
|
|
248
|
-
run: [
|
|
249
|
-
retry(
|
|
250
|
-
routes.length
|
|
251
|
-
? shown(cwd, path.join(routes[0], 'theme-preview'))
|
|
252
|
-
: null,
|
|
253
|
-
),
|
|
254
|
-
],
|
|
255
|
-
},
|
|
256
|
-
);
|
|
257
|
-
}
|
|
446
|
+
if (opts.write) checkPlacement(cwd, dir, `--path ${where}`, retry);
|
|
258
447
|
return {dir, app: appOf(dir)};
|
|
259
448
|
}
|
|
260
449
|
const candidates = routes.map(r => path.join(r, 'theme-preview'));
|
|
@@ -265,8 +454,11 @@ function previewDir(cwd, where, opts = {}) {
|
|
|
265
454
|
const recorded = [
|
|
266
455
|
...new Set(editedThemes(cwd).flatMap(t => recordedPreviews(cwd, t))),
|
|
267
456
|
].filter(d => fs.existsSync(path.join(d, PREVIEW_DATA)));
|
|
268
|
-
if (recorded.length === 1)
|
|
457
|
+
if (recorded.length === 1) {
|
|
458
|
+
if (opts.write)
|
|
459
|
+
checkPlacement(cwd, recorded[0], shownDir(cwd, recorded[0]), retry);
|
|
269
460
|
return {dir: recorded[0], app: appOf(recorded[0])};
|
|
461
|
+
}
|
|
270
462
|
if (recorded.length > 1)
|
|
271
463
|
refuse(
|
|
272
464
|
E.ERR_INVALID_ARGUMENT,
|
|
@@ -278,17 +470,16 @@ function previewDir(cwd, where, opts = {}) {
|
|
|
278
470
|
const older = [cwd, path.join(cwd, 'src')].find(d =>
|
|
279
471
|
fs.existsSync(path.join(d, PREVIEW_DATA)),
|
|
280
472
|
);
|
|
281
|
-
if (older
|
|
282
|
-
if (opts.write)
|
|
283
|
-
refuse(
|
|
284
|
-
E.ERR_INVALID_ARGUMENT,
|
|
285
|
-
`An older theme preview is in ${shownDir(cwd, older)}/, a folder that holds the app itself (its pages/ may be public routes of the app); remove it, then add the preview again.`,
|
|
286
|
-
{run: [`${PREVIEW} remove --path ${shq(shownDir(cwd, older))}`]},
|
|
287
|
-
);
|
|
473
|
+
if (older) {
|
|
474
|
+
if (opts.write) checkPlacement(cwd, older, shownDir(cwd, older), retry);
|
|
288
475
|
return {dir: older, app: null};
|
|
289
476
|
}
|
|
290
477
|
}
|
|
291
|
-
|
|
478
|
+
// The routes folder Next serves: app/ when both app/ and src/app/ are there.
|
|
479
|
+
const served = routes[0];
|
|
480
|
+
const nested =
|
|
481
|
+
served && !known ? (nestedRootLayouts(served)[0] ?? null) : null;
|
|
482
|
+
const dir = known ?? (served && path.join(nested ?? served, 'theme-preview'));
|
|
292
483
|
if (!dir && opts.optional) return /** @type {any} */ (null);
|
|
293
484
|
if (!dir)
|
|
294
485
|
refuse(
|
|
@@ -296,11 +487,12 @@ function previewDir(cwd, where, opts = {}) {
|
|
|
296
487
|
'No app routes folder here (app/ or src/app/), so there is no app to put the preview in.',
|
|
297
488
|
{
|
|
298
489
|
run: [
|
|
299
|
-
`${PREVIEW} add --path
|
|
300
|
-
`${PREVIEW} app
|
|
490
|
+
`${PREVIEW} add --path app/theme-preview # an app routes folder beside the app's own`,
|
|
491
|
+
`${PREVIEW} app theme-app # a new starter app with the preview in it`,
|
|
301
492
|
],
|
|
302
493
|
},
|
|
303
494
|
);
|
|
495
|
+
if (opts.write) checkPlacement(cwd, dir, `${shownDir(cwd, dir)}/`, retry);
|
|
304
496
|
return {dir, app: appOf(dir)};
|
|
305
497
|
}
|
|
306
498
|
|
|
@@ -383,7 +575,7 @@ export default function Home() {
|
|
|
383
575
|
*/
|
|
384
576
|
function planStarterApp(cwd, where, extra, dependencies, slug = null) {
|
|
385
577
|
const again = (/** @type {string | null} */ p) =>
|
|
386
|
-
`${PREVIEW} app ${
|
|
578
|
+
`${PREVIEW} app ${quoteCommandArgument(p ?? 'theme-app')}${slug == null ? '' : ` --theme ${quoteCommandArgument(slug)}`}`;
|
|
387
579
|
checkFolderName(where, 'The app folder', again);
|
|
388
580
|
/** @type {string} */
|
|
389
581
|
let root;
|
|
@@ -663,15 +855,36 @@ function previewProblem(d) {
|
|
|
663
855
|
*/
|
|
664
856
|
function damagedPreview(cwd, dir, why, pf) {
|
|
665
857
|
const folder = shownDir(cwd, dir);
|
|
858
|
+
const record = `${folder}/${PREVIEW_DATA}`;
|
|
666
859
|
refuse(
|
|
667
860
|
E.ERR_THEME_INVALID,
|
|
668
|
-
`${
|
|
861
|
+
`${record} is damaged: ${why}. It lists the preview's pages and the files it wrote, so the preview commands leave the folder alone until it is fixed.`,
|
|
669
862
|
{
|
|
670
|
-
fix:
|
|
863
|
+
fix: holdsApp(cwd, dir)
|
|
864
|
+
? `fix ${record} by hand, or give it another name (for example ${PREVIEW_DATA}.broken): the folder holds your app, so keep the folder as it is; then add the preview in its own folder with ${PREVIEW} add`
|
|
865
|
+
: `fix ${record} by hand; or give the folder a new name that starts with _ (Next does not serve such a folder, and nothing in it is lost), then run ${PREVIEW} add${pf} again`,
|
|
671
866
|
},
|
|
672
867
|
);
|
|
673
868
|
}
|
|
674
869
|
|
|
870
|
+
/**
|
|
871
|
+
* Whether a folder holds the app itself: the project root, src/, a routes
|
|
872
|
+
* folder (app/ or src/app/), or any folder with a package.json. Nothing
|
|
873
|
+
* the preview writes goes there, and nothing there is deleted on a record's
|
|
874
|
+
* word alone.
|
|
875
|
+
* @param {string} cwd
|
|
876
|
+
* @param {string} dir
|
|
877
|
+
*/
|
|
878
|
+
function holdsApp(cwd, dir) {
|
|
879
|
+
return (
|
|
880
|
+
dir === cwd ||
|
|
881
|
+
dir === path.join(cwd, 'src') ||
|
|
882
|
+
dir === path.join(cwd, 'app') ||
|
|
883
|
+
dir === path.join(cwd, 'src', 'app') ||
|
|
884
|
+
fs.existsSync(path.join(dir, 'package.json'))
|
|
885
|
+
);
|
|
886
|
+
}
|
|
887
|
+
|
|
675
888
|
/**
|
|
676
889
|
* The preview's data, null when the folder has none; a refusal when the
|
|
677
890
|
* file is there but unreadable or of the wrong shape.
|
|
@@ -684,7 +897,10 @@ function readPreview(cwd, dir, pf = '') {
|
|
|
684
897
|
/** @type {string} */
|
|
685
898
|
let text;
|
|
686
899
|
try {
|
|
687
|
-
|
|
900
|
+
// A byte-order mark (Notepad, PowerShell) is not part of the JSON.
|
|
901
|
+
text = fs
|
|
902
|
+
.readFileSync(path.join(dir, PREVIEW_DATA), 'utf8')
|
|
903
|
+
.replace(/^\uFEFF/, '');
|
|
688
904
|
} catch (e) {
|
|
689
905
|
const code = /** @type {any} */ (e)?.code;
|
|
690
906
|
if (code === 'ENOENT' || code === 'ENOTDIR') return null;
|
|
@@ -713,12 +929,251 @@ const digest = text =>
|
|
|
713
929
|
createHash('sha256').update(text).digest('hex').slice(0, 16);
|
|
714
930
|
|
|
715
931
|
/**
|
|
716
|
-
*
|
|
717
|
-
*
|
|
718
|
-
*
|
|
932
|
+
* A file's text as a formatter could have left it: white space outside
|
|
933
|
+
* strings collapsed (and dropped next to punctuation), statement
|
|
934
|
+
* semicolons and trailing commas dropped, and every string written with one
|
|
935
|
+
* quote kind. The text inside strings, comments, template literals and
|
|
936
|
+
* JSX text is kept, so a hand edit there always shows.
|
|
937
|
+
* @param {string} text
|
|
938
|
+
*/
|
|
939
|
+
function formatterNeutral(text) {
|
|
940
|
+
const PUNCT = /[{}()[\],;:<>=/.?!&|+*%^~-]/;
|
|
941
|
+
// A quote starts a string after an operator, an opening bracket or one
|
|
942
|
+
// of these words; elsewhere (JSX text such as "don't") it is plain text.
|
|
943
|
+
const STRING_AFTER =
|
|
944
|
+
/(?:^|[=({[,:;!&|?+<]|\b(?:from|import|return|case|in|of|typeof|export|default))$/;
|
|
945
|
+
let out = '';
|
|
946
|
+
let space = false;
|
|
947
|
+
/** @param {string} t */
|
|
948
|
+
const put = t => {
|
|
949
|
+
if (space && out && !PUNCT.test(out.at(-1) ?? '') && !PUNCT.test(t[0]))
|
|
950
|
+
out += ' ';
|
|
951
|
+
space = false;
|
|
952
|
+
out += t;
|
|
953
|
+
};
|
|
954
|
+
let i = 0;
|
|
955
|
+
while (i < text.length) {
|
|
956
|
+
const c = text[i];
|
|
957
|
+
if (/\s/.test(c)) {
|
|
958
|
+
space = true;
|
|
959
|
+
i++;
|
|
960
|
+
continue;
|
|
961
|
+
}
|
|
962
|
+
if (c === '/' && (text[i + 1] === '/' || text[i + 1] === '*')) {
|
|
963
|
+
const end =
|
|
964
|
+
text[i + 1] === '/'
|
|
965
|
+
? text.indexOf('\n', i) === -1
|
|
966
|
+
? text.length
|
|
967
|
+
: text.indexOf('\n', i)
|
|
968
|
+
: text.indexOf('*/', i + 2) === -1
|
|
969
|
+
? text.length
|
|
970
|
+
: text.indexOf('*/', i + 2) + 2;
|
|
971
|
+
put(text.slice(i, end).replace(/\s+/g, ' ').trim());
|
|
972
|
+
space = true;
|
|
973
|
+
i = end;
|
|
974
|
+
continue;
|
|
975
|
+
}
|
|
976
|
+
if ((c === '"' || c === "'") && STRING_AFTER.test(out.trimEnd())) {
|
|
977
|
+
let j = i + 1;
|
|
978
|
+
let body = '';
|
|
979
|
+
while (j < text.length && text[j] !== c && text[j] !== '\n') {
|
|
980
|
+
if (text[j] === '\\' && j + 1 < text.length) {
|
|
981
|
+
const n = text[j + 1];
|
|
982
|
+
body += n === '"' || n === "'" ? n : text.slice(j, j + 2);
|
|
983
|
+
j += 2;
|
|
984
|
+
} else body += text[j++];
|
|
985
|
+
}
|
|
986
|
+
if (text[j] === c) {
|
|
987
|
+
put(`'${body.replace(/'/g, "\\'")}'`);
|
|
988
|
+
i = j + 1;
|
|
989
|
+
continue;
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
if (c === '`') {
|
|
993
|
+
let j = i + 1;
|
|
994
|
+
let depth = 0;
|
|
995
|
+
while (j < text.length && !(text[j] === '`' && depth === 0)) {
|
|
996
|
+
if (text[j] === '\\') j++;
|
|
997
|
+
else if (text[j] === '$' && text[j + 1] === '{') depth++;
|
|
998
|
+
else if (text[j] === '}' && depth > 0) depth--;
|
|
999
|
+
j++;
|
|
1000
|
+
}
|
|
1001
|
+
put(text.slice(i, j + 1));
|
|
1002
|
+
i = j + 1;
|
|
1003
|
+
continue;
|
|
1004
|
+
}
|
|
1005
|
+
if (c === ';') {
|
|
1006
|
+
space = true;
|
|
1007
|
+
i++;
|
|
1008
|
+
continue;
|
|
1009
|
+
}
|
|
1010
|
+
if (c === ',') {
|
|
1011
|
+
let j = i + 1;
|
|
1012
|
+
while (j < text.length && /\s/.test(text[j])) j++;
|
|
1013
|
+
if (/[}\])]/.test(text[j] ?? '')) {
|
|
1014
|
+
i++;
|
|
1015
|
+
continue;
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
put(c);
|
|
1019
|
+
i++;
|
|
1020
|
+
}
|
|
1021
|
+
return out;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
/**
|
|
1025
|
+
* The hash recorded for a file the preview writes, of its formatter-neutral
|
|
1026
|
+
* text, so a formatter run (prettier, eslint --fix) does not count as a
|
|
1027
|
+
* change by hand while an edit inside a string does.
|
|
1028
|
+
* @param {string | Buffer} text
|
|
1029
|
+
*/
|
|
1030
|
+
const formatterHash = text => `v2:${digest(formatterNeutral(String(text)))}`;
|
|
1031
|
+
|
|
1032
|
+
/** Fields of a syntax tree node that say where or how it was written. */
|
|
1033
|
+
const LAYOUT = new Set([
|
|
1034
|
+
'start',
|
|
1035
|
+
'end',
|
|
1036
|
+
'loc',
|
|
1037
|
+
'range',
|
|
1038
|
+
'extra',
|
|
1039
|
+
'leadingComments',
|
|
1040
|
+
'trailingComments',
|
|
1041
|
+
'innerComments',
|
|
1042
|
+
'comments',
|
|
1043
|
+
'tokens',
|
|
1044
|
+
'errors',
|
|
1045
|
+
]);
|
|
1046
|
+
|
|
1047
|
+
/**
|
|
1048
|
+
* The text a run of JSX text renders, as JSX reads it: lines trimmed where
|
|
1049
|
+
* they meet a line break, blank lines dropped, the rest joined by a space.
|
|
1050
|
+
* @param {string} value
|
|
1051
|
+
*/
|
|
1052
|
+
function jsxText(value) {
|
|
1053
|
+
const lines = value.split(/\r\n|\n|\r/);
|
|
1054
|
+
let last = 0;
|
|
1055
|
+
lines.forEach((l, i) => {
|
|
1056
|
+
if (/[^ \t]/.test(l)) last = i;
|
|
1057
|
+
});
|
|
1058
|
+
let out = '';
|
|
1059
|
+
lines.forEach((line, i) => {
|
|
1060
|
+
let t = line.replace(/\t/g, ' ');
|
|
1061
|
+
if (i !== 0) t = t.replace(/^ +/, '');
|
|
1062
|
+
if (i !== lines.length - 1) t = t.replace(/ +$/, '');
|
|
1063
|
+
if (t) out += i === last ? t : `${t} `;
|
|
1064
|
+
});
|
|
1065
|
+
return out;
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
/**
|
|
1069
|
+
* JSX children as they render: text and string expressions (`{" "}`) are
|
|
1070
|
+
* one run of text, and runs that render nothing are dropped, so a
|
|
1071
|
+
* formatter's line breaks and `{" "}` change nothing.
|
|
1072
|
+
* @param {any[]} children
|
|
1073
|
+
* @param {(node: any) => any} walk
|
|
1074
|
+
*/
|
|
1075
|
+
function jsxChildren(children, walk) {
|
|
1076
|
+
/** @type {any[]} */
|
|
1077
|
+
const out = [];
|
|
1078
|
+
for (const c of children ?? []) {
|
|
1079
|
+
const text =
|
|
1080
|
+
c.type === 'JSXText'
|
|
1081
|
+
? jsxText(c.value)
|
|
1082
|
+
: c.type === 'JSXExpressionContainer' &&
|
|
1083
|
+
c.expression?.type === 'StringLiteral'
|
|
1084
|
+
? c.expression.value
|
|
1085
|
+
: null;
|
|
1086
|
+
if (text == null) out.push(walk(c));
|
|
1087
|
+
else if (out.at(-1)?.type === 'Text') out.at(-1).value += text;
|
|
1088
|
+
else out.push({type: 'Text', value: text});
|
|
1089
|
+
}
|
|
1090
|
+
return out.filter(c => !(c.type === 'Text' && c.value === ''));
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
/**
|
|
1094
|
+
* A file's code in a form no formatter changes: its syntax tree, without
|
|
1095
|
+
* positions, parentheses, quote kinds, trailing commas or semicolons, with
|
|
1096
|
+
* object keys written the same quoted or bare, white space in JSX text
|
|
1097
|
+
* collapsed, and its comments by text. Null when it does not parse.
|
|
1098
|
+
* @param {string} text
|
|
1099
|
+
* @returns {string | null}
|
|
1100
|
+
*/
|
|
1101
|
+
function canonicalCode(text) {
|
|
1102
|
+
/** @type {any} */
|
|
1103
|
+
let file;
|
|
1104
|
+
try {
|
|
1105
|
+
file = parseModule(text, {
|
|
1106
|
+
sourceType: 'module',
|
|
1107
|
+
plugins: ['jsx', 'typescript'],
|
|
1108
|
+
errorRecovery: false,
|
|
1109
|
+
});
|
|
1110
|
+
} catch {
|
|
1111
|
+
return null;
|
|
1112
|
+
}
|
|
1113
|
+
/** @param {any} node @returns {any} */
|
|
1114
|
+
const walk = node => {
|
|
1115
|
+
if (Array.isArray(node)) return node.map(walk);
|
|
1116
|
+
if (!node || typeof node !== 'object') return node;
|
|
1117
|
+
/** @type {Record<string, any>} */
|
|
1118
|
+
const out = {};
|
|
1119
|
+
for (const [k, v] of Object.entries(node)) {
|
|
1120
|
+
if (LAYOUT.has(k)) continue;
|
|
1121
|
+
out[k] = walk(v);
|
|
1122
|
+
}
|
|
1123
|
+
// A key written "a" or a is the same key.
|
|
1124
|
+
if (
|
|
1125
|
+
(node.type === 'ObjectProperty' ||
|
|
1126
|
+
node.type === 'ObjectMethod' ||
|
|
1127
|
+
node.type === 'TSPropertySignature' ||
|
|
1128
|
+
node.type === 'ClassProperty') &&
|
|
1129
|
+
!node.computed &&
|
|
1130
|
+
node.key?.type === 'StringLiteral' &&
|
|
1131
|
+
/^[A-Za-z_$][\w$]*$/.test(node.key.value)
|
|
1132
|
+
)
|
|
1133
|
+
out.key = {type: 'Identifier', name: node.key.value};
|
|
1134
|
+
if (node.type === 'JSXElement' || node.type === 'JSXFragment')
|
|
1135
|
+
out.children = jsxChildren(node.children, walk);
|
|
1136
|
+
return out;
|
|
1137
|
+
};
|
|
1138
|
+
const comments = (file.comments ?? []).map((/** @type {any} */ c) =>
|
|
1139
|
+
c.value.replace(/\s+/g, ' ').trim(),
|
|
1140
|
+
);
|
|
1141
|
+
return JSON.stringify([walk(file.program), comments]);
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
/**
|
|
1145
|
+
* The hash recorded for a file the preview writes, of its code in a form no
|
|
1146
|
+
* formatter changes ({@link canonicalCode}), so prettier or eslint --fix
|
|
1147
|
+
* with any options does not count as a change by hand, while an edit to
|
|
1148
|
+
* code, a string or a comment does. A file that does not parse falls back
|
|
1149
|
+
* to the text form ({@link formatterNeutral}).
|
|
1150
|
+
* @param {string | Buffer} text
|
|
1151
|
+
*/
|
|
1152
|
+
const contentHash = text => {
|
|
1153
|
+
const code = canonicalCode(String(text));
|
|
1154
|
+
return code == null ? formatterHash(text) : `v3:${digest(code)}`;
|
|
1155
|
+
};
|
|
1156
|
+
|
|
1157
|
+
/**
|
|
1158
|
+
* Whether a file's text matches a recorded hash of any kind a release
|
|
1159
|
+
* wrote: v3 (syntax tree), v2 (text outside strings), or older.
|
|
1160
|
+
* @param {string | Buffer} text
|
|
1161
|
+
* @param {string} h
|
|
1162
|
+
*/
|
|
1163
|
+
const sameAsRecorded = (text, h) =>
|
|
1164
|
+
h.startsWith('v3:')
|
|
1165
|
+
? contentHash(text) === h
|
|
1166
|
+
: h.startsWith('v2:')
|
|
1167
|
+
? formatterHash(text) === h
|
|
1168
|
+
: oldContentHash(text) === h || digest(text) === h;
|
|
1169
|
+
|
|
1170
|
+
/**
|
|
1171
|
+
* The normalized hash of an earlier release (white space, semicolons,
|
|
1172
|
+
* commas and quote kinds dropped everywhere). Read only for records that
|
|
1173
|
+
* hold it; the next write records {@link contentHash}.
|
|
719
1174
|
* @param {string | Buffer} text
|
|
720
1175
|
*/
|
|
721
|
-
const
|
|
1176
|
+
const oldContentHash = text =>
|
|
722
1177
|
digest(
|
|
723
1178
|
String(text)
|
|
724
1179
|
.replace(/["'`]/g, "'")
|
|
@@ -727,7 +1182,8 @@ const contentHash = text =>
|
|
|
727
1182
|
|
|
728
1183
|
/**
|
|
729
1184
|
* Whether a file the preview wrote was changed since, by its recorded hash.
|
|
730
|
-
* A file with no hash
|
|
1185
|
+
* A file with no hash counts as unchanged here; {@link ownedUnchanged}
|
|
1186
|
+
* decides deletes, and never deletes a file with no hash.
|
|
731
1187
|
* @param {string} dir
|
|
732
1188
|
* @param {PreviewData} data
|
|
733
1189
|
* @param {string} rel
|
|
@@ -737,13 +1193,47 @@ function editedByHand(dir, data, rel) {
|
|
|
737
1193
|
if (!was) return false;
|
|
738
1194
|
try {
|
|
739
1195
|
const text = fs.readFileSync(path.join(dir, rel));
|
|
740
|
-
|
|
741
|
-
|
|
1196
|
+
if (was.startsWith('v2:') || was.startsWith('v3:'))
|
|
1197
|
+
return !sameAsRecorded(text, was);
|
|
1198
|
+
// Older records hold the exact or the earlier normalized hash.
|
|
1199
|
+
return oldContentHash(text) !== was && digest(text) !== was;
|
|
742
1200
|
} catch {
|
|
743
1201
|
return false;
|
|
744
1202
|
}
|
|
745
1203
|
}
|
|
746
1204
|
|
|
1205
|
+
/**
|
|
1206
|
+
* Whether the preview may delete a file its record lists: only its own
|
|
1207
|
+
* route file, its record, and the page file of a recorded page (in
|
|
1208
|
+
* _pages/, or pages/ for an older release), and only when a recorded hash
|
|
1209
|
+
* shows the file is as the preview wrote it. Anything else the record names
|
|
1210
|
+
* is kept: a record can be copied, merged or edited by hand.
|
|
1211
|
+
* @param {string} dir
|
|
1212
|
+
* @param {PreviewData} data
|
|
1213
|
+
* @param {string} rel
|
|
1214
|
+
* @param {Set<string>} ids the recorded page ids
|
|
1215
|
+
* @returns {'delete' | 'not-preview' | 'no-hash' | 'edited'}
|
|
1216
|
+
*/
|
|
1217
|
+
function ownedUnchanged(dir, data, rel, ids) {
|
|
1218
|
+
if (rel === PREVIEW_DATA) return 'delete';
|
|
1219
|
+
const page = /^(?:_pages|pages)\/([a-z0-9][a-z0-9-]*)\.tsx$/.exec(rel);
|
|
1220
|
+
if (rel !== 'page.tsx' && !(page && ids.has(page[1]))) return 'not-preview';
|
|
1221
|
+
if (!data.hashes?.[rel]) return 'no-hash';
|
|
1222
|
+
return editedByHand(dir, data, rel) ? 'edited' : 'delete';
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
/**
|
|
1226
|
+
* Why a file the preview listed is kept, for the result notes.
|
|
1227
|
+
* @param {string} name
|
|
1228
|
+
* @param {'not-preview' | 'no-hash' | 'edited'} why
|
|
1229
|
+
*/
|
|
1230
|
+
const keptNote = (name, why) =>
|
|
1231
|
+
why === 'not-preview'
|
|
1232
|
+
? `${name} is kept: theme-preview.json lists it, but the preview only writes page.tsx and page files in ${PAGES_DIR}/`
|
|
1233
|
+
: why === 'no-hash'
|
|
1234
|
+
? `${name} is kept: theme-preview.json has no record of how the preview wrote it, so it may hold your changes; it stays until you remove it yourself`
|
|
1235
|
+
: `${name} was changed by hand after the preview wrote it, so it is kept`;
|
|
1236
|
+
|
|
747
1237
|
/**
|
|
748
1238
|
* Check one planned write before anything is written. A file the preview
|
|
749
1239
|
* did not write is never replaced, unless it already holds exactly the
|
|
@@ -793,7 +1283,7 @@ function checkWrite(cwd, dir, data, w, elsewhere) {
|
|
|
793
1283
|
E.ERR_FILE_EXISTS,
|
|
794
1284
|
`${name} was changed by hand after the preview wrote it, so it is not replaced.`,
|
|
795
1285
|
{
|
|
796
|
-
fix: `copy your changes out of ${name}, then
|
|
1286
|
+
fix: `copy your changes out of ${name}, then give it another name, and run the command again`,
|
|
797
1287
|
},
|
|
798
1288
|
);
|
|
799
1289
|
return 'replace';
|
|
@@ -856,16 +1346,63 @@ function commitPreview(cwd, dir, data, writes, deletes, again) {
|
|
|
856
1346
|
};
|
|
857
1347
|
let current = dir;
|
|
858
1348
|
let step = 'written';
|
|
1349
|
+
const pendingFile = path.join(dir, PENDING);
|
|
1350
|
+
/** The record as it is before this call. @type {any} */
|
|
1351
|
+
let old;
|
|
1352
|
+
try {
|
|
1353
|
+
old = JSON.parse(String(before(path.join(dir, PREVIEW_DATA)) ?? 'null'));
|
|
1354
|
+
} catch {
|
|
1355
|
+
old = null;
|
|
1356
|
+
}
|
|
859
1357
|
try {
|
|
1358
|
+
const hashes = {...data.hashes};
|
|
1359
|
+
for (const w of writes)
|
|
1360
|
+
if (!w.keepHash || !hashes[w.rel]) hashes[w.rel] = contentHash(w.text);
|
|
1361
|
+
for (const rel of deletes) delete hashes[rel];
|
|
1362
|
+
for (const rel of Object.keys(hashes))
|
|
1363
|
+
if (!data.files.includes(rel)) delete hashes[rel];
|
|
1364
|
+
data.hashes = hashes;
|
|
1365
|
+
if (!data.files.includes(PREVIEW_DATA)) data.files.push(PREVIEW_DATA);
|
|
860
1366
|
for (const w of writes) {
|
|
861
1367
|
current = confined(path.join(dir, w.rel), dir, 'a preview file');
|
|
862
1368
|
mkdir(path.dirname(current));
|
|
863
1369
|
stage(current, w.text);
|
|
864
1370
|
}
|
|
1371
|
+
// The record this call ends with, the one it starts from and the temp
|
|
1372
|
+
// files and deletes between them, written before the first rename: a
|
|
1373
|
+
// command stopped part way (a kill) leaves it, and the next preview
|
|
1374
|
+
// command finishes the step from it ({@link recoverPreview}).
|
|
1375
|
+
current = pendingFile;
|
|
1376
|
+
writeWhole(
|
|
1377
|
+
pendingFile,
|
|
1378
|
+
`${JSON.stringify(
|
|
1379
|
+
{
|
|
1380
|
+
version: 1,
|
|
1381
|
+
record: data,
|
|
1382
|
+
old,
|
|
1383
|
+
temps: [...temps.values()].map(t => path.relative(dir, t)),
|
|
1384
|
+
deletes,
|
|
1385
|
+
// What each file to delete holds now: after a stop, it goes only
|
|
1386
|
+
// when it still holds that.
|
|
1387
|
+
deleteHashes: Object.fromEntries(
|
|
1388
|
+
deletes.flatMap(rel => {
|
|
1389
|
+
const text = before(
|
|
1390
|
+
confined(path.join(dir, rel), dir, 'a preview file'),
|
|
1391
|
+
);
|
|
1392
|
+
return text ? [[rel, contentHash(text)]] : [];
|
|
1393
|
+
}),
|
|
1394
|
+
),
|
|
1395
|
+
},
|
|
1396
|
+
null,
|
|
1397
|
+
2,
|
|
1398
|
+
)}\n`,
|
|
1399
|
+
);
|
|
1400
|
+
commitStep('pending');
|
|
865
1401
|
for (const w of writes) {
|
|
866
1402
|
current = path.join(dir, w.rel);
|
|
867
1403
|
touched.push({file: current, before: before(current)});
|
|
868
1404
|
fs.renameSync(tmp(current), current);
|
|
1405
|
+
commitStep('rename');
|
|
869
1406
|
}
|
|
870
1407
|
staged.length = 0;
|
|
871
1408
|
step = 'removed';
|
|
@@ -875,21 +1412,18 @@ function commitPreview(cwd, dir, data, writes, deletes, again) {
|
|
|
875
1412
|
if (was == null) continue;
|
|
876
1413
|
touched.push({file: current, before: was});
|
|
877
1414
|
fs.rmSync(current);
|
|
1415
|
+
commitStep('delete');
|
|
878
1416
|
}
|
|
879
1417
|
step = 'written';
|
|
880
|
-
const hashes = {...data.hashes};
|
|
881
|
-
for (const w of writes)
|
|
882
|
-
if (!w.keepHash || !hashes[w.rel]) hashes[w.rel] = contentHash(w.text);
|
|
883
|
-
for (const rel of deletes) delete hashes[rel];
|
|
884
|
-
for (const rel of Object.keys(hashes))
|
|
885
|
-
if (!data.files.includes(rel)) delete hashes[rel];
|
|
886
|
-
data.hashes = hashes;
|
|
887
|
-
if (!data.files.includes(PREVIEW_DATA)) data.files.push(PREVIEW_DATA);
|
|
888
1418
|
current = path.join(dir, PREVIEW_DATA);
|
|
889
1419
|
touched.push({file: current, before: before(current)});
|
|
890
1420
|
stage(current, `${JSON.stringify(data, null, 2)}\n`);
|
|
891
1421
|
fs.renameSync(tmp(current), current);
|
|
1422
|
+
fs.rmSync(pendingFile, {force: true});
|
|
892
1423
|
} catch (e) {
|
|
1424
|
+
// A stop injected by a test stands for a kill: nothing is put back.
|
|
1425
|
+
if (e === STOPPED) throw e;
|
|
1426
|
+
fs.rmSync(pendingFile, {force: true});
|
|
893
1427
|
for (const t of staged) fs.rmSync(t, {force: true});
|
|
894
1428
|
for (const t of touched.reverse())
|
|
895
1429
|
try {
|
|
@@ -913,6 +1447,229 @@ function commitPreview(cwd, dir, data, writes, deletes, again) {
|
|
|
913
1447
|
}
|
|
914
1448
|
}
|
|
915
1449
|
|
|
1450
|
+
/** The step record a preview change leaves while it runs. */
|
|
1451
|
+
const PENDING = '.theme-preview.pending.json';
|
|
1452
|
+
|
|
1453
|
+
/** What a test throws to stop a change part way, as a kill would. */
|
|
1454
|
+
const STOPPED = Symbol('stopped');
|
|
1455
|
+
|
|
1456
|
+
/** @type {((step: string) => boolean) | null} */
|
|
1457
|
+
let stopAt = null;
|
|
1458
|
+
|
|
1459
|
+
/**
|
|
1460
|
+
* For tests: stop the next preview changes at the step where `when`
|
|
1461
|
+
* returns true, as a kill would, with nothing put back. Null clears it.
|
|
1462
|
+
* @param {((step: string) => boolean) | null} when
|
|
1463
|
+
*/
|
|
1464
|
+
export function stopPreviewCommitAt(when) {
|
|
1465
|
+
stopAt = when;
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
/** Whether a process is running here. @param {number} pid */
|
|
1469
|
+
function alive(pid) {
|
|
1470
|
+
if (pid === process.pid) return true;
|
|
1471
|
+
try {
|
|
1472
|
+
process.kill(pid, 0);
|
|
1473
|
+
return true;
|
|
1474
|
+
} catch (e) {
|
|
1475
|
+
return /** @type {any} */ (e)?.code === 'EPERM';
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
/** @param {string} step */
|
|
1480
|
+
function commitStep(step) {
|
|
1481
|
+
if (stopAt?.(step)) throw STOPPED;
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
/**
|
|
1485
|
+
* Finish a preview change that was stopped part way (a kill), from the
|
|
1486
|
+
* step record it left: its leftover temp files go; each file it was writing
|
|
1487
|
+
* keeps the hash of what is on disk when that is the new or the old text,
|
|
1488
|
+
* and no hash otherwise (so it is never deleted on the record's word); a
|
|
1489
|
+
* page whose new file never landed keeps its old file; an old file it was
|
|
1490
|
+
* removing goes only when the preview wrote it and its new file is there.
|
|
1491
|
+
* The record is then written and the step record removed. Nothing happens
|
|
1492
|
+
* when there is no step record.
|
|
1493
|
+
* @param {string} cwd
|
|
1494
|
+
* @param {string} dir
|
|
1495
|
+
* @returns {string | null} a note for the result, when a change was finished
|
|
1496
|
+
*/
|
|
1497
|
+
function recoverPreview(cwd, dir) {
|
|
1498
|
+
const pendingFile = path.join(dir, PENDING);
|
|
1499
|
+
/** @type {any} */
|
|
1500
|
+
let p;
|
|
1501
|
+
try {
|
|
1502
|
+
p = JSON.parse(fs.readFileSync(pendingFile, 'utf8'));
|
|
1503
|
+
} catch (e) {
|
|
1504
|
+
if (/** @type {any} */ (e)?.code === 'ENOENT') return null;
|
|
1505
|
+
// Unreadable: the change is treated as never started. Its temp files
|
|
1506
|
+
// are found by the names only the editor makes, when their process is
|
|
1507
|
+
// gone.
|
|
1508
|
+
for (const sub of ['', PAGES_DIR, LEGACY_PAGES_DIR]) {
|
|
1509
|
+
/** @type {string[]} */
|
|
1510
|
+
let names;
|
|
1511
|
+
try {
|
|
1512
|
+
names = fs.readdirSync(path.join(dir, sub));
|
|
1513
|
+
} catch {
|
|
1514
|
+
continue;
|
|
1515
|
+
}
|
|
1516
|
+
for (const name of names) {
|
|
1517
|
+
const m = /^\..+\.astryx-(\d+)-[a-z0-9]+\.tmp$/.exec(name);
|
|
1518
|
+
if (m && !alive(Number(m[1])))
|
|
1519
|
+
fs.rmSync(path.join(dir, sub, name), {force: true});
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
fs.rmSync(pendingFile, {force: true});
|
|
1523
|
+
return `removed ${shown(cwd, pendingFile)}, a damaged step record of a preview change that was stopped`;
|
|
1524
|
+
}
|
|
1525
|
+
const inside = (/** @type {string} */ rel) =>
|
|
1526
|
+
confined(path.join(dir, rel), dir, PENDING);
|
|
1527
|
+
for (const t of Array.isArray(p?.temps) ? p.temps : [])
|
|
1528
|
+
if (typeof t === 'string' && /\.astryx-[^/]*\.tmp$/.test(t))
|
|
1529
|
+
fs.rmSync(inside(t), {force: true});
|
|
1530
|
+
const record = p?.record;
|
|
1531
|
+
if (previewProblem(record)) {
|
|
1532
|
+
fs.rmSync(pendingFile, {force: true});
|
|
1533
|
+
return null;
|
|
1534
|
+
}
|
|
1535
|
+
const old = p.old && !previewProblem(p.old) ? p.old : null;
|
|
1536
|
+
// The route decides which way the change goes: a page.tsx that is not
|
|
1537
|
+
// the new one means the route never changed, so the change is undone and
|
|
1538
|
+
// the record stays as it was; else it is finished.
|
|
1539
|
+
const route = (() => {
|
|
1540
|
+
try {
|
|
1541
|
+
return fs.readFileSync(inside('page.tsx'));
|
|
1542
|
+
} catch {
|
|
1543
|
+
return null;
|
|
1544
|
+
}
|
|
1545
|
+
})();
|
|
1546
|
+
const newRoute = record.hashes?.['page.tsx'];
|
|
1547
|
+
if (newRoute && !(route && hashMatches(route, newRoute)))
|
|
1548
|
+
return undoStopped(cwd, dir, record, old);
|
|
1549
|
+
/** @param {string} rel */
|
|
1550
|
+
const onDisk = rel => {
|
|
1551
|
+
try {
|
|
1552
|
+
return fs.readFileSync(inside(rel));
|
|
1553
|
+
} catch {
|
|
1554
|
+
return null;
|
|
1555
|
+
}
|
|
1556
|
+
};
|
|
1557
|
+
const matches = hashMatches;
|
|
1558
|
+
const hashes = {...record.hashes};
|
|
1559
|
+
for (const rel of Object.keys(hashes)) {
|
|
1560
|
+
const text = onDisk(rel);
|
|
1561
|
+
if (!text) continue;
|
|
1562
|
+
if (matches(text, hashes[rel])) continue;
|
|
1563
|
+
if (old?.hashes?.[rel] && matches(text, old.hashes[rel]))
|
|
1564
|
+
hashes[rel] = old.hashes[rel];
|
|
1565
|
+
else delete hashes[rel];
|
|
1566
|
+
}
|
|
1567
|
+
// A page whose new file never landed keeps its old file, or else leaves
|
|
1568
|
+
// the record, so the record never lists a page the route cannot show.
|
|
1569
|
+
for (const page of [...record.pages]) {
|
|
1570
|
+
const rel = `${page.file}.tsx`;
|
|
1571
|
+
if (onDisk(rel)) continue;
|
|
1572
|
+
const was = old?.pages.find((/** @type {any} */ x) => x.id === page.id);
|
|
1573
|
+
const oldRel = was && `${was.file}.tsx`;
|
|
1574
|
+
if (oldRel && oldRel !== rel && onDisk(oldRel)) {
|
|
1575
|
+
page.file = was.file;
|
|
1576
|
+
record.files = record.files.map((/** @type {string} */ f) =>
|
|
1577
|
+
f === rel ? oldRel : f,
|
|
1578
|
+
);
|
|
1579
|
+
delete hashes[rel];
|
|
1580
|
+
if (old.hashes?.[oldRel]) hashes[oldRel] = old.hashes[oldRel];
|
|
1581
|
+
} else {
|
|
1582
|
+
record.pages = record.pages.filter((/** @type {any} */ x) => x !== page);
|
|
1583
|
+
record.files = record.files.filter(
|
|
1584
|
+
(/** @type {string} */ f) => f !== rel,
|
|
1585
|
+
);
|
|
1586
|
+
delete hashes[rel];
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
// An old file the change was removing goes when it still holds what it
|
|
1590
|
+
// held when the change planned it, and no page of the record uses it now;
|
|
1591
|
+
// one that changed since is kept and named.
|
|
1592
|
+
/** @type {string[]} */
|
|
1593
|
+
const left = [];
|
|
1594
|
+
for (const rel of Array.isArray(p.deletes) ? p.deletes : []) {
|
|
1595
|
+
if (typeof rel !== 'string' || record.files.includes(rel)) continue;
|
|
1596
|
+
const text = onDisk(rel);
|
|
1597
|
+
if (!text) continue;
|
|
1598
|
+
// A page moved from pages/ to _pages/: the old copy goes once the new
|
|
1599
|
+
// one holds the same text.
|
|
1600
|
+
const moved = /^pages\/([a-z0-9][a-z0-9-]*)\.tsx$/.exec(rel);
|
|
1601
|
+
const copy = moved && onDisk(`${PAGES_DIR}/${moved[1]}.tsx`);
|
|
1602
|
+
if (
|
|
1603
|
+
matches(text, p.deleteHashes?.[rel]) ||
|
|
1604
|
+
matches(text, old?.hashes?.[rel]) ||
|
|
1605
|
+
(copy && copy.equals(text))
|
|
1606
|
+
)
|
|
1607
|
+
fs.rmSync(inside(rel));
|
|
1608
|
+
else left.push(shown(cwd, inside(rel)));
|
|
1609
|
+
}
|
|
1610
|
+
record.hashes = hashes;
|
|
1611
|
+
writeWhole(
|
|
1612
|
+
path.join(dir, PREVIEW_DATA),
|
|
1613
|
+
`${JSON.stringify(record, null, 2)}\n`,
|
|
1614
|
+
);
|
|
1615
|
+
fs.rmSync(pendingFile, {force: true});
|
|
1616
|
+
tidyPageFolders(dir);
|
|
1617
|
+
return `finished a preview change that was stopped part way (${shown(cwd, dir) || '.'}/${PENDING})${left.length ? `; kept ${left.join(', ')}: ${left.length > 1 ? 'they' : 'it'} changed after the change began, so ${left.length > 1 ? 'they' : 'it'} may hold your changes and ${left.length > 1 ? 'stay' : 'stays'} until you remove ${left.length > 1 ? 'them' : 'it'}` : ''}`;
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
/**
|
|
1621
|
+
* Whether a file's text matches a recorded hash, of any kind a release
|
|
1622
|
+
* wrote.
|
|
1623
|
+
* @param {Buffer} text
|
|
1624
|
+
* @param {string | undefined} h
|
|
1625
|
+
*/
|
|
1626
|
+
function hashMatches(text, h) {
|
|
1627
|
+
return !!h && sameAsRecorded(text, h);
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
/**
|
|
1631
|
+
* Undo a preview change stopped before its route landed: the record stays
|
|
1632
|
+
* as it was, each new file of the change that landed and the old record
|
|
1633
|
+
* does not list goes (only when it is as the change wrote it), and a file
|
|
1634
|
+
* both records list that now holds the change's text keeps that text's
|
|
1635
|
+
* hash, so it is not taken for a hand edit.
|
|
1636
|
+
* @param {string} cwd
|
|
1637
|
+
* @param {string} dir
|
|
1638
|
+
* @param {PreviewData} record the record the change was writing
|
|
1639
|
+
* @param {PreviewData | null} old the record before it
|
|
1640
|
+
* @returns {string}
|
|
1641
|
+
*/
|
|
1642
|
+
function undoStopped(cwd, dir, record, old) {
|
|
1643
|
+
const inside = (/** @type {string} */ rel) =>
|
|
1644
|
+
confined(path.join(dir, rel), dir, PENDING);
|
|
1645
|
+
/** @param {string} rel */
|
|
1646
|
+
const onDisk = rel => {
|
|
1647
|
+
try {
|
|
1648
|
+
return fs.readFileSync(inside(rel));
|
|
1649
|
+
} catch {
|
|
1650
|
+
return null;
|
|
1651
|
+
}
|
|
1652
|
+
};
|
|
1653
|
+
const keep = new Set(old?.files ?? []);
|
|
1654
|
+
const hashes = {...old?.hashes};
|
|
1655
|
+
for (const [rel, h] of Object.entries(record.hashes ?? {})) {
|
|
1656
|
+
const text = onDisk(rel);
|
|
1657
|
+
if (!text || !hashMatches(text, h)) continue;
|
|
1658
|
+
if (keep.has(rel)) hashes[rel] = h;
|
|
1659
|
+
else if (rel !== PREVIEW_DATA) fs.rmSync(inside(rel));
|
|
1660
|
+
}
|
|
1661
|
+
if (old) {
|
|
1662
|
+
old.hashes = hashes;
|
|
1663
|
+
writeWhole(
|
|
1664
|
+
path.join(dir, PREVIEW_DATA),
|
|
1665
|
+
`${JSON.stringify(old, null, 2)}\n`,
|
|
1666
|
+
);
|
|
1667
|
+
}
|
|
1668
|
+
fs.rmSync(path.join(dir, PENDING), {force: true});
|
|
1669
|
+
tidyPageFolders(dir);
|
|
1670
|
+
return `undid a preview change that was stopped before its route was written (${shown(cwd, dir) || '.'}/${PENDING})`;
|
|
1671
|
+
}
|
|
1672
|
+
|
|
916
1673
|
/**
|
|
917
1674
|
* Move the pages an older release wrote into pages/ to {@link PAGES_DIR}/,
|
|
918
1675
|
* in the plan and in `data`: a pages/ folder at the project root or in
|
|
@@ -984,34 +1741,69 @@ function tidyPageFolders(dir) {
|
|
|
984
1741
|
}
|
|
985
1742
|
}
|
|
986
1743
|
|
|
987
|
-
/** The themes this command made, by name. @param {string} cwd */
|
|
988
|
-
function editedThemes(cwd) {
|
|
989
|
-
const root = path.join(cwd, 'themes');
|
|
990
|
-
return fs.existsSync(root)
|
|
991
|
-
? fs
|
|
992
|
-
.readdirSync(root)
|
|
993
|
-
.filter(d => fs.existsSync(themeFiles(cwd, d).settings))
|
|
994
|
-
.sort()
|
|
995
|
-
: [];
|
|
996
|
-
}
|
|
997
|
-
|
|
998
1744
|
/**
|
|
999
|
-
* The theme the route imports
|
|
1000
|
-
*
|
|
1001
|
-
*
|
|
1745
|
+
* The theme the route imports; null for the app's own theme. A theme that
|
|
1746
|
+
* does not load is refused here, before the route or any page is written.
|
|
1747
|
+
* With `build`, a theme with no build yet is built now; without it,
|
|
1748
|
+
* `unbuilt` says so and the caller builds it once every check has passed
|
|
1749
|
+
* ({@link buildWorn}).
|
|
1002
1750
|
* @param {string} cwd
|
|
1003
1751
|
* @param {string} dir
|
|
1004
1752
|
* @param {PreviewData} data
|
|
1005
|
-
* @
|
|
1753
|
+
* @param {{build?: boolean}} [opts]
|
|
1754
|
+
* @returns {Promise<{name: string, module: string, css: string[], mode: 'system' | 'light' | 'dark', unbuilt?: boolean} | null>}
|
|
1006
1755
|
*/
|
|
1007
|
-
async function routeTheme(cwd, dir, data) {
|
|
1756
|
+
async function routeTheme(cwd, dir, data, opts = {}) {
|
|
1008
1757
|
if (!data.theme) return null;
|
|
1009
1758
|
const t = loadTheme(cwd, data.theme);
|
|
1010
1759
|
const ctx = await context(cwd);
|
|
1011
|
-
|
|
1012
|
-
|
|
1760
|
+
const unbuilt = !fs.existsSync(themeFiles(cwd, data.theme).js);
|
|
1761
|
+
if (unbuilt && opts.build !== false) await buildTheme(ctx, data.theme, t);
|
|
1013
1762
|
const ti = themeImports(ctx, data.theme, t, dir);
|
|
1014
|
-
return {
|
|
1763
|
+
return {
|
|
1764
|
+
name: data.theme,
|
|
1765
|
+
module: ti.module,
|
|
1766
|
+
css: ti.css,
|
|
1767
|
+
mode: ti.mode,
|
|
1768
|
+
...(unbuilt && opts.build === false ? {unbuilt: true} : {}),
|
|
1769
|
+
};
|
|
1770
|
+
}
|
|
1771
|
+
|
|
1772
|
+
/**
|
|
1773
|
+
* Run `fn` under a theme's lock when the command was given one (the
|
|
1774
|
+
* adapter's `lockTheme`), else as it is.
|
|
1775
|
+
* @param {any} options
|
|
1776
|
+
* @param {string} slug
|
|
1777
|
+
* @param {() => unknown} fn
|
|
1778
|
+
*/
|
|
1779
|
+
async function themeLocked(options, slug, fn) {
|
|
1780
|
+
if (typeof options?.lockTheme === 'function')
|
|
1781
|
+
await options.lockTheme(slug, async () => fn());
|
|
1782
|
+
else fn();
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
/**
|
|
1786
|
+
* Build the theme a preview wears when it has no build yet, holding that
|
|
1787
|
+
* theme's lock (an `editor set` on it waits), with a note: a preview
|
|
1788
|
+
* command writes the theme's build only once every check has passed.
|
|
1789
|
+
* @param {string} cwd
|
|
1790
|
+
* @param {{name: string, unbuilt?: boolean} | null} theme from {@link routeTheme}
|
|
1791
|
+
* @param {any} options the command's options, with `lockTheme` from the adapter
|
|
1792
|
+
* @param {string[]} notes
|
|
1793
|
+
*/
|
|
1794
|
+
async function buildWorn(cwd, theme, options, notes) {
|
|
1795
|
+
if (!theme?.unbuilt) return;
|
|
1796
|
+
const slug = theme.name;
|
|
1797
|
+
const build = async () => {
|
|
1798
|
+
if (fs.existsSync(themeFiles(cwd, slug).js)) return;
|
|
1799
|
+
await buildTheme(await context(cwd), slug, loadTheme(cwd, slug));
|
|
1800
|
+
};
|
|
1801
|
+
await (typeof options?.lockTheme === 'function'
|
|
1802
|
+
? options.lockTheme(slug, build)
|
|
1803
|
+
: build());
|
|
1804
|
+
notes.push(
|
|
1805
|
+
`built ${shown(cwd, themeFiles(cwd, slug).dir)}/ first: its build was missing`,
|
|
1806
|
+
);
|
|
1015
1807
|
}
|
|
1016
1808
|
|
|
1017
1809
|
/** The Colors page: its id and file, relative to the route folder. */
|
|
@@ -1061,13 +1853,7 @@ async function colorsPage(cwd, dir, slug) {
|
|
|
1061
1853
|
const uses = {};
|
|
1062
1854
|
// Only the theme's own modes have shades; text on a fill is marked on
|
|
1063
1855
|
// the neutral shade it uses, and pure black or white on no shade.
|
|
1064
|
-
const modes =
|
|
1065
|
-
data.mode === 'light-only'
|
|
1066
|
-
? ['light']
|
|
1067
|
-
: data.mode === 'dark-only'
|
|
1068
|
-
? ['dark']
|
|
1069
|
-
: ['light', 'dark']
|
|
1070
|
-
);
|
|
1856
|
+
const modes = modesOf(data);
|
|
1071
1857
|
for (const [token, v] of Object.entries(pal.tokens)) {
|
|
1072
1858
|
for (const m of modes) {
|
|
1073
1859
|
if (exact(token, m)) continue;
|
|
@@ -1163,6 +1949,14 @@ export async function refreshPreviewRoute(cwd, slug) {
|
|
|
1163
1949
|
}
|
|
1164
1950
|
if (!data || data.theme !== slug || !data.files.includes('page.tsx'))
|
|
1165
1951
|
continue;
|
|
1952
|
+
// A change stopped part way is finished by the next preview command;
|
|
1953
|
+
// until then the preview is left as it is, and the result says so.
|
|
1954
|
+
if (fs.existsSync(path.join(dir, PENDING))) {
|
|
1955
|
+
stale.push(
|
|
1956
|
+
`${shownDir(cwd, dir)}/ (a preview change was stopped part way; ${PREVIEW} add finishes it and brings the preview up to date)`,
|
|
1957
|
+
);
|
|
1958
|
+
continue;
|
|
1959
|
+
}
|
|
1166
1960
|
const writes = (
|
|
1167
1961
|
await routeWrites(cwd, dir, data, await routeTheme(cwd, dir, data))
|
|
1168
1962
|
).filter(w => {
|
|
@@ -1235,16 +2029,25 @@ function recordPreview(cwd, slug, dir, keep = true) {
|
|
|
1235
2029
|
* @param {{dir: string, app: string | null}} at
|
|
1236
2030
|
*/
|
|
1237
2031
|
function openLine(cwd, at) {
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
.
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
2032
|
+
// The routes folder by place, also one this command has just made.
|
|
2033
|
+
const app =
|
|
2034
|
+
at.app ??
|
|
2035
|
+
[path.join(cwd, 'app'), path.join(cwd, 'src', 'app')].find(r =>
|
|
2036
|
+
at.dir.startsWith(r + path.sep),
|
|
2037
|
+
);
|
|
2038
|
+
if (app) {
|
|
2039
|
+
// A (group) folder is not part of the URL; a [param] folder is a value
|
|
2040
|
+
// the person picks; each other folder is one URL segment, encoded.
|
|
2041
|
+
const segs = urlSegments(app, at.dir);
|
|
2042
|
+
const params = segs.filter(seg => /^\[.+\]$/.test(seg));
|
|
2043
|
+
const url = segs
|
|
2044
|
+
.map(seg =>
|
|
2045
|
+
/^\[.+\]$/.test(seg)
|
|
2046
|
+
? `<${seg.replace(/^\[+\.*|\]+$/g, '')}>`
|
|
2047
|
+
: encodeURIComponent(seg),
|
|
2048
|
+
)
|
|
1246
2049
|
.join('/');
|
|
1247
|
-
return `open /${url} in the running app`;
|
|
2050
|
+
return `open /${url} in the running app${params.length ? `, with a value for ${params.join(', ')}` : ''}`;
|
|
1248
2051
|
}
|
|
1249
2052
|
const routes = ['app', path.join('src', 'app')].filter(r =>
|
|
1250
2053
|
fs.existsSync(path.join(cwd, r)),
|
|
@@ -1254,8 +2057,6 @@ function openLine(cwd, at) {
|
|
|
1254
2057
|
return `no app routes folder (app/ or src/app/) was found, so the preview is in ${shownDir(cwd, at.dir)}/; serve page.tsx from a route of your app, or make a starter app around a preview with ${PREVIEW} app <dir>`;
|
|
1255
2058
|
}
|
|
1256
2059
|
|
|
1257
|
-
const STYLEX_IMPORT = /from\s+['"]@stylexjs\/stylex['"]/;
|
|
1258
|
-
|
|
1259
2060
|
/**
|
|
1260
2061
|
* Why a template page was skipped.
|
|
1261
2062
|
* @param {{missingPackages: string[], notInstalled: string[], stylexCompiler: boolean}} s
|
|
@@ -1503,33 +2304,64 @@ function installed(cwd, pkg) {
|
|
|
1503
2304
|
}
|
|
1504
2305
|
|
|
1505
2306
|
/**
|
|
1506
|
-
*
|
|
1507
|
-
*
|
|
1508
|
-
*
|
|
2307
|
+
* What a template page needs that the app lacks, from its real imports:
|
|
2308
|
+
* packages the app's package.json does not name (with the command that
|
|
2309
|
+
* adds them), packages it names that are not installed (with the install
|
|
2310
|
+
* command), and a StyleX compiler.
|
|
1509
2311
|
* @param {string} cwd
|
|
1510
2312
|
* @param {string} source
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
2313
|
+
*/
|
|
2314
|
+
function pageNeeds(cwd, source) {
|
|
2315
|
+
const pkgs = [...extractImportedPackages(source)];
|
|
2316
|
+
const needs = analyzeTemplateNeeds(source, cwd);
|
|
2317
|
+
/** @type {Record<string, string>} */
|
|
2318
|
+
let declared = {};
|
|
2319
|
+
try {
|
|
2320
|
+
const pkg = JSON.parse(
|
|
2321
|
+
fs.readFileSync(path.join(cwd, 'package.json'), 'utf8'),
|
|
2322
|
+
);
|
|
2323
|
+
declared = {
|
|
2324
|
+
...pkg.optionalDependencies,
|
|
2325
|
+
...pkg.peerDependencies,
|
|
2326
|
+
...pkg.dependencies,
|
|
2327
|
+
...pkg.devDependencies,
|
|
2328
|
+
};
|
|
2329
|
+
} catch {
|
|
2330
|
+
// No readable package.json: nothing is declared.
|
|
2331
|
+
}
|
|
2332
|
+
const absent = pkgs.filter(
|
|
2333
|
+
p =>
|
|
2334
|
+
!needs.missingPackages.includes(p) &&
|
|
2335
|
+
!/^(react|react-dom|next)$/.test(p) &&
|
|
2336
|
+
!p.startsWith('@astryxdesign/') &&
|
|
2337
|
+
!installed(cwd, p),
|
|
1520
2338
|
);
|
|
2339
|
+
const notInstalled = absent.filter(p => p in declared);
|
|
2340
|
+
const undeclared = absent.filter(p => !(p in declared));
|
|
2341
|
+
const missingPackages = [...needs.missingPackages, ...undeclared];
|
|
2342
|
+
const commands = [
|
|
2343
|
+
...(needs.installCommand
|
|
2344
|
+
? [[needs.installCommand, ...undeclared].join(' ')]
|
|
2345
|
+
: undeclared.length
|
|
2346
|
+
? [installLine(cwd, undeclared)]
|
|
2347
|
+
: []),
|
|
2348
|
+
...(notInstalled.length ? [installCommandOf(cwd)] : []),
|
|
2349
|
+
];
|
|
2350
|
+
return {
|
|
2351
|
+
missingPackages,
|
|
2352
|
+
notInstalled,
|
|
2353
|
+
installCommand: commands.length ? commands.join(' && ') : null,
|
|
2354
|
+
noCommand: needs.missingPackages.length > 0 && !needs.installCommand,
|
|
2355
|
+
stylexCompiler:
|
|
2356
|
+
pkgs.includes('@stylexjs/stylex') &&
|
|
2357
|
+
detectStylingSystem(cwd) !== 'stylex',
|
|
2358
|
+
};
|
|
1521
2359
|
}
|
|
1522
2360
|
|
|
1523
2361
|
/** The command that installs what the package.json names. @param {string} cwd */
|
|
1524
2362
|
function installCommandOf(cwd) {
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
['yarn.lock', 'yarn'],
|
|
1528
|
-
['bun.lockb', 'bun'],
|
|
1529
|
-
['bun.lock', 'bun'],
|
|
1530
|
-
]))
|
|
1531
|
-
if (fs.existsSync(path.join(cwd, lock))) return `${pm} install`;
|
|
1532
|
-
return 'npm install';
|
|
2363
|
+
// The package manager `installLine` names, with its install verb.
|
|
2364
|
+
return installLine(cwd, []).trim().replace(/ add$/, ' install');
|
|
1533
2365
|
}
|
|
1534
2366
|
|
|
1535
2367
|
/**
|
|
@@ -1546,24 +2378,34 @@ function wornTheme(cwd, slug, again, without) {
|
|
|
1546
2378
|
return loadTheme(cwd, slug);
|
|
1547
2379
|
} catch (e) {
|
|
1548
2380
|
const code = /** @type {any} */ (e)?.code;
|
|
1549
|
-
|
|
1550
|
-
|
|
2381
|
+
// A theme that is there but does not load is its own refusal.
|
|
2382
|
+
if (code === E.ERR_THEME_INVALID || code === E.ERR_THEME_LOAD) throw e;
|
|
1551
2383
|
const name = String(slug)
|
|
1552
2384
|
.trim()
|
|
1553
2385
|
.toLowerCase()
|
|
1554
|
-
.replace(/^@astryxdesign\/theme-/, '')
|
|
2386
|
+
.replace(/^@astryxdesign\/theme-/, '')
|
|
2387
|
+
.replace(/^\.?\/?themes\//, '')
|
|
2388
|
+
.replace(/\/+$/, '');
|
|
1555
2389
|
const themes = editedThemes(cwd);
|
|
1556
|
-
if (!themes.includes(name) && isPackageTheme(cwd, name))
|
|
2390
|
+
if (!themes.includes(name) && isPackageTheme(cwd, name)) {
|
|
2391
|
+
const missing =
|
|
2392
|
+
name !== 'neutral' && !installed(cwd, `@astryxdesign/theme-${name}`);
|
|
1557
2393
|
refuse(
|
|
1558
2394
|
E.ERR_UNKNOWN_THEME,
|
|
1559
2395
|
`${slug} is a package theme, not a theme made by ${EDITOR}; ${without}.`,
|
|
1560
2396
|
{
|
|
1561
2397
|
run: [
|
|
1562
2398
|
again(null),
|
|
1563
|
-
|
|
2399
|
+
...(missing
|
|
2400
|
+
? [
|
|
2401
|
+
`${installLine(cwd, [`@astryxdesign/theme-${name}`])} # then a theme of your own can start from it`,
|
|
2402
|
+
]
|
|
2403
|
+
: []),
|
|
2404
|
+
`${EDITOR} new my-${name} --like ${name} # a theme of your own that starts from it`,
|
|
1564
2405
|
],
|
|
1565
2406
|
},
|
|
1566
2407
|
);
|
|
2408
|
+
}
|
|
1567
2409
|
const near = themes.includes(name) ? [name] : nearest(name, themes);
|
|
1568
2410
|
if (near.length)
|
|
1569
2411
|
refuse(
|
|
@@ -1582,13 +2424,17 @@ function wornTheme(cwd, slug, again, without) {
|
|
|
1582
2424
|
* @param {string} name
|
|
1583
2425
|
*/
|
|
1584
2426
|
function isPackageTheme(cwd, name) {
|
|
1585
|
-
return
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
2427
|
+
if (!/^[a-z][a-z0-9-]*$/.test(name)) return false;
|
|
2428
|
+
// The base themes ship with the CLI, installed or not.
|
|
2429
|
+
const bases = fs.existsSync(BASE_THEMES)
|
|
2430
|
+
? fs.readdirSync(BASE_THEMES)
|
|
2431
|
+
: ['neutral'];
|
|
2432
|
+
return bases.includes(name) || installed(cwd, `@astryxdesign/theme-${name}`);
|
|
1590
2433
|
}
|
|
1591
2434
|
|
|
2435
|
+
/** The base themes this CLI knows, one folder each. */
|
|
2436
|
+
const BASE_THEMES = path.join(CLI_PACKAGE, 'assets', 'templates', 'themes');
|
|
2437
|
+
|
|
1592
2438
|
/**
|
|
1593
2439
|
* The page template `--template` names, or a refusal: a block or component
|
|
1594
2440
|
* example is not a page, and the suggestions are page templates only, as
|
|
@@ -1600,11 +2446,13 @@ function isPackageTheme(cwd, name) {
|
|
|
1600
2446
|
*/
|
|
1601
2447
|
async function namedTemplate(cwd, id, flags) {
|
|
1602
2448
|
const pages = await pageTemplates(cwd);
|
|
1603
|
-
|
|
2449
|
+
// Page ids are lowercase: LOGIN names login.
|
|
2450
|
+
const hit =
|
|
2451
|
+
pages.find(t => t.id === id) ?? pages.find(t => t.id === id.toLowerCase());
|
|
1604
2452
|
if (hit)
|
|
1605
2453
|
return {
|
|
1606
|
-
name: id,
|
|
1607
|
-
displayName: hit.name ?? id,
|
|
2454
|
+
name: hit.id,
|
|
2455
|
+
displayName: hit.name ?? hit.id,
|
|
1608
2456
|
why: 'named with --template',
|
|
1609
2457
|
};
|
|
1610
2458
|
try {
|
|
@@ -1661,11 +2509,11 @@ async function pageAgain(cwd, id, defaultPage) {
|
|
|
1661
2509
|
await template(id, {cwd, type: 'page'})
|
|
1662
2510
|
);
|
|
1663
2511
|
const source = shownTemplate.data.source;
|
|
1664
|
-
const needs =
|
|
2512
|
+
const needs = pageNeeds(cwd, source);
|
|
1665
2513
|
if (
|
|
1666
2514
|
needs.missingPackages.length ||
|
|
1667
|
-
notInstalled
|
|
1668
|
-
|
|
2515
|
+
needs.notInstalled.length ||
|
|
2516
|
+
needs.stylexCompiler
|
|
1669
2517
|
)
|
|
1670
2518
|
return null;
|
|
1671
2519
|
return source;
|
|
@@ -1688,6 +2536,41 @@ async function pageAgain(cwd, id, defaultPage) {
|
|
|
1688
2536
|
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditPreviewAddResponse>}
|
|
1689
2537
|
*/
|
|
1690
2538
|
export async function themeEditPreviewAdd(description, options = {}) {
|
|
2539
|
+
return reportingRecovery(box => addPreview(description, options, box));
|
|
2540
|
+
}
|
|
2541
|
+
|
|
2542
|
+
/**
|
|
2543
|
+
* Run a preview command that may first finish a change stopped part way:
|
|
2544
|
+
* a refusal after that says what the recovery wrote, in place of "Nothing
|
|
2545
|
+
* was changed".
|
|
2546
|
+
* @template T
|
|
2547
|
+
* @param {(box: {note: string | null}) => Promise<T>} fn
|
|
2548
|
+
* @returns {Promise<T>}
|
|
2549
|
+
*/
|
|
2550
|
+
async function reportingRecovery(fn) {
|
|
2551
|
+
/** @type {{note: string | null}} */
|
|
2552
|
+
const box = {note: null};
|
|
2553
|
+
try {
|
|
2554
|
+
return await fn(box);
|
|
2555
|
+
} catch (e) {
|
|
2556
|
+
const err = /** @type {any} */ (e);
|
|
2557
|
+
if (box.note && typeof err?.message === 'string' && err.code)
|
|
2558
|
+
err.message = err.message.replace(
|
|
2559
|
+
/Nothing was changed\.$/,
|
|
2560
|
+
`Before that, it ${box.note}; nothing else was changed.`,
|
|
2561
|
+
);
|
|
2562
|
+
throw e;
|
|
2563
|
+
}
|
|
2564
|
+
}
|
|
2565
|
+
|
|
2566
|
+
/**
|
|
2567
|
+
* {@link themeEditPreviewAdd}, with the note of a recovery in `box`.
|
|
2568
|
+
* @param {string | undefined} description
|
|
2569
|
+
* @param {import('./theme-edit.type.mjs').ThemeEditPreviewAddOptions} options
|
|
2570
|
+
* @param {{note: string | null}} box
|
|
2571
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditPreviewAddResponse>}
|
|
2572
|
+
*/
|
|
2573
|
+
async function addPreview(description, options, box) {
|
|
1691
2574
|
const {cwd = process.cwd()} = options;
|
|
1692
2575
|
if (isOutsideProject(cwd))
|
|
1693
2576
|
refuse(
|
|
@@ -1700,8 +2583,12 @@ export async function themeEditPreviewAdd(description, options = {}) {
|
|
|
1700
2583
|
],
|
|
1701
2584
|
},
|
|
1702
2585
|
);
|
|
1703
|
-
const pf =
|
|
1704
|
-
|
|
2586
|
+
const pf =
|
|
2587
|
+
options.path != null ? ` --path ${quoteCommandArgument(options.path)}` : '';
|
|
2588
|
+
const tf =
|
|
2589
|
+
options.theme != null
|
|
2590
|
+
? ` --theme ${quoteCommandArgument(options.theme)}`
|
|
2591
|
+
: '';
|
|
1705
2592
|
const said = (description ?? '').trim();
|
|
1706
2593
|
const named =
|
|
1707
2594
|
options.template == null ? null : String(options.template).trim();
|
|
@@ -1718,8 +2605,8 @@ export async function themeEditPreviewAdd(description, options = {}) {
|
|
|
1718
2605
|
'--template names the page; a description asks the ranking to pick one, so drop one of them.',
|
|
1719
2606
|
{
|
|
1720
2607
|
run: [
|
|
1721
|
-
`${PREVIEW} add --template ${
|
|
1722
|
-
`${PREVIEW} add ${
|
|
2608
|
+
`${PREVIEW} add --template ${quoteCommandArgument(named)}${tf}${pf}`,
|
|
2609
|
+
`${PREVIEW} add ${quoteCommandArgument(said)}${tf}${pf}`,
|
|
1723
2610
|
],
|
|
1724
2611
|
},
|
|
1725
2612
|
);
|
|
@@ -1730,7 +2617,7 @@ export async function themeEditPreviewAdd(description, options = {}) {
|
|
|
1730
2617
|
`--top takes 1, 2 or 3, not "${String(options.top)}".`,
|
|
1731
2618
|
{
|
|
1732
2619
|
run: [
|
|
1733
|
-
`${PREVIEW} add ${
|
|
2620
|
+
`${PREVIEW} add ${quoteCommandArgument(said || 'a sales dashboard')} --top 2${tf}${pf}`,
|
|
1734
2621
|
],
|
|
1735
2622
|
},
|
|
1736
2623
|
);
|
|
@@ -1743,7 +2630,7 @@ export async function themeEditPreviewAdd(description, options = {}) {
|
|
|
1743
2630
|
{
|
|
1744
2631
|
run: [
|
|
1745
2632
|
named != null
|
|
1746
|
-
? `${PREVIEW} add --template ${
|
|
2633
|
+
? `${PREVIEW} add --template ${quoteCommandArgument(named)}${tf}${pf}`
|
|
1747
2634
|
: `${PREVIEW} add 'a sales dashboard' --top ${String(options.top)}${tf}${pf}`,
|
|
1748
2635
|
],
|
|
1749
2636
|
},
|
|
@@ -1768,14 +2655,17 @@ export async function themeEditPreviewAdd(description, options = {}) {
|
|
|
1768
2655
|
);
|
|
1769
2656
|
}
|
|
1770
2657
|
const retry = (/** @type {string | null} */ p) =>
|
|
1771
|
-
`${PREVIEW} add${said ? ` ${
|
|
2658
|
+
`${PREVIEW} add${said ? ` ${quoteCommandArgument(said)}` : ''}${named != null ? ` --template ${quoteCommandArgument(named)}` : ''}${tf}${p == null ? '' : ` --path ${quoteCommandArgument(p)}`}`;
|
|
1772
2659
|
const at = previewDir(cwd, options.path, {write: true, retry});
|
|
2660
|
+
const recovered = recoverPreview(cwd, at.dir);
|
|
2661
|
+
if (recovered) notes.push(recovered);
|
|
2662
|
+
box.note = recovered;
|
|
1773
2663
|
const existing = readPreview(cwd, at.dir, pf);
|
|
1774
2664
|
/** @type {PreviewData} */
|
|
1775
2665
|
const data = existing ?? {version: 1, theme: null, pages: [], files: []};
|
|
1776
2666
|
const wore = data.theme;
|
|
1777
2667
|
const againWith = (/** @type {string | null} */ t) =>
|
|
1778
|
-
`${PREVIEW} add${said ? ` ${
|
|
2668
|
+
`${PREVIEW} add${said ? ` ${quoteCommandArgument(said)}` : ''}${named != null ? ` --template ${quoteCommandArgument(named)}` : ''}${t == null ? '' : ` --theme ${t}`}${pf}`;
|
|
1779
2669
|
const without = "leave out --theme and the pages wear the app's own theme";
|
|
1780
2670
|
if (options.theme != null) {
|
|
1781
2671
|
wornTheme(cwd, options.theme, againWith, without);
|
|
@@ -1855,23 +2745,23 @@ export async function themeEditPreviewAdd(description, options = {}) {
|
|
|
1855
2745
|
await template(pick.name, {cwd, type: 'page'})
|
|
1856
2746
|
);
|
|
1857
2747
|
const source = shownTemplate.data.source;
|
|
1858
|
-
const needs =
|
|
1859
|
-
const absent = notInstalled(cwd, source, needs.missingPackages);
|
|
2748
|
+
const needs = pageNeeds(cwd, source);
|
|
1860
2749
|
// A StyleX page calls stylex.create when its module loads, so without
|
|
1861
2750
|
// a StyleX compiler it breaks the whole route.
|
|
1862
|
-
const needsCompiler =
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
2751
|
+
const needsCompiler = needs.stylexCompiler;
|
|
2752
|
+
if (
|
|
2753
|
+
needs.missingPackages.length ||
|
|
2754
|
+
needs.notInstalled.length ||
|
|
2755
|
+
needsCompiler
|
|
2756
|
+
) {
|
|
2757
|
+
const installCommand = needs.installCommand;
|
|
1868
2758
|
skipped.push({
|
|
1869
2759
|
page: pick.name,
|
|
1870
2760
|
reason: `${skipReason({
|
|
1871
2761
|
missingPackages: needs.missingPackages,
|
|
1872
|
-
notInstalled:
|
|
2762
|
+
notInstalled: needs.notInstalled,
|
|
1873
2763
|
stylexCompiler: needsCompiler,
|
|
1874
|
-
})}${needs.
|
|
2764
|
+
})}${needs.noCommand ? ' (the app has more than one lockfile, so install it with the package manager you use)' : ''}`,
|
|
1875
2765
|
installCommand,
|
|
1876
2766
|
});
|
|
1877
2767
|
if (installCommand) runs.push(installCommand);
|
|
@@ -1987,12 +2877,13 @@ export async function themeEditPreviewAdd(description, options = {}) {
|
|
|
1987
2877
|
);
|
|
1988
2878
|
if (!data.files.includes('page.tsx'))
|
|
1989
2879
|
checkWrite(cwd, at.dir, data, {rel: 'page.tsx', text: '\0'}, elsewhere);
|
|
1990
|
-
const theme = await routeTheme(cwd, at.dir, data);
|
|
2880
|
+
const theme = await routeTheme(cwd, at.dir, data, {build: false});
|
|
1991
2881
|
for (const w of await routeWrites(cwd, at.dir, data, theme, {gone})) {
|
|
1992
2882
|
if (!writes.has(w.rel)) checkWrite(cwd, at.dir, data, w, elsewhere);
|
|
1993
2883
|
writes.set(w.rel, w);
|
|
1994
2884
|
if (!data.files.includes(w.rel)) data.files.push(w.rel);
|
|
1995
2885
|
}
|
|
2886
|
+
await buildWorn(cwd, theme, options, notes);
|
|
1996
2887
|
commitPreview(
|
|
1997
2888
|
cwd,
|
|
1998
2889
|
at.dir,
|
|
@@ -2006,8 +2897,15 @@ export async function themeEditPreviewAdd(description, options = {}) {
|
|
|
2006
2897
|
notes.push(
|
|
2007
2898
|
`moved ${move.moved.join(', ')} from ${LEGACY_PAGES_DIR}/ to ${PAGES_DIR}/, a folder Next never serves as a route`,
|
|
2008
2899
|
);
|
|
2009
|
-
|
|
2010
|
-
|
|
2900
|
+
// previews.json is a theme file: written under that theme's lock, so a
|
|
2901
|
+
// change running on the theme never takes it back.
|
|
2902
|
+
if (wore && wore !== data.theme)
|
|
2903
|
+
await themeLocked(options, wore, () =>
|
|
2904
|
+
recordPreview(cwd, wore, at.dir, false),
|
|
2905
|
+
);
|
|
2906
|
+
const worn = data.theme;
|
|
2907
|
+
if (worn)
|
|
2908
|
+
await themeLocked(options, worn, () => recordPreview(cwd, worn, at.dir));
|
|
2011
2909
|
return {
|
|
2012
2910
|
type: 'themeEdit.previewAdd',
|
|
2013
2911
|
data: {
|
|
@@ -2051,7 +2949,8 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
2051
2949
|
: wornTheme(
|
|
2052
2950
|
cwd,
|
|
2053
2951
|
slug,
|
|
2054
|
-
t =>
|
|
2952
|
+
t =>
|
|
2953
|
+
`${PREVIEW} app ${quoteCommandArgument(dir)}${t == null ? '' : ` --theme ${t}`}`,
|
|
2055
2954
|
'leave out --theme and the app wears the neutral theme',
|
|
2056
2955
|
);
|
|
2057
2956
|
// A new app has no Core installed yet: its pages come from this CLI's own
|
|
@@ -2081,7 +2980,20 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
2081
2980
|
}
|
|
2082
2981
|
};
|
|
2083
2982
|
const built = Math.min(mtime(from.js), mtime(from.css));
|
|
2084
|
-
|
|
2983
|
+
// The editor writes the settings a moment after the build, so only a
|
|
2984
|
+
// settings file clearly newer than the build means the build is stale.
|
|
2985
|
+
// Without the theme's lock (it cannot be taken), a change may be in
|
|
2986
|
+
// progress: nothing is built, and only a build that matches its
|
|
2987
|
+
// settings is copied.
|
|
2988
|
+
if (/** @type {any} */ (options).readOnly) {
|
|
2989
|
+
const why = staleBuild(cwd, slug, theme);
|
|
2990
|
+
if (why)
|
|
2991
|
+
refuse(
|
|
2992
|
+
E.ERR_THEME_BUSY,
|
|
2993
|
+
`The lock for ${slug} could not be taken, and its build is not current (${why}), so it is not copied. Nothing was changed.`,
|
|
2994
|
+
{run: [`astryx experimental theme editor show ${slug}`]},
|
|
2995
|
+
);
|
|
2996
|
+
} else if (built < 0 || mtime(from.settings) > built + 2000) {
|
|
2085
2997
|
await buildTheme(ctx, slug, theme);
|
|
2086
2998
|
notes.push(
|
|
2087
2999
|
`built ${shown(cwd, from.dir)}/ first: its build was ${built < 0 ? 'missing' : 'older than its settings'}, and the app gets a current copy`,
|
|
@@ -2195,7 +3107,7 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
2195
3107
|
cwd,
|
|
2196
3108
|
plan,
|
|
2197
3109
|
d =>
|
|
2198
|
-
`${PREVIEW} app ${
|
|
3110
|
+
`${PREVIEW} app ${quoteCommandArgument(d)}${slug == null ? '' : ` --theme ${quoteCommandArgument(slug)}`}`,
|
|
2199
3111
|
);
|
|
2200
3112
|
const folder = shownDir(cwd, plan.root);
|
|
2201
3113
|
if (
|
|
@@ -2214,7 +3126,7 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
2214
3126
|
pages: data.pages.map(x => x.id),
|
|
2215
3127
|
files,
|
|
2216
3128
|
notes,
|
|
2217
|
-
open: `cd ${
|
|
3129
|
+
open: `cd ${quoteCommandArgument(folder)} && npm install && npm run dev, then open /theme-preview`,
|
|
2218
3130
|
},
|
|
2219
3131
|
};
|
|
2220
3132
|
}
|
|
@@ -2249,18 +3161,29 @@ function readVersion(project, pkg) {
|
|
|
2249
3161
|
export async function themeEditPreviewList(options = {}) {
|
|
2250
3162
|
const {cwd = process.cwd()} = options;
|
|
2251
3163
|
requireProject(cwd);
|
|
2252
|
-
const pf =
|
|
3164
|
+
const pf =
|
|
3165
|
+
options.path != null ? ` --path ${quoteCommandArgument(options.path)}` : '';
|
|
2253
3166
|
const at = previewDir(cwd, options.path, {
|
|
2254
3167
|
optional: true,
|
|
2255
|
-
retry: p =>
|
|
3168
|
+
retry: p =>
|
|
3169
|
+
`${PREVIEW} list${p == null ? '' : ` --path ${quoteCommandArgument(p)}`}`,
|
|
2256
3170
|
});
|
|
2257
3171
|
const data = at && readPreview(cwd, at.dir, pf);
|
|
3172
|
+
/** @type {string[]} */
|
|
3173
|
+
const notes = [];
|
|
3174
|
+
if (data?.theme && !fs.existsSync(themeFiles(cwd, data.theme).dir)) {
|
|
3175
|
+
const themes = editedThemes(cwd);
|
|
3176
|
+
notes.push(
|
|
3177
|
+
`the preview wears ${data.theme}, which is gone, so its route does not build; ${PREVIEW} add${pf} switches it to ${themes.length === 1 ? themes[0] : "the app's own theme"}`,
|
|
3178
|
+
);
|
|
3179
|
+
}
|
|
2258
3180
|
return {
|
|
2259
3181
|
type: 'themeEdit.previewList',
|
|
2260
3182
|
data: {
|
|
2261
3183
|
folder: at && data ? shownDir(cwd, at.dir) : null,
|
|
2262
3184
|
theme: data?.theme ?? null,
|
|
2263
3185
|
pages: (data?.pages ?? []).map(x => ({id: x.id, label: x.label})),
|
|
3186
|
+
notes,
|
|
2264
3187
|
},
|
|
2265
3188
|
};
|
|
2266
3189
|
}
|
|
@@ -2276,19 +3199,54 @@ export async function themeEditPreviewList(options = {}) {
|
|
|
2276
3199
|
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditPreviewRemoveResponse>}
|
|
2277
3200
|
*/
|
|
2278
3201
|
export async function themeEditPreviewRemove(page, options = {}) {
|
|
3202
|
+
return reportingRecovery(box => removePreview(page, options, box));
|
|
3203
|
+
}
|
|
3204
|
+
|
|
3205
|
+
/**
|
|
3206
|
+
* {@link themeEditPreviewRemove}, with the note of a recovery in `box`.
|
|
3207
|
+
* @param {string | undefined} page
|
|
3208
|
+
* @param {import('./theme-edit.type.mjs').ThemeEditPreviewListOptions} options
|
|
3209
|
+
* @param {{note: string | null}} box
|
|
3210
|
+
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditPreviewRemoveResponse>}
|
|
3211
|
+
*/
|
|
3212
|
+
async function removePreview(page, options, box) {
|
|
2279
3213
|
const {cwd = process.cwd()} = options;
|
|
2280
3214
|
requireProject(cwd);
|
|
2281
|
-
const pf =
|
|
3215
|
+
const pf =
|
|
3216
|
+
options.path != null ? ` --path ${quoteCommandArgument(options.path)}` : '';
|
|
2282
3217
|
const retry = (/** @type {string | null} */ p) =>
|
|
2283
|
-
`${PREVIEW} remove${page != null ? ` ${
|
|
3218
|
+
`${PREVIEW} remove${page != null ? ` ${quoteCommandArgument(page)}` : ''}${p == null ? '' : ` --path ${quoteCommandArgument(p)}`}`;
|
|
2284
3219
|
const at = previewDir(cwd, options.path, {retry});
|
|
3220
|
+
const recovered = recoverPreview(cwd, at.dir);
|
|
3221
|
+
box.note = recovered;
|
|
2285
3222
|
const data = readPreview(cwd, at.dir, pf);
|
|
2286
|
-
if (!data)
|
|
3223
|
+
if (!data) {
|
|
3224
|
+
// A folder that holds the app takes no preview: the way on is the
|
|
3225
|
+
// preview elsewhere, or a new one in its own folder.
|
|
3226
|
+
/** @type {string | null} */
|
|
3227
|
+
let other = null;
|
|
3228
|
+
if (holdsApp(cwd, at.dir))
|
|
3229
|
+
try {
|
|
3230
|
+
const d = previewDir(cwd, undefined, {optional: true, retry})?.dir;
|
|
3231
|
+
if (d && d !== at.dir && fs.existsSync(path.join(d, PREVIEW_DATA)))
|
|
3232
|
+
other = d;
|
|
3233
|
+
} catch {
|
|
3234
|
+
// Several previews, or none: add is the way on.
|
|
3235
|
+
}
|
|
2287
3236
|
refuse(
|
|
2288
3237
|
E.ERR_NOT_FOUND,
|
|
2289
3238
|
`There is no theme preview in ${shownDir(cwd, at.dir)}/ (no ${PREVIEW_DATA}).`,
|
|
2290
|
-
{
|
|
3239
|
+
{
|
|
3240
|
+
run: [
|
|
3241
|
+
holdsApp(cwd, at.dir)
|
|
3242
|
+
? other
|
|
3243
|
+
? `${PREVIEW} remove${page != null ? ` ${quoteCommandArgument(page)}` : ''} # the preview in ${shownDir(cwd, other)}/`
|
|
3244
|
+
: `${PREVIEW} add`
|
|
3245
|
+
: `${PREVIEW} add${pf}`,
|
|
3246
|
+
],
|
|
3247
|
+
},
|
|
2291
3248
|
);
|
|
3249
|
+
}
|
|
2292
3250
|
const folder = shownDir(cwd, at.dir);
|
|
2293
3251
|
// theme-preview.json is a file anyone can edit: every path in it must
|
|
2294
3252
|
// stay in the route folder before anything is removed.
|
|
@@ -2297,7 +3255,7 @@ export async function themeEditPreviewRemove(page, options = {}) {
|
|
|
2297
3255
|
for (const rel of data.files) inside(rel);
|
|
2298
3256
|
for (const p of data.pages) inside(`${p.file}.tsx`);
|
|
2299
3257
|
/** @type {string[]} */
|
|
2300
|
-
const notes = [];
|
|
3258
|
+
const notes = recovered ? [recovered] : [];
|
|
2301
3259
|
/** @type {string[]} */
|
|
2302
3260
|
const kept = [];
|
|
2303
3261
|
/** @type {string[]} */
|
|
@@ -2326,17 +3284,17 @@ export async function themeEditPreviewRemove(page, options = {}) {
|
|
|
2326
3284
|
const name = shown(cwd, inside(rel));
|
|
2327
3285
|
/** @type {string[]} */
|
|
2328
3286
|
const deletes = [];
|
|
3287
|
+
const ids = new Set(data.pages.map(p => p.id));
|
|
3288
|
+
const verdict = ownedUnchanged(at.dir, data, rel, ids);
|
|
2329
3289
|
if (!data.files.includes(rel)) {
|
|
2330
3290
|
kept.push(name);
|
|
2331
3291
|
notes.push(
|
|
2332
3292
|
`${name} was not written by the preview, so it is kept; only its nav item is gone`,
|
|
2333
3293
|
);
|
|
2334
3294
|
} else if (!fs.existsSync(inside(rel))) missing.push(name);
|
|
2335
|
-
else if (
|
|
3295
|
+
else if (verdict !== 'delete') {
|
|
2336
3296
|
kept.push(name);
|
|
2337
|
-
notes.push(
|
|
2338
|
-
`${name} was changed by hand after the preview wrote it, so it is kept`,
|
|
2339
|
-
);
|
|
3297
|
+
notes.push(keptNote(name, verdict));
|
|
2340
3298
|
} else deletes.push(rel);
|
|
2341
3299
|
data.pages = data.pages.filter(x => x !== target);
|
|
2342
3300
|
data.files = data.files.filter(f => f !== rel);
|
|
@@ -2375,8 +3333,10 @@ export async function themeEditPreviewRemove(page, options = {}) {
|
|
|
2375
3333
|
}
|
|
2376
3334
|
/** @type {Array<{rel: string, text: string}>} */
|
|
2377
3335
|
let planned;
|
|
3336
|
+
/** @type {Awaited<ReturnType<typeof routeTheme>>} */
|
|
3337
|
+
let theme;
|
|
2378
3338
|
try {
|
|
2379
|
-
|
|
3339
|
+
theme = await routeTheme(cwd, at.dir, data, {build: false});
|
|
2380
3340
|
planned = await routeWrites(cwd, at.dir, data, theme, {gone});
|
|
2381
3341
|
} catch (e) {
|
|
2382
3342
|
const err = /** @type {any} */ (e);
|
|
@@ -2406,6 +3366,7 @@ export async function themeEditPreviewRemove(page, options = {}) {
|
|
|
2406
3366
|
if (!data.files.includes(w.rel)) data.files.push(w.rel);
|
|
2407
3367
|
if (was !== 'same') rewritten.push(shown(cwd, path.join(at.dir, w.rel)));
|
|
2408
3368
|
}
|
|
3369
|
+
await buildWorn(cwd, theme, options, notes);
|
|
2409
3370
|
commitPreview(
|
|
2410
3371
|
cwd,
|
|
2411
3372
|
at.dir,
|
|
@@ -2415,7 +3376,10 @@ export async function themeEditPreviewRemove(page, options = {}) {
|
|
|
2415
3376
|
retry(options.path ?? null),
|
|
2416
3377
|
);
|
|
2417
3378
|
tidyPageFolders(at.dir);
|
|
2418
|
-
if (gone)
|
|
3379
|
+
if (gone)
|
|
3380
|
+
await themeLocked(options, gone, () =>
|
|
3381
|
+
recordPreview(cwd, gone, at.dir, false),
|
|
3382
|
+
);
|
|
2419
3383
|
return {
|
|
2420
3384
|
type: 'themeEdit.previewRemove',
|
|
2421
3385
|
data: {
|
|
@@ -2438,9 +3402,15 @@ export async function themeEditPreviewRemove(page, options = {}) {
|
|
|
2438
3402
|
E.ERR_FILE_EXISTS,
|
|
2439
3403
|
`${shown(cwd, path.join(at.dir, 'page.tsx'))} was changed by hand after the preview wrote it. It imports the preview pages, so removing them would leave a route that does not build.`,
|
|
2440
3404
|
{
|
|
2441
|
-
fix: `copy your changes out of ${shown(cwd, path.join(at.dir, 'page.tsx'))},
|
|
3405
|
+
fix: `copy your changes out of ${shown(cwd, path.join(at.dir, 'page.tsx'))}, give it another name, then run ${PREVIEW} remove${pf} again`,
|
|
2442
3406
|
},
|
|
2443
3407
|
);
|
|
3408
|
+
const ids = new Set(data.pages.map(p => p.id));
|
|
3409
|
+
// A route the preview keeps still imports its pages, so they stay too.
|
|
3410
|
+
const routeKept =
|
|
3411
|
+
fs.existsSync(path.join(at.dir, 'page.tsx')) &&
|
|
3412
|
+
(!data.files.includes('page.tsx') ||
|
|
3413
|
+
ownedUnchanged(at.dir, data, 'page.tsx', ids) !== 'delete');
|
|
2444
3414
|
/** @type {Array<{file: string, before: Buffer}>} */
|
|
2445
3415
|
const plan = [];
|
|
2446
3416
|
for (const rel of data.files) {
|
|
@@ -2460,10 +3430,16 @@ export async function themeEditPreviewRemove(page, options = {}) {
|
|
|
2460
3430
|
`"files" holds ${rel}, which is a folder; the preview writes only files`,
|
|
2461
3431
|
pf,
|
|
2462
3432
|
);
|
|
2463
|
-
|
|
3433
|
+
const verdict = ownedUnchanged(at.dir, data, rel, ids);
|
|
3434
|
+
if (verdict !== 'delete') {
|
|
3435
|
+
kept.push(shown(cwd, file));
|
|
3436
|
+
notes.push(keptNote(shown(cwd, file), verdict));
|
|
3437
|
+
continue;
|
|
3438
|
+
}
|
|
3439
|
+
if (routeKept && rel !== PREVIEW_DATA) {
|
|
2464
3440
|
kept.push(shown(cwd, file));
|
|
2465
3441
|
notes.push(
|
|
2466
|
-
`${shown(cwd, file)}
|
|
3442
|
+
`${shown(cwd, file)} is kept: page.tsx stays, and it imports the pages`,
|
|
2467
3443
|
);
|
|
2468
3444
|
continue;
|
|
2469
3445
|
}
|
|
@@ -2497,10 +3473,21 @@ export async function themeEditPreviewRemove(page, options = {}) {
|
|
|
2497
3473
|
tidyPageFolders(at.dir);
|
|
2498
3474
|
try {
|
|
2499
3475
|
fs.rmdirSync(at.dir);
|
|
3476
|
+
// Route group folders, such as (tools)/, that only held the preview.
|
|
3477
|
+
for (
|
|
3478
|
+
let up = path.dirname(at.dir);
|
|
3479
|
+
/^\(.+\)$/.test(path.basename(up)) && at.app && up.startsWith(at.app);
|
|
3480
|
+
up = path.dirname(up)
|
|
3481
|
+
)
|
|
3482
|
+
fs.rmdirSync(up);
|
|
2500
3483
|
} catch {
|
|
2501
3484
|
// Not empty: something there is not the preview's.
|
|
2502
3485
|
}
|
|
2503
|
-
|
|
3486
|
+
const worn = data.theme;
|
|
3487
|
+
if (worn)
|
|
3488
|
+
await themeLocked(options, worn, () =>
|
|
3489
|
+
recordPreview(cwd, worn, at.dir, false),
|
|
3490
|
+
);
|
|
2504
3491
|
return {
|
|
2505
3492
|
type: 'themeEdit.previewRemove',
|
|
2506
3493
|
data: {folder, removed, kept, missing, rewritten: [], notes, pages: []},
|