@astryxdesign/cli 0.7.15 → 0.7.16-canary.g3aa244b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -3
- package/api/build/_adapter.d.mts +1 -0
- package/api/build/blocks/blocks.d.mts +1 -0
- package/api/build/blocks/blocks.mjs +18 -1
- package/api/component/_adapter.d.mts +1 -1
- package/api/component/_adapter.mjs +28 -1
- package/api/doctor/_adapter.d.mts +35 -0
- package/api/doctor/_adapter.mjs +69 -0
- package/api/doctor/built-checks.mjs +1 -1
- package/api/doctor/doctor.d.mts +18 -0
- package/api/doctor/doctor.doc.mjs +1 -1
- package/api/doctor/doctor.mjs +104 -4
- package/api/doctor/theme-checks.mjs +6 -10
- package/api/integration/add-contribution.mjs +3 -3
- package/api/integration/pack-check.mjs +14 -8
- package/api/integration/validate-integration.mjs +1 -1
- package/api/search/_adapter.mjs +15 -16
- package/api/search/_entries.d.mts +2 -1
- package/api/search/_entries.mjs +28 -1
- package/api/swizzle/_moduleGraph.mjs +1 -1
- package/api/theme/_adapter.d.mts +5 -2
- package/api/theme/_adapter.mjs +43 -12
- package/api/theme/_fork.mjs +1 -1
- package/api/theme/build/build.d.mts +116 -32
- package/api/theme/build/build.mjs +449 -102
- package/api/theme/eject/eject.mjs +22 -1
- package/api/theme/template/template.d.mts +19 -0
- package/api/theme/template/template.mjs +102 -25
- package/api/theme/theme.type.d.mts +5 -1
- package/api/theme/theme.type.mjs +2 -1
- package/api/theme/themeBuild.doc.mjs +3 -3
- package/api/theme/themeEject.doc.mjs +1 -1
- package/api/theme/themeTemplate.doc.mjs +9 -3
- package/api/theme-edit/_adapter.d.mts +32 -2
- package/api/theme-edit/_adapter.mjs +264 -40
- package/api/theme-edit/_check.d.mts +93 -4
- package/api/theme-edit/_check.mjs +1727 -223
- package/api/theme-edit/_color.d.mts +2 -8
- package/api/theme-edit/_color.mjs +292 -219
- package/api/theme-edit/_compiler.d.mts +1 -0
- package/api/theme-edit/_compiler.mjs +20 -1
- package/api/theme-edit/_context.d.mts +31 -1
- package/api/theme-edit/_context.mjs +445 -41
- package/api/theme-edit/_css.d.mts +60 -1
- package/api/theme-edit/_css.mjs +404 -28
- package/api/theme-edit/_edit.d.mts +25 -0
- package/api/theme-edit/_edit.mjs +1532 -149
- package/api/theme-edit/_how-commands.d.mts +11 -26
- package/api/theme-edit/_how-commands.mjs +30 -2
- package/api/theme-edit/_how-question.d.mts +7 -0
- package/api/theme-edit/_how-question.mjs +1304 -176
- package/api/theme-edit/_how.mjs +144 -30
- package/api/theme-edit/_overview.mjs +2 -0
- package/api/theme-edit/_palette.d.mts +38 -0
- package/api/theme-edit/_palette.mjs +305 -71
- package/api/theme-edit/_parts.d.mts +33 -0
- package/api/theme-edit/_parts.mjs +138 -9
- package/api/theme-edit/_png.d.mts +18 -0
- package/api/theme-edit/_png.mjs +95 -14
- package/api/theme-edit/_preview-app.d.mts +36 -1
- package/api/theme-edit/_preview-app.mjs +1415 -96
- package/api/theme-edit/_preview.d.mts +64 -3
- package/api/theme-edit/_preview.mjs +514 -31
- package/api/theme-edit/_project.d.mts +11 -0
- package/api/theme-edit/_project.mjs +46 -12
- package/api/theme-edit/_settings.d.mts +278 -5
- package/api/theme-edit/_settings.mjs +2741 -195
- package/api/theme-edit/_tables.d.mts +24 -0
- package/api/theme-edit/_tables.mjs +211 -149
- package/api/theme-edit/_vet.d.mts +42 -0
- package/api/theme-edit/_vet.mjs +144 -0
- package/api/theme-edit/theme-edit.type.d.mts +20 -2
- package/api/theme-edit/theme-edit.type.mjs +6 -2
- package/api/theme-edit/themeEditNew.doc.mjs +1 -1
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +1 -1
- package/api/theme-edit/themeEditShow.doc.mjs +11 -3
- package/api/upgrade/registry/registry.mjs +6 -1
- package/assets/codemods/parse.mjs +2 -2
- package/assets/docs/tree/single-component.doc.mjs +11 -2
- package/assets/docs/tree/tokens-and-setup.doc.mjs +39 -3
- package/assets/docs.generated.json +2 -2
- package/assets/preview-app/next.config.mjs +7 -1
- package/assets/preview-app/package.json +1 -0
- package/assets/preview-app/src/app/globals.css +1 -0
- package/assets/self-audit.generated.json +1 -1
- package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.tsx +53 -0
- package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.tsx +33 -0
- package/assets/templates/components.generated.json +1 -1
- package/assets/templates/pages/dashboard/page.tsx +1 -1
- package/authoring/doctypes/_schema.d.mts +52 -5
- package/authoring/doctypes/_schema.mjs +62 -7
- package/authoring/doctypes/base/type.ts +10 -0
- package/authoring/doctypes/component/component.doc.mjs +1 -1
- package/authoring/doctypes/function/function.doc.mjs +6 -0
- package/authoring/doctypes/function/type.ts +3 -0
- package/authoring/doctypes/legacy.mjs +5 -1
- package/authoring/index.d.ts +1 -0
- package/authoring/shadcn/source-variants.d.mts +1 -1
- package/authoring/shadcn/source-variants.mjs +32 -10
- package/clients/cli/build-id.generated.json +1 -1
- package/clients/cli/commands/doctor.doc.mjs +2 -2
- package/clients/cli/commands/experimental/theme.mjs +239 -45
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +3 -3
- 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 +1 -1
- 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-template.doc.mjs +2 -2
- package/clients/cli/commands/theme-build.doc.mjs +2 -2
- package/clients/cli/commands/theme-eject.doc.mjs +1 -1
- package/clients/cli/commands/theme-template.doc.mjs +3 -1
- package/clients/cli/commands/theme.mjs +190 -46
- package/clients/cli/formatters/index.mjs +7 -4
- package/clients/cli/lib/cli-error.mjs +9 -3
- package/clients/cli/lib/component-format.mjs +27 -9
- package/clients/cli/lib/json-shim.mjs +72 -2
- package/clients/cli/manifest.generated.json +1 -1
- package/clients/cli/run.mjs +49 -0
- package/foundation/agent-docs/agent-docs-target.d.mts +2 -6
- package/foundation/agent-docs/agent-docs-target.mjs +9 -17
- package/foundation/agent-docs/agent-docs.mjs +3 -2
- package/foundation/config/config-writer.mjs +2 -2
- package/foundation/config/project.mjs +60 -14
- package/foundation/discovery/component-discovery.mjs +5 -17
- package/foundation/discovery/icon-set-writer.mjs +1 -1
- package/foundation/discovery/module-exports.mjs +1 -15
- package/foundation/discovery/template-adapter.mjs +44 -19
- package/foundation/discovery/template-source-tree.mjs +3 -16
- package/foundation/discovery/theme-discovery.d.mts +5 -1
- package/foundation/discovery/theme-discovery.mjs +61 -43
- package/foundation/fs/exact-codec.d.mts +35 -0
- package/foundation/fs/exact-codec.mjs +148 -0
- package/foundation/fs/gather-cache.d.mts +15 -5
- package/foundation/fs/gather-cache.mjs +129 -31
- package/foundation/fs/paths.d.mts +39 -0
- package/foundation/fs/paths.mjs +44 -11
- package/foundation/fs/read-record.mjs +11 -1
- package/foundation/integrations/contribution-fixes.mjs +1 -16
- package/foundation/integrations/may-have-integrations.mjs +3 -2
- package/foundation/integrations/public-imports.mjs +1 -1
- package/foundation/render/embedded-imports.mjs +1 -1
- package/foundation/render/host-exit.d.mts +8 -0
- package/foundation/render/host-exit.mjs +11 -0
- package/foundation/render/page-host.mjs +56 -10
- package/foundation/render/page-worker.mjs +14 -0
- package/foundation/render/render-page.mjs +37 -2
- package/foundation/response/error-codes.d.mts +2 -1
- package/foundation/response/error-codes.d.ts +1 -0
- package/foundation/response/error-codes.doc.mjs +5 -0
- package/foundation/response/error-codes.mjs +3 -0
- package/foundation/response/json.d.mts +15 -0
- package/foundation/response/json.mjs +52 -1
- package/foundation/response/response-types.doc.mjs +1 -1
- package/foundation/runtime/lazy-dependencies.d.mts +14 -0
- package/foundation/runtime/lazy-dependencies.mjs +82 -0
- package/foundation/xle/browser.d.mts +2 -2
- package/package.json +7 -6
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
import {parse as parseModule} from '@babel/parser';
|
|
14
14
|
import * as fs from 'node:fs';
|
|
15
15
|
import * as path from 'node:path';
|
|
16
|
+
import {createRequire} from 'node:module';
|
|
16
17
|
import {fileURLToPath} from 'node:url';
|
|
17
18
|
import {cliVersion} from '../../foundation/release/run-context.mjs';
|
|
18
19
|
import {isOutsideProject} from '../../foundation/env/project-scope.mjs';
|
|
@@ -21,9 +22,10 @@ import {
|
|
|
21
22
|
PathSafetyError,
|
|
22
23
|
} from '../../foundation/fs/path-safety.mjs';
|
|
23
24
|
import {baseResolved, installLine, openProject} from './_project.mjs';
|
|
24
|
-
import {P} from './_tables.mjs';
|
|
25
|
+
import {P, wordTable} from './_tables.mjs';
|
|
25
26
|
import {
|
|
26
27
|
buildTheme,
|
|
28
|
+
camel,
|
|
27
29
|
editedThemes,
|
|
28
30
|
fontPackages,
|
|
29
31
|
loadTheme,
|
|
@@ -33,16 +35,20 @@ import {
|
|
|
33
35
|
shown,
|
|
34
36
|
staleBuild,
|
|
35
37
|
themeFiles,
|
|
38
|
+
usableNewName,
|
|
36
39
|
windowsReserved,
|
|
37
40
|
} from './_settings.mjs';
|
|
38
41
|
import {
|
|
42
|
+
baseFontsCss,
|
|
39
43
|
colorsPageSource,
|
|
40
44
|
componentsPageSource,
|
|
41
45
|
overviewPageSource,
|
|
42
46
|
previewRouteSource,
|
|
47
|
+
appCopyBlocker,
|
|
43
48
|
themeImports,
|
|
44
49
|
} from './_preview.mjs';
|
|
45
50
|
import {
|
|
51
|
+
isEditorTemp,
|
|
46
52
|
PALETTE_STAND_IN,
|
|
47
53
|
paletteFiles,
|
|
48
54
|
paletteMapping,
|
|
@@ -60,9 +66,13 @@ import {
|
|
|
60
66
|
import {detectStylingSystem} from '../../foundation/agent-docs/agent-docs.mjs';
|
|
61
67
|
import {requireProject, context} from './_context.mjs';
|
|
62
68
|
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
|
|
63
|
-
import {
|
|
69
|
+
import {
|
|
70
|
+
formatCliCommand,
|
|
71
|
+
quoteCommandArgument,
|
|
72
|
+
} from '../../foundation/env/package-manager.mjs';
|
|
64
73
|
import {sha256} from '../../foundation/render/source-block.mjs';
|
|
65
74
|
import {findNodeModulesPackage} from '../../foundation/fs/paths.mjs';
|
|
75
|
+
import {normalizeEntry} from './_check.mjs';
|
|
66
76
|
|
|
67
77
|
const E = ERROR_CODES;
|
|
68
78
|
|
|
@@ -84,7 +94,9 @@ const MAX_WORDS = 40;
|
|
|
84
94
|
|
|
85
95
|
/**
|
|
86
96
|
* @typedef {{id: string, label: string, file: string}} PreviewPage
|
|
87
|
-
* @typedef {{version: 1, theme: string | null, pages: PreviewPage[], files: string[], hashes?: Record<string, string
|
|
97
|
+
* @typedef {{version: 1, theme: string | null, pages: PreviewPage[], files: string[], hashes?: Record<string, string>, parents?: number}} PreviewData
|
|
98
|
+
* `parents`: how many folders over the preview folder its first add made,
|
|
99
|
+
* which remove takes away again when they are empty.
|
|
88
100
|
* `hashes`: a short hash of each file as the preview wrote it, so a file
|
|
89
101
|
* changed by hand is never replaced or deleted
|
|
90
102
|
* @typedef {(path: string | null) => string} Retry the command again, with
|
|
@@ -134,6 +146,97 @@ function checkFolderName(where, what, retry) {
|
|
|
134
146
|
);
|
|
135
147
|
}
|
|
136
148
|
|
|
149
|
+
/**
|
|
150
|
+
* Whether a stylesheet comes in through the files the root layout imports,
|
|
151
|
+
* followed through relative and `@/` imports (a root `styles/globals.css`,
|
|
152
|
+
* as create-next-app writes it), a few levels deep.
|
|
153
|
+
* @param {string} cwd
|
|
154
|
+
* @param {string} app the routes folder
|
|
155
|
+
* @param {RegExp} sheet
|
|
156
|
+
*/
|
|
157
|
+
function layoutImportsSheet(cwd, app, sheet, dir = app) {
|
|
158
|
+
// Every layout over the preview: the routes folder's, and one in a route
|
|
159
|
+
// group or [param] folder on the way.
|
|
160
|
+
/** @type {string[]} */
|
|
161
|
+
const starts = [];
|
|
162
|
+
for (let at = dir; ; at = path.dirname(at)) {
|
|
163
|
+
for (const x of ['tsx', 'ts', 'jsx', 'js'])
|
|
164
|
+
if (fs.existsSync(path.join(at, `layout.${x}`)))
|
|
165
|
+
starts.push(path.join(at, `layout.${x}`));
|
|
166
|
+
if (at === app || !at.startsWith(app + path.sep)) break;
|
|
167
|
+
}
|
|
168
|
+
if (!starts.length) return false;
|
|
169
|
+
const aliases = tsconfigAliases(cwd);
|
|
170
|
+
const seen = new Set();
|
|
171
|
+
/** @param {string} file @param {number} depth @returns {boolean} */
|
|
172
|
+
const reads = (file, depth) => {
|
|
173
|
+
if (seen.has(file) || depth > 4) return false;
|
|
174
|
+
seen.add(file);
|
|
175
|
+
/** @type {string} */
|
|
176
|
+
let text;
|
|
177
|
+
try {
|
|
178
|
+
text = fs.readFileSync(file, 'utf8');
|
|
179
|
+
} catch {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
if (sheet.test(text)) return true;
|
|
183
|
+
for (const m of text.matchAll(
|
|
184
|
+
/(?:import|@import)\s*(?:[^'"]*from\s*)?(?:url\()?['"]([^'"]+)['"]/g,
|
|
185
|
+
)) {
|
|
186
|
+
const spec = m[1];
|
|
187
|
+
// An alias the tsconfig sets; else `@/` is the src folder or the
|
|
188
|
+
// project root.
|
|
189
|
+
const alias = aliases.find(a => spec.startsWith(a.prefix));
|
|
190
|
+
const bases = alias
|
|
191
|
+
? alias.to.map(t => path.join(t, spec.slice(alias.prefix.length)))
|
|
192
|
+
: spec.startsWith('@/')
|
|
193
|
+
? [
|
|
194
|
+
path.join(cwd, 'src', spec.slice(2)),
|
|
195
|
+
path.join(cwd, spec.slice(2)),
|
|
196
|
+
]
|
|
197
|
+
: spec.startsWith('.')
|
|
198
|
+
? [path.resolve(path.dirname(file), spec)]
|
|
199
|
+
: [];
|
|
200
|
+
const hit = bases
|
|
201
|
+
.flatMap(b =>
|
|
202
|
+
['', '.css', '.scss', '.ts', '.tsx', '.js', '.jsx'].map(x => b + x),
|
|
203
|
+
)
|
|
204
|
+
.find(f => fs.existsSync(f) && fs.statSync(f).isFile());
|
|
205
|
+
if (hit && reads(hit, depth + 1)) return true;
|
|
206
|
+
}
|
|
207
|
+
return false;
|
|
208
|
+
};
|
|
209
|
+
return starts.some(start => reads(start, 0));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The import aliases the app's tsconfig `paths` set (`~/*`, `#app/*`), each
|
|
214
|
+
* with the folders it stands for.
|
|
215
|
+
* @param {string} cwd
|
|
216
|
+
* @returns {Array<{prefix: string, to: string[]}>}
|
|
217
|
+
*/
|
|
218
|
+
function tsconfigAliases(cwd) {
|
|
219
|
+
const o = appCompilerOptions(cwd);
|
|
220
|
+
const paths = o && typeof o.paths === 'object' && o.paths ? o.paths : {};
|
|
221
|
+
const baseUrl = path.resolve(
|
|
222
|
+
cwd,
|
|
223
|
+
typeof o?.baseUrl === 'string' ? o.baseUrl : '.',
|
|
224
|
+
);
|
|
225
|
+
return Object.entries(/** @type {Record<string, unknown>} */ (paths)).flatMap(
|
|
226
|
+
([key, targets]) =>
|
|
227
|
+
key.endsWith('/*') && Array.isArray(targets)
|
|
228
|
+
? [
|
|
229
|
+
{
|
|
230
|
+
prefix: key.slice(0, -1),
|
|
231
|
+
to: targets
|
|
232
|
+
.filter(t => typeof t === 'string' && t.endsWith('/*'))
|
|
233
|
+
.map(t => path.resolve(baseUrl, String(t).slice(0, -2))),
|
|
234
|
+
},
|
|
235
|
+
]
|
|
236
|
+
: [],
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
137
240
|
/**
|
|
138
241
|
* A note when the app never imports Core's stylesheet, or null: without
|
|
139
242
|
* it, components render unstyled, and some show nothing. Read from the
|
|
@@ -180,7 +283,8 @@ function coreStylesheetMissing(cwd, at) {
|
|
|
180
283
|
const src = path.join(cwd, 'src');
|
|
181
284
|
if (
|
|
182
285
|
imports(at.app) ||
|
|
183
|
-
(at.app.startsWith(src + path.sep) ? false : imports(src))
|
|
286
|
+
(at.app.startsWith(src + path.sep) ? false : imports(src)) ||
|
|
287
|
+
layoutImportsSheet(cwd, at.app, SHEET, at.dir)
|
|
184
288
|
)
|
|
185
289
|
return null;
|
|
186
290
|
const layout = ['tsx', 'ts', 'jsx', 'js']
|
|
@@ -204,11 +308,12 @@ function readNextConfig(cwd) {
|
|
|
204
308
|
basePath: undefined,
|
|
205
309
|
distDir: undefined,
|
|
206
310
|
};
|
|
311
|
+
// The order Next loads them in; it refuses next.config.cjs.
|
|
207
312
|
for (const name of [
|
|
208
|
-
'next.config.mjs',
|
|
209
313
|
'next.config.js',
|
|
314
|
+
'next.config.mjs',
|
|
210
315
|
'next.config.ts',
|
|
211
|
-
'next.config.
|
|
316
|
+
'next.config.mts',
|
|
212
317
|
]) {
|
|
213
318
|
/** @type {string} */
|
|
214
319
|
let text;
|
|
@@ -270,6 +375,11 @@ function readNextConfig(cwd) {
|
|
|
270
375
|
let exported = null;
|
|
271
376
|
for (const st of body) {
|
|
272
377
|
if (st.type === 'ExportDefaultDeclaration') exported = st.declaration;
|
|
378
|
+
// export {config as default}
|
|
379
|
+
if (st.type === 'ExportNamedDeclaration' && !st.source)
|
|
380
|
+
for (const sp of st.specifiers ?? [])
|
|
381
|
+
if ((sp.exported?.name ?? sp.exported?.value) === 'default')
|
|
382
|
+
exported = sp.local;
|
|
273
383
|
if (
|
|
274
384
|
st.type === 'ExpressionStatement' &&
|
|
275
385
|
st.expression.type === 'AssignmentExpression' &&
|
|
@@ -281,6 +391,66 @@ function readNextConfig(cwd) {
|
|
|
281
391
|
}
|
|
282
392
|
const config = exported && configOf(exported);
|
|
283
393
|
if (!config) return exported ? unknown : none;
|
|
394
|
+
const wrapped = resolve(exported)?.type === 'CallExpression';
|
|
395
|
+
// Changes to the config after it is made: `config.key = value` sets a
|
|
396
|
+
// key (the last one counts), and anything else (Object.assign, a
|
|
397
|
+
// computed key) makes every key unknown.
|
|
398
|
+
const names = new Set(
|
|
399
|
+
[...consts]
|
|
400
|
+
.filter(([, init]) => resolve(init) === config)
|
|
401
|
+
.map(([n]) => n),
|
|
402
|
+
);
|
|
403
|
+
/** @type {Map<string, any>} */
|
|
404
|
+
const later = new Map();
|
|
405
|
+
let changed = false;
|
|
406
|
+
// Only a statement at the top of the file surely runs: a key set inside
|
|
407
|
+
// an `if` or a function, or deleted, is known only by running it.
|
|
408
|
+
const UNSURE = Symbol('unsure');
|
|
409
|
+
const topLevel = new Set(
|
|
410
|
+
body
|
|
411
|
+
.filter(st => st.type === 'ExpressionStatement')
|
|
412
|
+
.map(st => st.expression),
|
|
413
|
+
);
|
|
414
|
+
/** @param {any} n */
|
|
415
|
+
const visit = n => {
|
|
416
|
+
if (!n || typeof n !== 'object') return;
|
|
417
|
+
if (Array.isArray(n)) return n.forEach(visit);
|
|
418
|
+
if (
|
|
419
|
+
n.type === 'AssignmentExpression' &&
|
|
420
|
+
n.left?.type === 'MemberExpression' &&
|
|
421
|
+
names.has(n.left.object?.name)
|
|
422
|
+
) {
|
|
423
|
+
if (n.left.computed) changed = true;
|
|
424
|
+
else
|
|
425
|
+
later.set(
|
|
426
|
+
n.left.property.name,
|
|
427
|
+
topLevel.has(n) && n.operator === '=' ? n.right : UNSURE,
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
if (
|
|
431
|
+
n.type === 'UnaryExpression' &&
|
|
432
|
+
n.operator === 'delete' &&
|
|
433
|
+
n.argument?.type === 'MemberExpression' &&
|
|
434
|
+
names.has(n.argument.object?.name)
|
|
435
|
+
) {
|
|
436
|
+
if (n.argument.computed) changed = true;
|
|
437
|
+
else later.set(n.argument.property.name, UNSURE);
|
|
438
|
+
}
|
|
439
|
+
if (
|
|
440
|
+
n.type === 'CallExpression' &&
|
|
441
|
+
n.arguments?.some(
|
|
442
|
+
(/** @type {any} */ a) =>
|
|
443
|
+
a.type === 'Identifier' && names.has(a.name),
|
|
444
|
+
) &&
|
|
445
|
+
n.callee?.type === 'MemberExpression' &&
|
|
446
|
+
n.callee.object?.name === 'Object'
|
|
447
|
+
)
|
|
448
|
+
changed = true;
|
|
449
|
+
for (const [k, v] of Object.entries(n))
|
|
450
|
+
if (k !== 'loc' && k !== 'start' && k !== 'end') visit(v);
|
|
451
|
+
};
|
|
452
|
+
visit(body);
|
|
453
|
+
if (changed) return unknown;
|
|
284
454
|
// The config's keys in order, with a spread of a constant object read
|
|
285
455
|
// in place; a spread of anything else may set any key (`null`).
|
|
286
456
|
/** @type {Array<{key: string, value: any} | null>} */
|
|
@@ -302,8 +472,10 @@ function readNextConfig(cwd) {
|
|
|
302
472
|
}
|
|
303
473
|
};
|
|
304
474
|
flatten(config, 0);
|
|
305
|
-
/** The entry that sets a key last; null when an unreadable spread may; undefined when nothing does. @param {string} key */
|
|
475
|
+
/** The entry that sets a key last: a later assignment, else the last key; null when an unreadable spread may; undefined when nothing does. @param {string} key */
|
|
306
476
|
const prop = key => {
|
|
477
|
+
if (later.has(key))
|
|
478
|
+
return later.get(key) === UNSURE ? null : {key, value: later.get(key)};
|
|
307
479
|
for (let i = entries.length - 1; i >= 0; i--) {
|
|
308
480
|
const e = entries[i];
|
|
309
481
|
if (e === null) return null;
|
|
@@ -332,15 +504,18 @@ function readNextConfig(cwd) {
|
|
|
332
504
|
const out = n.elements.map((/** @type {any} */ e) => str(e));
|
|
333
505
|
return out.every((/** @type {any} */ e) => e != null) ? out : null;
|
|
334
506
|
}),
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
507
|
+
// A wrapper such as withX(config) may add a basePath of its own.
|
|
508
|
+
basePath: wrapped
|
|
509
|
+
? null
|
|
510
|
+
: value('basePath', n => {
|
|
511
|
+
const v = str(n);
|
|
512
|
+
return v == null ? null : v.replace(/\/+$/, '');
|
|
513
|
+
}),
|
|
339
514
|
distDir: value('distDir', n => {
|
|
340
515
|
const v = str(n);
|
|
341
516
|
return v == null
|
|
342
517
|
? null
|
|
343
|
-
:
|
|
518
|
+
: v.replace(/^(\.\/)+/, '').replace(/\/+$/, '') || null;
|
|
344
519
|
}),
|
|
345
520
|
};
|
|
346
521
|
}
|
|
@@ -394,9 +569,15 @@ function reservedFolder(cwd, dir) {
|
|
|
394
569
|
return 'node_modules/ belongs to the package manager, which replaces it';
|
|
395
570
|
if (segs[0] === 'public')
|
|
396
571
|
return 'Next serves every file in public/ as it is, at the site root, so anyone could download it';
|
|
572
|
+
// The build folders by their real path: a distDir with a subfolder
|
|
573
|
+
// (src/.next-build) covers that folder only, not the rest of src/.
|
|
397
574
|
const built = [
|
|
398
575
|
...new Set(['.next', 'out', readNextConfig(cwd).distDir]),
|
|
399
|
-
].find(d =>
|
|
576
|
+
].find(d => {
|
|
577
|
+
if (typeof d !== 'string' || !d.trim()) return false;
|
|
578
|
+
const at = path.resolve(cwd, d);
|
|
579
|
+
return at !== cwd && (dir === at || dir.startsWith(at + path.sep));
|
|
580
|
+
});
|
|
400
581
|
if (built)
|
|
401
582
|
return `${built}/ is where Next writes its build, and the next build empties it`;
|
|
402
583
|
const odd = segs.find(seg => windowsReserved(seg) || /^\.+$/.test(seg));
|
|
@@ -407,6 +588,134 @@ function reservedFolder(cwd, dir) {
|
|
|
407
588
|
return null;
|
|
408
589
|
}
|
|
409
590
|
|
|
591
|
+
/**
|
|
592
|
+
* The app's tsconfig.json compiler options (comments and trailing commas
|
|
593
|
+
* allowed), or null when there is none.
|
|
594
|
+
* @param {string} cwd
|
|
595
|
+
* @returns {Record<string, unknown> | null}
|
|
596
|
+
*/
|
|
597
|
+
function appCompilerOptions(cwd) {
|
|
598
|
+
/** @type {string} */
|
|
599
|
+
let text;
|
|
600
|
+
try {
|
|
601
|
+
text = fs.readFileSync(path.join(cwd, 'tsconfig.json'), 'utf8');
|
|
602
|
+
} catch {
|
|
603
|
+
return null;
|
|
604
|
+
}
|
|
605
|
+
try {
|
|
606
|
+
const json = text
|
|
607
|
+
.replace(
|
|
608
|
+
/("(?:[^"\\]|\\.)*")|\/\*[\s\S]*?\*\/|\/\/.*$/gm,
|
|
609
|
+
(m, str) => str ?? '',
|
|
610
|
+
)
|
|
611
|
+
.replace(/,(\s*[}\]])/g, '$1');
|
|
612
|
+
const o = JSON.parse(json)?.compilerOptions;
|
|
613
|
+
return o && typeof o === 'object' ? o : {};
|
|
614
|
+
} catch {
|
|
615
|
+
return {};
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* Notes on what the preview's TypeScript files need from the app: an app
|
|
621
|
+
* with no TypeScript gets the install line (Next sets it up on the next
|
|
622
|
+
* build otherwise), and an app whose tsconfig turns on
|
|
623
|
+
* `noUncheckedIndexedAccess` is told which template pages were written as
|
|
624
|
+
* their template ships them, which may not pass it.
|
|
625
|
+
* @param {string} cwd
|
|
626
|
+
* @param {string} dir
|
|
627
|
+
* @param {string[]} added page ids added by this command
|
|
628
|
+
* @returns {string[]}
|
|
629
|
+
*/
|
|
630
|
+
function typeScriptNotes(cwd, dir, added) {
|
|
631
|
+
const options = appCompilerOptions(cwd);
|
|
632
|
+
if (!options) {
|
|
633
|
+
const hasTs = (() => {
|
|
634
|
+
try {
|
|
635
|
+
return Boolean(
|
|
636
|
+
createRequire(path.join(cwd, 'package.json')).resolve('typescript'),
|
|
637
|
+
);
|
|
638
|
+
} catch {
|
|
639
|
+
return false;
|
|
640
|
+
}
|
|
641
|
+
})();
|
|
642
|
+
return hasTs
|
|
643
|
+
? []
|
|
644
|
+
: [
|
|
645
|
+
`the app has no TypeScript, and the preview's files are .tsx, so the next build sets it up; to do it now: ${installLine(cwd, ['typescript', '@types/react', '@types/node']).replace(/ (add|install) /, ' $1 -D ')}`,
|
|
646
|
+
];
|
|
647
|
+
}
|
|
648
|
+
const templates = added.filter(
|
|
649
|
+
id => !['overview', 'components', COLORS_PAGE.id].includes(id),
|
|
650
|
+
);
|
|
651
|
+
if (options.noUncheckedIndexedAccess !== true || !templates.length) return [];
|
|
652
|
+
const files = templates.map(id =>
|
|
653
|
+
shown(cwd, path.join(dir, PAGES_DIR, `${id}.tsx`)),
|
|
654
|
+
);
|
|
655
|
+
return [
|
|
656
|
+
`the app's tsconfig.json turns on noUncheckedIndexedAccess, and ${files.join(', ')} ${files.length > 1 ? 'are' : 'is'} written as the page template ships, which may not pass it; if the build stops there, fix the reported lines or remove the page with ${PREVIEW} remove ${templates[0]}`,
|
|
657
|
+
];
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* Why a file cannot be read (its error code), or null when it can.
|
|
662
|
+
* @param {string} file
|
|
663
|
+
* @returns {string | null}
|
|
664
|
+
*/
|
|
665
|
+
function unreadable(file) {
|
|
666
|
+
try {
|
|
667
|
+
fs.closeSync(fs.openSync(file, 'r'));
|
|
668
|
+
return null;
|
|
669
|
+
} catch (e) {
|
|
670
|
+
const code = /** @type {any} */ (e)?.code;
|
|
671
|
+
return code === 'ENOENT' ? null : (code ?? 'error');
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* Whether a folder beside `dir` has its name in another case.
|
|
677
|
+
* @param {string} dir
|
|
678
|
+
*/
|
|
679
|
+
function twinBeside(dir) {
|
|
680
|
+
const name = path.basename(dir);
|
|
681
|
+
try {
|
|
682
|
+
return fs
|
|
683
|
+
.readdirSync(path.dirname(dir))
|
|
684
|
+
.some(n => n !== name && n.toLowerCase() === name.toLowerCase());
|
|
685
|
+
} catch {
|
|
686
|
+
return false;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
/**
|
|
691
|
+
* A folder beside `dir`, or beside one of its parents, whose name differs
|
|
692
|
+
* only in case, or null.
|
|
693
|
+
* @param {string} dir
|
|
694
|
+
* @returns {string | null}
|
|
695
|
+
*/
|
|
696
|
+
function sameCaseSibling(dir) {
|
|
697
|
+
for (let at = dir; path.dirname(at) !== at; at = path.dirname(at)) {
|
|
698
|
+
const parent = path.dirname(at);
|
|
699
|
+
const name = path.basename(at);
|
|
700
|
+
/** @type {string[]} */
|
|
701
|
+
let names;
|
|
702
|
+
try {
|
|
703
|
+
names = fs.readdirSync(parent);
|
|
704
|
+
} catch {
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
const twin = names.find(
|
|
708
|
+
n => n !== name && n.toLowerCase() === name.toLowerCase(),
|
|
709
|
+
);
|
|
710
|
+
if (twin) return path.join(parent, twin);
|
|
711
|
+
if (fs.existsSync(path.join(parent, 'package.json'))) break;
|
|
712
|
+
}
|
|
713
|
+
return null;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
/** An intercepting route folder: `(.)x`, `(..)x`, `(..)(..)x`, `(...)x`. */
|
|
717
|
+
const INTERCEPT = /^\((\.{1,3})\)/;
|
|
718
|
+
|
|
410
719
|
/**
|
|
411
720
|
* Why Next does not serve a folder inside the routes folder at its own
|
|
412
721
|
* URL, or null: a `_private` folder is never routed, an `@slot` folder is a
|
|
@@ -418,7 +727,9 @@ function unroutedSegment(app, dir) {
|
|
|
418
727
|
const seg = path
|
|
419
728
|
.relative(app, dir)
|
|
420
729
|
.split(path.sep)
|
|
421
|
-
.find(x => /^[_@[]/.test(x));
|
|
730
|
+
.find(x => /^[_@[]/.test(x) || INTERCEPT.test(x));
|
|
731
|
+
if (seg && INTERCEPT.test(seg))
|
|
732
|
+
return `Next reads the folder ${seg} as an intercepting route ((.), (..) and (...) folders show another route's page), not a URL of its own, and a page there breaks the app`;
|
|
422
733
|
return seg
|
|
423
734
|
? `Next does not serve the folder ${seg} at a URL of its own (_private folders are never routed, @slot and [param] folders are not plain URL segments)`
|
|
424
735
|
: null;
|
|
@@ -615,6 +926,75 @@ function checkPlacement(cwd, dir, where, retry) {
|
|
|
615
926
|
`${where} is no place for the preview: ${why}.`,
|
|
616
927
|
{run: [fallback]},
|
|
617
928
|
);
|
|
929
|
+
// A folder name that is not a plain URL segment (a space, an accent,
|
|
930
|
+
// ?, #, %) is not served at the URL it seems to have. The characters a
|
|
931
|
+
// URL path takes as they are (RFC 3986 unreserved and sub-delims, @, ~)
|
|
932
|
+
// are served.
|
|
933
|
+
const PLAIN = /^[A-Za-z0-9._~!$&'()+,;=@-]+$/;
|
|
934
|
+
const segs = app ? path.relative(app, dir).split(path.sep) : [];
|
|
935
|
+
const odd = segs.filter(
|
|
936
|
+
seg => seg && !/^\(.+\)$|^\[.+\]$/.test(seg) && !PLAIN.test(seg),
|
|
937
|
+
);
|
|
938
|
+
if (app && odd.length) {
|
|
939
|
+
const slug = (/** @type {string} */ seg) =>
|
|
940
|
+
seg
|
|
941
|
+
.normalize('NFD')
|
|
942
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
943
|
+
.replace(/[^A-Za-z0-9._-]+/g, '-')
|
|
944
|
+
.replace(/^-+|-+$/g, '')
|
|
945
|
+
.toLowerCase() || 'theme-preview';
|
|
946
|
+
// Every odd segment slugged, then the first free folder from it.
|
|
947
|
+
const base = path.join(
|
|
948
|
+
app,
|
|
949
|
+
...segs.map(seg => (odd.includes(seg) ? slug(seg) : seg)),
|
|
950
|
+
);
|
|
951
|
+
let fixed = base;
|
|
952
|
+
for (
|
|
953
|
+
let n = 2;
|
|
954
|
+
fs.existsSync(fixed) || twinBeside(fixed) || sameUrlPage(cwd, app, fixed);
|
|
955
|
+
n++
|
|
956
|
+
)
|
|
957
|
+
fixed = `${base}-${n}`;
|
|
958
|
+
refuse(
|
|
959
|
+
E.ERR_INVALID_ARGUMENT,
|
|
960
|
+
`${where} is no place for the preview: Next does not serve the folder "${odd[0]}" at the URL its name seems to give (a space, a letter outside A-Z, %, # or ? are no plain URL segment).`,
|
|
961
|
+
{run: [retry(shown(cwd, fixed))]},
|
|
962
|
+
);
|
|
963
|
+
}
|
|
964
|
+
// A folder whose name differs from a folder beside it only in case is the
|
|
965
|
+
// same folder on macOS and Windows: one of them is lost in a checkout.
|
|
966
|
+
const twin = sameCaseSibling(dir);
|
|
967
|
+
if (twin) {
|
|
968
|
+
// A twin of a folder on the way: the same path through the folder that
|
|
969
|
+
// is there. A twin of the folder itself: the first free name beside it.
|
|
970
|
+
const base = path.dirname(twin);
|
|
971
|
+
const rel = path.relative(base, dir).split(path.sep);
|
|
972
|
+
// Both spellings are folders already: no path through either is free,
|
|
973
|
+
// so the way on is a new folder beside them.
|
|
974
|
+
const bothThere = rel.length > 1 && fs.existsSync(path.join(base, rel[0]));
|
|
975
|
+
let free = path.join(twin, ...rel.slice(1));
|
|
976
|
+
if (rel.length === 1 || bothThere) {
|
|
977
|
+
const start = rel.length === 1 ? dir : path.join(base, 'theme-preview');
|
|
978
|
+
free = start;
|
|
979
|
+
for (let n = 2; fs.existsSync(free) || twinBeside(free); n++)
|
|
980
|
+
free = `${start}-${n}`;
|
|
981
|
+
}
|
|
982
|
+
const existing = fs.existsSync(path.join(dir, PREVIEW_DATA));
|
|
983
|
+
refuse(
|
|
984
|
+
E.ERR_INVALID_ARGUMENT,
|
|
985
|
+
`${where} is no place for the preview: ${shownDir(cwd, twin)}/ differs from it only in case, and on macOS and Windows they are one folder, so the project could not be checked out there.${existing ? ` The preview already there can still be removed.` : ''}`,
|
|
986
|
+
{
|
|
987
|
+
run: [
|
|
988
|
+
retry(shown(cwd, free)),
|
|
989
|
+
...(existing
|
|
990
|
+
? [
|
|
991
|
+
`${PREVIEW} remove --path ${quoteCommandArgument(shownDir(cwd, dir))}`,
|
|
992
|
+
]
|
|
993
|
+
: []),
|
|
994
|
+
],
|
|
995
|
+
},
|
|
996
|
+
);
|
|
997
|
+
}
|
|
618
998
|
if (!app) return;
|
|
619
999
|
if (served && app !== served)
|
|
620
1000
|
refuse(
|
|
@@ -646,18 +1026,19 @@ function checkPlacement(cwd, dir, where, retry) {
|
|
|
646
1026
|
);
|
|
647
1027
|
}
|
|
648
1028
|
if (
|
|
649
|
-
served
|
|
650
|
-
|
|
651
|
-
|
|
1029
|
+
(app && !served) ||
|
|
1030
|
+
(served &&
|
|
1031
|
+
!rootLayouts.length &&
|
|
1032
|
+
!fs.readdirSync(served).some(f => /^layout\.(tsx|ts|jsx|js)$/.test(f)))
|
|
652
1033
|
) {
|
|
653
1034
|
let free = 'theme-app';
|
|
654
1035
|
for (let n = 2; fs.existsSync(path.join(cwd, free)); n++)
|
|
655
1036
|
free = `theme-app-${n}`;
|
|
656
1037
|
refuse(
|
|
657
1038
|
E.ERR_INVALID_ARGUMENT,
|
|
658
|
-
`${shown(cwd, served)}/ has no root layout (no layout.tsx in it or in a route group), so Next cannot build a page in ${where}.`,
|
|
1039
|
+
`${shown(cwd, served ?? app)}/ has no root layout (no layout.tsx in it or in a route group)${served ? '' : ' and is not there yet'}, so Next cannot build a page in ${where}.`,
|
|
659
1040
|
{
|
|
660
|
-
fix: `add a root layout to ${shown(cwd, served)}/ to use the preview in this app`,
|
|
1041
|
+
fix: `add a root layout to ${shown(cwd, served ?? app)}/ to use the preview in this app`,
|
|
661
1042
|
run: [
|
|
662
1043
|
`${PREVIEW} app ${free} # a new app with its own root layout and the preview`,
|
|
663
1044
|
],
|
|
@@ -676,8 +1057,12 @@ function checkPlacement(cwd, dir, where, retry) {
|
|
|
676
1057
|
);
|
|
677
1058
|
const clash = sameUrlPage(cwd, app, dir);
|
|
678
1059
|
if (clash) {
|
|
679
|
-
|
|
680
|
-
|
|
1060
|
+
// The first free name from the base name, whose URL no other page
|
|
1061
|
+
// serves either.
|
|
1062
|
+
const base = dir.replace(/-\d+$/, '');
|
|
1063
|
+
let free = `${base}-2`;
|
|
1064
|
+
for (let n = 3; fs.existsSync(free) || sameUrlPage(cwd, app, free); n++)
|
|
1065
|
+
free = `${base}-${n}`;
|
|
681
1066
|
refuse(
|
|
682
1067
|
E.ERR_FILE_EXISTS,
|
|
683
1068
|
`${shown(cwd, clash)} already serves /${urlSegments(app, dir).join('/')} or the URLs under it, so a preview in ${where} would stop the app from building, or take that URL from it.`,
|
|
@@ -727,6 +1112,52 @@ function previewDir(cwd, where, opts = {}) {
|
|
|
727
1112
|
);
|
|
728
1113
|
throw e;
|
|
729
1114
|
}
|
|
1115
|
+
// A file on the way, or a path longer than the system takes, would
|
|
1116
|
+
// fail every command on it with a system error.
|
|
1117
|
+
const blocker = (() => {
|
|
1118
|
+
for (
|
|
1119
|
+
let at = path.dirname(dir);
|
|
1120
|
+
at.startsWith(cwd + path.sep);
|
|
1121
|
+
at = path.dirname(at)
|
|
1122
|
+
)
|
|
1123
|
+
try {
|
|
1124
|
+
if (!fs.statSync(at).isDirectory()) return at;
|
|
1125
|
+
} catch {
|
|
1126
|
+
// Not there yet: made with the folder.
|
|
1127
|
+
}
|
|
1128
|
+
return null;
|
|
1129
|
+
})();
|
|
1130
|
+
if (blocker)
|
|
1131
|
+
refuse(
|
|
1132
|
+
E.ERR_INVALID_ARGUMENT,
|
|
1133
|
+
`--path ${where} goes through ${shown(cwd, blocker)}, which is a file, so no folder can be made there.`,
|
|
1134
|
+
{
|
|
1135
|
+
run: [
|
|
1136
|
+
retry(
|
|
1137
|
+
shown(cwd, path.join(path.dirname(blocker), 'theme-preview')),
|
|
1138
|
+
),
|
|
1139
|
+
],
|
|
1140
|
+
},
|
|
1141
|
+
);
|
|
1142
|
+
// A folder name is at most 255 bytes on every common file system; the
|
|
1143
|
+
// whole path, with the preview's own files below it, is kept under
|
|
1144
|
+
// 3,500 bytes.
|
|
1145
|
+
const longName = path
|
|
1146
|
+
.relative(cwd, dir)
|
|
1147
|
+
.split(path.sep)
|
|
1148
|
+
.find(seg => Buffer.byteLength(seg) > 255);
|
|
1149
|
+
if (longName)
|
|
1150
|
+
refuse(
|
|
1151
|
+
E.ERR_INVALID_ARGUMENT,
|
|
1152
|
+
`--path ${where} has a folder name longer than 255 bytes, the most a file system takes for one name.`,
|
|
1153
|
+
{run: [retry(null)]},
|
|
1154
|
+
);
|
|
1155
|
+
if (Buffer.byteLength(dir) > 3500)
|
|
1156
|
+
refuse(
|
|
1157
|
+
E.ERR_INVALID_ARGUMENT,
|
|
1158
|
+
`--path ${where} is longer than 3,500 bytes, with the preview's files in it.`,
|
|
1159
|
+
{run: [retry(null)]},
|
|
1160
|
+
);
|
|
730
1161
|
if (opts.write) checkPlacement(cwd, dir, `--path ${where}`, retry);
|
|
731
1162
|
return {dir, app: appOf(dir)};
|
|
732
1163
|
}
|
|
@@ -766,22 +1197,172 @@ function previewDir(cwd, where, opts = {}) {
|
|
|
766
1197
|
served && !known ? (nestedRootLayouts(served)[0] ?? null) : null;
|
|
767
1198
|
const dir = known ?? (served && path.join(nested ?? served, 'theme-preview'));
|
|
768
1199
|
if (!dir && opts.optional) return /** @type {any} */ (null);
|
|
769
|
-
if (!dir)
|
|
1200
|
+
if (!dir) {
|
|
1201
|
+
// A workspace root: the Next apps below it, each with a --path into
|
|
1202
|
+
// its routes folder.
|
|
1203
|
+
const apps = nextAppsUnder(cwd);
|
|
770
1204
|
refuse(
|
|
771
1205
|
E.ERR_NOT_FOUND,
|
|
772
|
-
|
|
1206
|
+
apps.length
|
|
1207
|
+
? `No app routes folder here (app/ or src/app/); the Next ${apps.length > 1 ? 'apps' : 'app'} below ${apps.length > 1 ? 'are' : 'is'} ${apps.map(a => `${shown(cwd, a.root)}/`).join(', ')}.`
|
|
1208
|
+
: 'No app routes folder here (app/ or src/app/), so there is no app to put the preview in.',
|
|
773
1209
|
{
|
|
774
1210
|
run: [
|
|
775
|
-
|
|
1211
|
+
...apps
|
|
1212
|
+
.slice(0, 3)
|
|
1213
|
+
.map(
|
|
1214
|
+
a =>
|
|
1215
|
+
`${PREVIEW} add --path ${quoteCommandArgument(shown(cwd, path.join(a.routes, 'theme-preview')))}`,
|
|
1216
|
+
),
|
|
776
1217
|
`${PREVIEW} app theme-app # a new starter app with the preview in it`,
|
|
777
1218
|
],
|
|
778
1219
|
},
|
|
779
1220
|
);
|
|
1221
|
+
}
|
|
780
1222
|
inProjectOrRefuse(cwd, dir, retry);
|
|
781
1223
|
if (opts.write) checkPlacement(cwd, dir, `${shownDir(cwd, dir)}/`, retry);
|
|
782
1224
|
return {dir, app: appOf(dir)};
|
|
783
1225
|
}
|
|
784
1226
|
|
|
1227
|
+
/**
|
|
1228
|
+
* The Next apps in folders below `cwd` (a few levels, node_modules left
|
|
1229
|
+
* out): each folder with a package.json that depends on `next` and a routes
|
|
1230
|
+
* folder with a root layout.
|
|
1231
|
+
* @param {string} cwd
|
|
1232
|
+
* @returns {Array<{root: string, routes: string}>}
|
|
1233
|
+
*/
|
|
1234
|
+
function nextAppsUnder(cwd) {
|
|
1235
|
+
/** @type {Array<{root: string, routes: string}>} */
|
|
1236
|
+
const out = [];
|
|
1237
|
+
/** @param {string} dir @param {number} depth */
|
|
1238
|
+
const walk = (dir, depth) => {
|
|
1239
|
+
if (depth > 3 || out.length >= 10) return;
|
|
1240
|
+
/** @type {fs.Dirent[]} */
|
|
1241
|
+
let entries;
|
|
1242
|
+
try {
|
|
1243
|
+
entries = fs.readdirSync(dir, {withFileTypes: true});
|
|
1244
|
+
} catch {
|
|
1245
|
+
return;
|
|
1246
|
+
}
|
|
1247
|
+
for (const e of entries) {
|
|
1248
|
+
if (
|
|
1249
|
+
!e.isDirectory() ||
|
|
1250
|
+
e.name === 'node_modules' ||
|
|
1251
|
+
e.name.startsWith('.')
|
|
1252
|
+
)
|
|
1253
|
+
continue;
|
|
1254
|
+
const sub = path.join(dir, e.name);
|
|
1255
|
+
const routes = nextRoutesOf(sub);
|
|
1256
|
+
if (routes) out.push({root: sub, routes});
|
|
1257
|
+
else walk(sub, depth + 1);
|
|
1258
|
+
}
|
|
1259
|
+
};
|
|
1260
|
+
walk(cwd, 1);
|
|
1261
|
+
return out.sort((a, b) => a.root.localeCompare(b.root));
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
/**
|
|
1265
|
+
* The steps that put an editor theme into the app through its app copy
|
|
1266
|
+
* (src/themes/<theme>/): one `theme add`, which prints the one-time wiring
|
|
1267
|
+
* of the app's theme module, and what keeps the copy in step. The one text
|
|
1268
|
+
* every place that says how to use an editor theme gives.
|
|
1269
|
+
* @param {string} cwd
|
|
1270
|
+
* @param {string} slug
|
|
1271
|
+
* @param {'light' | 'dark' | 'system'} [mode]
|
|
1272
|
+
* @returns {string[]}
|
|
1273
|
+
*/
|
|
1274
|
+
export function addWiring(cwd, slug, mode = 'system') {
|
|
1275
|
+
return [
|
|
1276
|
+
`${formatCliCommand(`theme add ${slug}`, cwd)}: adds the theme's app copy (src/themes/${slug}/, written again after every build) to the app's theme module, and prints the one-time wiring of that module in the app root; ${formatCliCommand(`theme use ${slug}`, cwd)} makes it the default`,
|
|
1277
|
+
// Core has no mode inside a theme: a one-mode theme's mode is the one
|
|
1278
|
+
// the app's <Theme> is given.
|
|
1279
|
+
...(mode === 'system'
|
|
1280
|
+
? []
|
|
1281
|
+
: [
|
|
1282
|
+
`${slug} is ${mode}-only: give the <Theme> that theme add's wiring puts in the app root mode="${mode}" (<Theme theme={…} mode="${mode}">); the theme keeps both halves of its colors, so a toast or a lightbox Core draws in the other mode stays readable`,
|
|
1283
|
+
]),
|
|
1284
|
+
`every set and undo builds the theme and its app copy again, so the app takes each change; after editing ${shown(cwd, themeFiles(cwd, slug).ts)} by hand, ${P.replace(/ editor$/, '')} compile ${shown(cwd, themeFiles(cwd, slug).ts)} builds both again`,
|
|
1285
|
+
];
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
/**
|
|
1289
|
+
* How to use an editor theme in this app: `theme add <theme>` for the app
|
|
1290
|
+
* copy the editor writes in src/themes/<theme>/; when a folder of the
|
|
1291
|
+
* user's is there instead, step by step by hand: the file of the app's root
|
|
1292
|
+
* (a Next app's root layout, else the file that renders the app), the
|
|
1293
|
+
* imports the preview route uses for the same theme (its module, its
|
|
1294
|
+
* stylesheet and the font stylesheets) and the Theme around the app. Then
|
|
1295
|
+
* what builds it.
|
|
1296
|
+
* @param {import('./_settings.mjs').Ctx} ctx
|
|
1297
|
+
* @param {string} slug
|
|
1298
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
1299
|
+
* @returns {string[]}
|
|
1300
|
+
*/
|
|
1301
|
+
export function appWiring(ctx, slug, data) {
|
|
1302
|
+
const cwd = ctx.project.cwd;
|
|
1303
|
+
const routes = nextRoutesOf(cwd);
|
|
1304
|
+
const layout = routes
|
|
1305
|
+
? fs.readdirSync(routes).find(f => /^layout\.(tsx|ts|jsx|js)$/.test(f))
|
|
1306
|
+
: undefined;
|
|
1307
|
+
const file = routes && layout ? path.join(routes, layout) : null;
|
|
1308
|
+
const ti = themeImports(ctx, slug, data, file ? path.dirname(file) : cwd);
|
|
1309
|
+
const name = `${camel(slug)}Theme`;
|
|
1310
|
+
const where = file
|
|
1311
|
+
? `${shown(cwd, file)} (the app's root layout)`
|
|
1312
|
+
: "the file that renders your app's root (paths below are from the app folder)";
|
|
1313
|
+
const appModule = ['src/astryx-themes.ts', 'astryx-themes.ts'].find(m =>
|
|
1314
|
+
fs.existsSync(path.join(cwd, m)),
|
|
1315
|
+
);
|
|
1316
|
+
// The app's copy (src/themes/<theme>/, a standard local theme) is what
|
|
1317
|
+
// the stable theme commands add: one command, which prints the one-time
|
|
1318
|
+
// app wiring itself.
|
|
1319
|
+
// Only while the copy is the editor's (the same test its writer uses);
|
|
1320
|
+
// else the steps by hand, naming what is in the way.
|
|
1321
|
+
const blocker = appCopyBlocker(cwd, slug);
|
|
1322
|
+
if (blocker === null && fs.existsSync(path.join(cwd, 'src', 'themes', slug)))
|
|
1323
|
+
return addWiring(cwd, slug, ti.mode);
|
|
1324
|
+
return [
|
|
1325
|
+
...(blocker
|
|
1326
|
+
? [
|
|
1327
|
+
`${blocker} is not the editor's, so the editor does not write the theme's app copy in src/themes/${slug}/ (and theme add does not take it); the steps by hand:`,
|
|
1328
|
+
]
|
|
1329
|
+
: []),
|
|
1330
|
+
`in ${where}, import the theme and its stylesheets: import {Theme} from '@astryxdesign/core/theme'; import {${name}} from '${ti.module.replace(/\.js$/, '')}'; ${ti.css.map(c => `import '${c}';`).join(' ')}`,
|
|
1331
|
+
`wrap the app in it: <Theme theme={${name}}${ti.mode === 'system' ? '' : ` mode="${ti.mode}"`}>{children}</Theme>`,
|
|
1332
|
+
...(appModule
|
|
1333
|
+
? [
|
|
1334
|
+
`${appModule} (written by theme add and use) stays as it is: to keep its themes too, merge them in your own file ({...themes, ${JSON.stringify(slug)}: ${name}}), never in ${appModule}, which add, use and remove write again`,
|
|
1335
|
+
]
|
|
1336
|
+
: []),
|
|
1337
|
+
`every set and undo builds ${shown(cwd, themeFiles(cwd, slug).js)} and ${shown(cwd, themeFiles(cwd, slug).css)} again, so the app takes each change; after editing ${shown(cwd, themeFiles(cwd, slug).ts)} by hand, ${P.replace(/ editor$/, '')} compile ${shown(cwd, themeFiles(cwd, slug).ts)} builds it again`,
|
|
1338
|
+
];
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
/**
|
|
1342
|
+
* The routes folder of a Next app in `dir` (app/, else src/app/, with a
|
|
1343
|
+
* root layout), when its package.json depends on `next`; else null.
|
|
1344
|
+
* @param {string} dir
|
|
1345
|
+
* @returns {string | null}
|
|
1346
|
+
*/
|
|
1347
|
+
function nextRoutesOf(dir) {
|
|
1348
|
+
try {
|
|
1349
|
+
const pkg = JSON.parse(
|
|
1350
|
+
fs.readFileSync(path.join(dir, 'package.json'), 'utf8'),
|
|
1351
|
+
);
|
|
1352
|
+
if (!['dependencies', 'devDependencies'].some(k => pkg?.[k]?.next))
|
|
1353
|
+
return null;
|
|
1354
|
+
} catch {
|
|
1355
|
+
return null;
|
|
1356
|
+
}
|
|
1357
|
+
return (
|
|
1358
|
+
[path.join(dir, 'app'), path.join(dir, 'src', 'app')].find(
|
|
1359
|
+
r =>
|
|
1360
|
+
fs.existsSync(r) &&
|
|
1361
|
+
fs.readdirSync(r).some(f => /^layout\.(tsx|ts|jsx|js)$/.test(f)),
|
|
1362
|
+
) ?? null
|
|
1363
|
+
);
|
|
1364
|
+
}
|
|
1365
|
+
|
|
785
1366
|
/**
|
|
786
1367
|
* Refuse a preview folder that reaches outside the project through a link,
|
|
787
1368
|
* as `--path` refuses one: every file the preview writes would land there.
|
|
@@ -827,9 +1408,11 @@ const STARTER = path.join(CLI_PACKAGE, 'assets', 'preview-app');
|
|
|
827
1408
|
* @param {string} file absolute
|
|
828
1409
|
* @param {string} root absolute
|
|
829
1410
|
* @param {string} what what the path is, for the refusal
|
|
1411
|
+
* @param {{cwd?: string, pf?: string}} [opts] `cwd`: paths in the refusal
|
|
1412
|
+
* are shown from it; `pf`: the caller's --path flag, for the suggestion
|
|
830
1413
|
* @returns {string}
|
|
831
1414
|
*/
|
|
832
|
-
function confined(file, root, what) {
|
|
1415
|
+
function confined(file, root, what, opts = {}) {
|
|
833
1416
|
try {
|
|
834
1417
|
return assertWithin(file, root, {allowAbsolute: true, label: what});
|
|
835
1418
|
} catch (e) {
|
|
@@ -848,12 +1431,14 @@ function confined(file, root, what) {
|
|
|
848
1431
|
} catch {
|
|
849
1432
|
// Not there: look at its folder.
|
|
850
1433
|
}
|
|
1434
|
+
const at = (/** @type {string} */ f) =>
|
|
1435
|
+
opts.cwd ? shown(opts.cwd, f) || '.' : f;
|
|
851
1436
|
refuse(
|
|
852
1437
|
E.ERR_PATH_TRAVERSAL,
|
|
853
1438
|
link == null
|
|
854
|
-
? `${what} names ${file}, which is outside ${root}.`
|
|
855
|
-
: `${link.at} is a link to ${link.to}, outside ${root}; the preview never writes through a link.`,
|
|
856
|
-
{run: [`${PREVIEW} list`]},
|
|
1439
|
+
? `${what} names ${at(file)}, which is outside ${at(root)}.`
|
|
1440
|
+
: `${at(link.at)} is a link to ${link.to}, outside ${at(root)}; the preview never writes through a link.`,
|
|
1441
|
+
{run: [`${PREVIEW} list${opts.pf ?? ''}`]},
|
|
857
1442
|
);
|
|
858
1443
|
}
|
|
859
1444
|
throw e;
|
|
@@ -938,7 +1523,11 @@ function planStarterApp(cwd, where, extra, dependencies, slug = null) {
|
|
|
938
1523
|
reservedFolder(cwd, root) ??
|
|
939
1524
|
(routes.some(r => root === r || root.startsWith(r + path.sep))
|
|
940
1525
|
? "it is inside this app's routes folder, where Next would serve the new app's files as pages of this one"
|
|
941
|
-
: null)
|
|
1526
|
+
: null) ??
|
|
1527
|
+
(t =>
|
|
1528
|
+
t
|
|
1529
|
+
? `${shownDir(cwd, t)}/ differs from it only in case, and on macOS and Windows they are one folder`
|
|
1530
|
+
: null)(sameCaseSibling(root));
|
|
942
1531
|
if (why) {
|
|
943
1532
|
let free = 'theme-app';
|
|
944
1533
|
for (let n = 2; fs.existsSync(path.join(cwd, free)); n++)
|
|
@@ -1135,6 +1724,11 @@ function previewProblem(d) {
|
|
|
1135
1724
|
return '"theme" is not a theme name or null';
|
|
1136
1725
|
if (!Array.isArray(d.pages)) return '"pages" is not a list';
|
|
1137
1726
|
if (!Array.isArray(d.files)) return '"files" is not a list';
|
|
1727
|
+
if (
|
|
1728
|
+
d.parents !== undefined &&
|
|
1729
|
+
!(Number.isInteger(d.parents) && d.parents >= 0 && d.parents <= 20)
|
|
1730
|
+
)
|
|
1731
|
+
return '"parents" is not a small whole number';
|
|
1138
1732
|
const ids = new Set();
|
|
1139
1733
|
for (const p of d.pages) {
|
|
1140
1734
|
if (!p || typeof p !== 'object' || Array.isArray(p))
|
|
@@ -1161,6 +1755,8 @@ function previewProblem(d) {
|
|
|
1161
1755
|
f.split('/').some(x => x === '' || x === '.')
|
|
1162
1756
|
)
|
|
1163
1757
|
return `"files" holds ${JSON.stringify(f)}, which is not a file path relative to the folder`;
|
|
1758
|
+
else if (f.split('/').includes('..'))
|
|
1759
|
+
return `"files" holds ${JSON.stringify(f)}, which leads outside the folder (${OUTSIDE_FIX})`;
|
|
1164
1760
|
if (
|
|
1165
1761
|
d.hashes != null &&
|
|
1166
1762
|
(typeof d.hashes !== 'object' ||
|
|
@@ -1213,15 +1809,21 @@ function holdsApp(cwd, dir) {
|
|
|
1213
1809
|
);
|
|
1214
1810
|
}
|
|
1215
1811
|
|
|
1812
|
+
/** What a record entry that leads outside the folder takes. */
|
|
1813
|
+
const OUTSIDE_FIX = `${PREVIEW} remove drops it and deletes nothing outside`;
|
|
1814
|
+
|
|
1216
1815
|
/**
|
|
1217
1816
|
* The preview's data, null when the folder has none; a refusal when the
|
|
1218
1817
|
* file is there but unreadable or of the wrong shape.
|
|
1219
1818
|
* @param {string} cwd
|
|
1220
1819
|
* @param {string} dir
|
|
1221
1820
|
* @param {string} [pf] the caller's --path flag, for the refusal
|
|
1821
|
+
* @param {{dropped?: string[]}} [opts] `dropped`: entries of "files" that
|
|
1822
|
+
* lead outside the folder are taken out and collected here, as `remove`
|
|
1823
|
+
* does
|
|
1222
1824
|
* @returns {PreviewData | null}
|
|
1223
1825
|
*/
|
|
1224
|
-
function readPreview(cwd, dir, pf = '') {
|
|
1826
|
+
function readPreview(cwd, dir, pf = '', opts = {}) {
|
|
1225
1827
|
/** @type {string} */
|
|
1226
1828
|
let text;
|
|
1227
1829
|
try {
|
|
@@ -1246,6 +1848,14 @@ function readPreview(cwd, dir, pf = '') {
|
|
|
1246
1848
|
pf,
|
|
1247
1849
|
);
|
|
1248
1850
|
}
|
|
1851
|
+
// `remove` drops an entry that leads outside the folder, and deletes
|
|
1852
|
+
// nothing there.
|
|
1853
|
+
if (opts.dropped && data && Array.isArray(data.files)) {
|
|
1854
|
+
const outside = (/** @type {unknown} */ f) =>
|
|
1855
|
+
typeof f === 'string' && f.split('/').includes('..');
|
|
1856
|
+
for (const f of data.files) if (outside(f)) opts.dropped.push(f);
|
|
1857
|
+
data.files = data.files.filter((/** @type {unknown} */ f) => !outside(f));
|
|
1858
|
+
}
|
|
1249
1859
|
const problem = previewProblem(data);
|
|
1250
1860
|
if (problem) damagedPreview(cwd, dir, problem, pf);
|
|
1251
1861
|
data.files = [...new Set(data.files)];
|
|
@@ -1598,7 +2208,7 @@ function checkWrite(cwd, dir, data, w, elsewhere) {
|
|
|
1598
2208
|
page
|
|
1599
2209
|
? {
|
|
1600
2210
|
fix: `give ${name} another name if you want to keep it, then run the command again; or add another page`,
|
|
1601
|
-
run: ['
|
|
2211
|
+
run: [formatCliCommand('template --list --type page', cwd)],
|
|
1602
2212
|
}
|
|
1603
2213
|
: {
|
|
1604
2214
|
fix: `move ${name} out of the folder, then run the command again; or put the preview in another folder`,
|
|
@@ -1613,7 +2223,9 @@ function checkWrite(cwd, dir, data, w, elsewhere) {
|
|
|
1613
2223
|
E.ERR_FILE_EXISTS,
|
|
1614
2224
|
`${name} was changed by hand after the preview wrote it, so it is not replaced.`,
|
|
1615
2225
|
{
|
|
1616
|
-
|
|
2226
|
+
// A copy renamed in the folder would still import the preview's
|
|
2227
|
+
// pages and break when they go: it moves out, importing none.
|
|
2228
|
+
fix: `move ${name} out of ${shownDir(cwd, dir)}/ (copy what you changed somewhere else in the app, importing nothing from the preview folder), then run the command again`,
|
|
1617
2229
|
},
|
|
1618
2230
|
);
|
|
1619
2231
|
return 'replace';
|
|
@@ -1811,6 +2423,186 @@ function commitStep(step) {
|
|
|
1811
2423
|
if (stopAt?.(step)) throw STOPPED;
|
|
1812
2424
|
}
|
|
1813
2425
|
|
|
2426
|
+
/** Folders the importer search never enters: installs and build output. */
|
|
2427
|
+
const NOT_SOURCE = new Set([
|
|
2428
|
+
'node_modules',
|
|
2429
|
+
'out',
|
|
2430
|
+
'dist',
|
|
2431
|
+
'build',
|
|
2432
|
+
'coverage',
|
|
2433
|
+
]);
|
|
2434
|
+
|
|
2435
|
+
/** At most this many source files are read; past it, nothing is deleted. */
|
|
2436
|
+
let IMPORTER_SCAN_MAX = 50000;
|
|
2437
|
+
|
|
2438
|
+
/**
|
|
2439
|
+
* For tests: read at most `n` source files when checking for importers
|
|
2440
|
+
* (null: the default).
|
|
2441
|
+
* @param {number | null} n
|
|
2442
|
+
*/
|
|
2443
|
+
export function setImporterScanMax(n) {
|
|
2444
|
+
IMPORTER_SCAN_MAX = n ?? 50000;
|
|
2445
|
+
}
|
|
2446
|
+
|
|
2447
|
+
/**
|
|
2448
|
+
* Files of the project's own that import any of `targets` (preview files
|
|
2449
|
+
* about to be deleted), so deleting them would break the app's build. The
|
|
2450
|
+
* whole project root is read: every .ts/.tsx/.js/.jsx/.mjs/.cjs/.mdx file
|
|
2451
|
+
* outside node_modules, build output and dot folders, the preview's own
|
|
2452
|
+
* files left out. Each file's import and export-from declarations,
|
|
2453
|
+
* import() and require() calls (a string or a template with no
|
|
2454
|
+
* substitution) are read from its syntax tree and resolved: relative,
|
|
2455
|
+
* `@/`, or a tsconfig `paths` alias. `tooBig` when the project holds more
|
|
2456
|
+
* source files than are read, so the caller refuses instead of guessing.
|
|
2457
|
+
* @param {string} cwd
|
|
2458
|
+
* @param {PreviewData} data
|
|
2459
|
+
* @param {string} dir the preview folder
|
|
2460
|
+
* @param {string[]} targets absolute paths, without extension
|
|
2461
|
+
* @returns {{importers: string[], tooBig: boolean}}
|
|
2462
|
+
*/
|
|
2463
|
+
function pageImporters(cwd, data, dir, targets) {
|
|
2464
|
+
const wanted = new Set(targets);
|
|
2465
|
+
if (!wanted.size) return {importers: [], tooBig: false};
|
|
2466
|
+
const names = [...new Set(targets.map(t => path.basename(t)))];
|
|
2467
|
+
const own = new Set(data.files.map(f => path.join(dir, f)));
|
|
2468
|
+
const aliases = tsconfigAliases(cwd);
|
|
2469
|
+
/** @type {Set<string>} */
|
|
2470
|
+
const out = new Set();
|
|
2471
|
+
let files = 0;
|
|
2472
|
+
let tooBig = false;
|
|
2473
|
+
/** @param {string} file @param {string} spec */
|
|
2474
|
+
const resolves = (file, spec) => {
|
|
2475
|
+
const alias = aliases.find(x => spec.startsWith(x.prefix));
|
|
2476
|
+
const bases = alias
|
|
2477
|
+
? alias.to.map(t => path.join(t, spec.slice(alias.prefix.length)))
|
|
2478
|
+
: spec.startsWith('@/')
|
|
2479
|
+
? [path.join(cwd, 'src', spec.slice(2)), path.join(cwd, spec.slice(2))]
|
|
2480
|
+
: spec.startsWith('.')
|
|
2481
|
+
? [path.resolve(path.dirname(file), spec)]
|
|
2482
|
+
: [];
|
|
2483
|
+
return bases.some(
|
|
2484
|
+
b =>
|
|
2485
|
+
wanted.has(b.replace(/\.(tsx|ts|jsx|js|mjs|cjs)$/, '')) ||
|
|
2486
|
+
wanted.has(path.join(b, 'index')),
|
|
2487
|
+
);
|
|
2488
|
+
};
|
|
2489
|
+
/** @param {any} node @param {(spec: string) => void} found */
|
|
2490
|
+
const specifiers = (node, found) => {
|
|
2491
|
+
if (!node || typeof node !== 'object') return;
|
|
2492
|
+
if (Array.isArray(node)) {
|
|
2493
|
+
for (const x of node) specifiers(x, found);
|
|
2494
|
+
return;
|
|
2495
|
+
}
|
|
2496
|
+
const text = (/** @type {any} */ n) =>
|
|
2497
|
+
n?.type === 'StringLiteral'
|
|
2498
|
+
? n.value
|
|
2499
|
+
: n?.type === 'TemplateLiteral' && n.expressions.length === 0
|
|
2500
|
+
? n.quasis.map((/** @type {any} */ q) => q.value.cooked).join('')
|
|
2501
|
+
: null;
|
|
2502
|
+
if (
|
|
2503
|
+
(node.type === 'ImportDeclaration' ||
|
|
2504
|
+
node.type === 'ExportNamedDeclaration' ||
|
|
2505
|
+
node.type === 'ExportAllDeclaration') &&
|
|
2506
|
+
node.source
|
|
2507
|
+
) {
|
|
2508
|
+
const t = text(node.source);
|
|
2509
|
+
if (t != null) found(t);
|
|
2510
|
+
} else if (node.type === 'ImportExpression') {
|
|
2511
|
+
const t = text(node.source);
|
|
2512
|
+
if (t != null) found(t);
|
|
2513
|
+
} else if (
|
|
2514
|
+
node.type === 'CallExpression' &&
|
|
2515
|
+
(node.callee?.type === 'Import' ||
|
|
2516
|
+
(node.callee?.type === 'Identifier' && node.callee.name === 'require'))
|
|
2517
|
+
) {
|
|
2518
|
+
const t = text(node.arguments?.[0]);
|
|
2519
|
+
if (t != null) found(t);
|
|
2520
|
+
} else if (
|
|
2521
|
+
node.type === 'TSImportType' ||
|
|
2522
|
+
node.type === 'TSExternalModuleReference'
|
|
2523
|
+
) {
|
|
2524
|
+
const t = text(node.argument ?? node.expression);
|
|
2525
|
+
if (t != null) found(t);
|
|
2526
|
+
}
|
|
2527
|
+
for (const [k, v] of Object.entries(node))
|
|
2528
|
+
if (
|
|
2529
|
+
k !== 'loc' &&
|
|
2530
|
+
k !== 'start' &&
|
|
2531
|
+
k !== 'end' &&
|
|
2532
|
+
v &&
|
|
2533
|
+
typeof v === 'object'
|
|
2534
|
+
)
|
|
2535
|
+
specifiers(v, found);
|
|
2536
|
+
};
|
|
2537
|
+
/** @param {string} at */
|
|
2538
|
+
const walk = at => {
|
|
2539
|
+
if (tooBig) return;
|
|
2540
|
+
/** @type {fs.Dirent[]} */
|
|
2541
|
+
let entries;
|
|
2542
|
+
try {
|
|
2543
|
+
entries = fs.readdirSync(at, {withFileTypes: true});
|
|
2544
|
+
} catch {
|
|
2545
|
+
return;
|
|
2546
|
+
}
|
|
2547
|
+
for (const e of entries) {
|
|
2548
|
+
const full = path.join(at, e.name);
|
|
2549
|
+
if (e.isDirectory()) {
|
|
2550
|
+
if (!NOT_SOURCE.has(e.name) && !e.name.startsWith('.')) walk(full);
|
|
2551
|
+
if (tooBig) return;
|
|
2552
|
+
continue;
|
|
2553
|
+
}
|
|
2554
|
+
if (!/\.(tsx|ts|jsx|js|mjs|cjs|mdx)$/.test(e.name) || own.has(full))
|
|
2555
|
+
continue;
|
|
2556
|
+
if (e.name.endsWith('.d.ts')) continue;
|
|
2557
|
+
if (++files > IMPORTER_SCAN_MAX) {
|
|
2558
|
+
tooBig = true;
|
|
2559
|
+
return;
|
|
2560
|
+
}
|
|
2561
|
+
/** @type {string} */
|
|
2562
|
+
let source;
|
|
2563
|
+
try {
|
|
2564
|
+
source = fs.readFileSync(full, 'utf8');
|
|
2565
|
+
} catch {
|
|
2566
|
+
continue;
|
|
2567
|
+
}
|
|
2568
|
+
// Only a file that names a target's file name can import it.
|
|
2569
|
+
if (!names.some(n => source.includes(n))) continue;
|
|
2570
|
+
/** @type {string[]} */
|
|
2571
|
+
const specs = [];
|
|
2572
|
+
try {
|
|
2573
|
+
const ast = parseModule(
|
|
2574
|
+
e.name.endsWith('.mdx')
|
|
2575
|
+
? source
|
|
2576
|
+
.split('\n')
|
|
2577
|
+
.filter(l => /^\s*(import|export)\b/.test(l))
|
|
2578
|
+
.join('\n')
|
|
2579
|
+
: source,
|
|
2580
|
+
{
|
|
2581
|
+
sourceType: 'unambiguous',
|
|
2582
|
+
errorRecovery: true,
|
|
2583
|
+
plugins: [
|
|
2584
|
+
'jsx',
|
|
2585
|
+
...(/\.(ts|tsx|mts|cts)$/.test(e.name)
|
|
2586
|
+
? [/** @type {const} */ ('typescript')]
|
|
2587
|
+
: []),
|
|
2588
|
+
],
|
|
2589
|
+
},
|
|
2590
|
+
);
|
|
2591
|
+
specifiers(ast.program, spec => specs.push(spec));
|
|
2592
|
+
} catch {
|
|
2593
|
+
// Not parsed: read its import strings as text.
|
|
2594
|
+
for (const m of source.matchAll(
|
|
2595
|
+
/(?:from\s*|import\s*\(\s*|require\s*\(\s*|import\s+)['"`]([^'"`$]+)['"`]/g,
|
|
2596
|
+
))
|
|
2597
|
+
specs.push(m[1]);
|
|
2598
|
+
}
|
|
2599
|
+
if (specs.some(sp => resolves(full, sp))) out.add(shown(cwd, full));
|
|
2600
|
+
}
|
|
2601
|
+
};
|
|
2602
|
+
walk(cwd);
|
|
2603
|
+
return {importers: [...out].sort(), tooBig};
|
|
2604
|
+
}
|
|
2605
|
+
|
|
1814
2606
|
/**
|
|
1815
2607
|
* Page files in the preview folder that theme-preview.json does not list:
|
|
1816
2608
|
* a stopped change can leave one behind when its step record is damaged.
|
|
@@ -1868,6 +2660,29 @@ function pendingEscapes(dir, p) {
|
|
|
1868
2660
|
});
|
|
1869
2661
|
}
|
|
1870
2662
|
|
|
2663
|
+
/**
|
|
2664
|
+
* Delete the temp files a preview command left in a preview folder, found
|
|
2665
|
+
* by the names only the editor makes (`.<file>.astryx-<pid>-<id>.tmp`),
|
|
2666
|
+
* once their process is gone.
|
|
2667
|
+
* @param {string} dir
|
|
2668
|
+
*/
|
|
2669
|
+
function sweepTemps(dir) {
|
|
2670
|
+
for (const sub of ['', PAGES_DIR, LEGACY_PAGES_DIR]) {
|
|
2671
|
+
/** @type {string[]} */
|
|
2672
|
+
let names;
|
|
2673
|
+
try {
|
|
2674
|
+
names = fs.readdirSync(path.join(dir, sub));
|
|
2675
|
+
} catch {
|
|
2676
|
+
continue;
|
|
2677
|
+
}
|
|
2678
|
+
for (const name of names) {
|
|
2679
|
+
const m = /^\..+\.astryx-(\d+)-[a-z0-9]+\.tmp$/.exec(name);
|
|
2680
|
+
if (m && (!alive(Number(m[1])) || Number(m[1]) === process.pid))
|
|
2681
|
+
fs.rmSync(path.join(dir, sub, name), {force: true});
|
|
2682
|
+
}
|
|
2683
|
+
}
|
|
2684
|
+
}
|
|
2685
|
+
|
|
1871
2686
|
/**
|
|
1872
2687
|
* Finish a preview change that was stopped part way (a kill), from the
|
|
1873
2688
|
* step record it left: its leftover temp files go; each file it was writing
|
|
@@ -1888,22 +2703,7 @@ function recoverPreview(cwd, dir) {
|
|
|
1888
2703
|
// Unreadable, or naming a path outside the folder: the change is
|
|
1889
2704
|
// treated as never started.
|
|
1890
2705
|
const abandon = () => {
|
|
1891
|
-
|
|
1892
|
-
// their process is gone.
|
|
1893
|
-
for (const sub of ['', PAGES_DIR, LEGACY_PAGES_DIR]) {
|
|
1894
|
-
/** @type {string[]} */
|
|
1895
|
-
let names;
|
|
1896
|
-
try {
|
|
1897
|
-
names = fs.readdirSync(path.join(dir, sub));
|
|
1898
|
-
} catch {
|
|
1899
|
-
continue;
|
|
1900
|
-
}
|
|
1901
|
-
for (const name of names) {
|
|
1902
|
-
const m = /^\..+\.astryx-(\d+)-[a-z0-9]+\.tmp$/.exec(name);
|
|
1903
|
-
if (m && !alive(Number(m[1])))
|
|
1904
|
-
fs.rmSync(path.join(dir, sub, name), {force: true});
|
|
1905
|
-
}
|
|
1906
|
-
}
|
|
2706
|
+
sweepTemps(dir);
|
|
1907
2707
|
fs.rmSync(pendingFile, {force: true});
|
|
1908
2708
|
const strays = unrecordedPages(dir).map(rel =>
|
|
1909
2709
|
shown(cwd, path.join(dir, rel)),
|
|
@@ -1913,7 +2713,12 @@ function recoverPreview(cwd, dir) {
|
|
|
1913
2713
|
try {
|
|
1914
2714
|
p = JSON.parse(fs.readFileSync(pendingFile, 'utf8'));
|
|
1915
2715
|
} catch (e) {
|
|
1916
|
-
|
|
2716
|
+
// No step record: temp files a stopped command left (one killed before
|
|
2717
|
+
// it wrote the record) go, once their process is gone.
|
|
2718
|
+
if (/** @type {any} */ (e)?.code === 'ENOENT') {
|
|
2719
|
+
sweepTemps(dir);
|
|
2720
|
+
return null;
|
|
2721
|
+
}
|
|
1917
2722
|
return abandon();
|
|
1918
2723
|
}
|
|
1919
2724
|
if (pendingEscapes(dir, p)) return abandon();
|
|
@@ -2152,7 +2957,10 @@ async function routeTheme(cwd, dir, data, opts = {}) {
|
|
|
2152
2957
|
if (!data.theme) return null;
|
|
2153
2958
|
const t = loadTheme(cwd, data.theme);
|
|
2154
2959
|
const ctx = await context(cwd);
|
|
2155
|
-
|
|
2960
|
+
// The same rule `preview app` uses: any output missing, or a build that
|
|
2961
|
+
// does not match the settings, is built again (judged by content).
|
|
2962
|
+
const stale = staleBuild(cwd, data.theme, t);
|
|
2963
|
+
const unbuilt = stale != null;
|
|
2156
2964
|
if (unbuilt && opts.build !== false) await buildTheme(ctx, data.theme, t);
|
|
2157
2965
|
const ti = themeImports(ctx, data.theme, t, dir);
|
|
2158
2966
|
return {
|
|
@@ -2189,24 +2997,29 @@ async function themeLocked(options, slug, fn) {
|
|
|
2189
2997
|
async function buildWorn(cwd, theme, options, notes) {
|
|
2190
2998
|
if (!theme?.unbuilt) return;
|
|
2191
2999
|
const slug = theme.name;
|
|
3000
|
+
/** @type {string | null} */
|
|
3001
|
+
let why = null;
|
|
2192
3002
|
const build = async () => {
|
|
2193
|
-
|
|
2194
|
-
|
|
3003
|
+
const t = loadTheme(cwd, slug);
|
|
3004
|
+
why = staleBuild(cwd, slug, t);
|
|
3005
|
+
if (!why) return;
|
|
3006
|
+
await buildTheme(await context(cwd), slug, t);
|
|
2195
3007
|
};
|
|
2196
3008
|
await (typeof options?.lockTheme === 'function'
|
|
2197
3009
|
? options.lockTheme(slug, build)
|
|
2198
3010
|
: build());
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
3011
|
+
if (why)
|
|
3012
|
+
notes.push(
|
|
3013
|
+
`built ${shown(cwd, themeFiles(cwd, slug).dir)}/ first: its build was not current (${why})`,
|
|
3014
|
+
);
|
|
2202
3015
|
}
|
|
2203
3016
|
|
|
2204
3017
|
/** The Colors page: its id and file, relative to the route folder. */
|
|
2205
|
-
const COLORS_PAGE = {
|
|
3018
|
+
const COLORS_PAGE = wordTable({
|
|
2206
3019
|
id: 'colors',
|
|
2207
3020
|
label: 'Colors',
|
|
2208
3021
|
file: `${PAGES_DIR}/colors`,
|
|
2209
|
-
};
|
|
3022
|
+
});
|
|
2210
3023
|
|
|
2211
3024
|
/**
|
|
2212
3025
|
* The Colors page source of a theme, and whether the theme has generated
|
|
@@ -2276,6 +3089,38 @@ async function colorsPage(cwd, dir, slug) {
|
|
|
2276
3089
|
};
|
|
2277
3090
|
}
|
|
2278
3091
|
|
|
3092
|
+
/**
|
|
3093
|
+
* When the theme a preview wore is gone, its Colors page goes too: it shows
|
|
3094
|
+
* that theme's colors and tells you to set them. The file is deleted when it
|
|
3095
|
+
* is as the preview wrote it; a page changed by hand is kept, out of the nav.
|
|
3096
|
+
* @param {string} cwd
|
|
3097
|
+
* @param {string} dir
|
|
3098
|
+
* @param {PreviewData} data
|
|
3099
|
+
* @param {{writes: Array<{rel: string}>, deletes: string[]}} move
|
|
3100
|
+
* @param {string} gone
|
|
3101
|
+
* @param {string[]} notes
|
|
3102
|
+
*/
|
|
3103
|
+
function dropGoneColors(cwd, dir, data, move, gone, notes) {
|
|
3104
|
+
const page = data.pages.find(p => p.id === COLORS_PAGE.id);
|
|
3105
|
+
if (!page) return;
|
|
3106
|
+
const rel = `${page.file}.tsx`;
|
|
3107
|
+
const ids = new Set(data.pages.map(p => p.id));
|
|
3108
|
+
const owned =
|
|
3109
|
+
fs.existsSync(path.join(dir, rel)) &&
|
|
3110
|
+
ownedUnchanged(dir, data, rel, ids) === 'delete';
|
|
3111
|
+
data.pages = data.pages.filter(p => p !== page);
|
|
3112
|
+
data.files = data.files.filter(f => f !== rel);
|
|
3113
|
+
move.writes = move.writes.filter(
|
|
3114
|
+
w => w.rel !== rel && w.rel !== `${PAGES_DIR}/${COLORS_PAGE.id}.tsx`,
|
|
3115
|
+
);
|
|
3116
|
+
if (owned) move.deletes.push(rel);
|
|
3117
|
+
notes.push(
|
|
3118
|
+
owned
|
|
3119
|
+
? `removed the Colors page: it showed the colors of ${gone}, which is gone`
|
|
3120
|
+
: `left the Colors page out of the nav: it showed the colors of ${gone}, which is gone, and was changed by hand, so ${shown(cwd, path.join(dir, rel))} is kept`,
|
|
3121
|
+
);
|
|
3122
|
+
}
|
|
3123
|
+
|
|
2279
3124
|
/**
|
|
2280
3125
|
* The route file and, when the preview has one, the Colors page as the
|
|
2281
3126
|
* page list and the theme say now. With `gone`, the theme the preview wore
|
|
@@ -2332,6 +3177,19 @@ function paletteStandIn(cwd, slug) {
|
|
|
2332
3177
|
return shown(cwd, file);
|
|
2333
3178
|
}
|
|
2334
3179
|
|
|
3180
|
+
/**
|
|
3181
|
+
* The --path a preview command needs for a preview folder: none for the
|
|
3182
|
+
* default one.
|
|
3183
|
+
* @param {string} cwd
|
|
3184
|
+
* @param {string} dir
|
|
3185
|
+
*/
|
|
3186
|
+
function dirFlag(cwd, dir) {
|
|
3187
|
+
const fallback = previewDir(cwd, undefined, {optional: true})?.dir;
|
|
3188
|
+
return fallback === dir
|
|
3189
|
+
? ''
|
|
3190
|
+
: ` --path ${quoteCommandArgument(shownDir(cwd, dir))}`;
|
|
3191
|
+
}
|
|
3192
|
+
|
|
2335
3193
|
/**
|
|
2336
3194
|
* Rewrite the in-app preview routes of a theme when their imports changed, so
|
|
2337
3195
|
* a font package installed after `set` loads on the next change or `show`,
|
|
@@ -2392,6 +3250,15 @@ export async function refreshPreviewRoute(cwd, slug) {
|
|
|
2392
3250
|
}
|
|
2393
3251
|
return true;
|
|
2394
3252
|
});
|
|
3253
|
+
// A theme that has generated colors now, under a preview with no
|
|
3254
|
+
// Colors page: the next add adds it.
|
|
3255
|
+
if (
|
|
3256
|
+
!data.pages.some(p => p.id === COLORS_PAGE.id) &&
|
|
3257
|
+
(await colorsPage(cwd, dir, slug)).colors
|
|
3258
|
+
)
|
|
3259
|
+
stale.push(
|
|
3260
|
+
`${shownDir(cwd, dir)}/ (${slug} has generated colors now; ${PREVIEW} add${dirFlag(cwd, dir)} adds its Colors page)`,
|
|
3261
|
+
);
|
|
2395
3262
|
if (!writes.length) continue;
|
|
2396
3263
|
commitPreview(cwd, dir, data, writes, [], `${EDITOR} show ${slug}`);
|
|
2397
3264
|
changed = true;
|
|
@@ -2536,7 +3403,61 @@ async function rankedPages(words, cwd, notes, flags) {
|
|
|
2536
3403
|
String(r.reason ?? '').replace(/^matches \d+\/\d+ terms: /, ''),
|
|
2537
3404
|
]),
|
|
2538
3405
|
);
|
|
3406
|
+
// A description that is a template's id or display name names it.
|
|
3407
|
+
const plain = (/** @type {string} */ x) =>
|
|
3408
|
+
x
|
|
3409
|
+
.toLowerCase()
|
|
3410
|
+
.replace(/[^a-z0-9]+/g, ' ')
|
|
3411
|
+
.trim();
|
|
3412
|
+
const exact = listed.find(
|
|
3413
|
+
t => plain(t.id) === plain(words) || plain(t.name ?? '') === plain(words),
|
|
3414
|
+
)?.id;
|
|
3415
|
+
if (exact && frames.has(exact))
|
|
3416
|
+
refuse(
|
|
3417
|
+
E.ERR_INVALID_ARGUMENT,
|
|
3418
|
+
`${exact} is an app frame, not a page, so a description does not add it; name it with --template to add it as a page anyway.`,
|
|
3419
|
+
{run: [`${PREVIEW} add --template ${exact}${flags}`]},
|
|
3420
|
+
);
|
|
3421
|
+
// Build's page ranking, when build picked no page itself (it fell back to
|
|
3422
|
+
// a frame): its pages the description names (by name, keyword or docs,
|
|
3423
|
+
// or a word of it in the page's id), best score first; on a tie, a page whose id holds a word of the
|
|
3424
|
+
// description goes first.
|
|
3425
|
+
const said = plain(words)
|
|
3426
|
+
.split(' ')
|
|
3427
|
+
.filter(w => w.length > 2);
|
|
3428
|
+
const byId = (/** @type {string} */ id) =>
|
|
3429
|
+
said.some(w => id.split('-').includes(w)) ? 1 : 0;
|
|
3430
|
+
const fellBack =
|
|
3431
|
+
!start || (start.basis === 'fallback' && frames.has(start.name));
|
|
3432
|
+
const ranked = (fellBack ? [...(kit.data.pages ?? [])] : [])
|
|
3433
|
+
.map((r, i) => ({
|
|
3434
|
+
name: r.name,
|
|
3435
|
+
score: Number(r.score) || 0,
|
|
3436
|
+
reason: String(r.reason ?? ''),
|
|
3437
|
+
i,
|
|
3438
|
+
}))
|
|
3439
|
+
.filter(
|
|
3440
|
+
r =>
|
|
3441
|
+
listed.some(t => t.id === r.name) &&
|
|
3442
|
+
(/^(exact name|name|keyword|docs mention|description mentions)\b/.test(
|
|
3443
|
+
r.reason,
|
|
3444
|
+
) ||
|
|
3445
|
+
r.score >= 100 ||
|
|
3446
|
+
byId(r.name)),
|
|
3447
|
+
)
|
|
3448
|
+
.sort(
|
|
3449
|
+
(a, b) => b.score - a.score || byId(b.name) - byId(a.name) || a.i - b.i,
|
|
3450
|
+
)
|
|
3451
|
+
.map(r => r.name);
|
|
3452
|
+
for (const r of kit.data.pages ?? [])
|
|
3453
|
+
if (!matchedWords.has(r.name))
|
|
3454
|
+
matchedWords.set(
|
|
3455
|
+
r.name,
|
|
3456
|
+
String(r.reason ?? '').replace(/^matches \d+\/\d+ terms: /, ''),
|
|
3457
|
+
);
|
|
2539
3458
|
const names = [
|
|
3459
|
+
...(exact ? [exact] : []),
|
|
3460
|
+
...ranked,
|
|
2540
3461
|
...(start ? [start.name, ...start.alternatives.map(a => a.name)] : []),
|
|
2541
3462
|
...found.map((/** @type {any} */ r) => r.name),
|
|
2542
3463
|
].filter((n, i, a) => a.indexOf(n) === i);
|
|
@@ -2553,7 +3474,7 @@ async function rankedPages(words, cwd, notes, flags) {
|
|
|
2553
3474
|
listed.some(t => t.id === id),
|
|
2554
3475
|
),
|
|
2555
3476
|
),
|
|
2556
|
-
'
|
|
3477
|
+
formatCliCommand('template --list --type page', cwd),
|
|
2557
3478
|
];
|
|
2558
3479
|
// No page relates when the ranking fell back to a frame with no direct
|
|
2559
3480
|
// match, and no page the search found matches three words of the request
|
|
@@ -2569,6 +3490,8 @@ async function rankedPages(words, cwd, notes, flags) {
|
|
|
2569
3490
|
const named = APP_PAGES.some(([re]) => re.test(words.toLowerCase()));
|
|
2570
3491
|
const unrelated =
|
|
2571
3492
|
!named &&
|
|
3493
|
+
!exact &&
|
|
3494
|
+
!ranked.some(n => !frames.has(n)) &&
|
|
2572
3495
|
(!pages.length ||
|
|
2573
3496
|
(start &&
|
|
2574
3497
|
start.basis === 'fallback' &&
|
|
@@ -2583,7 +3506,10 @@ async function rankedPages(words, cwd, notes, flags) {
|
|
|
2583
3506
|
`No page template relates to "${words}". Name a page to add it anyway.`,
|
|
2584
3507
|
{
|
|
2585
3508
|
run: pages.length
|
|
2586
|
-
? [
|
|
3509
|
+
? [
|
|
3510
|
+
...suggest(pages),
|
|
3511
|
+
formatCliCommand('template --list --type page', cwd),
|
|
3512
|
+
]
|
|
2587
3513
|
: fallback,
|
|
2588
3514
|
},
|
|
2589
3515
|
);
|
|
@@ -2654,7 +3580,7 @@ async function rankedPages(words, cwd, notes, flags) {
|
|
|
2654
3580
|
displayName: label(name),
|
|
2655
3581
|
why: reason
|
|
2656
3582
|
? `${reason}${words ? `; it covers ${words}` : ''}. ${shape}`
|
|
2657
|
-
: i === 0 && closest
|
|
3583
|
+
: i === 0 && closest && name !== exact
|
|
2658
3584
|
? `the closest page, not exactly the one described${words ? `: it covers ${words}` : ''}; the rest of the description is not on it. ${shape}`
|
|
2659
3585
|
: `${i === 0 ? 'the best match' : 'the next match'}${words ? `: it covers ${words}` : ''}. ${shape}`,
|
|
2660
3586
|
};
|
|
@@ -2722,6 +3648,17 @@ function installed(cwd, pkg) {
|
|
|
2722
3648
|
return findNodeModulesPackage(cwd, pkg) != null;
|
|
2723
3649
|
}
|
|
2724
3650
|
|
|
3651
|
+
/**
|
|
3652
|
+
* The folder of the nearest package.json over `dir`, up to `cwd`.
|
|
3653
|
+
* @param {string} cwd
|
|
3654
|
+
* @param {string} dir
|
|
3655
|
+
*/
|
|
3656
|
+
function appRootOf(cwd, dir) {
|
|
3657
|
+
for (let at = dir; at.startsWith(cwd + path.sep); at = path.dirname(at))
|
|
3658
|
+
if (fs.existsSync(path.join(at, 'package.json'))) return at;
|
|
3659
|
+
return cwd;
|
|
3660
|
+
}
|
|
3661
|
+
|
|
2725
3662
|
/**
|
|
2726
3663
|
* What a template page needs that the app lacks, from its real imports:
|
|
2727
3664
|
* packages the app's package.json does not name (with the command that
|
|
@@ -2820,7 +3757,7 @@ function wornTheme(cwd, slug, again, without) {
|
|
|
2820
3757
|
`${installLine(cwd, [`@astryxdesign/theme-${name}`])} # then a theme of your own can start from it`,
|
|
2821
3758
|
]
|
|
2822
3759
|
: []),
|
|
2823
|
-
`${EDITOR} new my-${name} --like ${name} # a theme of your own that starts from it`,
|
|
3760
|
+
`${EDITOR} new ${usableNewName(cwd, `my-${name}`)} --like ${name} # a theme of your own that starts from it`,
|
|
2824
3761
|
],
|
|
2825
3762
|
},
|
|
2826
3763
|
);
|
|
@@ -2905,7 +3842,7 @@ async function namedTemplate(cwd, id, flags) {
|
|
|
2905
3842
|
{
|
|
2906
3843
|
run: [
|
|
2907
3844
|
...near.map(n => `${PREVIEW} add --template ${n}${flags}`),
|
|
2908
|
-
'
|
|
3845
|
+
formatCliCommand('template --list --type page', cwd),
|
|
2909
3846
|
],
|
|
2910
3847
|
},
|
|
2911
3848
|
);
|
|
@@ -2982,6 +3919,20 @@ async function reportingRecovery(fn) {
|
|
|
2982
3919
|
}
|
|
2983
3920
|
}
|
|
2984
3921
|
|
|
3922
|
+
/**
|
|
3923
|
+
* A --theme value as the editor reads a pasted entry: `theme: t`,
|
|
3924
|
+
* `--theme=t` and the like name the theme t.
|
|
3925
|
+
* @param {string | undefined} v
|
|
3926
|
+
* @returns {string | undefined}
|
|
3927
|
+
*/
|
|
3928
|
+
function themeArg(v) {
|
|
3929
|
+
if (typeof v !== 'string') return v;
|
|
3930
|
+
const {thing, value} = normalizeEntry(v, undefined);
|
|
3931
|
+
return /^-*theme$/i.test(thing) && value?.trim()
|
|
3932
|
+
? value.trim().replace(/^['"]|['"]$/g, '')
|
|
3933
|
+
: v;
|
|
3934
|
+
}
|
|
3935
|
+
|
|
2985
3936
|
/**
|
|
2986
3937
|
* {@link themeEditPreviewAdd}, with the note of a recovery in `box`.
|
|
2987
3938
|
* @param {string | undefined} description
|
|
@@ -2990,6 +3941,7 @@ async function reportingRecovery(fn) {
|
|
|
2990
3941
|
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditPreviewAddResponse>}
|
|
2991
3942
|
*/
|
|
2992
3943
|
async function addPreview(description, options, box) {
|
|
3944
|
+
options = {...options, theme: themeArg(options.theme)};
|
|
2993
3945
|
const {cwd = process.cwd()} = options;
|
|
2994
3946
|
if (isOutsideProject(cwd))
|
|
2995
3947
|
refuse(
|
|
@@ -3008,6 +3960,25 @@ async function addPreview(description, options, box) {
|
|
|
3008
3960
|
options.theme != null
|
|
3009
3961
|
? ` --theme ${quoteCommandArgument(options.theme)}`
|
|
3010
3962
|
: '';
|
|
3963
|
+
// A default page's id (overview, components, colors), as `remove` takes
|
|
3964
|
+
// it, adds exactly that page back (or is refused, saying why).
|
|
3965
|
+
const DEFAULT_IDS = ['overview', 'components', COLORS_PAGE.id];
|
|
3966
|
+
const isDefault = (/** @type {unknown} */ v) =>
|
|
3967
|
+
typeof v === 'string' && DEFAULT_IDS.includes(v.trim().toLowerCase());
|
|
3968
|
+
// Beside another page asked for, it is refused below (drop one).
|
|
3969
|
+
const blank = (/** @type {unknown} */ v) => v == null || !String(v).trim();
|
|
3970
|
+
const askedId = [description, options.template]
|
|
3971
|
+
.find(isDefault)
|
|
3972
|
+
?.trim()
|
|
3973
|
+
.toLowerCase();
|
|
3974
|
+
const again =
|
|
3975
|
+
askedId != null &&
|
|
3976
|
+
(blank(description) || isDefault(description)) &&
|
|
3977
|
+
(blank(options.template) || isDefault(options.template));
|
|
3978
|
+
if (again) {
|
|
3979
|
+
description = undefined;
|
|
3980
|
+
options = {...options, template: undefined};
|
|
3981
|
+
}
|
|
3011
3982
|
const said = (description ?? '').trim();
|
|
3012
3983
|
const named =
|
|
3013
3984
|
options.template == null ? null : String(options.template).trim();
|
|
@@ -3015,7 +3986,7 @@ async function addPreview(description, options, box) {
|
|
|
3015
3986
|
refuse(E.ERR_INVALID_ARGUMENT, '--template needs a page template id.', {
|
|
3016
3987
|
run: [
|
|
3017
3988
|
`${PREVIEW} add --template dashboard${tf}${pf}`,
|
|
3018
|
-
'
|
|
3989
|
+
formatCliCommand('template --list --type page', cwd),
|
|
3019
3990
|
],
|
|
3020
3991
|
});
|
|
3021
3992
|
if (said && named != null)
|
|
@@ -3085,10 +4056,31 @@ async function addPreview(description, options, box) {
|
|
|
3085
4056
|
const existing = readPreview(cwd, at.dir, pf);
|
|
3086
4057
|
/** @type {PreviewData} */
|
|
3087
4058
|
const data = existing ?? {version: 1, theme: null, pages: [], files: []};
|
|
4059
|
+
// The folders over a new preview folder that are not there yet: the first
|
|
4060
|
+
// add makes them, and remove takes them away again when they are empty.
|
|
4061
|
+
if (!existing) {
|
|
4062
|
+
let made = 0;
|
|
4063
|
+
for (
|
|
4064
|
+
let up = path.dirname(at.dir);
|
|
4065
|
+
up.startsWith((at.app ?? cwd) + path.sep) && !fs.existsSync(up);
|
|
4066
|
+
up = path.dirname(up)
|
|
4067
|
+
)
|
|
4068
|
+
made++;
|
|
4069
|
+
if (made) data.parents = made;
|
|
4070
|
+
}
|
|
3088
4071
|
const wore = data.theme;
|
|
3089
4072
|
const againWith = (/** @type {string | null} */ t) =>
|
|
3090
4073
|
`${PREVIEW} add${said ? ` ${quoteCommandArgument(said)}` : ''}${named != null ? ` --template ${quoteCommandArgument(named)}` : ''}${t == null ? '' : ` --theme ${t}`}${pf}`;
|
|
3091
|
-
|
|
4074
|
+
// What leaving out --theme does here, exactly: the theme the preview
|
|
4075
|
+
// wears keeps, else the one editor theme there is, else the app's own.
|
|
4076
|
+
const without = (() => {
|
|
4077
|
+
if (data.theme && fs.existsSync(themeFiles(cwd, data.theme).dir))
|
|
4078
|
+
return `leave out --theme and the preview keeps wearing ${data.theme}`;
|
|
4079
|
+
const themes = editedThemes(cwd);
|
|
4080
|
+
return themes.length === 1
|
|
4081
|
+
? `leave out --theme and the pages wear ${themes[0]}, the one theme under themes/`
|
|
4082
|
+
: "leave out --theme and the pages wear the app's own theme";
|
|
4083
|
+
})();
|
|
3092
4084
|
if (options.theme != null) {
|
|
3093
4085
|
wornTheme(cwd, options.theme, againWith, without);
|
|
3094
4086
|
data.theme = options.theme;
|
|
@@ -3117,13 +4109,22 @@ async function addPreview(description, options, box) {
|
|
|
3117
4109
|
`the preview now wears ${data.theme ?? "the app's own theme"} (was ${wore ?? "the app's own theme"}), so its pages are written again`,
|
|
3118
4110
|
);
|
|
3119
4111
|
const project = await openProject(cwd);
|
|
4112
|
+
// Whether the preview has, or gets with a bare add, a Colors page.
|
|
4113
|
+
const hasColors = async () =>
|
|
4114
|
+
data.pages.some(p => p.id === COLORS_PAGE.id) ||
|
|
4115
|
+
(data.theme != null &&
|
|
4116
|
+
fs.existsSync(themeFiles(cwd, data.theme).dir) &&
|
|
4117
|
+
(await colorsPage(cwd, at.dir, data.theme)).colors);
|
|
3120
4118
|
/** @param {string} id */
|
|
3121
4119
|
const defaultPage = async id =>
|
|
3122
4120
|
id === 'overview'
|
|
3123
4121
|
? {
|
|
3124
4122
|
id,
|
|
3125
4123
|
label: 'Overview',
|
|
3126
|
-
text: overviewPageSource(project, {
|
|
4124
|
+
text: overviewPageSource(project, {
|
|
4125
|
+
theme: data.theme,
|
|
4126
|
+
colors: await hasColors(),
|
|
4127
|
+
}),
|
|
3127
4128
|
}
|
|
3128
4129
|
: {
|
|
3129
4130
|
id,
|
|
@@ -3155,6 +4156,33 @@ async function addPreview(description, options, box) {
|
|
|
3155
4156
|
text: colors.source,
|
|
3156
4157
|
});
|
|
3157
4158
|
}
|
|
4159
|
+
// A default page named on its own adds exactly that page, when it is
|
|
4160
|
+
// not there yet.
|
|
4161
|
+
if (askedId != null) {
|
|
4162
|
+
const there = data.pages.find(p => p.id === askedId);
|
|
4163
|
+
if (there && fs.existsSync(path.join(at.dir, `${there.file}.tsx`)))
|
|
4164
|
+
refuse(
|
|
4165
|
+
E.ERR_FILE_EXISTS,
|
|
4166
|
+
`The ${askedId} page is already in the preview; it is kept up to date as the theme changes.`,
|
|
4167
|
+
{run: [`${PREVIEW} list${pf}`]},
|
|
4168
|
+
);
|
|
4169
|
+
if (!add.some(a => a.id === askedId))
|
|
4170
|
+
refuse(
|
|
4171
|
+
E.ERR_NOT_FOUND,
|
|
4172
|
+
askedId === COLORS_PAGE.id
|
|
4173
|
+
? `There is no Colors page to add: ${data.theme ? `${data.theme} has no generated colors yet` : "the preview wears the app's own theme, and the page shows an editor theme's colors"}.`
|
|
4174
|
+
: `The ${askedId} page cannot be added here.`,
|
|
4175
|
+
{
|
|
4176
|
+
run:
|
|
4177
|
+
askedId === COLORS_PAGE.id && data.theme
|
|
4178
|
+
? [
|
|
4179
|
+
`${EDITOR} set ${data.theme} accent "#7c3aed" # then add it again`,
|
|
4180
|
+
]
|
|
4181
|
+
: [`${PREVIEW} list${pf}`],
|
|
4182
|
+
},
|
|
4183
|
+
);
|
|
4184
|
+
add.splice(0, add.length, ...add.filter(a => a.id === askedId));
|
|
4185
|
+
}
|
|
3158
4186
|
} else {
|
|
3159
4187
|
const flags = `${tf}${pf}`;
|
|
3160
4188
|
const picks = explicit
|
|
@@ -3167,7 +4195,9 @@ async function addPreview(description, options, box) {
|
|
|
3167
4195
|
await template(pick.name, {cwd, type: 'page'})
|
|
3168
4196
|
);
|
|
3169
4197
|
const source = shownTemplate.data.source;
|
|
3170
|
-
|
|
4198
|
+
// Packages are the app's: the nearest package.json over the preview
|
|
4199
|
+
// (in a workspace, the app's own, not the root's).
|
|
4200
|
+
const needs = pageNeeds(appRootOf(cwd, at.dir), source);
|
|
3171
4201
|
// A StyleX page calls stylex.create when its module loads, so without
|
|
3172
4202
|
// a StyleX compiler it breaks the whole route.
|
|
3173
4203
|
const needsCompiler = needs.stylexCompiler;
|
|
@@ -3229,6 +4259,7 @@ async function addPreview(description, options, box) {
|
|
|
3229
4259
|
})();
|
|
3230
4260
|
const elsewhere = retry(free);
|
|
3231
4261
|
const move = movedPages(cwd, at.dir, data);
|
|
4262
|
+
if (gone) dropGoneColors(cwd, at.dir, data, move, gone, notes);
|
|
3232
4263
|
/** @type {Map<string, {rel: string, text: string, keepHash?: boolean}>} */
|
|
3233
4264
|
const writes = new Map(move.writes.map(w => [w.rel, w]));
|
|
3234
4265
|
/** @type {string[]} */
|
|
@@ -3243,7 +4274,13 @@ async function addPreview(description, options, box) {
|
|
|
3243
4274
|
writes.set(rel, w);
|
|
3244
4275
|
if (!data.files.includes(rel)) data.files.push(rel);
|
|
3245
4276
|
const had = data.pages.find(x => x.id === page.id);
|
|
3246
|
-
|
|
4277
|
+
// A recorded page whose file is gone is added back, not refreshed.
|
|
4278
|
+
if (!had || !fs.existsSync(path.join(at.dir, rel))) {
|
|
4279
|
+
if (had) {
|
|
4280
|
+
had.label = page.label;
|
|
4281
|
+
added.push(page.id);
|
|
4282
|
+
continue;
|
|
4283
|
+
}
|
|
3247
4284
|
data.pages.push({
|
|
3248
4285
|
id: page.id,
|
|
3249
4286
|
label: page.label,
|
|
@@ -3305,6 +4342,19 @@ async function addPreview(description, options, box) {
|
|
|
3305
4342
|
writes.set(w.rel, w);
|
|
3306
4343
|
if (!data.files.includes(w.rel)) data.files.push(w.rel);
|
|
3307
4344
|
}
|
|
4345
|
+
// Every recorded page this command writes again is named, the Colors page
|
|
4346
|
+
// the route writes included.
|
|
4347
|
+
for (const p of data.pages)
|
|
4348
|
+
if (
|
|
4349
|
+
writes.has(`${p.file}.tsx`) &&
|
|
4350
|
+
!added.includes(p.id) &&
|
|
4351
|
+
!refreshed.includes(p.id) &&
|
|
4352
|
+
!rewrote.includes(p.id) &&
|
|
4353
|
+
fs.existsSync(path.join(at.dir, `${p.file}.tsx`)) &&
|
|
4354
|
+
fs.readFileSync(path.join(at.dir, `${p.file}.tsx`), 'utf8') !==
|
|
4355
|
+
writes.get(`${p.file}.tsx`)?.text
|
|
4356
|
+
)
|
|
4357
|
+
refreshed.push(p.id);
|
|
3308
4358
|
await buildWorn(cwd, theme, options, notes);
|
|
3309
4359
|
commitPreview(
|
|
3310
4360
|
cwd,
|
|
@@ -3328,6 +4378,33 @@ async function addPreview(description, options, box) {
|
|
|
3328
4378
|
const worn = data.theme;
|
|
3329
4379
|
if (worn)
|
|
3330
4380
|
await themeLocked(options, worn, () => recordPreview(cwd, worn, at.dir));
|
|
4381
|
+
// A font the theme names whose package the app lacks shows as the next
|
|
4382
|
+
// font in the stack.
|
|
4383
|
+
if (data.theme && fs.existsSync(themeFiles(cwd, data.theme).dir))
|
|
4384
|
+
try {
|
|
4385
|
+
const ctx = await context(cwd);
|
|
4386
|
+
for (const f of fontPackages(ctx, loadTheme(cwd, data.theme)))
|
|
4387
|
+
if (!f.installed)
|
|
4388
|
+
notes.push(
|
|
4389
|
+
`${f.family} is not installed, so the preview shows the next font in the stack in its place; install it: ${installLine(cwd, [f.pkg])}`,
|
|
4390
|
+
);
|
|
4391
|
+
} catch {
|
|
4392
|
+
// A theme that does not load is reported where it is used.
|
|
4393
|
+
}
|
|
4394
|
+
if (again)
|
|
4395
|
+
notes.push(
|
|
4396
|
+
`a default page's id brings back the default pages that are missing, as ${PREVIEW} add does`,
|
|
4397
|
+
);
|
|
4398
|
+
notes.push(...typeScriptNotes(cwd, at.dir, added));
|
|
4399
|
+
if (
|
|
4400
|
+
askedId === COLORS_PAGE.id &&
|
|
4401
|
+
!data.pages.some(p => p.id === COLORS_PAGE.id)
|
|
4402
|
+
)
|
|
4403
|
+
notes.push(
|
|
4404
|
+
data.theme
|
|
4405
|
+
? `the Colors page shows a theme's generated colors, and ${data.theme} has none yet; it appears once it does: ${EDITOR} set ${data.theme} accent <color>`
|
|
4406
|
+
: `the Colors page shows a theme's generated colors, and the preview wears the app's own theme; it appears with an editor theme that has colors: ${PREVIEW} add --theme <theme>`,
|
|
4407
|
+
);
|
|
3331
4408
|
return {
|
|
3332
4409
|
type: 'themeEdit.previewAdd',
|
|
3333
4410
|
data: {
|
|
@@ -3347,7 +4424,8 @@ async function addPreview(description, options, box) {
|
|
|
3347
4424
|
|
|
3348
4425
|
/**
|
|
3349
4426
|
* Make a new starter app (Next.js app router, Core and the neutral theme,
|
|
3350
|
-
* with the Astryx packages pinned to this CLI's release
|
|
4427
|
+
* with the Astryx packages pinned to this CLI's release, or with `theme` to
|
|
4428
|
+
* the versions the project has) in `dir`, with the
|
|
3351
4429
|
* theme preview route and its default pages inside. With `theme`, the theme
|
|
3352
4430
|
* folder is copied into the app and the pages wear it. No install runs.
|
|
3353
4431
|
* Every check runs before the first write, so a refusal changes nothing.
|
|
@@ -3364,6 +4442,7 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
3364
4442
|
'Name the folder for the new app: a new or empty folder inside the current one.',
|
|
3365
4443
|
{run: [`${PREVIEW} app theme-app`]},
|
|
3366
4444
|
);
|
|
4445
|
+
options = {...options, theme: themeArg(options.theme)};
|
|
3367
4446
|
const slug = options.theme ?? null;
|
|
3368
4447
|
const theme =
|
|
3369
4448
|
slug == null
|
|
@@ -3434,7 +4513,12 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
3434
4513
|
})) {
|
|
3435
4514
|
const sub = path.join(rel, entry.name);
|
|
3436
4515
|
if (entry.isDirectory()) copyDir(sub);
|
|
3437
|
-
else if (
|
|
4516
|
+
else if (
|
|
4517
|
+
entry.isFile() &&
|
|
4518
|
+
!(rel === '' && skip.has(entry.name)) &&
|
|
4519
|
+
// A temp file a stopped command left is no part of the theme.
|
|
4520
|
+
!isEditorTemp(entry.name)
|
|
4521
|
+
)
|
|
3438
4522
|
themeCopy.push({
|
|
3439
4523
|
rel: path.join(to.dir, sub),
|
|
3440
4524
|
text: fs.readFileSync(path.join(from.dir, sub)),
|
|
@@ -3444,6 +4528,27 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
3444
4528
|
copyDir('');
|
|
3445
4529
|
if (theme.like && theme.like !== 'neutral')
|
|
3446
4530
|
dependencies[`@astryxdesign/theme-${theme.like}`] = cliVersion();
|
|
4531
|
+
// The copied build was made on this project's Core and base theme: the
|
|
4532
|
+
// new app installs those same versions, so the build is current there.
|
|
4533
|
+
/** @type {string[]} */
|
|
4534
|
+
const pinnedTo = [];
|
|
4535
|
+
for (const pkg of [
|
|
4536
|
+
'@astryxdesign/core',
|
|
4537
|
+
'@astryxdesign/theme-neutral',
|
|
4538
|
+
...(theme.like && theme.like !== 'neutral'
|
|
4539
|
+
? [`@astryxdesign/theme-${theme.like}`]
|
|
4540
|
+
: []),
|
|
4541
|
+
]) {
|
|
4542
|
+
const v = installedVersion(cwd, pkg);
|
|
4543
|
+
if (v && v !== cliVersion()) {
|
|
4544
|
+
dependencies[pkg] = v;
|
|
4545
|
+
pinnedTo.push(`${pkg} ${v}`);
|
|
4546
|
+
}
|
|
4547
|
+
}
|
|
4548
|
+
if (pinnedTo.length)
|
|
4549
|
+
notes.push(
|
|
4550
|
+
`the new app installs ${pinnedTo.join(', ')}, the versions this project has and ${slug}'s build was made on, not this CLI's ${cliVersion()}; to move it to ${cliVersion()}, update those packages in the new app and run ${EDITOR} show ${slug} there`,
|
|
4551
|
+
);
|
|
3447
4552
|
/** @param {string} rel */
|
|
3448
4553
|
const fromRoute = rel => {
|
|
3449
4554
|
const r = path.relative(routeDir, rel).split(path.sep).join('/');
|
|
@@ -3458,7 +4563,11 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
3458
4563
|
routeTheme = {
|
|
3459
4564
|
name: slug,
|
|
3460
4565
|
module: fromRoute(to.js),
|
|
3461
|
-
css: [
|
|
4566
|
+
css: [
|
|
4567
|
+
fromRoute(to.css),
|
|
4568
|
+
...baseFontsCss(ctx, theme),
|
|
4569
|
+
...fonts.map(x => `${x.pkg}/index.css`),
|
|
4570
|
+
],
|
|
3462
4571
|
mode: themeImports(ctx, slug, theme, path.join(cwd, routeDir)).mode,
|
|
3463
4572
|
};
|
|
3464
4573
|
// The app's theme folder sits where this project's does, so the Colors
|
|
@@ -3472,7 +4581,14 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
3472
4581
|
const preview = [];
|
|
3473
4582
|
/** @type {Array<[string, string, string]>} */
|
|
3474
4583
|
const pages = [
|
|
3475
|
-
[
|
|
4584
|
+
[
|
|
4585
|
+
'overview',
|
|
4586
|
+
'Overview',
|
|
4587
|
+
overviewPageSource(project, {
|
|
4588
|
+
theme: slug,
|
|
4589
|
+
colors: colorsSource != null,
|
|
4590
|
+
}),
|
|
4591
|
+
],
|
|
3476
4592
|
[
|
|
3477
4593
|
'components',
|
|
3478
4594
|
'Components',
|
|
@@ -3548,6 +4664,26 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
3548
4664
|
/** The version range of an open font package the host project lacks. */
|
|
3549
4665
|
const OPEN_FONT_RANGE = '^5.0.0';
|
|
3550
4666
|
|
|
4667
|
+
/**
|
|
4668
|
+
* The version of a package installed for the project, read off disk (a
|
|
4669
|
+
* package need not export its package.json).
|
|
4670
|
+
* @param {string} cwd
|
|
4671
|
+
* @param {string} pkg
|
|
4672
|
+
* @returns {string | null}
|
|
4673
|
+
*/
|
|
4674
|
+
function installedVersion(cwd, pkg) {
|
|
4675
|
+
const found = findNodeModulesPackage(cwd, pkg);
|
|
4676
|
+
if (!found) return null;
|
|
4677
|
+
try {
|
|
4678
|
+
const v = JSON.parse(
|
|
4679
|
+
fs.readFileSync(path.join(found.packageDir, 'package.json'), 'utf8'),
|
|
4680
|
+
).version;
|
|
4681
|
+
return typeof v === 'string' ? v : null;
|
|
4682
|
+
} catch {
|
|
4683
|
+
return null;
|
|
4684
|
+
}
|
|
4685
|
+
}
|
|
4686
|
+
|
|
3551
4687
|
/**
|
|
3552
4688
|
* The installed version of a package, as the project resolves it.
|
|
3553
4689
|
* @param {import('./_project.mjs').ThemeProject} project
|
|
@@ -3611,6 +4747,37 @@ export async function themeEditPreviewList(options = {}) {
|
|
|
3611
4747
|
const themeMissing = Boolean(
|
|
3612
4748
|
data?.theme && !fs.existsSync(themeFiles(cwd, data.theme).dir),
|
|
3613
4749
|
);
|
|
4750
|
+
if (at && fs.existsSync(path.join(at.dir, PENDING)))
|
|
4751
|
+
notes.push(
|
|
4752
|
+
`a preview change was stopped part way, so this list may be out of date; ${PREVIEW} add${pf} finishes it`,
|
|
4753
|
+
);
|
|
4754
|
+
// A page whose file is gone breaks the route, which still imports it.
|
|
4755
|
+
const missing = new Set(
|
|
4756
|
+
(data?.pages ?? [])
|
|
4757
|
+
.filter(p => at && !fs.existsSync(path.join(at.dir, `${p.file}.tsx`)))
|
|
4758
|
+
.map(p => p.id),
|
|
4759
|
+
);
|
|
4760
|
+
if (at && missing.size)
|
|
4761
|
+
notes.push(
|
|
4762
|
+
`${[...missing].join(', ')} ${missing.size > 1 ? 'are' : 'is'} listed, but ${missing.size > 1 ? 'their files are' : 'its file is'} gone, so the route does not build; ${PREVIEW} add${pf} writes ${missing.size > 1 ? 'them' : 'it'} again`,
|
|
4763
|
+
);
|
|
4764
|
+
// No preview yet: say so when `add` would refuse the folder, with its
|
|
4765
|
+
// reason and the way on, rather than suggest it.
|
|
4766
|
+
if (!data) {
|
|
4767
|
+
try {
|
|
4768
|
+
previewDir(cwd, options.path, {
|
|
4769
|
+
write: true,
|
|
4770
|
+
retry: p =>
|
|
4771
|
+
`${PREVIEW} add${p == null ? '' : ` --path ${quoteCommandArgument(p)}`}`,
|
|
4772
|
+
});
|
|
4773
|
+
} catch (e) {
|
|
4774
|
+
if (typeof (/** @type {any} */ (e)?.code) !== 'string') throw e;
|
|
4775
|
+
const way = /** @type {any} */ (e).suggestions?.[0]?.name;
|
|
4776
|
+
notes.push(
|
|
4777
|
+
`${PREVIEW} add${pf} refuses this folder: ${String(/** @type {any} */ (e).message).replace(/ Nothing was (written|changed)\.$/, '')}${way ? ` Instead: ${way}` : ''}`,
|
|
4778
|
+
);
|
|
4779
|
+
}
|
|
4780
|
+
}
|
|
3614
4781
|
if (data?.theme && themeMissing) {
|
|
3615
4782
|
const themes = editedThemes(cwd);
|
|
3616
4783
|
notes.push(
|
|
@@ -3623,7 +4790,11 @@ export async function themeEditPreviewList(options = {}) {
|
|
|
3623
4790
|
folder: at && data ? shownDir(cwd, at.dir) : null,
|
|
3624
4791
|
theme: data?.theme ?? null,
|
|
3625
4792
|
themeMissing,
|
|
3626
|
-
pages: (data?.pages ?? []).map(x => ({
|
|
4793
|
+
pages: (data?.pages ?? []).map(x => ({
|
|
4794
|
+
id: x.id,
|
|
4795
|
+
label: x.label,
|
|
4796
|
+
...(missing.has(x.id) ? {missing: true} : {}),
|
|
4797
|
+
})),
|
|
3627
4798
|
notes,
|
|
3628
4799
|
},
|
|
3629
4800
|
};
|
|
@@ -3643,6 +4814,39 @@ export async function themeEditPreviewRemove(page, options = {}) {
|
|
|
3643
4814
|
return reportingRecovery(box => removePreview(page, options, box));
|
|
3644
4815
|
}
|
|
3645
4816
|
|
|
4817
|
+
/**
|
|
4818
|
+
* The preview folders in the app: every folder under the routes folders
|
|
4819
|
+
* (app/, src/app/) holding a theme-preview.json, a few levels deep.
|
|
4820
|
+
* @param {string} cwd
|
|
4821
|
+
* @returns {string[]}
|
|
4822
|
+
*/
|
|
4823
|
+
function previewsIn(cwd) {
|
|
4824
|
+
/** @type {string[]} */
|
|
4825
|
+
const out = [];
|
|
4826
|
+
/** @param {string} dir @param {number} depth */
|
|
4827
|
+
const walk = (dir, depth) => {
|
|
4828
|
+
if (depth > 6 || out.length >= 20) return;
|
|
4829
|
+
if (fs.existsSync(path.join(dir, PREVIEW_DATA))) out.push(dir);
|
|
4830
|
+
/** @type {fs.Dirent[]} */
|
|
4831
|
+
let entries;
|
|
4832
|
+
try {
|
|
4833
|
+
entries = fs.readdirSync(dir, {withFileTypes: true});
|
|
4834
|
+
} catch {
|
|
4835
|
+
return;
|
|
4836
|
+
}
|
|
4837
|
+
for (const e of entries)
|
|
4838
|
+
if (
|
|
4839
|
+
e.isDirectory() &&
|
|
4840
|
+
e.name !== 'node_modules' &&
|
|
4841
|
+
!e.name.startsWith('.')
|
|
4842
|
+
)
|
|
4843
|
+
walk(path.join(dir, e.name), depth + 1);
|
|
4844
|
+
};
|
|
4845
|
+
for (const r of [path.join(cwd, 'app'), path.join(cwd, 'src', 'app')])
|
|
4846
|
+
if (fs.existsSync(r)) walk(r, 0);
|
|
4847
|
+
return out.sort();
|
|
4848
|
+
}
|
|
4849
|
+
|
|
3646
4850
|
/**
|
|
3647
4851
|
* {@link themeEditPreviewRemove}, with the note of a recovery in `box`.
|
|
3648
4852
|
* @param {string | undefined} page
|
|
@@ -3658,32 +4862,66 @@ async function removePreview(page, options, box) {
|
|
|
3658
4862
|
const retry = (/** @type {string | null} */ p) =>
|
|
3659
4863
|
`${PREVIEW} remove${page != null ? ` ${quoteCommandArgument(page)}` : ''}${p == null ? '' : ` --path ${quoteCommandArgument(p)}`}`;
|
|
3660
4864
|
const at = previewDir(cwd, options.path, {retry});
|
|
4865
|
+
// The pages a stopped change was removing, read before it is finished.
|
|
4866
|
+
/** @type {string[]} */
|
|
4867
|
+
let removing = [];
|
|
4868
|
+
try {
|
|
4869
|
+
const pend = JSON.parse(
|
|
4870
|
+
fs.readFileSync(path.join(at.dir, PENDING), 'utf8'),
|
|
4871
|
+
);
|
|
4872
|
+
const ids = (/** @type {any} */ r) =>
|
|
4873
|
+
Array.isArray(r?.pages)
|
|
4874
|
+
? r.pages.map((/** @type {any} */ x) => String(x?.id))
|
|
4875
|
+
: [];
|
|
4876
|
+
const after = ids(pend?.record);
|
|
4877
|
+
removing = ids(pend?.old).filter(
|
|
4878
|
+
(/** @type {string} */ id) => !after.includes(id),
|
|
4879
|
+
);
|
|
4880
|
+
} catch {
|
|
4881
|
+
// No stopped change, or a damaged one.
|
|
4882
|
+
}
|
|
3661
4883
|
const recovered = recoverPreview(cwd, at.dir);
|
|
3662
4884
|
box.note = recovered;
|
|
3663
|
-
|
|
4885
|
+
/** @type {string[]} */
|
|
4886
|
+
const dropped = [];
|
|
4887
|
+
const data = readPreview(cwd, at.dir, pf, {dropped});
|
|
3664
4888
|
if (!data) {
|
|
3665
4889
|
// A folder that holds the app takes no preview: the way on is the
|
|
3666
4890
|
// preview elsewhere, or a new one in its own folder.
|
|
3667
|
-
|
|
3668
|
-
|
|
3669
|
-
|
|
3670
|
-
|
|
3671
|
-
|
|
3672
|
-
|
|
3673
|
-
|
|
3674
|
-
|
|
3675
|
-
|
|
3676
|
-
|
|
4891
|
+
// The previews there are, each with its remove; `add` there only
|
|
4892
|
+
// when add would take the folder.
|
|
4893
|
+
const others = previewsIn(cwd).filter(d => d !== at.dir);
|
|
4894
|
+
/** @type {string | undefined} */
|
|
4895
|
+
let usual;
|
|
4896
|
+
try {
|
|
4897
|
+
usual = previewDir(cwd, undefined, {optional: true, retry})?.dir;
|
|
4898
|
+
} catch {
|
|
4899
|
+
// Several previews, or none.
|
|
4900
|
+
}
|
|
4901
|
+
const pageArg = page != null ? ` ${quoteCommandArgument(page)}` : '';
|
|
4902
|
+
let addable = true;
|
|
4903
|
+
try {
|
|
4904
|
+
previewDir(cwd, options.path, {write: true, retry});
|
|
4905
|
+
} catch {
|
|
4906
|
+
addable = false;
|
|
4907
|
+
}
|
|
3677
4908
|
refuse(
|
|
3678
4909
|
E.ERR_NOT_FOUND,
|
|
3679
|
-
`There is no theme preview in ${shownDir(cwd, at.dir)}/ (no ${PREVIEW_DATA})
|
|
4910
|
+
`There is no theme preview in ${shownDir(cwd, at.dir)}/ (no ${PREVIEW_DATA}).${others.length ? ` ${others.length > 1 ? 'The previews here are in' : 'The preview here is in'} ${others.map(d => `${shownDir(cwd, d)}/`).join(', ')}.` : ''}`,
|
|
3680
4911
|
{
|
|
3681
4912
|
run: [
|
|
3682
|
-
|
|
3683
|
-
|
|
3684
|
-
|
|
3685
|
-
|
|
3686
|
-
|
|
4913
|
+
...others.slice(0, 3).map(
|
|
4914
|
+
d =>
|
|
4915
|
+
// The usual preview folder needs no --path.
|
|
4916
|
+
`${PREVIEW} remove${pageArg}${d === usual ? '' : ` --path ${quoteCommandArgument(shownDir(cwd, d))}`}`,
|
|
4917
|
+
),
|
|
4918
|
+
...(others.length
|
|
4919
|
+
? []
|
|
4920
|
+
: [
|
|
4921
|
+
addable && !holdsApp(cwd, at.dir)
|
|
4922
|
+
? `${PREVIEW} add${pf}`
|
|
4923
|
+
: `${PREVIEW} add`,
|
|
4924
|
+
]),
|
|
3687
4925
|
],
|
|
3688
4926
|
},
|
|
3689
4927
|
);
|
|
@@ -3692,11 +4930,15 @@ async function removePreview(page, options, box) {
|
|
|
3692
4930
|
// theme-preview.json is a file anyone can edit: every path in it must
|
|
3693
4931
|
// stay in the route folder before anything is removed.
|
|
3694
4932
|
const inside = (/** @type {string} */ rel) =>
|
|
3695
|
-
confined(path.join(at.dir, rel), at.dir,
|
|
4933
|
+
confined(path.join(at.dir, rel), at.dir, PREVIEW_DATA, {cwd, pf});
|
|
3696
4934
|
for (const rel of data.files) inside(rel);
|
|
3697
4935
|
for (const p of data.pages) inside(`${p.file}.tsx`);
|
|
3698
4936
|
/** @type {string[]} */
|
|
3699
4937
|
const notes = recovered ? [recovered] : [];
|
|
4938
|
+
if (dropped.length)
|
|
4939
|
+
notes.push(
|
|
4940
|
+
`dropped ${dropped.map(f => JSON.stringify(f)).join(', ')} from ${PREVIEW_DATA}: ${dropped.length > 1 ? 'they lead' : 'it leads'} outside the folder, so nothing there was deleted`,
|
|
4941
|
+
);
|
|
3700
4942
|
/** @type {string[]} */
|
|
3701
4943
|
const kept = [];
|
|
3702
4944
|
/** @type {string[]} */
|
|
@@ -3709,6 +4951,28 @@ async function removePreview(page, options, box) {
|
|
|
3709
4951
|
one =
|
|
3710
4952
|
data.pages.find(x => x.id === key) ??
|
|
3711
4953
|
data.pages.find(x => x.id.toLowerCase() === key.toLowerCase());
|
|
4954
|
+
// The same remove run again after it was stopped: the change just
|
|
4955
|
+
// finished removed the page.
|
|
4956
|
+
if (
|
|
4957
|
+
!one &&
|
|
4958
|
+
recovered &&
|
|
4959
|
+
removing.some(id => id.toLowerCase() === key.toLowerCase())
|
|
4960
|
+
)
|
|
4961
|
+
return {
|
|
4962
|
+
type: 'themeEdit.previewRemove',
|
|
4963
|
+
data: {
|
|
4964
|
+
folder,
|
|
4965
|
+
removed: [],
|
|
4966
|
+
kept: [],
|
|
4967
|
+
missing: [],
|
|
4968
|
+
rewritten: [],
|
|
4969
|
+
notes: [
|
|
4970
|
+
...notes,
|
|
4971
|
+
`${key} is no longer in the preview: the change that was stopped removed it`,
|
|
4972
|
+
],
|
|
4973
|
+
pages: data.pages.map(x => x.id),
|
|
4974
|
+
},
|
|
4975
|
+
};
|
|
3712
4976
|
if (!one)
|
|
3713
4977
|
refuse(E.ERR_NOT_FOUND, `The preview has no page "${page}".`, {
|
|
3714
4978
|
didYouMean: nearest(
|
|
@@ -3719,6 +4983,40 @@ async function removePreview(page, options, box) {
|
|
|
3719
4983
|
});
|
|
3720
4984
|
}
|
|
3721
4985
|
|
|
4986
|
+
// A file of the app's own that imports a page about to go would break:
|
|
4987
|
+
// the page stays, and the file is named. One guard for a page and for the
|
|
4988
|
+
// whole preview.
|
|
4989
|
+
{
|
|
4990
|
+
const going = one && data.pages.length > 1 ? [one] : data.pages;
|
|
4991
|
+
// The whole preview takes its route and layout too, which other files
|
|
4992
|
+
// may import (a re-export at another URL).
|
|
4993
|
+
const targets = [
|
|
4994
|
+
...going.map(p => path.join(at.dir, p.file)),
|
|
4995
|
+
...(going === data.pages
|
|
4996
|
+
? data.files
|
|
4997
|
+
.filter(f => /^(page|layout)\.(tsx|ts|jsx|js)$/.test(f))
|
|
4998
|
+
.map(f => path.join(at.dir, f.replace(/\.[a-z]+$/, '')))
|
|
4999
|
+
: []),
|
|
5000
|
+
];
|
|
5001
|
+
const {importers, tooBig} = pageImporters(cwd, data, at.dir, targets);
|
|
5002
|
+
if (tooBig)
|
|
5003
|
+
refuse(
|
|
5004
|
+
E.ERR_INVALID_ARGUMENT,
|
|
5005
|
+
`This project holds more than ${IMPORTER_SCAN_MAX} source files, too many to check that none of them imports ${going.length > 1 ? 'the preview pages' : 'the page'}, so nothing was removed.`,
|
|
5006
|
+
{
|
|
5007
|
+
fix: `delete ${folder}/ by hand once nothing imports from it`,
|
|
5008
|
+
},
|
|
5009
|
+
);
|
|
5010
|
+
if (importers.length)
|
|
5011
|
+
refuse(
|
|
5012
|
+
E.ERR_FILE_EXISTS,
|
|
5013
|
+
`${importers.slice(0, 3).join(', ')}${importers.length > 3 ? ` and ${importers.length - 3} more` : ''} ${importers.length > 1 ? 'import' : 'imports'} ${going.length > 1 ? 'the preview pages' : `the ${going[0]?.id} page`}, and the preview did not write ${importers.length > 1 ? 'them' : 'it'}, so removing ${going.length > 1 ? 'them' : 'it'} would break the app's build.`,
|
|
5014
|
+
{
|
|
5015
|
+
fix: `stop ${importers.length > 1 ? 'those files' : importers[0]} importing from ${folder}/ (copy what ${importers.length > 1 ? 'they need' : 'it needs'} out of it first), then run ${PREVIEW} remove${one ? ` ${one.id}` : ''}${pf} again`,
|
|
5016
|
+
},
|
|
5017
|
+
);
|
|
5018
|
+
}
|
|
5019
|
+
|
|
3722
5020
|
if (one && data.pages.length > 1) {
|
|
3723
5021
|
const target = one;
|
|
3724
5022
|
const rel = `${target.file}.tsx`;
|
|
@@ -3733,12 +5031,19 @@ async function removePreview(page, options, box) {
|
|
|
3733
5031
|
`${name} was not written by the preview, so it is kept; only its nav item is gone`,
|
|
3734
5032
|
);
|
|
3735
5033
|
} else if (!fs.existsSync(inside(rel))) missing.push(name);
|
|
3736
|
-
else if (
|
|
5034
|
+
else if (unreadable(inside(rel))) {
|
|
5035
|
+
kept.push(name);
|
|
5036
|
+
notes.push(
|
|
5037
|
+
`${name} could not be read (${unreadable(inside(rel))}), so it is left in place and stays in ${PREVIEW_DATA}; fix its permissions, then run ${PREVIEW} remove${pf} again`,
|
|
5038
|
+
);
|
|
5039
|
+
} else if (verdict !== 'delete') {
|
|
3737
5040
|
kept.push(name);
|
|
3738
5041
|
notes.push(keptNote(name, verdict));
|
|
3739
5042
|
} else deletes.push(rel);
|
|
3740
5043
|
data.pages = data.pages.filter(x => x !== target);
|
|
3741
|
-
|
|
5044
|
+
// A file left in place because it could not be read stays recorded.
|
|
5045
|
+
if (!unreadable(inside(rel)))
|
|
5046
|
+
data.files = data.files.filter(f => f !== rel);
|
|
3742
5047
|
const wore = data.theme;
|
|
3743
5048
|
/** @type {string | null} */
|
|
3744
5049
|
let gone = null;
|
|
@@ -3750,6 +5055,7 @@ async function removePreview(page, options, box) {
|
|
|
3750
5055
|
);
|
|
3751
5056
|
}
|
|
3752
5057
|
const move = movedPages(cwd, at.dir, data);
|
|
5058
|
+
if (gone) dropGoneColors(cwd, at.dir, data, move, gone, notes);
|
|
3753
5059
|
// A page whose file is gone leaves the nav, so the route never imports
|
|
3754
5060
|
// a missing file.
|
|
3755
5061
|
const moving = new Set(move.writes.map(w => w.rel));
|
|
@@ -3844,7 +5150,7 @@ async function removePreview(page, options, box) {
|
|
|
3844
5150
|
E.ERR_FILE_EXISTS,
|
|
3845
5151
|
`${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.`,
|
|
3846
5152
|
{
|
|
3847
|
-
fix: `
|
|
5153
|
+
fix: `move ${shown(cwd, path.join(at.dir, 'page.tsx'))} out of ${folder}/ (copy what you changed somewhere else in the app), then run ${PREVIEW} remove${pf} again`,
|
|
3848
5154
|
},
|
|
3849
5155
|
);
|
|
3850
5156
|
const ids = new Set(data.pages.map(p => p.id));
|
|
@@ -3885,6 +5191,14 @@ async function removePreview(page, options, box) {
|
|
|
3885
5191
|
);
|
|
3886
5192
|
continue;
|
|
3887
5193
|
}
|
|
5194
|
+
const cannot = unreadable(file);
|
|
5195
|
+
if (cannot) {
|
|
5196
|
+
kept.push(shown(cwd, file));
|
|
5197
|
+
notes.push(
|
|
5198
|
+
`${shown(cwd, file)} could not be read (${cannot}), so it is left in place; fix its permissions, then run ${PREVIEW} remove${pf} again`,
|
|
5199
|
+
);
|
|
5200
|
+
continue;
|
|
5201
|
+
}
|
|
3888
5202
|
plan.push({file, before: fs.readFileSync(file)});
|
|
3889
5203
|
}
|
|
3890
5204
|
// theme-preview.json goes last, so an interrupted remove can run again.
|
|
@@ -3912,6 +5226,7 @@ async function removePreview(page, options, box) {
|
|
|
3912
5226
|
{run: [`${retry(options.path ?? null)} # once the cause is fixed`]},
|
|
3913
5227
|
);
|
|
3914
5228
|
}
|
|
5229
|
+
sweepTemps(at.dir);
|
|
3915
5230
|
tidyPageFolders(at.dir);
|
|
3916
5231
|
// Files the record does not list stay: the preview never deletes what it
|
|
3917
5232
|
// has no record of writing. Each is named, so the folder left is no
|
|
@@ -3926,11 +5241,15 @@ async function removePreview(page, options, box) {
|
|
|
3926
5241
|
}
|
|
3927
5242
|
try {
|
|
3928
5243
|
fs.rmdirSync(at.dir);
|
|
3929
|
-
//
|
|
5244
|
+
// The folders the first add made over it, and route group folders,
|
|
5245
|
+
// such as (tools)/, that only held the preview; never one with
|
|
5246
|
+
// anything else in it.
|
|
5247
|
+
let made = data.parents ?? 0;
|
|
3930
5248
|
for (
|
|
3931
5249
|
let up = path.dirname(at.dir);
|
|
3932
|
-
/^\(.+\)$/.test(path.basename(up)) &&
|
|
3933
|
-
up
|
|
5250
|
+
(made > 0 || (at.app != null && /^\(.+\)$/.test(path.basename(up)))) &&
|
|
5251
|
+
up.startsWith((at.app ?? cwd) + path.sep);
|
|
5252
|
+
up = path.dirname(up), made--
|
|
3934
5253
|
)
|
|
3935
5254
|
fs.rmdirSync(up);
|
|
3936
5255
|
} catch {
|