@astryxdesign/cli 0.7.15 → 0.7.16-canary.g22213fd
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 +6 -4
- package/api/build/_adapter.d.mts +10 -0
- package/api/build/_adapter.mjs +24 -0
- package/api/build/blocks/blocks.d.mts +1 -0
- package/api/build/blocks/blocks.mjs +18 -1
- package/api/build/kit/shell.weights.json +1 -0
- package/api/build/kit/shellcall.d.mts +26 -0
- package/api/build/kit/shellcall.mjs +87 -0
- package/api/build/kit/start.mjs +67 -4
- package/api/build/kit/weights.d.mts +15 -0
- package/api/build/kit/weights.mjs +27 -0
- package/api/component/__fixtures__/resolutions/modules.txt +1 -1
- package/api/component/_adapter.d.mts +1 -1
- package/api/component/_adapter.mjs +28 -1
- package/api/doctor/_adapter.d.mts +38 -2
- package/api/doctor/_adapter.mjs +76 -1
- package/api/doctor/built-checks.mjs +1 -1
- package/api/doctor/doctor.d.mts +26 -0
- package/api/doctor/doctor.doc.mjs +1 -1
- package/api/doctor/doctor.mjs +156 -11
- package/api/doctor/theme-checks.mjs +163 -18
- package/api/icon/_adapter.d.mts +7 -3
- package/api/icon/_adapter.mjs +12 -3
- package/api/icon/icon.type.d.mts +15 -0
- package/api/icon/icon.type.mjs +10 -3
- package/api/icon/iconUse.doc.mjs +19 -5
- package/api/icon/list/list.mjs +7 -1
- package/api/icon/use/use.d.mts +8 -5
- package/api/icon/use/use.mjs +95 -13
- package/api/init/run/run.mjs +1 -0
- 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/template/template.type.d.mts +6 -2
- package/api/template/template.type.mjs +2 -1
- package/api/theme/_adapter.d.mts +5 -2
- package/api/theme/_adapter.mjs +59 -14
- package/api/theme/_fork.mjs +1 -1
- package/api/theme/build/build.d.mts +120 -32
- package/api/theme/build/build.mjs +471 -111
- package/api/theme/eject/eject.mjs +22 -1
- package/api/theme/generateTonalPalette.doc.mjs +2 -2
- package/api/theme/palette/generate/generator.mjs +141 -2
- package/api/theme/template/template.d.mts +19 -0
- package/api/theme/template/template.mjs +102 -25
- package/api/theme/theme.type.d.mts +44 -1
- package/api/theme/theme.type.mjs +23 -1
- package/api/theme/themeBuild.doc.mjs +4 -4
- package/api/theme/themeEject.doc.mjs +1 -1
- package/api/theme/themePaletteGenerate.doc.mjs +1 -1
- package/api/theme/themeTemplate.doc.mjs +9 -3
- package/api/theme-edit/_adapter.d.mts +33 -2
- package/api/theme-edit/_adapter.mjs +276 -41
- package/api/theme-edit/_check.d.mts +93 -4
- package/api/theme-edit/_check.mjs +1712 -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 +1620 -154
- 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 +15 -0
- package/api/theme-edit/_how-question.mjs +1469 -194
- package/api/theme-edit/_how.mjs +144 -30
- package/api/theme-edit/_marks.d.mts +13 -0
- package/api/theme-edit/_marks.mjs +15 -0
- package/api/theme-edit/_overview.mjs +2 -0
- package/api/theme-edit/_palette.d.mts +38 -0
- package/api/theme-edit/_palette.mjs +306 -73
- 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 +55 -1
- package/api/theme-edit/_preview-app.mjs +1552 -101
- package/api/theme-edit/_preview.d.mts +71 -3
- package/api/theme-edit/_preview.mjs +565 -35
- package/api/theme-edit/_project.d.mts +15 -0
- package/api/theme-edit/_project.mjs +68 -13
- package/api/theme-edit/_settings.d.mts +299 -5
- package/api/theme-edit/_settings.mjs +2806 -158
- package/api/theme-edit/_tables.d.mts +24 -0
- package/api/theme-edit/_tables.mjs +212 -150
- 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 +31 -2
- package/api/theme-edit/theme-edit.type.mjs +10 -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/author-a-theme.doc.mjs +1 -1
- package/assets/docs/theme.doc.dense.mjs +1 -1
- package/assets/docs/theme.doc.mjs +1 -1
- package/assets/docs/theme.doc.zh.mjs +4 -3
- package/assets/docs/tree/define-the-theme.doc.mjs +1 -1
- package/assets/docs/tree/icon-sets.doc.mjs +63 -8
- 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/Outline/useOutlineFromDOMHookUsage.doc.mjs +21 -0
- package/assets/templates/blocks/components/Outline/useOutlineFromDOMHookUsage.tsx +62 -0
- 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/ai-chat/page.tsx +20 -2
- package/assets/templates/pages/dashboard/page.tsx +1 -1
- package/assets/templates/themes/neutral/neutralPalettes.generated.receipt.json +2 -0
- package/authoring/config/config.doc.mjs +8 -0
- package/authoring/config/parse.d.mts +1 -0
- package/authoring/config/parse.mjs +12 -0
- package/authoring/config/type.ts +8 -0
- 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/icon-set/icon-set-data.doc.mjs +3 -3
- 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/icon.mjs +61 -6
- package/clients/cli/commands/experimental/theme.mjs +244 -45
- package/clients/cli/commands/experimental-icon-use.doc.mjs +12 -5
- 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.doc.mjs +7 -2
- package/clients/cli/commands/theme.mjs +222 -55
- package/clients/cli/commands.generated.json +1 -1
- package/clients/cli/formatters/index.mjs +7 -4
- package/clients/cli/index.mjs +25 -5
- 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 +4 -3
- package/foundation/config/config-writer.d.mts +23 -0
- package/foundation/config/config-writer.mjs +152 -58
- package/foundation/config/project.mjs +103 -16
- package/foundation/discovery/component-discovery.mjs +11 -22
- package/foundation/discovery/icon-set-checks.mjs +164 -48
- package/foundation/discovery/icon-set-discovery.d.mts +38 -5
- package/foundation/discovery/icon-set-discovery.mjs +50 -6
- package/foundation/discovery/icon-set-theme-maps.d.mts +59 -0
- package/foundation/discovery/icon-set-theme-maps.mjs +95 -0
- package/foundation/discovery/icon-set-writer.d.mts +2 -3
- package/foundation/discovery/icon-set-writer.mjs +58 -17
- package/foundation/discovery/module-exports.mjs +1 -15
- package/foundation/discovery/template-adapter.mjs +44 -19
- package/foundation/discovery/template-icons.d.mts +27 -10
- package/foundation/discovery/template-icons.mjs +142 -26
- package/foundation/discovery/template-source-tree.mjs +3 -16
- package/foundation/discovery/theme-discovery.d.mts +30 -1
- package/foundation/discovery/theme-discovery.mjs +109 -50
- 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/autolink.d.mts +4 -2
- package/foundation/integrations/autolink.mjs +4 -2
- package/foundation/integrations/cli-requirement.d.mts +16 -5
- package/foundation/integrations/cli-requirement.mjs +27 -6
- package/foundation/integrations/contribution-fixes.mjs +1 -16
- package/foundation/integrations/may-have-integrations.mjs +7 -3
- package/foundation/integrations/missing-dependency.d.mts +9 -0
- package/foundation/integrations/missing-dependency.mjs +100 -0
- 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,21 @@ import {
|
|
|
33
35
|
shown,
|
|
34
36
|
staleBuild,
|
|
35
37
|
themeFiles,
|
|
38
|
+
unlabeledThemeName,
|
|
39
|
+
usableNewName,
|
|
36
40
|
windowsReserved,
|
|
37
41
|
} from './_settings.mjs';
|
|
38
42
|
import {
|
|
43
|
+
baseFontsCss,
|
|
39
44
|
colorsPageSource,
|
|
40
45
|
componentsPageSource,
|
|
41
46
|
overviewPageSource,
|
|
42
47
|
previewRouteSource,
|
|
48
|
+
appCopyBlocker,
|
|
43
49
|
themeImports,
|
|
44
50
|
} from './_preview.mjs';
|
|
45
51
|
import {
|
|
52
|
+
isEditorTemp,
|
|
46
53
|
PALETTE_STAND_IN,
|
|
47
54
|
paletteFiles,
|
|
48
55
|
paletteMapping,
|
|
@@ -60,7 +67,10 @@ import {
|
|
|
60
67
|
import {detectStylingSystem} from '../../foundation/agent-docs/agent-docs.mjs';
|
|
61
68
|
import {requireProject, context} from './_context.mjs';
|
|
62
69
|
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
|
|
63
|
-
import {
|
|
70
|
+
import {
|
|
71
|
+
formatCliCommand,
|
|
72
|
+
quoteCommandArgument,
|
|
73
|
+
} from '../../foundation/env/package-manager.mjs';
|
|
64
74
|
import {sha256} from '../../foundation/render/source-block.mjs';
|
|
65
75
|
import {findNodeModulesPackage} from '../../foundation/fs/paths.mjs';
|
|
66
76
|
|
|
@@ -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;
|
|
@@ -2446,17 +3313,121 @@ function recordPreview(cwd, slug, dir, keep = true) {
|
|
|
2446
3313
|
}
|
|
2447
3314
|
|
|
2448
3315
|
/**
|
|
2449
|
-
*
|
|
3316
|
+
* The routes folder a preview folder is in, by place (also one a command
|
|
3317
|
+
* has just made), or null.
|
|
2450
3318
|
* @param {string} cwd
|
|
2451
3319
|
* @param {{dir: string, app: string | null}} at
|
|
2452
3320
|
*/
|
|
2453
|
-
function
|
|
2454
|
-
|
|
2455
|
-
const app =
|
|
3321
|
+
function routesOf(cwd, at) {
|
|
3322
|
+
return (
|
|
2456
3323
|
at.app ??
|
|
2457
3324
|
[path.join(cwd, 'app'), path.join(cwd, 'src', 'app')].find(r =>
|
|
2458
3325
|
at.dir.startsWith(r + path.sep),
|
|
2459
|
-
)
|
|
3326
|
+
) ??
|
|
3327
|
+
null
|
|
3328
|
+
);
|
|
3329
|
+
}
|
|
3330
|
+
|
|
3331
|
+
/**
|
|
3332
|
+
* The URL path the app serves a preview at (with the basePath next.config
|
|
3333
|
+
* sets), or null when no URL can be told: outside the routes folder, under
|
|
3334
|
+
* a [param] folder, or a basePath set from code.
|
|
3335
|
+
* @param {string} cwd
|
|
3336
|
+
* @param {{dir: string, app: string | null}} at
|
|
3337
|
+
* @returns {string | null}
|
|
3338
|
+
*/
|
|
3339
|
+
function previewPath(cwd, at) {
|
|
3340
|
+
const app = routesOf(cwd, at);
|
|
3341
|
+
if (!app) return null;
|
|
3342
|
+
const segs = urlSegments(app, at.dir);
|
|
3343
|
+
if (segs.some(seg => /^\[.+\]$/.test(seg))) return null;
|
|
3344
|
+
const base = nextBasePath(cwd);
|
|
3345
|
+
if (base == null) return null;
|
|
3346
|
+
return `${base}/${segs.map(seg => encodeURIComponent(seg)).join('/')}`;
|
|
3347
|
+
}
|
|
3348
|
+
|
|
3349
|
+
/**
|
|
3350
|
+
* Each page's own URL in the running app: the preview's path with
|
|
3351
|
+
* ?page=<id>, the first page at the bare path too. Empty when no URL can
|
|
3352
|
+
* be told.
|
|
3353
|
+
* @param {string} cwd
|
|
3354
|
+
* @param {{dir: string, app: string | null}} at
|
|
3355
|
+
* @param {string[]} ids
|
|
3356
|
+
* @returns {Record<string, string>}
|
|
3357
|
+
*/
|
|
3358
|
+
function pageUrls(cwd, at, ids) {
|
|
3359
|
+
const base = previewPath(cwd, at);
|
|
3360
|
+
if (base == null) return {};
|
|
3361
|
+
return Object.fromEntries(ids.map(id => [id, `${base}?page=${id}`]));
|
|
3362
|
+
}
|
|
3363
|
+
|
|
3364
|
+
/**
|
|
3365
|
+
* One line that says how to see a theme after a change. First a look that
|
|
3366
|
+
* changes nothing in the app: the editor's own preview.html (current after
|
|
3367
|
+
* the change) and --png, which takes pictures with no dev server; then the
|
|
3368
|
+
* in-app preview page most about what changed (Colors after a color,
|
|
3369
|
+
* Components after a part, else Overview) at its own URL when there is a
|
|
3370
|
+
* preview; adding one to the app last, as an option. A change that could
|
|
3371
|
+
* not be rendered names why instead of the editor's page.
|
|
3372
|
+
* @param {string} cwd
|
|
3373
|
+
* @param {string} slug
|
|
3374
|
+
* @param {'colors' | 'components' | 'overview'} focus
|
|
3375
|
+
* @param {{code: string, cause: string} | null} [notChecked] why the change
|
|
3376
|
+
* could not be rendered
|
|
3377
|
+
* @returns {string}
|
|
3378
|
+
*/
|
|
3379
|
+
export function seeLine(cwd, slug, focus, notChecked = null) {
|
|
3380
|
+
/** @type {string[]} */
|
|
3381
|
+
const urls = [];
|
|
3382
|
+
/** Previews whose URL depends on the app's routing. @type {string[]} */
|
|
3383
|
+
const unplain = [];
|
|
3384
|
+
// The previews recorded for the theme, and any found in the routes
|
|
3385
|
+
// folders that wear it (a folder renamed by hand), as remove finds them.
|
|
3386
|
+
const dirs = [
|
|
3387
|
+
...new Set([...recordedPreviews(cwd, slug), ...previewsIn(cwd)]),
|
|
3388
|
+
];
|
|
3389
|
+
for (const dir of dirs) {
|
|
3390
|
+
/** @type {any} */
|
|
3391
|
+
let data;
|
|
3392
|
+
try {
|
|
3393
|
+
data = JSON.parse(fs.readFileSync(path.join(dir, PREVIEW_DATA), 'utf8'));
|
|
3394
|
+
} catch {
|
|
3395
|
+
continue;
|
|
3396
|
+
}
|
|
3397
|
+
if (data?.theme !== slug) continue;
|
|
3398
|
+
const ids = Array.isArray(data?.pages)
|
|
3399
|
+
? data.pages.map((/** @type {any} */ x) => String(x?.id))
|
|
3400
|
+
: [];
|
|
3401
|
+
// Only a page the preview has.
|
|
3402
|
+
const id = ids.includes(focus) ? focus : ids[0];
|
|
3403
|
+
const url = id ? pageUrls(cwd, {dir, app: null}, [id])[id] : undefined;
|
|
3404
|
+
if (url) urls.push(url);
|
|
3405
|
+
else
|
|
3406
|
+
unplain.push(
|
|
3407
|
+
`${shownDir(cwd, dir)}/${id ? ` (its ${id} page: add ?page=${id} to its URL)` : ''}`,
|
|
3408
|
+
);
|
|
3409
|
+
}
|
|
3410
|
+
const f = themeFiles(cwd, slug);
|
|
3411
|
+
const own = notChecked
|
|
3412
|
+
? /could not be written/.test(notChecked.cause)
|
|
3413
|
+
? `the change is saved, but ${notChecked.cause}; --png takes no pictures until that is fixed`
|
|
3414
|
+
: `the change is saved, but the components could not be rendered here (${notChecked.cause}), so ${shown(cwd, f.preview)} still shows the theme as it was and --png takes no pictures until that is fixed`
|
|
3415
|
+
: `look without changing your app: open or screenshot ${shown(cwd, f.preview)} (one self-contained page, current after this change; ${EDITOR} show ${slug} also writes ${shown(cwd, f.sheet)}, every component), or run it with --png for pictures in ${shownDir(cwd, f.dir)}/ with no dev server (it needs a Playwright or a Chrome)`;
|
|
3416
|
+
if (urls.length)
|
|
3417
|
+
return `${own}; in the running app: open ${urls.slice(0, 2).join(' or ')}`;
|
|
3418
|
+
if (unplain.length)
|
|
3419
|
+
return `${own}; the in-app preview is in ${unplain.slice(0, 2).join(', ')}, at a URL that depends on the app's routing`;
|
|
3420
|
+
return `${own}; to see it inside your app, ${PREVIEW} add puts the preview pages there, each at its own URL`;
|
|
3421
|
+
}
|
|
3422
|
+
|
|
3423
|
+
/**
|
|
3424
|
+
* How to open the preview, from where it is.
|
|
3425
|
+
* @param {string} cwd
|
|
3426
|
+
* @param {{dir: string, app: string | null}} at
|
|
3427
|
+
*/
|
|
3428
|
+
function openLine(cwd, at) {
|
|
3429
|
+
// The routes folder by place, also one this command has just made.
|
|
3430
|
+
const app = routesOf(cwd, at);
|
|
2460
3431
|
if (app) {
|
|
2461
3432
|
// A (group) folder is not part of the URL; a [param] folder is a value
|
|
2462
3433
|
// the person picks; each other folder is one URL segment, encoded.
|
|
@@ -2536,7 +3507,61 @@ async function rankedPages(words, cwd, notes, flags) {
|
|
|
2536
3507
|
String(r.reason ?? '').replace(/^matches \d+\/\d+ terms: /, ''),
|
|
2537
3508
|
]),
|
|
2538
3509
|
);
|
|
3510
|
+
// A description that is a template's id or display name names it.
|
|
3511
|
+
const plain = (/** @type {string} */ x) =>
|
|
3512
|
+
x
|
|
3513
|
+
.toLowerCase()
|
|
3514
|
+
.replace(/[^a-z0-9]+/g, ' ')
|
|
3515
|
+
.trim();
|
|
3516
|
+
const exact = listed.find(
|
|
3517
|
+
t => plain(t.id) === plain(words) || plain(t.name ?? '') === plain(words),
|
|
3518
|
+
)?.id;
|
|
3519
|
+
if (exact && frames.has(exact))
|
|
3520
|
+
refuse(
|
|
3521
|
+
E.ERR_INVALID_ARGUMENT,
|
|
3522
|
+
`${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.`,
|
|
3523
|
+
{run: [`${PREVIEW} add --template ${exact}${flags}`]},
|
|
3524
|
+
);
|
|
3525
|
+
// Build's page ranking, when build picked no page itself (it fell back to
|
|
3526
|
+
// a frame): its pages the description names (by name, keyword or docs,
|
|
3527
|
+
// 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
|
|
3528
|
+
// description goes first.
|
|
3529
|
+
const said = plain(words)
|
|
3530
|
+
.split(' ')
|
|
3531
|
+
.filter(w => w.length > 2);
|
|
3532
|
+
const byId = (/** @type {string} */ id) =>
|
|
3533
|
+
said.some(w => id.split('-').includes(w)) ? 1 : 0;
|
|
3534
|
+
const fellBack =
|
|
3535
|
+
!start || (start.basis === 'fallback' && frames.has(start.name));
|
|
3536
|
+
const ranked = (fellBack ? [...(kit.data.pages ?? [])] : [])
|
|
3537
|
+
.map((r, i) => ({
|
|
3538
|
+
name: r.name,
|
|
3539
|
+
score: Number(r.score) || 0,
|
|
3540
|
+
reason: String(r.reason ?? ''),
|
|
3541
|
+
i,
|
|
3542
|
+
}))
|
|
3543
|
+
.filter(
|
|
3544
|
+
r =>
|
|
3545
|
+
listed.some(t => t.id === r.name) &&
|
|
3546
|
+
(/^(exact name|name|keyword|docs mention|description mentions)\b/.test(
|
|
3547
|
+
r.reason,
|
|
3548
|
+
) ||
|
|
3549
|
+
r.score >= 100 ||
|
|
3550
|
+
byId(r.name)),
|
|
3551
|
+
)
|
|
3552
|
+
.sort(
|
|
3553
|
+
(a, b) => b.score - a.score || byId(b.name) - byId(a.name) || a.i - b.i,
|
|
3554
|
+
)
|
|
3555
|
+
.map(r => r.name);
|
|
3556
|
+
for (const r of kit.data.pages ?? [])
|
|
3557
|
+
if (!matchedWords.has(r.name))
|
|
3558
|
+
matchedWords.set(
|
|
3559
|
+
r.name,
|
|
3560
|
+
String(r.reason ?? '').replace(/^matches \d+\/\d+ terms: /, ''),
|
|
3561
|
+
);
|
|
2539
3562
|
const names = [
|
|
3563
|
+
...(exact ? [exact] : []),
|
|
3564
|
+
...ranked,
|
|
2540
3565
|
...(start ? [start.name, ...start.alternatives.map(a => a.name)] : []),
|
|
2541
3566
|
...found.map((/** @type {any} */ r) => r.name),
|
|
2542
3567
|
].filter((n, i, a) => a.indexOf(n) === i);
|
|
@@ -2553,7 +3578,7 @@ async function rankedPages(words, cwd, notes, flags) {
|
|
|
2553
3578
|
listed.some(t => t.id === id),
|
|
2554
3579
|
),
|
|
2555
3580
|
),
|
|
2556
|
-
'
|
|
3581
|
+
formatCliCommand('template --list --type page', cwd),
|
|
2557
3582
|
];
|
|
2558
3583
|
// No page relates when the ranking fell back to a frame with no direct
|
|
2559
3584
|
// match, and no page the search found matches three words of the request
|
|
@@ -2569,6 +3594,8 @@ async function rankedPages(words, cwd, notes, flags) {
|
|
|
2569
3594
|
const named = APP_PAGES.some(([re]) => re.test(words.toLowerCase()));
|
|
2570
3595
|
const unrelated =
|
|
2571
3596
|
!named &&
|
|
3597
|
+
!exact &&
|
|
3598
|
+
!ranked.some(n => !frames.has(n)) &&
|
|
2572
3599
|
(!pages.length ||
|
|
2573
3600
|
(start &&
|
|
2574
3601
|
start.basis === 'fallback' &&
|
|
@@ -2583,7 +3610,10 @@ async function rankedPages(words, cwd, notes, flags) {
|
|
|
2583
3610
|
`No page template relates to "${words}". Name a page to add it anyway.`,
|
|
2584
3611
|
{
|
|
2585
3612
|
run: pages.length
|
|
2586
|
-
? [
|
|
3613
|
+
? [
|
|
3614
|
+
...suggest(pages),
|
|
3615
|
+
formatCliCommand('template --list --type page', cwd),
|
|
3616
|
+
]
|
|
2587
3617
|
: fallback,
|
|
2588
3618
|
},
|
|
2589
3619
|
);
|
|
@@ -2654,7 +3684,7 @@ async function rankedPages(words, cwd, notes, flags) {
|
|
|
2654
3684
|
displayName: label(name),
|
|
2655
3685
|
why: reason
|
|
2656
3686
|
? `${reason}${words ? `; it covers ${words}` : ''}. ${shape}`
|
|
2657
|
-
: i === 0 && closest
|
|
3687
|
+
: i === 0 && closest && name !== exact
|
|
2658
3688
|
? `the closest page, not exactly the one described${words ? `: it covers ${words}` : ''}; the rest of the description is not on it. ${shape}`
|
|
2659
3689
|
: `${i === 0 ? 'the best match' : 'the next match'}${words ? `: it covers ${words}` : ''}. ${shape}`,
|
|
2660
3690
|
};
|
|
@@ -2722,6 +3752,17 @@ function installed(cwd, pkg) {
|
|
|
2722
3752
|
return findNodeModulesPackage(cwd, pkg) != null;
|
|
2723
3753
|
}
|
|
2724
3754
|
|
|
3755
|
+
/**
|
|
3756
|
+
* The folder of the nearest package.json over `dir`, up to `cwd`.
|
|
3757
|
+
* @param {string} cwd
|
|
3758
|
+
* @param {string} dir
|
|
3759
|
+
*/
|
|
3760
|
+
function appRootOf(cwd, dir) {
|
|
3761
|
+
for (let at = dir; at.startsWith(cwd + path.sep); at = path.dirname(at))
|
|
3762
|
+
if (fs.existsSync(path.join(at, 'package.json'))) return at;
|
|
3763
|
+
return cwd;
|
|
3764
|
+
}
|
|
3765
|
+
|
|
2725
3766
|
/**
|
|
2726
3767
|
* What a template page needs that the app lacks, from its real imports:
|
|
2727
3768
|
* packages the app's package.json does not name (with the command that
|
|
@@ -2820,7 +3861,7 @@ function wornTheme(cwd, slug, again, without) {
|
|
|
2820
3861
|
`${installLine(cwd, [`@astryxdesign/theme-${name}`])} # then a theme of your own can start from it`,
|
|
2821
3862
|
]
|
|
2822
3863
|
: []),
|
|
2823
|
-
`${EDITOR} new my-${name} --like ${name} # a theme of your own that starts from it`,
|
|
3864
|
+
`${EDITOR} new ${usableNewName(cwd, `my-${name}`)} --like ${name} # a theme of your own that starts from it`,
|
|
2824
3865
|
],
|
|
2825
3866
|
},
|
|
2826
3867
|
);
|
|
@@ -2905,7 +3946,7 @@ async function namedTemplate(cwd, id, flags) {
|
|
|
2905
3946
|
{
|
|
2906
3947
|
run: [
|
|
2907
3948
|
...near.map(n => `${PREVIEW} add --template ${n}${flags}`),
|
|
2908
|
-
'
|
|
3949
|
+
formatCliCommand('template --list --type page', cwd),
|
|
2909
3950
|
],
|
|
2910
3951
|
},
|
|
2911
3952
|
);
|
|
@@ -2982,6 +4023,16 @@ async function reportingRecovery(fn) {
|
|
|
2982
4023
|
}
|
|
2983
4024
|
}
|
|
2984
4025
|
|
|
4026
|
+
/**
|
|
4027
|
+
* A --theme value as the editor reads a pasted entry: `theme: t`,
|
|
4028
|
+
* `--theme=t` and the like name the theme t.
|
|
4029
|
+
* @param {string | undefined} v
|
|
4030
|
+
* @returns {string | undefined}
|
|
4031
|
+
*/
|
|
4032
|
+
function themeArg(v) {
|
|
4033
|
+
return typeof v === 'string' ? unlabeledThemeName(v) : v;
|
|
4034
|
+
}
|
|
4035
|
+
|
|
2985
4036
|
/**
|
|
2986
4037
|
* {@link themeEditPreviewAdd}, with the note of a recovery in `box`.
|
|
2987
4038
|
* @param {string | undefined} description
|
|
@@ -2990,6 +4041,7 @@ async function reportingRecovery(fn) {
|
|
|
2990
4041
|
* @returns {Promise<import('./theme-edit.type.mjs').ThemeEditPreviewAddResponse>}
|
|
2991
4042
|
*/
|
|
2992
4043
|
async function addPreview(description, options, box) {
|
|
4044
|
+
options = {...options, theme: themeArg(options.theme)};
|
|
2993
4045
|
const {cwd = process.cwd()} = options;
|
|
2994
4046
|
if (isOutsideProject(cwd))
|
|
2995
4047
|
refuse(
|
|
@@ -3008,6 +4060,25 @@ async function addPreview(description, options, box) {
|
|
|
3008
4060
|
options.theme != null
|
|
3009
4061
|
? ` --theme ${quoteCommandArgument(options.theme)}`
|
|
3010
4062
|
: '';
|
|
4063
|
+
// A default page's id (overview, components, colors), as `remove` takes
|
|
4064
|
+
// it, adds exactly that page back (or is refused, saying why).
|
|
4065
|
+
const DEFAULT_IDS = ['overview', 'components', COLORS_PAGE.id];
|
|
4066
|
+
const isDefault = (/** @type {unknown} */ v) =>
|
|
4067
|
+
typeof v === 'string' && DEFAULT_IDS.includes(v.trim().toLowerCase());
|
|
4068
|
+
// Beside another page asked for, it is refused below (drop one).
|
|
4069
|
+
const blank = (/** @type {unknown} */ v) => v == null || !String(v).trim();
|
|
4070
|
+
const askedId = [description, options.template]
|
|
4071
|
+
.find(isDefault)
|
|
4072
|
+
?.trim()
|
|
4073
|
+
.toLowerCase();
|
|
4074
|
+
const again =
|
|
4075
|
+
askedId != null &&
|
|
4076
|
+
(blank(description) || isDefault(description)) &&
|
|
4077
|
+
(blank(options.template) || isDefault(options.template));
|
|
4078
|
+
if (again) {
|
|
4079
|
+
description = undefined;
|
|
4080
|
+
options = {...options, template: undefined};
|
|
4081
|
+
}
|
|
3011
4082
|
const said = (description ?? '').trim();
|
|
3012
4083
|
const named =
|
|
3013
4084
|
options.template == null ? null : String(options.template).trim();
|
|
@@ -3015,7 +4086,7 @@ async function addPreview(description, options, box) {
|
|
|
3015
4086
|
refuse(E.ERR_INVALID_ARGUMENT, '--template needs a page template id.', {
|
|
3016
4087
|
run: [
|
|
3017
4088
|
`${PREVIEW} add --template dashboard${tf}${pf}`,
|
|
3018
|
-
'
|
|
4089
|
+
formatCliCommand('template --list --type page', cwd),
|
|
3019
4090
|
],
|
|
3020
4091
|
});
|
|
3021
4092
|
if (said && named != null)
|
|
@@ -3085,10 +4156,31 @@ async function addPreview(description, options, box) {
|
|
|
3085
4156
|
const existing = readPreview(cwd, at.dir, pf);
|
|
3086
4157
|
/** @type {PreviewData} */
|
|
3087
4158
|
const data = existing ?? {version: 1, theme: null, pages: [], files: []};
|
|
4159
|
+
// The folders over a new preview folder that are not there yet: the first
|
|
4160
|
+
// add makes them, and remove takes them away again when they are empty.
|
|
4161
|
+
if (!existing) {
|
|
4162
|
+
let made = 0;
|
|
4163
|
+
for (
|
|
4164
|
+
let up = path.dirname(at.dir);
|
|
4165
|
+
up.startsWith((at.app ?? cwd) + path.sep) && !fs.existsSync(up);
|
|
4166
|
+
up = path.dirname(up)
|
|
4167
|
+
)
|
|
4168
|
+
made++;
|
|
4169
|
+
if (made) data.parents = made;
|
|
4170
|
+
}
|
|
3088
4171
|
const wore = data.theme;
|
|
3089
4172
|
const againWith = (/** @type {string | null} */ t) =>
|
|
3090
4173
|
`${PREVIEW} add${said ? ` ${quoteCommandArgument(said)}` : ''}${named != null ? ` --template ${quoteCommandArgument(named)}` : ''}${t == null ? '' : ` --theme ${t}`}${pf}`;
|
|
3091
|
-
|
|
4174
|
+
// What leaving out --theme does here, exactly: the theme the preview
|
|
4175
|
+
// wears keeps, else the one editor theme there is, else the app's own.
|
|
4176
|
+
const without = (() => {
|
|
4177
|
+
if (data.theme && fs.existsSync(themeFiles(cwd, data.theme).dir))
|
|
4178
|
+
return `leave out --theme and the preview keeps wearing ${data.theme}`;
|
|
4179
|
+
const themes = editedThemes(cwd);
|
|
4180
|
+
return themes.length === 1
|
|
4181
|
+
? `leave out --theme and the pages wear ${themes[0]}, the one theme under themes/`
|
|
4182
|
+
: "leave out --theme and the pages wear the app's own theme";
|
|
4183
|
+
})();
|
|
3092
4184
|
if (options.theme != null) {
|
|
3093
4185
|
wornTheme(cwd, options.theme, againWith, without);
|
|
3094
4186
|
data.theme = options.theme;
|
|
@@ -3117,13 +4209,22 @@ async function addPreview(description, options, box) {
|
|
|
3117
4209
|
`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
4210
|
);
|
|
3119
4211
|
const project = await openProject(cwd);
|
|
4212
|
+
// Whether the preview has, or gets with a bare add, a Colors page.
|
|
4213
|
+
const hasColors = async () =>
|
|
4214
|
+
data.pages.some(p => p.id === COLORS_PAGE.id) ||
|
|
4215
|
+
(data.theme != null &&
|
|
4216
|
+
fs.existsSync(themeFiles(cwd, data.theme).dir) &&
|
|
4217
|
+
(await colorsPage(cwd, at.dir, data.theme)).colors);
|
|
3120
4218
|
/** @param {string} id */
|
|
3121
4219
|
const defaultPage = async id =>
|
|
3122
4220
|
id === 'overview'
|
|
3123
4221
|
? {
|
|
3124
4222
|
id,
|
|
3125
4223
|
label: 'Overview',
|
|
3126
|
-
text: overviewPageSource(project, {
|
|
4224
|
+
text: overviewPageSource(project, {
|
|
4225
|
+
theme: data.theme,
|
|
4226
|
+
colors: await hasColors(),
|
|
4227
|
+
}),
|
|
3127
4228
|
}
|
|
3128
4229
|
: {
|
|
3129
4230
|
id,
|
|
@@ -3155,6 +4256,33 @@ async function addPreview(description, options, box) {
|
|
|
3155
4256
|
text: colors.source,
|
|
3156
4257
|
});
|
|
3157
4258
|
}
|
|
4259
|
+
// A default page named on its own adds exactly that page, when it is
|
|
4260
|
+
// not there yet.
|
|
4261
|
+
if (askedId != null) {
|
|
4262
|
+
const there = data.pages.find(p => p.id === askedId);
|
|
4263
|
+
if (there && fs.existsSync(path.join(at.dir, `${there.file}.tsx`)))
|
|
4264
|
+
refuse(
|
|
4265
|
+
E.ERR_FILE_EXISTS,
|
|
4266
|
+
`The ${askedId} page is already in the preview; it is kept up to date as the theme changes.`,
|
|
4267
|
+
{run: [`${PREVIEW} list${pf}`]},
|
|
4268
|
+
);
|
|
4269
|
+
if (!add.some(a => a.id === askedId))
|
|
4270
|
+
refuse(
|
|
4271
|
+
E.ERR_NOT_FOUND,
|
|
4272
|
+
askedId === COLORS_PAGE.id
|
|
4273
|
+
? `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"}.`
|
|
4274
|
+
: `The ${askedId} page cannot be added here.`,
|
|
4275
|
+
{
|
|
4276
|
+
run:
|
|
4277
|
+
askedId === COLORS_PAGE.id && data.theme
|
|
4278
|
+
? [
|
|
4279
|
+
`${EDITOR} set ${data.theme} accent "#7c3aed" # then add it again`,
|
|
4280
|
+
]
|
|
4281
|
+
: [`${PREVIEW} list${pf}`],
|
|
4282
|
+
},
|
|
4283
|
+
);
|
|
4284
|
+
add.splice(0, add.length, ...add.filter(a => a.id === askedId));
|
|
4285
|
+
}
|
|
3158
4286
|
} else {
|
|
3159
4287
|
const flags = `${tf}${pf}`;
|
|
3160
4288
|
const picks = explicit
|
|
@@ -3167,7 +4295,9 @@ async function addPreview(description, options, box) {
|
|
|
3167
4295
|
await template(pick.name, {cwd, type: 'page'})
|
|
3168
4296
|
);
|
|
3169
4297
|
const source = shownTemplate.data.source;
|
|
3170
|
-
|
|
4298
|
+
// Packages are the app's: the nearest package.json over the preview
|
|
4299
|
+
// (in a workspace, the app's own, not the root's).
|
|
4300
|
+
const needs = pageNeeds(appRootOf(cwd, at.dir), source);
|
|
3171
4301
|
// A StyleX page calls stylex.create when its module loads, so without
|
|
3172
4302
|
// a StyleX compiler it breaks the whole route.
|
|
3173
4303
|
const needsCompiler = needs.stylexCompiler;
|
|
@@ -3229,6 +4359,7 @@ async function addPreview(description, options, box) {
|
|
|
3229
4359
|
})();
|
|
3230
4360
|
const elsewhere = retry(free);
|
|
3231
4361
|
const move = movedPages(cwd, at.dir, data);
|
|
4362
|
+
if (gone) dropGoneColors(cwd, at.dir, data, move, gone, notes);
|
|
3232
4363
|
/** @type {Map<string, {rel: string, text: string, keepHash?: boolean}>} */
|
|
3233
4364
|
const writes = new Map(move.writes.map(w => [w.rel, w]));
|
|
3234
4365
|
/** @type {string[]} */
|
|
@@ -3243,7 +4374,13 @@ async function addPreview(description, options, box) {
|
|
|
3243
4374
|
writes.set(rel, w);
|
|
3244
4375
|
if (!data.files.includes(rel)) data.files.push(rel);
|
|
3245
4376
|
const had = data.pages.find(x => x.id === page.id);
|
|
3246
|
-
|
|
4377
|
+
// A recorded page whose file is gone is added back, not refreshed.
|
|
4378
|
+
if (!had || !fs.existsSync(path.join(at.dir, rel))) {
|
|
4379
|
+
if (had) {
|
|
4380
|
+
had.label = page.label;
|
|
4381
|
+
added.push(page.id);
|
|
4382
|
+
continue;
|
|
4383
|
+
}
|
|
3247
4384
|
data.pages.push({
|
|
3248
4385
|
id: page.id,
|
|
3249
4386
|
label: page.label,
|
|
@@ -3305,6 +4442,19 @@ async function addPreview(description, options, box) {
|
|
|
3305
4442
|
writes.set(w.rel, w);
|
|
3306
4443
|
if (!data.files.includes(w.rel)) data.files.push(w.rel);
|
|
3307
4444
|
}
|
|
4445
|
+
// Every recorded page this command writes again is named, the Colors page
|
|
4446
|
+
// the route writes included.
|
|
4447
|
+
for (const p of data.pages)
|
|
4448
|
+
if (
|
|
4449
|
+
writes.has(`${p.file}.tsx`) &&
|
|
4450
|
+
!added.includes(p.id) &&
|
|
4451
|
+
!refreshed.includes(p.id) &&
|
|
4452
|
+
!rewrote.includes(p.id) &&
|
|
4453
|
+
fs.existsSync(path.join(at.dir, `${p.file}.tsx`)) &&
|
|
4454
|
+
fs.readFileSync(path.join(at.dir, `${p.file}.tsx`), 'utf8') !==
|
|
4455
|
+
writes.get(`${p.file}.tsx`)?.text
|
|
4456
|
+
)
|
|
4457
|
+
refreshed.push(p.id);
|
|
3308
4458
|
await buildWorn(cwd, theme, options, notes);
|
|
3309
4459
|
commitPreview(
|
|
3310
4460
|
cwd,
|
|
@@ -3328,6 +4478,33 @@ async function addPreview(description, options, box) {
|
|
|
3328
4478
|
const worn = data.theme;
|
|
3329
4479
|
if (worn)
|
|
3330
4480
|
await themeLocked(options, worn, () => recordPreview(cwd, worn, at.dir));
|
|
4481
|
+
// A font the theme names whose package the app lacks shows as the next
|
|
4482
|
+
// font in the stack.
|
|
4483
|
+
if (data.theme && fs.existsSync(themeFiles(cwd, data.theme).dir))
|
|
4484
|
+
try {
|
|
4485
|
+
const ctx = await context(cwd);
|
|
4486
|
+
for (const f of fontPackages(ctx, loadTheme(cwd, data.theme)))
|
|
4487
|
+
if (!f.installed)
|
|
4488
|
+
notes.push(
|
|
4489
|
+
`${f.family} is not installed, so the preview shows the next font in the stack in its place; install it: ${installLine(cwd, [f.pkg])}`,
|
|
4490
|
+
);
|
|
4491
|
+
} catch {
|
|
4492
|
+
// A theme that does not load is reported where it is used.
|
|
4493
|
+
}
|
|
4494
|
+
if (again)
|
|
4495
|
+
notes.push(
|
|
4496
|
+
`a default page's id brings back the default pages that are missing, as ${PREVIEW} add does`,
|
|
4497
|
+
);
|
|
4498
|
+
notes.push(...typeScriptNotes(cwd, at.dir, added));
|
|
4499
|
+
if (
|
|
4500
|
+
askedId === COLORS_PAGE.id &&
|
|
4501
|
+
!data.pages.some(p => p.id === COLORS_PAGE.id)
|
|
4502
|
+
)
|
|
4503
|
+
notes.push(
|
|
4504
|
+
data.theme
|
|
4505
|
+
? `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>`
|
|
4506
|
+
: `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>`,
|
|
4507
|
+
);
|
|
3331
4508
|
return {
|
|
3332
4509
|
type: 'themeEdit.previewAdd',
|
|
3333
4510
|
data: {
|
|
@@ -3341,13 +4518,20 @@ async function addPreview(description, options, box) {
|
|
|
3341
4518
|
pages: data.pages.map(x => x.id),
|
|
3342
4519
|
files: data.files.map(f => shown(cwd, path.join(at.dir, f))),
|
|
3343
4520
|
open: openLine(cwd, at),
|
|
4521
|
+
// Each page's own URL, so any page can be opened or pictured.
|
|
4522
|
+
urls: pageUrls(
|
|
4523
|
+
cwd,
|
|
4524
|
+
at,
|
|
4525
|
+
data.pages.map(x => x.id),
|
|
4526
|
+
),
|
|
3344
4527
|
},
|
|
3345
4528
|
};
|
|
3346
4529
|
}
|
|
3347
4530
|
|
|
3348
4531
|
/**
|
|
3349
4532
|
* 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
|
|
4533
|
+
* with the Astryx packages pinned to this CLI's release, or with `theme` to
|
|
4534
|
+
* the versions the project has) in `dir`, with the
|
|
3351
4535
|
* theme preview route and its default pages inside. With `theme`, the theme
|
|
3352
4536
|
* folder is copied into the app and the pages wear it. No install runs.
|
|
3353
4537
|
* Every check runs before the first write, so a refusal changes nothing.
|
|
@@ -3364,6 +4548,7 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
3364
4548
|
'Name the folder for the new app: a new or empty folder inside the current one.',
|
|
3365
4549
|
{run: [`${PREVIEW} app theme-app`]},
|
|
3366
4550
|
);
|
|
4551
|
+
options = {...options, theme: themeArg(options.theme)};
|
|
3367
4552
|
const slug = options.theme ?? null;
|
|
3368
4553
|
const theme =
|
|
3369
4554
|
slug == null
|
|
@@ -3434,7 +4619,12 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
3434
4619
|
})) {
|
|
3435
4620
|
const sub = path.join(rel, entry.name);
|
|
3436
4621
|
if (entry.isDirectory()) copyDir(sub);
|
|
3437
|
-
else if (
|
|
4622
|
+
else if (
|
|
4623
|
+
entry.isFile() &&
|
|
4624
|
+
!(rel === '' && skip.has(entry.name)) &&
|
|
4625
|
+
// A temp file a stopped command left is no part of the theme.
|
|
4626
|
+
!isEditorTemp(entry.name)
|
|
4627
|
+
)
|
|
3438
4628
|
themeCopy.push({
|
|
3439
4629
|
rel: path.join(to.dir, sub),
|
|
3440
4630
|
text: fs.readFileSync(path.join(from.dir, sub)),
|
|
@@ -3444,6 +4634,27 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
3444
4634
|
copyDir('');
|
|
3445
4635
|
if (theme.like && theme.like !== 'neutral')
|
|
3446
4636
|
dependencies[`@astryxdesign/theme-${theme.like}`] = cliVersion();
|
|
4637
|
+
// The copied build was made on this project's Core and base theme: the
|
|
4638
|
+
// new app installs those same versions, so the build is current there.
|
|
4639
|
+
/** @type {string[]} */
|
|
4640
|
+
const pinnedTo = [];
|
|
4641
|
+
for (const pkg of [
|
|
4642
|
+
'@astryxdesign/core',
|
|
4643
|
+
'@astryxdesign/theme-neutral',
|
|
4644
|
+
...(theme.like && theme.like !== 'neutral'
|
|
4645
|
+
? [`@astryxdesign/theme-${theme.like}`]
|
|
4646
|
+
: []),
|
|
4647
|
+
]) {
|
|
4648
|
+
const v = installedVersion(cwd, pkg);
|
|
4649
|
+
if (v && v !== cliVersion()) {
|
|
4650
|
+
dependencies[pkg] = v;
|
|
4651
|
+
pinnedTo.push(`${pkg} ${v}`);
|
|
4652
|
+
}
|
|
4653
|
+
}
|
|
4654
|
+
if (pinnedTo.length)
|
|
4655
|
+
notes.push(
|
|
4656
|
+
`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`,
|
|
4657
|
+
);
|
|
3447
4658
|
/** @param {string} rel */
|
|
3448
4659
|
const fromRoute = rel => {
|
|
3449
4660
|
const r = path.relative(routeDir, rel).split(path.sep).join('/');
|
|
@@ -3458,7 +4669,11 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
3458
4669
|
routeTheme = {
|
|
3459
4670
|
name: slug,
|
|
3460
4671
|
module: fromRoute(to.js),
|
|
3461
|
-
css: [
|
|
4672
|
+
css: [
|
|
4673
|
+
fromRoute(to.css),
|
|
4674
|
+
...baseFontsCss(ctx, theme),
|
|
4675
|
+
...fonts.map(x => `${x.pkg}/index.css`),
|
|
4676
|
+
],
|
|
3462
4677
|
mode: themeImports(ctx, slug, theme, path.join(cwd, routeDir)).mode,
|
|
3463
4678
|
};
|
|
3464
4679
|
// The app's theme folder sits where this project's does, so the Colors
|
|
@@ -3472,7 +4687,14 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
3472
4687
|
const preview = [];
|
|
3473
4688
|
/** @type {Array<[string, string, string]>} */
|
|
3474
4689
|
const pages = [
|
|
3475
|
-
[
|
|
4690
|
+
[
|
|
4691
|
+
'overview',
|
|
4692
|
+
'Overview',
|
|
4693
|
+
overviewPageSource(project, {
|
|
4694
|
+
theme: slug,
|
|
4695
|
+
colors: colorsSource != null,
|
|
4696
|
+
}),
|
|
4697
|
+
],
|
|
3476
4698
|
[
|
|
3477
4699
|
'components',
|
|
3478
4700
|
'Components',
|
|
@@ -3537,6 +4759,9 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
3537
4759
|
folder,
|
|
3538
4760
|
theme: slug,
|
|
3539
4761
|
pages: data.pages.map(x => x.id),
|
|
4762
|
+
urls: Object.fromEntries(
|
|
4763
|
+
data.pages.map(x => [x.id, `/theme-preview?page=${x.id}`]),
|
|
4764
|
+
),
|
|
3540
4765
|
files,
|
|
3541
4766
|
notes,
|
|
3542
4767
|
// A folder named like a flag (`-`, `-k`) is a path to cd only with ./.
|
|
@@ -3548,6 +4773,26 @@ export async function themeEditPreviewApp(dir, options = {}) {
|
|
|
3548
4773
|
/** The version range of an open font package the host project lacks. */
|
|
3549
4774
|
const OPEN_FONT_RANGE = '^5.0.0';
|
|
3550
4775
|
|
|
4776
|
+
/**
|
|
4777
|
+
* The version of a package installed for the project, read off disk (a
|
|
4778
|
+
* package need not export its package.json).
|
|
4779
|
+
* @param {string} cwd
|
|
4780
|
+
* @param {string} pkg
|
|
4781
|
+
* @returns {string | null}
|
|
4782
|
+
*/
|
|
4783
|
+
function installedVersion(cwd, pkg) {
|
|
4784
|
+
const found = findNodeModulesPackage(cwd, pkg);
|
|
4785
|
+
if (!found) return null;
|
|
4786
|
+
try {
|
|
4787
|
+
const v = JSON.parse(
|
|
4788
|
+
fs.readFileSync(path.join(found.packageDir, 'package.json'), 'utf8'),
|
|
4789
|
+
).version;
|
|
4790
|
+
return typeof v === 'string' ? v : null;
|
|
4791
|
+
} catch {
|
|
4792
|
+
return null;
|
|
4793
|
+
}
|
|
4794
|
+
}
|
|
4795
|
+
|
|
3551
4796
|
/**
|
|
3552
4797
|
* The installed version of a package, as the project resolves it.
|
|
3553
4798
|
* @param {import('./_project.mjs').ThemeProject} project
|
|
@@ -3611,6 +4856,55 @@ export async function themeEditPreviewList(options = {}) {
|
|
|
3611
4856
|
const themeMissing = Boolean(
|
|
3612
4857
|
data?.theme && !fs.existsSync(themeFiles(cwd, data.theme).dir),
|
|
3613
4858
|
);
|
|
4859
|
+
if (at && fs.existsSync(path.join(at.dir, PENDING)))
|
|
4860
|
+
notes.push(
|
|
4861
|
+
`a preview change was stopped part way, so this list may be out of date; ${PREVIEW} add${pf} finishes it`,
|
|
4862
|
+
);
|
|
4863
|
+
// A page whose file is gone breaks the route, which still imports it.
|
|
4864
|
+
const missing = new Set(
|
|
4865
|
+
(data?.pages ?? [])
|
|
4866
|
+
.filter(p => at && !fs.existsSync(path.join(at.dir, `${p.file}.tsx`)))
|
|
4867
|
+
.map(p => p.id),
|
|
4868
|
+
);
|
|
4869
|
+
if (at && missing.size)
|
|
4870
|
+
notes.push(
|
|
4871
|
+
`${[...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`,
|
|
4872
|
+
);
|
|
4873
|
+
// No preview here, but one elsewhere in the routes folders (a folder
|
|
4874
|
+
// renamed by hand, as remove finds it): name it, with its list.
|
|
4875
|
+
const elsewhere = !data
|
|
4876
|
+
? previewsIn(cwd).filter(d => !at || d !== at.dir)
|
|
4877
|
+
: [];
|
|
4878
|
+
if (elsewhere.length)
|
|
4879
|
+
notes.push(
|
|
4880
|
+
`no preview here; ${elsewhere.length > 1 ? 'the previews are in' : 'the preview is in'} ${elsewhere
|
|
4881
|
+
.slice(0, 3)
|
|
4882
|
+
.map(d => `${shownDir(cwd, d)}/`)
|
|
4883
|
+
.join(', ')}: ${elsewhere
|
|
4884
|
+
.slice(0, 3)
|
|
4885
|
+
.map(
|
|
4886
|
+
d =>
|
|
4887
|
+
`${PREVIEW} list --path ${quoteCommandArgument(shownDir(cwd, d))}`,
|
|
4888
|
+
)
|
|
4889
|
+
.join('; ')}`,
|
|
4890
|
+
);
|
|
4891
|
+
// No preview yet: say so when `add` would refuse the folder, with its
|
|
4892
|
+
// reason and the way on, rather than suggest it.
|
|
4893
|
+
if (!data && !elsewhere.length) {
|
|
4894
|
+
try {
|
|
4895
|
+
previewDir(cwd, options.path, {
|
|
4896
|
+
write: true,
|
|
4897
|
+
retry: p =>
|
|
4898
|
+
`${PREVIEW} add${p == null ? '' : ` --path ${quoteCommandArgument(p)}`}`,
|
|
4899
|
+
});
|
|
4900
|
+
} catch (e) {
|
|
4901
|
+
if (typeof (/** @type {any} */ (e)?.code) !== 'string') throw e;
|
|
4902
|
+
const way = /** @type {any} */ (e).suggestions?.[0]?.name;
|
|
4903
|
+
notes.push(
|
|
4904
|
+
`${PREVIEW} add${pf} refuses this folder: ${String(/** @type {any} */ (e).message).replace(/ Nothing was (written|changed)\.$/, '')}${way ? ` Instead: ${way}` : ''}`,
|
|
4905
|
+
);
|
|
4906
|
+
}
|
|
4907
|
+
}
|
|
3614
4908
|
if (data?.theme && themeMissing) {
|
|
3615
4909
|
const themes = editedThemes(cwd);
|
|
3616
4910
|
notes.push(
|
|
@@ -3623,7 +4917,16 @@ export async function themeEditPreviewList(options = {}) {
|
|
|
3623
4917
|
folder: at && data ? shownDir(cwd, at.dir) : null,
|
|
3624
4918
|
theme: data?.theme ?? null,
|
|
3625
4919
|
themeMissing,
|
|
3626
|
-
pages: (data?.pages ?? []).map(x =>
|
|
4920
|
+
pages: (data?.pages ?? []).map(x => {
|
|
4921
|
+
const url = at && data ? pageUrls(cwd, at, [x.id])[x.id] : undefined;
|
|
4922
|
+
return {
|
|
4923
|
+
id: x.id,
|
|
4924
|
+
label: x.label,
|
|
4925
|
+
// The page's own URL in the running app, when one can be told.
|
|
4926
|
+
...(url ? {url} : {}),
|
|
4927
|
+
...(missing.has(x.id) ? {missing: true} : {}),
|
|
4928
|
+
};
|
|
4929
|
+
}),
|
|
3627
4930
|
notes,
|
|
3628
4931
|
},
|
|
3629
4932
|
};
|
|
@@ -3643,6 +4946,39 @@ export async function themeEditPreviewRemove(page, options = {}) {
|
|
|
3643
4946
|
return reportingRecovery(box => removePreview(page, options, box));
|
|
3644
4947
|
}
|
|
3645
4948
|
|
|
4949
|
+
/**
|
|
4950
|
+
* The preview folders in the app: every folder under the routes folders
|
|
4951
|
+
* (app/, src/app/) holding a theme-preview.json, a few levels deep.
|
|
4952
|
+
* @param {string} cwd
|
|
4953
|
+
* @returns {string[]}
|
|
4954
|
+
*/
|
|
4955
|
+
function previewsIn(cwd) {
|
|
4956
|
+
/** @type {string[]} */
|
|
4957
|
+
const out = [];
|
|
4958
|
+
/** @param {string} dir @param {number} depth */
|
|
4959
|
+
const walk = (dir, depth) => {
|
|
4960
|
+
if (depth > 6 || out.length >= 20) return;
|
|
4961
|
+
if (fs.existsSync(path.join(dir, PREVIEW_DATA))) out.push(dir);
|
|
4962
|
+
/** @type {fs.Dirent[]} */
|
|
4963
|
+
let entries;
|
|
4964
|
+
try {
|
|
4965
|
+
entries = fs.readdirSync(dir, {withFileTypes: true});
|
|
4966
|
+
} catch {
|
|
4967
|
+
return;
|
|
4968
|
+
}
|
|
4969
|
+
for (const e of entries)
|
|
4970
|
+
if (
|
|
4971
|
+
e.isDirectory() &&
|
|
4972
|
+
e.name !== 'node_modules' &&
|
|
4973
|
+
!e.name.startsWith('.')
|
|
4974
|
+
)
|
|
4975
|
+
walk(path.join(dir, e.name), depth + 1);
|
|
4976
|
+
};
|
|
4977
|
+
for (const r of [path.join(cwd, 'app'), path.join(cwd, 'src', 'app')])
|
|
4978
|
+
if (fs.existsSync(r)) walk(r, 0);
|
|
4979
|
+
return out.sort();
|
|
4980
|
+
}
|
|
4981
|
+
|
|
3646
4982
|
/**
|
|
3647
4983
|
* {@link themeEditPreviewRemove}, with the note of a recovery in `box`.
|
|
3648
4984
|
* @param {string | undefined} page
|
|
@@ -3658,32 +4994,66 @@ async function removePreview(page, options, box) {
|
|
|
3658
4994
|
const retry = (/** @type {string | null} */ p) =>
|
|
3659
4995
|
`${PREVIEW} remove${page != null ? ` ${quoteCommandArgument(page)}` : ''}${p == null ? '' : ` --path ${quoteCommandArgument(p)}`}`;
|
|
3660
4996
|
const at = previewDir(cwd, options.path, {retry});
|
|
4997
|
+
// The pages a stopped change was removing, read before it is finished.
|
|
4998
|
+
/** @type {string[]} */
|
|
4999
|
+
let removing = [];
|
|
5000
|
+
try {
|
|
5001
|
+
const pend = JSON.parse(
|
|
5002
|
+
fs.readFileSync(path.join(at.dir, PENDING), 'utf8'),
|
|
5003
|
+
);
|
|
5004
|
+
const ids = (/** @type {any} */ r) =>
|
|
5005
|
+
Array.isArray(r?.pages)
|
|
5006
|
+
? r.pages.map((/** @type {any} */ x) => String(x?.id))
|
|
5007
|
+
: [];
|
|
5008
|
+
const after = ids(pend?.record);
|
|
5009
|
+
removing = ids(pend?.old).filter(
|
|
5010
|
+
(/** @type {string} */ id) => !after.includes(id),
|
|
5011
|
+
);
|
|
5012
|
+
} catch {
|
|
5013
|
+
// No stopped change, or a damaged one.
|
|
5014
|
+
}
|
|
3661
5015
|
const recovered = recoverPreview(cwd, at.dir);
|
|
3662
5016
|
box.note = recovered;
|
|
3663
|
-
|
|
5017
|
+
/** @type {string[]} */
|
|
5018
|
+
const dropped = [];
|
|
5019
|
+
const data = readPreview(cwd, at.dir, pf, {dropped});
|
|
3664
5020
|
if (!data) {
|
|
3665
5021
|
// A folder that holds the app takes no preview: the way on is the
|
|
3666
5022
|
// preview elsewhere, or a new one in its own folder.
|
|
3667
|
-
|
|
3668
|
-
|
|
3669
|
-
|
|
3670
|
-
|
|
3671
|
-
|
|
3672
|
-
|
|
3673
|
-
|
|
3674
|
-
|
|
3675
|
-
|
|
3676
|
-
|
|
5023
|
+
// The previews there are, each with its remove; `add` there only
|
|
5024
|
+
// when add would take the folder.
|
|
5025
|
+
const others = previewsIn(cwd).filter(d => d !== at.dir);
|
|
5026
|
+
/** @type {string | undefined} */
|
|
5027
|
+
let usual;
|
|
5028
|
+
try {
|
|
5029
|
+
usual = previewDir(cwd, undefined, {optional: true, retry})?.dir;
|
|
5030
|
+
} catch {
|
|
5031
|
+
// Several previews, or none.
|
|
5032
|
+
}
|
|
5033
|
+
const pageArg = page != null ? ` ${quoteCommandArgument(page)}` : '';
|
|
5034
|
+
let addable = true;
|
|
5035
|
+
try {
|
|
5036
|
+
previewDir(cwd, options.path, {write: true, retry});
|
|
5037
|
+
} catch {
|
|
5038
|
+
addable = false;
|
|
5039
|
+
}
|
|
3677
5040
|
refuse(
|
|
3678
5041
|
E.ERR_NOT_FOUND,
|
|
3679
|
-
`There is no theme preview in ${shownDir(cwd, at.dir)}/ (no ${PREVIEW_DATA})
|
|
5042
|
+
`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
5043
|
{
|
|
3681
5044
|
run: [
|
|
3682
|
-
|
|
3683
|
-
|
|
3684
|
-
|
|
3685
|
-
|
|
3686
|
-
|
|
5045
|
+
...others.slice(0, 3).map(
|
|
5046
|
+
d =>
|
|
5047
|
+
// The usual preview folder needs no --path.
|
|
5048
|
+
`${PREVIEW} remove${pageArg}${d === usual ? '' : ` --path ${quoteCommandArgument(shownDir(cwd, d))}`}`,
|
|
5049
|
+
),
|
|
5050
|
+
...(others.length
|
|
5051
|
+
? []
|
|
5052
|
+
: [
|
|
5053
|
+
addable && !holdsApp(cwd, at.dir)
|
|
5054
|
+
? `${PREVIEW} add${pf}`
|
|
5055
|
+
: `${PREVIEW} add`,
|
|
5056
|
+
]),
|
|
3687
5057
|
],
|
|
3688
5058
|
},
|
|
3689
5059
|
);
|
|
@@ -3692,11 +5062,15 @@ async function removePreview(page, options, box) {
|
|
|
3692
5062
|
// theme-preview.json is a file anyone can edit: every path in it must
|
|
3693
5063
|
// stay in the route folder before anything is removed.
|
|
3694
5064
|
const inside = (/** @type {string} */ rel) =>
|
|
3695
|
-
confined(path.join(at.dir, rel), at.dir,
|
|
5065
|
+
confined(path.join(at.dir, rel), at.dir, PREVIEW_DATA, {cwd, pf});
|
|
3696
5066
|
for (const rel of data.files) inside(rel);
|
|
3697
5067
|
for (const p of data.pages) inside(`${p.file}.tsx`);
|
|
3698
5068
|
/** @type {string[]} */
|
|
3699
5069
|
const notes = recovered ? [recovered] : [];
|
|
5070
|
+
if (dropped.length)
|
|
5071
|
+
notes.push(
|
|
5072
|
+
`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`,
|
|
5073
|
+
);
|
|
3700
5074
|
/** @type {string[]} */
|
|
3701
5075
|
const kept = [];
|
|
3702
5076
|
/** @type {string[]} */
|
|
@@ -3709,6 +5083,28 @@ async function removePreview(page, options, box) {
|
|
|
3709
5083
|
one =
|
|
3710
5084
|
data.pages.find(x => x.id === key) ??
|
|
3711
5085
|
data.pages.find(x => x.id.toLowerCase() === key.toLowerCase());
|
|
5086
|
+
// The same remove run again after it was stopped: the change just
|
|
5087
|
+
// finished removed the page.
|
|
5088
|
+
if (
|
|
5089
|
+
!one &&
|
|
5090
|
+
recovered &&
|
|
5091
|
+
removing.some(id => id.toLowerCase() === key.toLowerCase())
|
|
5092
|
+
)
|
|
5093
|
+
return {
|
|
5094
|
+
type: 'themeEdit.previewRemove',
|
|
5095
|
+
data: {
|
|
5096
|
+
folder,
|
|
5097
|
+
removed: [],
|
|
5098
|
+
kept: [],
|
|
5099
|
+
missing: [],
|
|
5100
|
+
rewritten: [],
|
|
5101
|
+
notes: [
|
|
5102
|
+
...notes,
|
|
5103
|
+
`${key} is no longer in the preview: the change that was stopped removed it`,
|
|
5104
|
+
],
|
|
5105
|
+
pages: data.pages.map(x => x.id),
|
|
5106
|
+
},
|
|
5107
|
+
};
|
|
3712
5108
|
if (!one)
|
|
3713
5109
|
refuse(E.ERR_NOT_FOUND, `The preview has no page "${page}".`, {
|
|
3714
5110
|
didYouMean: nearest(
|
|
@@ -3719,6 +5115,40 @@ async function removePreview(page, options, box) {
|
|
|
3719
5115
|
});
|
|
3720
5116
|
}
|
|
3721
5117
|
|
|
5118
|
+
// A file of the app's own that imports a page about to go would break:
|
|
5119
|
+
// the page stays, and the file is named. One guard for a page and for the
|
|
5120
|
+
// whole preview.
|
|
5121
|
+
{
|
|
5122
|
+
const going = one && data.pages.length > 1 ? [one] : data.pages;
|
|
5123
|
+
// The whole preview takes its route and layout too, which other files
|
|
5124
|
+
// may import (a re-export at another URL).
|
|
5125
|
+
const targets = [
|
|
5126
|
+
...going.map(p => path.join(at.dir, p.file)),
|
|
5127
|
+
...(going === data.pages
|
|
5128
|
+
? data.files
|
|
5129
|
+
.filter(f => /^(page|layout)\.(tsx|ts|jsx|js)$/.test(f))
|
|
5130
|
+
.map(f => path.join(at.dir, f.replace(/\.[a-z]+$/, '')))
|
|
5131
|
+
: []),
|
|
5132
|
+
];
|
|
5133
|
+
const {importers, tooBig} = pageImporters(cwd, data, at.dir, targets);
|
|
5134
|
+
if (tooBig)
|
|
5135
|
+
refuse(
|
|
5136
|
+
E.ERR_INVALID_ARGUMENT,
|
|
5137
|
+
`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.`,
|
|
5138
|
+
{
|
|
5139
|
+
fix: `delete ${folder}/ by hand once nothing imports from it`,
|
|
5140
|
+
},
|
|
5141
|
+
);
|
|
5142
|
+
if (importers.length)
|
|
5143
|
+
refuse(
|
|
5144
|
+
E.ERR_FILE_EXISTS,
|
|
5145
|
+
`${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.`,
|
|
5146
|
+
{
|
|
5147
|
+
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`,
|
|
5148
|
+
},
|
|
5149
|
+
);
|
|
5150
|
+
}
|
|
5151
|
+
|
|
3722
5152
|
if (one && data.pages.length > 1) {
|
|
3723
5153
|
const target = one;
|
|
3724
5154
|
const rel = `${target.file}.tsx`;
|
|
@@ -3733,12 +5163,19 @@ async function removePreview(page, options, box) {
|
|
|
3733
5163
|
`${name} was not written by the preview, so it is kept; only its nav item is gone`,
|
|
3734
5164
|
);
|
|
3735
5165
|
} else if (!fs.existsSync(inside(rel))) missing.push(name);
|
|
3736
|
-
else if (
|
|
5166
|
+
else if (unreadable(inside(rel))) {
|
|
5167
|
+
kept.push(name);
|
|
5168
|
+
notes.push(
|
|
5169
|
+
`${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`,
|
|
5170
|
+
);
|
|
5171
|
+
} else if (verdict !== 'delete') {
|
|
3737
5172
|
kept.push(name);
|
|
3738
5173
|
notes.push(keptNote(name, verdict));
|
|
3739
5174
|
} else deletes.push(rel);
|
|
3740
5175
|
data.pages = data.pages.filter(x => x !== target);
|
|
3741
|
-
|
|
5176
|
+
// A file left in place because it could not be read stays recorded.
|
|
5177
|
+
if (!unreadable(inside(rel)))
|
|
5178
|
+
data.files = data.files.filter(f => f !== rel);
|
|
3742
5179
|
const wore = data.theme;
|
|
3743
5180
|
/** @type {string | null} */
|
|
3744
5181
|
let gone = null;
|
|
@@ -3750,6 +5187,7 @@ async function removePreview(page, options, box) {
|
|
|
3750
5187
|
);
|
|
3751
5188
|
}
|
|
3752
5189
|
const move = movedPages(cwd, at.dir, data);
|
|
5190
|
+
if (gone) dropGoneColors(cwd, at.dir, data, move, gone, notes);
|
|
3753
5191
|
// A page whose file is gone leaves the nav, so the route never imports
|
|
3754
5192
|
// a missing file.
|
|
3755
5193
|
const moving = new Set(move.writes.map(w => w.rel));
|
|
@@ -3844,7 +5282,7 @@ async function removePreview(page, options, box) {
|
|
|
3844
5282
|
E.ERR_FILE_EXISTS,
|
|
3845
5283
|
`${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
5284
|
{
|
|
3847
|
-
fix: `
|
|
5285
|
+
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
5286
|
},
|
|
3849
5287
|
);
|
|
3850
5288
|
const ids = new Set(data.pages.map(p => p.id));
|
|
@@ -3885,6 +5323,14 @@ async function removePreview(page, options, box) {
|
|
|
3885
5323
|
);
|
|
3886
5324
|
continue;
|
|
3887
5325
|
}
|
|
5326
|
+
const cannot = unreadable(file);
|
|
5327
|
+
if (cannot) {
|
|
5328
|
+
kept.push(shown(cwd, file));
|
|
5329
|
+
notes.push(
|
|
5330
|
+
`${shown(cwd, file)} could not be read (${cannot}), so it is left in place; fix its permissions, then run ${PREVIEW} remove${pf} again`,
|
|
5331
|
+
);
|
|
5332
|
+
continue;
|
|
5333
|
+
}
|
|
3888
5334
|
plan.push({file, before: fs.readFileSync(file)});
|
|
3889
5335
|
}
|
|
3890
5336
|
// theme-preview.json goes last, so an interrupted remove can run again.
|
|
@@ -3912,6 +5358,7 @@ async function removePreview(page, options, box) {
|
|
|
3912
5358
|
{run: [`${retry(options.path ?? null)} # once the cause is fixed`]},
|
|
3913
5359
|
);
|
|
3914
5360
|
}
|
|
5361
|
+
sweepTemps(at.dir);
|
|
3915
5362
|
tidyPageFolders(at.dir);
|
|
3916
5363
|
// Files the record does not list stay: the preview never deletes what it
|
|
3917
5364
|
// has no record of writing. Each is named, so the folder left is no
|
|
@@ -3926,11 +5373,15 @@ async function removePreview(page, options, box) {
|
|
|
3926
5373
|
}
|
|
3927
5374
|
try {
|
|
3928
5375
|
fs.rmdirSync(at.dir);
|
|
3929
|
-
//
|
|
5376
|
+
// The folders the first add made over it, and route group folders,
|
|
5377
|
+
// such as (tools)/, that only held the preview; never one with
|
|
5378
|
+
// anything else in it.
|
|
5379
|
+
let made = data.parents ?? 0;
|
|
3930
5380
|
for (
|
|
3931
5381
|
let up = path.dirname(at.dir);
|
|
3932
|
-
/^\(.+\)$/.test(path.basename(up)) &&
|
|
3933
|
-
up
|
|
5382
|
+
(made > 0 || (at.app != null && /^\(.+\)$/.test(path.basename(up)))) &&
|
|
5383
|
+
up.startsWith((at.app ?? cwd) + path.sep);
|
|
5384
|
+
up = path.dirname(up), made--
|
|
3934
5385
|
)
|
|
3935
5386
|
fs.rmdirSync(up);
|
|
3936
5387
|
} catch {
|