@vizejs/vite-plugin-musea 0.782.0 → 0.784.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -9
- package/client.d.ts +79 -2
- package/dist/a11y/index.d.mts +1 -1
- package/dist/a11y/index.mjs +2 -2
- package/dist/{report-identity-dHHBq0Ne.mjs → art-processing-BQ7OYXsq.mjs} +352 -985
- package/dist/art-processing-BQ7OYXsq.mjs.map +1 -0
- package/dist/autogen/index.mjs +1 -1
- package/dist/{autogen-D9K1av3V.mjs → autogen-DQVOrkzB.mjs} +8 -3
- package/dist/autogen-DQVOrkzB.mjs.map +1 -0
- package/dist/cli/index.d.mts +2 -2
- package/dist/cli/index.mjs +52 -7
- package/dist/cli/index.mjs.map +1 -1
- package/dist/gallery/assets/{MonacoEditor-TAa2JDXq.js → MonacoEditor-0o-tEf_d.js} +2 -2
- package/dist/gallery/assets/{cssMode-HbYMEqla.js → cssMode-CTeGZPsJ.js} +1 -1
- package/dist/gallery/assets/{editor.api-BnAmILmn.js → editor.api-By71ZNNJ.js} +1 -1
- package/dist/gallery/assets/{freemarker2-CUxx3K6C.js → freemarker2-PodIPOLZ.js} +1 -1
- package/dist/gallery/assets/{handlebars-D-tFxs03.js → handlebars-CxH85JIs.js} +1 -1
- package/dist/gallery/assets/{html-BvCZbWop.js → html-CLgd1G74.js} +1 -1
- package/dist/gallery/assets/{htmlMode-CtpLmhU0.js → htmlMode-CSxazmZ_.js} +1 -1
- package/dist/gallery/assets/{index-9Jbnkznu.js → index-CrdkHjj1.js} +28 -28
- package/dist/gallery/assets/index-DjzvBel3.css +1 -0
- package/dist/gallery/assets/{javascript-5Q8XUioJ.js → javascript-nMYU-ClU.js} +1 -1
- package/dist/gallery/assets/{jsonMode-meopLbOo.js → jsonMode-CR3wuGKP.js} +1 -1
- package/dist/gallery/assets/{liquid-XBHPlM5o.js → liquid-D0UrqjBJ.js} +1 -1
- package/dist/gallery/assets/{lspLanguageFeatures-uQPmNYRO.js → lspLanguageFeatures-Dd1uggns.js} +1 -1
- package/dist/gallery/assets/{mdx-DPlyNjoV.js → mdx-BL8lD8I3.js} +1 -1
- package/dist/gallery/assets/{python-BJGVh1ia.js → python-Ctx797mC.js} +1 -1
- package/dist/gallery/assets/{razor-tl_w-IGC.js → razor-Ba3xTj_L.js} +1 -1
- package/dist/gallery/assets/{register-B1Z_0Wj1.js → register-DS0poJDf.js} +2 -2
- package/dist/gallery/assets/{toggleHighContrast-CwaJX3-m.js → toggleHighContrast-Dd6XSMyb.js} +1 -1
- package/dist/gallery/assets/{tsMode-BTkaxugD.js → tsMode-BT2twH2C.js} +1 -1
- package/dist/gallery/assets/{typescript-BfDavZMU.js → typescript-C5pkGWfc.js} +1 -1
- package/dist/gallery/assets/{vs-yheCI45x.js → vs-BKqjpgpj.js} +3 -3
- package/dist/gallery/assets/{workers-CxTFpB1G.js → workers-Dw3ZZZ2Q.js} +1 -1
- package/dist/gallery/assets/{xml-BwtrP1DO.js → xml-DKxYmeMe.js} +1 -1
- package/dist/gallery/assets/{yaml-jwkm0mVv.js → yaml-BLz7QoYz.js} +1 -1
- package/dist/gallery/index.html +2 -2
- package/dist/{gallery-DNxzJA4V.mjs → gallery-CLjSInLi.mjs} +2 -2
- package/dist/{gallery-DNxzJA4V.mjs.map → gallery-CLjSInLi.mjs.map} +1 -1
- package/dist/{html-BW9Z7gL3.mjs → html-N6TFwUQB.mjs} +11 -4
- package/dist/html-N6TFwUQB.mjs.map +1 -0
- package/dist/{index-ELy0IbiZ.d.mts → index-DBZ6pHEP.d.mts} +3 -3
- package/dist/{index-ELy0IbiZ.d.mts.map → index-DBZ6pHEP.d.mts.map} +1 -1
- package/dist/{index-BemK8p6d.d.mts → index-s1-tayec.d.mts} +16 -3
- package/dist/{index-BemK8p6d.d.mts.map → index-s1-tayec.d.mts.map} +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +6 -6
- package/dist/{module-BaxIBS_8.mjs → module-CUszeEyD.mjs} +2 -2
- package/dist/{module-BaxIBS_8.mjs.map → module-CUszeEyD.mjs.map} +1 -1
- package/dist/native-loader-T2dk9Xrf.mjs +304 -0
- package/dist/native-loader-T2dk9Xrf.mjs.map +1 -0
- package/dist/{plugin-D2KEfrj1.mjs → plugin-BhEarB-i.mjs} +301 -21
- package/dist/plugin-BhEarB-i.mjs.map +1 -0
- package/dist/{report-CHUEZNZA.mjs → report-BuvPnXYn.mjs} +2 -2
- package/dist/{report-CHUEZNZA.mjs.map → report-BuvPnXYn.mjs.map} +1 -1
- package/dist/{report-ZbXgDiN_.mjs → report-CNk-LzNl.mjs} +2 -2
- package/dist/{report-ZbXgDiN_.mjs.map → report-CNk-LzNl.mjs.map} +1 -1
- package/dist/report-identity-CvU6XOLJ.mjs +919 -0
- package/dist/report-identity-CvU6XOLJ.mjs.map +1 -0
- package/dist/{resolver-D8_4jxfT.mjs → resolver-DUipOWtm.mjs} +3 -254
- package/dist/resolver-DUipOWtm.mjs.map +1 -0
- package/dist/test-runner.d.mts +68 -0
- package/dist/test-runner.d.mts.map +1 -0
- package/dist/test-runner.mjs +160 -0
- package/dist/test-runner.mjs.map +1 -0
- package/dist/{vrt-CuAg5Eow.d.mts → vrt-DJwEoyjo.d.mts} +4 -3
- package/dist/vrt-DJwEoyjo.d.mts.map +1 -0
- package/dist/{vrt-B8j2yfiN.mjs → vrt-KEfbSnWX.mjs} +15 -5
- package/dist/vrt-KEfbSnWX.mjs.map +1 -0
- package/dist/vrt.d.mts +3 -3
- package/dist/vrt.mjs +4 -4
- package/package.json +8 -4
- package/dist/autogen-D9K1av3V.mjs.map +0 -1
- package/dist/gallery/assets/index-CT4hwaEG.css +0 -1
- package/dist/html-BW9Z7gL3.mjs.map +0 -1
- package/dist/plugin-D2KEfrj1.mjs.map +0 -1
- package/dist/report-identity-dHHBq0Ne.mjs.map +0 -1
- package/dist/resolver-D8_4jxfT.mjs.map +0 -1
- package/dist/vrt-B8j2yfiN.mjs.map +0 -1
- package/dist/vrt-CuAg5Eow.d.mts.map +0 -1
|
@@ -0,0 +1,919 @@
|
|
|
1
|
+
import { a as resolveReferences, c as writeRawTokenFile, i as readRawTokenFile, l as scanTokenUsage, n as deleteTokenAtPath, o as setTokenAtPath, r as findDependentTokens, s as validateSemanticReference, t as buildTokenMap, u as parseTokens } from "./resolver-DUipOWtm.mjs";
|
|
2
|
+
import { r as loadNative, t as analyzeSfcFallback } from "./native-loader-T2dk9Xrf.mjs";
|
|
3
|
+
import { r as resolveSnapshotIdentity } from "./snapshot-identity-Cm-t65Kl.mjs";
|
|
4
|
+
import { A as decodeUrlComponent, L as HttpError, M as isVueSourcePath, P as resolveInsideAny, T as parseJsonBody, a as parseScriptSetupForArt, d as allowedSourceRoots, f as resolveComponentSourcePath, n as sourceWithOptionalArtTitle, p as resolveReadableArtPath, u as resolveArtComponent } from "./art-processing-BQ7OYXsq.mjs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import crypto, { createHash } from "node:crypto";
|
|
8
|
+
import { lstat, readFile, readdir } from "node:fs/promises";
|
|
9
|
+
import ts from "typescript";
|
|
10
|
+
//#region src/api-tokens.ts
|
|
11
|
+
function resolveTokensPath(ctx) {
|
|
12
|
+
const allowedRoots = [ctx.config.root, ...ctx.scanRoots];
|
|
13
|
+
if (ctx.projectRoot) allowedRoots.push(ctx.projectRoot);
|
|
14
|
+
return resolveInsideAny(allowedRoots, ctx.tokensPath, "tokensPath");
|
|
15
|
+
}
|
|
16
|
+
function sendTokenMutationError(e, sendError) {
|
|
17
|
+
if (e instanceof HttpError) {
|
|
18
|
+
sendError(e.message, e.status);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (e instanceof Error && /(token path|token editing)/i.test(e.message)) {
|
|
22
|
+
sendError(e.message, 400);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
sendError(e instanceof Error ? e.message : String(e));
|
|
26
|
+
}
|
|
27
|
+
/** GET /api/tokens/usage */
|
|
28
|
+
async function handleTokensUsage(ctx, sendJson) {
|
|
29
|
+
if (!ctx.tokensPath) {
|
|
30
|
+
sendJson({});
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const categories = await parseTokens(resolveTokensPath(ctx));
|
|
35
|
+
resolveReferences(categories, buildTokenMap(categories));
|
|
36
|
+
const resolvedTokenMap = buildTokenMap(categories);
|
|
37
|
+
sendJson(scanTokenUsage(ctx.artFiles, resolvedTokenMap));
|
|
38
|
+
} catch (e) {
|
|
39
|
+
console.error("[musea] Failed to scan token usage:", e);
|
|
40
|
+
sendJson({});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** GET /api/tokens */
|
|
44
|
+
async function handleTokensGet(ctx, sendJson) {
|
|
45
|
+
if (!ctx.tokensPath) {
|
|
46
|
+
sendJson({
|
|
47
|
+
categories: [],
|
|
48
|
+
tokenMap: {},
|
|
49
|
+
meta: {
|
|
50
|
+
filePath: "",
|
|
51
|
+
tokenCount: 0,
|
|
52
|
+
primitiveCount: 0,
|
|
53
|
+
semanticCount: 0
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
const absoluteTokensPath = resolveTokensPath(ctx);
|
|
60
|
+
const categories = await parseTokens(absoluteTokensPath);
|
|
61
|
+
resolveReferences(categories, buildTokenMap(categories));
|
|
62
|
+
const resolvedTokenMap = buildTokenMap(categories);
|
|
63
|
+
let primitiveCount = 0;
|
|
64
|
+
let semanticCount = 0;
|
|
65
|
+
for (const token of Object.values(resolvedTokenMap)) if (token.$tier === "semantic") semanticCount++;
|
|
66
|
+
else primitiveCount++;
|
|
67
|
+
sendJson({
|
|
68
|
+
categories,
|
|
69
|
+
tokenMap: resolvedTokenMap,
|
|
70
|
+
meta: {
|
|
71
|
+
filePath: absoluteTokensPath,
|
|
72
|
+
tokenCount: Object.keys(resolvedTokenMap).length,
|
|
73
|
+
primitiveCount,
|
|
74
|
+
semanticCount
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
} catch (e) {
|
|
78
|
+
console.error("[musea] Failed to load tokens:", e);
|
|
79
|
+
sendJson({
|
|
80
|
+
categories: [],
|
|
81
|
+
tokenMap: {},
|
|
82
|
+
error: String(e)
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** POST /api/tokens (create) */
|
|
87
|
+
async function handleTokensCreate(ctx, readBody, sendJson, sendError) {
|
|
88
|
+
if (!ctx.tokensPath) {
|
|
89
|
+
sendError("No tokens path configured", 400);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const body = await readBody();
|
|
93
|
+
try {
|
|
94
|
+
const { path: dotPath, token } = parseJsonBody(body);
|
|
95
|
+
if (!dotPath || !token || token.value === void 0) {
|
|
96
|
+
sendError("Missing required fields: path, token.value", 400);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const absoluteTokensPath = resolveTokensPath(ctx);
|
|
100
|
+
const rawData = await readRawTokenFile(absoluteTokensPath);
|
|
101
|
+
const currentMap = buildTokenMap(await parseTokens(absoluteTokensPath));
|
|
102
|
+
if (currentMap[dotPath]) {
|
|
103
|
+
sendError(`Token already exists at path "${dotPath}"`, 409);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (token.$reference) {
|
|
107
|
+
const validation = validateSemanticReference(currentMap, token.$reference, dotPath);
|
|
108
|
+
if (!validation.valid) {
|
|
109
|
+
sendError(validation.error, 400);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
token.value = `{${token.$reference}}`;
|
|
113
|
+
token.$tier = "semantic";
|
|
114
|
+
}
|
|
115
|
+
setTokenAtPath(rawData, dotPath, token);
|
|
116
|
+
await writeRawTokenFile(absoluteTokensPath, rawData);
|
|
117
|
+
const categories = await parseTokens(absoluteTokensPath);
|
|
118
|
+
resolveReferences(categories, buildTokenMap(categories));
|
|
119
|
+
sendJson({
|
|
120
|
+
categories,
|
|
121
|
+
tokenMap: buildTokenMap(categories)
|
|
122
|
+
}, 201);
|
|
123
|
+
} catch (e) {
|
|
124
|
+
sendTokenMutationError(e, sendError);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/** PUT /api/tokens (update) */
|
|
128
|
+
async function handleTokensUpdate(ctx, readBody, sendJson, sendError) {
|
|
129
|
+
if (!ctx.tokensPath) {
|
|
130
|
+
sendError("No tokens path configured", 400);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const body = await readBody();
|
|
134
|
+
try {
|
|
135
|
+
const { path: dotPath, token } = parseJsonBody(body);
|
|
136
|
+
if (!dotPath || !token || token.value === void 0) {
|
|
137
|
+
sendError("Missing required fields: path, token.value", 400);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const absoluteTokensPath = resolveTokensPath(ctx);
|
|
141
|
+
if (token.$reference) {
|
|
142
|
+
const validation = validateSemanticReference(buildTokenMap(await parseTokens(absoluteTokensPath)), token.$reference, dotPath);
|
|
143
|
+
if (!validation.valid) {
|
|
144
|
+
sendError(validation.error, 400);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
token.value = `{${token.$reference}}`;
|
|
148
|
+
token.$tier = "semantic";
|
|
149
|
+
}
|
|
150
|
+
const rawData = await readRawTokenFile(absoluteTokensPath);
|
|
151
|
+
setTokenAtPath(rawData, dotPath, token);
|
|
152
|
+
await writeRawTokenFile(absoluteTokensPath, rawData);
|
|
153
|
+
const categories = await parseTokens(absoluteTokensPath);
|
|
154
|
+
resolveReferences(categories, buildTokenMap(categories));
|
|
155
|
+
sendJson({
|
|
156
|
+
categories,
|
|
157
|
+
tokenMap: buildTokenMap(categories)
|
|
158
|
+
});
|
|
159
|
+
} catch (e) {
|
|
160
|
+
sendTokenMutationError(e, sendError);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/** DELETE /api/tokens */
|
|
164
|
+
async function handleTokensDelete(ctx, readBody, sendJson, sendError) {
|
|
165
|
+
if (!ctx.tokensPath) {
|
|
166
|
+
sendError("No tokens path configured", 400);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const body = await readBody();
|
|
170
|
+
try {
|
|
171
|
+
const { path: dotPath } = parseJsonBody(body);
|
|
172
|
+
if (!dotPath) {
|
|
173
|
+
sendError("Missing required field: path", 400);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const absoluteTokensPath = resolveTokensPath(ctx);
|
|
177
|
+
const dependents = findDependentTokens(buildTokenMap(await parseTokens(absoluteTokensPath)), dotPath);
|
|
178
|
+
const rawData = await readRawTokenFile(absoluteTokensPath);
|
|
179
|
+
if (!deleteTokenAtPath(rawData, dotPath)) {
|
|
180
|
+
sendError(`Token not found at path "${dotPath}"`, 404);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
await writeRawTokenFile(absoluteTokensPath, rawData);
|
|
184
|
+
const categories = await parseTokens(absoluteTokensPath);
|
|
185
|
+
resolveReferences(categories, buildTokenMap(categories));
|
|
186
|
+
sendJson({
|
|
187
|
+
categories,
|
|
188
|
+
tokenMap: buildTokenMap(categories),
|
|
189
|
+
dependentsWarning: dependents.length > 0 ? dependents : void 0
|
|
190
|
+
});
|
|
191
|
+
} catch (e) {
|
|
192
|
+
sendTokenMutationError(e, sendError);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
//#endregion
|
|
196
|
+
//#region src/script-component-analysis.ts
|
|
197
|
+
/** Read a script component's runtime options without executing project code. */
|
|
198
|
+
function scriptKind(filename) {
|
|
199
|
+
if (filename.endsWith(".tsx")) return ts.ScriptKind.TSX;
|
|
200
|
+
if (filename.endsWith(".jsx")) return ts.ScriptKind.JSX;
|
|
201
|
+
if (filename.endsWith(".js")) return ts.ScriptKind.JS;
|
|
202
|
+
return ts.ScriptKind.TS;
|
|
203
|
+
}
|
|
204
|
+
function unwrap(expression) {
|
|
205
|
+
while (ts.isParenthesizedExpression(expression) || ts.isAsExpression(expression) || ts.isSatisfiesExpression(expression) || ts.isNonNullExpression(expression)) expression = expression.expression;
|
|
206
|
+
return expression;
|
|
207
|
+
}
|
|
208
|
+
function nameOf(name) {
|
|
209
|
+
if (ts.isIdentifier(name) || ts.isStringLiteral(name) || ts.isNumericLiteral(name)) return name.text;
|
|
210
|
+
if (ts.isComputedPropertyName(name) && ts.isStringLiteral(name.expression)) return name.expression.text;
|
|
211
|
+
}
|
|
212
|
+
function variable(sourceFile, name) {
|
|
213
|
+
for (const statement of sourceFile.statements) {
|
|
214
|
+
if (!ts.isVariableStatement(statement)) continue;
|
|
215
|
+
for (const declaration of statement.declarationList.declarations) if (ts.isIdentifier(declaration.name) && declaration.name.text === name) return declaration.initializer;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
function resolved(expression, sourceFile) {
|
|
219
|
+
expression = unwrap(expression);
|
|
220
|
+
if (ts.isIdentifier(expression)) {
|
|
221
|
+
const initializer = variable(sourceFile, expression.text);
|
|
222
|
+
if (initializer) return unwrap(initializer);
|
|
223
|
+
}
|
|
224
|
+
return expression;
|
|
225
|
+
}
|
|
226
|
+
function option(object, name, sourceFile) {
|
|
227
|
+
for (const property of object.properties) {
|
|
228
|
+
if (ts.isPropertyAssignment(property) && nameOf(property.name) === name) return resolved(property.initializer, sourceFile);
|
|
229
|
+
if (ts.isShorthandPropertyAssignment(property) && property.name.text === name) {
|
|
230
|
+
const initializer = variable(sourceFile, name);
|
|
231
|
+
if (initializer) return resolved(initializer, sourceFile);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
function componentOptions(sourceFile) {
|
|
236
|
+
const assignment = sourceFile.statements.find((statement) => ts.isExportAssignment(statement));
|
|
237
|
+
if (!assignment || assignment.isExportEquals) return void 0;
|
|
238
|
+
let expression = resolved(assignment.expression, sourceFile);
|
|
239
|
+
if (ts.isCallExpression(expression)) {
|
|
240
|
+
const callee = expression.expression;
|
|
241
|
+
if (!(ts.isIdentifier(callee) && callee.text === "defineComponent" || ts.isPropertyAccessExpression(callee) && callee.name.text === "defineComponent") || !expression.arguments[0]) return void 0;
|
|
242
|
+
expression = resolved(expression.arguments[0], sourceFile);
|
|
243
|
+
}
|
|
244
|
+
return ts.isObjectLiteralExpression(expression) ? expression : void 0;
|
|
245
|
+
}
|
|
246
|
+
function literal(expression) {
|
|
247
|
+
expression = unwrap(expression);
|
|
248
|
+
if (ts.isStringLiteral(expression) || ts.isNoSubstitutionTemplateLiteral(expression)) return expression.text;
|
|
249
|
+
if (ts.isNumericLiteral(expression)) return Number(expression.text);
|
|
250
|
+
if (expression.kind === ts.SyntaxKind.TrueKeyword) return true;
|
|
251
|
+
if (expression.kind === ts.SyntaxKind.FalseKeyword) return false;
|
|
252
|
+
if (expression.kind === ts.SyntaxKind.NullKeyword) return null;
|
|
253
|
+
if (ts.isPrefixUnaryExpression(expression) && ts.isNumericLiteral(expression.operand)) {
|
|
254
|
+
const value = Number(expression.operand.text);
|
|
255
|
+
if (expression.operator === ts.SyntaxKind.MinusToken) return -value;
|
|
256
|
+
if (expression.operator === ts.SyntaxKind.PlusToken) return value;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function propType(expression) {
|
|
260
|
+
if (!expression) return "unknown";
|
|
261
|
+
expression = unwrap(expression);
|
|
262
|
+
if (ts.isArrayLiteralExpression(expression)) return expression.elements.map((element) => propType(element)).join(" | ") || "unknown";
|
|
263
|
+
switch (ts.isIdentifier(expression) ? expression.text : ts.isPropertyAccessExpression(expression) ? expression.name.text : void 0) {
|
|
264
|
+
case "String": return "string";
|
|
265
|
+
case "Number": return "number";
|
|
266
|
+
case "Boolean": return "boolean";
|
|
267
|
+
case "Array": return "unknown[]";
|
|
268
|
+
case "Object": return "Record<string, unknown>";
|
|
269
|
+
default: return "unknown";
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
function analyzeProps(expression, sourceFile) {
|
|
273
|
+
if (!expression) return [];
|
|
274
|
+
if (ts.isArrayLiteralExpression(expression)) return expression.elements.flatMap((element) => ts.isStringLiteral(element) ? [{
|
|
275
|
+
name: element.text,
|
|
276
|
+
type: "unknown",
|
|
277
|
+
required: false
|
|
278
|
+
}] : []);
|
|
279
|
+
if (!ts.isObjectLiteralExpression(expression)) return [];
|
|
280
|
+
const props = [];
|
|
281
|
+
for (const property of expression.properties) {
|
|
282
|
+
if (!ts.isPropertyAssignment(property)) continue;
|
|
283
|
+
const name = nameOf(property.name);
|
|
284
|
+
if (!name) continue;
|
|
285
|
+
const value = resolved(property.initializer, sourceFile);
|
|
286
|
+
if (ts.isObjectLiteralExpression(value)) {
|
|
287
|
+
const type = propType(option(value, "type", sourceFile));
|
|
288
|
+
const required = option(value, "required", sourceFile)?.kind === ts.SyntaxKind.TrueKeyword;
|
|
289
|
+
const defaultExpression = option(value, "default", sourceFile);
|
|
290
|
+
const defaultValue = defaultExpression ? literal(defaultExpression) : void 0;
|
|
291
|
+
props.push({
|
|
292
|
+
name,
|
|
293
|
+
type,
|
|
294
|
+
required,
|
|
295
|
+
...defaultValue !== void 0 ? { default_value: defaultValue } : {}
|
|
296
|
+
});
|
|
297
|
+
} else props.push({
|
|
298
|
+
name,
|
|
299
|
+
type: propType(value),
|
|
300
|
+
required: false
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
return props;
|
|
304
|
+
}
|
|
305
|
+
function analyzeEmits(expression) {
|
|
306
|
+
if (!expression) return [];
|
|
307
|
+
if (ts.isArrayLiteralExpression(expression)) return expression.elements.flatMap((element) => ts.isStringLiteral(element) ? [element.text] : []);
|
|
308
|
+
if (ts.isObjectLiteralExpression(expression)) return expression.properties.flatMap((property) => (ts.isPropertyAssignment(property) || ts.isMethodDeclaration(property)) && nameOf(property.name) ? [nameOf(property.name)] : []);
|
|
309
|
+
return [];
|
|
310
|
+
}
|
|
311
|
+
function analyzeScriptComponent(source, filename) {
|
|
312
|
+
const sourceFile = ts.createSourceFile(filename, source, ts.ScriptTarget.Latest, true, scriptKind(filename));
|
|
313
|
+
const options = componentOptions(sourceFile);
|
|
314
|
+
if (!options) return {
|
|
315
|
+
props: [],
|
|
316
|
+
emits: []
|
|
317
|
+
};
|
|
318
|
+
return {
|
|
319
|
+
props: analyzeProps(option(options, "props", sourceFile), sourceFile),
|
|
320
|
+
emits: analyzeEmits(option(options, "emits", sourceFile))
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
//#endregion
|
|
324
|
+
//#region src/api-routes/handler-palette.ts
|
|
325
|
+
/**
|
|
326
|
+
* Palette handler for the Musea gallery API.
|
|
327
|
+
*
|
|
328
|
+
* Handles GET /api/arts/:path/palette endpoint.
|
|
329
|
+
*/
|
|
330
|
+
/** GET /api/arts/:path/palette */
|
|
331
|
+
async function handleArtPalette(ctx, match, sendJson, sendError) {
|
|
332
|
+
const artPath = decodeUrlComponent(match[1], "art path");
|
|
333
|
+
const art = ctx.artFiles.get(artPath);
|
|
334
|
+
if (!art) {
|
|
335
|
+
sendError("Art not found", 404);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
try {
|
|
339
|
+
const readableArtPath = resolveReadableArtPath(ctx.config.root, ctx.scanRoots, artPath);
|
|
340
|
+
const source = await fs.promises.readFile(readableArtPath, "utf-8");
|
|
341
|
+
const binding = loadNative();
|
|
342
|
+
let palette;
|
|
343
|
+
if (binding.generateArtPalette) {
|
|
344
|
+
const generated = binding.generateArtPalette(sourceWithOptionalArtTitle(source), { filename: readableArtPath });
|
|
345
|
+
palette = {
|
|
346
|
+
...generated,
|
|
347
|
+
title: art.metadata.title,
|
|
348
|
+
controls: generated.controls.map(({ defaultValue, ...control }) => ({
|
|
349
|
+
...control,
|
|
350
|
+
...defaultValue !== void 0 ? { default_value: defaultValue } : {}
|
|
351
|
+
}))
|
|
352
|
+
};
|
|
353
|
+
palette.json = JSON.stringify({
|
|
354
|
+
title: palette.title,
|
|
355
|
+
controls: palette.controls
|
|
356
|
+
}, null, 2);
|
|
357
|
+
} else palette = {
|
|
358
|
+
title: art.metadata.title,
|
|
359
|
+
controls: [],
|
|
360
|
+
groups: [],
|
|
361
|
+
json: "{}",
|
|
362
|
+
typescript: ""
|
|
363
|
+
};
|
|
364
|
+
palette.componentTagName = resolveArtComponent(art, artPath, art.scriptSetupContent ? parseScriptSetupForArt(art.scriptSetupContent) : null, {
|
|
365
|
+
root: ctx.config.root,
|
|
366
|
+
scanRoots: ctx.scanRoots
|
|
367
|
+
}).componentTagName;
|
|
368
|
+
const resolvedComponentPath = resolveComponentSourcePath(art, artPath, allowedSourceRoots(ctx.config.root, ctx.scanRoots));
|
|
369
|
+
if (!resolvedComponentPath) {
|
|
370
|
+
applyAuthoredVariantValues(palette, art.variants);
|
|
371
|
+
sendJson(palette);
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
try {
|
|
375
|
+
const componentSource = await fs.promises.readFile(resolvedComponentPath, "utf-8");
|
|
376
|
+
const analysis = isVueSourcePath(resolvedComponentPath) ? binding.analyzeSfc ? binding.analyzeSfc(componentSource, { filename: resolvedComponentPath }) : analyzeSfcFallback(componentSource, { filename: resolvedComponentPath }) : analyzeScriptComponent(componentSource, resolvedComponentPath);
|
|
377
|
+
if (analysis.props.length > 0) mergeSfcPropsIntoPalette(palette, analysis.props);
|
|
378
|
+
if (isVueSourcePath(resolvedComponentPath) && analysis.props.some((prop) => prop.name === "__proto__")) palette.unsupportedProps = ["__proto__"];
|
|
379
|
+
} catch {}
|
|
380
|
+
applyAuthoredVariantValues(palette, art.variants);
|
|
381
|
+
sendJson(palette);
|
|
382
|
+
} catch (e) {
|
|
383
|
+
if (e instanceof HttpError) {
|
|
384
|
+
sendError(e.message, e.status);
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
sendError(e instanceof Error ? e.message : String(e));
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
function applyAuthoredVariantValues(palette, variants) {
|
|
391
|
+
const variant = variants.find((candidate) => candidate.isDefault) ?? variants[0];
|
|
392
|
+
const values = variant?.props ?? variant?.args;
|
|
393
|
+
if (!values) return;
|
|
394
|
+
const controlsByName = new Map(palette.controls.map((control) => [normalizePropName(control.name), control]));
|
|
395
|
+
for (const [name, value] of Object.entries(values)) {
|
|
396
|
+
if (name === "viewport") continue;
|
|
397
|
+
const control = controlsByName.get(normalizePropName(name));
|
|
398
|
+
if (control) control.default_value = value;
|
|
399
|
+
}
|
|
400
|
+
palette.json = JSON.stringify({
|
|
401
|
+
title: palette.title,
|
|
402
|
+
controls: palette.controls
|
|
403
|
+
}, null, 2);
|
|
404
|
+
}
|
|
405
|
+
function mergeSfcPropsIntoPalette(palette, props) {
|
|
406
|
+
const controlsByName = /* @__PURE__ */ new Map();
|
|
407
|
+
for (const control of palette.controls) controlsByName.set(normalizePropName(control.name), control);
|
|
408
|
+
for (const prop of props) {
|
|
409
|
+
const control = controlsByName.get(normalizePropName(prop.name));
|
|
410
|
+
if (control) applyPropMetadata(control, prop);
|
|
411
|
+
else palette.controls.push(controlFromProp(prop));
|
|
412
|
+
}
|
|
413
|
+
palette.json = JSON.stringify({
|
|
414
|
+
title: palette.title,
|
|
415
|
+
controls: palette.controls
|
|
416
|
+
}, null, 2);
|
|
417
|
+
palette.typescript = generateTypescript(palette);
|
|
418
|
+
}
|
|
419
|
+
function applyPropMetadata(control, prop) {
|
|
420
|
+
const inferred = inferControl(prop.type);
|
|
421
|
+
control.name = prop.name;
|
|
422
|
+
control.required = prop.required;
|
|
423
|
+
if (control.default_value === void 0 && prop.default_value !== void 0) control.default_value = prop.default_value;
|
|
424
|
+
if (prop.unsupported_default) {
|
|
425
|
+
control.unsupported_default = true;
|
|
426
|
+
control.default_source = prop.default_source;
|
|
427
|
+
control.description = "The component retains its authored default; this expression is not evaluated for Controls.";
|
|
428
|
+
}
|
|
429
|
+
if (inferred.control !== "text" || control.control === "text") control.control = inferred.control;
|
|
430
|
+
if (inferred.options.length > 0) control.options = inferred.options;
|
|
431
|
+
}
|
|
432
|
+
function controlFromProp(prop) {
|
|
433
|
+
const inferred = inferControl(prop.type);
|
|
434
|
+
return {
|
|
435
|
+
name: prop.name,
|
|
436
|
+
control: inferred.control,
|
|
437
|
+
default_value: prop.default_value,
|
|
438
|
+
...prop.unsupported_default ? {
|
|
439
|
+
unsupported_default: true,
|
|
440
|
+
default_source: prop.default_source,
|
|
441
|
+
description: "The component retains its authored default; this expression is not evaluated for Controls."
|
|
442
|
+
} : {},
|
|
443
|
+
required: prop.required,
|
|
444
|
+
options: inferred.options,
|
|
445
|
+
range: void 0,
|
|
446
|
+
group: void 0
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
function inferControl(type) {
|
|
450
|
+
const normalized = type.trim();
|
|
451
|
+
if (normalized.includes("=>") || normalized === "Function") return {
|
|
452
|
+
control: "function",
|
|
453
|
+
options: []
|
|
454
|
+
};
|
|
455
|
+
const options = literalOptions(normalized);
|
|
456
|
+
if (options.length > 0) return {
|
|
457
|
+
control: "select",
|
|
458
|
+
options
|
|
459
|
+
};
|
|
460
|
+
if (normalized === "boolean") return {
|
|
461
|
+
control: "boolean",
|
|
462
|
+
options: []
|
|
463
|
+
};
|
|
464
|
+
if (normalized === "number") return {
|
|
465
|
+
control: "number",
|
|
466
|
+
options: []
|
|
467
|
+
};
|
|
468
|
+
if (normalized.includes("[]") || normalized.startsWith("Array<")) return {
|
|
469
|
+
control: "array",
|
|
470
|
+
options: []
|
|
471
|
+
};
|
|
472
|
+
if (normalized.startsWith("{") || normalized.startsWith("Record<")) return {
|
|
473
|
+
control: "object",
|
|
474
|
+
options: []
|
|
475
|
+
};
|
|
476
|
+
return {
|
|
477
|
+
control: "text",
|
|
478
|
+
options: []
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
function literalOptions(type) {
|
|
482
|
+
if (!type.includes("|") || type.includes("=>")) return [];
|
|
483
|
+
return type.split("|").map((part) => part.trim()).map((part) => {
|
|
484
|
+
const stringMatch = part.match(/^["']([^"']+)["']$/);
|
|
485
|
+
if (stringMatch) return stringMatch[1];
|
|
486
|
+
if (part === "true") return true;
|
|
487
|
+
if (part === "false") return false;
|
|
488
|
+
const numberValue = Number(part);
|
|
489
|
+
return Number.isFinite(numberValue) ? numberValue : void 0;
|
|
490
|
+
}).filter((value) => value !== void 0).map((value) => ({
|
|
491
|
+
label: String(value),
|
|
492
|
+
value
|
|
493
|
+
}));
|
|
494
|
+
}
|
|
495
|
+
function normalizePropName(name) {
|
|
496
|
+
return name.replace(/[-_]/g, "").toLowerCase();
|
|
497
|
+
}
|
|
498
|
+
function generateTypescript(palette) {
|
|
499
|
+
const fields = palette.controls.map((control) => {
|
|
500
|
+
return ` ${/^[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*$/u.test(control.name) ? control.name : JSON.stringify(control.name)}${control.required ? "" : "?"}: ${controlTsType(control)};`;
|
|
501
|
+
}).join("\n");
|
|
502
|
+
return `export interface ${pascalCase(palette.title)}Props {\n${fields}\n}\n`;
|
|
503
|
+
}
|
|
504
|
+
function controlTsType(control) {
|
|
505
|
+
if (control.control === "boolean") return "boolean";
|
|
506
|
+
if (control.control === "number" || control.control === "range") return "number";
|
|
507
|
+
if (control.control === "function") return "(...args: unknown[]) => unknown";
|
|
508
|
+
if (control.control === "array") return "unknown[]";
|
|
509
|
+
if (control.control === "object") return "Record<string, unknown>";
|
|
510
|
+
if (control.control === "select" && control.options.length > 0) return control.options.map((option) => JSON.stringify(option.value)).join(" | ");
|
|
511
|
+
return "string";
|
|
512
|
+
}
|
|
513
|
+
function pascalCase(value) {
|
|
514
|
+
return value.split(/[^A-Za-z0-9]+/).filter(Boolean).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
515
|
+
}
|
|
516
|
+
//#endregion
|
|
517
|
+
//#region src/api-routes/handlers.ts
|
|
518
|
+
/**
|
|
519
|
+
* Individual route handler functions for the Musea gallery API.
|
|
520
|
+
*
|
|
521
|
+
* Extracted from api-routes.ts to keep file sizes manageable.
|
|
522
|
+
* These handle GET /api/arts/:path/... sub-routes.
|
|
523
|
+
*/
|
|
524
|
+
function sendCaughtError(sendError, error) {
|
|
525
|
+
if (error instanceof HttpError) {
|
|
526
|
+
sendError(error.message, error.status);
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
sendError(error instanceof Error ? error.message : String(error));
|
|
530
|
+
}
|
|
531
|
+
/** GET /api/arts/:path/source */
|
|
532
|
+
async function handleArtSource(ctx, match, sendJson, sendError) {
|
|
533
|
+
const artPath = decodeUrlComponent(match[1], "art path");
|
|
534
|
+
if (!ctx.artFiles.get(artPath)) {
|
|
535
|
+
sendError("Art not found", 404);
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
try {
|
|
539
|
+
const readableArtPath = resolveReadableArtPath(ctx.config.root, ctx.scanRoots, artPath);
|
|
540
|
+
sendJson({
|
|
541
|
+
source: await fs.promises.readFile(readableArtPath, "utf-8"),
|
|
542
|
+
path: artPath
|
|
543
|
+
});
|
|
544
|
+
} catch (e) {
|
|
545
|
+
sendCaughtError(sendError, e);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
/** GET /api/arts/:path/analysis */
|
|
549
|
+
async function handleArtAnalysis(ctx, match, sendJson, sendError) {
|
|
550
|
+
const artPath = decodeUrlComponent(match[1], "art path");
|
|
551
|
+
const art = ctx.artFiles.get(artPath);
|
|
552
|
+
if (!art) {
|
|
553
|
+
sendError("Art not found", 404);
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
try {
|
|
557
|
+
const resolvedComponentPath = resolveComponentSourcePath(art, artPath, allowedSourceRoots(ctx.config.root, ctx.scanRoots));
|
|
558
|
+
if (resolvedComponentPath) {
|
|
559
|
+
const source = await fs.promises.readFile(resolvedComponentPath, "utf-8");
|
|
560
|
+
if (isVueSourcePath(resolvedComponentPath)) {
|
|
561
|
+
const binding = loadNative();
|
|
562
|
+
sendJson(binding.analyzeSfc ? binding.analyzeSfc(source, { filename: resolvedComponentPath }) : analyzeSfcFallback(source, { filename: resolvedComponentPath }));
|
|
563
|
+
} else sendJson(analyzeScriptComponent(source, resolvedComponentPath));
|
|
564
|
+
} else sendJson({
|
|
565
|
+
props: [],
|
|
566
|
+
emits: []
|
|
567
|
+
});
|
|
568
|
+
} catch (e) {
|
|
569
|
+
sendCaughtError(sendError, e);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
/** GET /api/arts/:path/docs */
|
|
573
|
+
async function handleArtDocs(ctx, match, sendJson, sendError) {
|
|
574
|
+
const artPath = decodeUrlComponent(match[1], "art path");
|
|
575
|
+
const art = ctx.artFiles.get(artPath);
|
|
576
|
+
if (!art) {
|
|
577
|
+
sendError("Art not found", 404);
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
try {
|
|
581
|
+
const readableArtPath = resolveReadableArtPath(ctx.config.root, ctx.scanRoots, artPath);
|
|
582
|
+
const source = await fs.promises.readFile(readableArtPath, "utf-8");
|
|
583
|
+
const binding = loadNative();
|
|
584
|
+
if (binding.generateArtDoc) {
|
|
585
|
+
const doc = binding.generateArtDoc(sourceWithOptionalArtTitle(source), { filename: readableArtPath });
|
|
586
|
+
let markdown = doc.markdown || "";
|
|
587
|
+
markdown = markdown.replace(/```(\w*)\n([\s\S]*?)```/g, (_match, lang, code) => {
|
|
588
|
+
const lines = code.split("\n");
|
|
589
|
+
let minIndent = Infinity;
|
|
590
|
+
for (const line of lines) if (line.trim()) {
|
|
591
|
+
const indent = line.match(/^(\s*)/)?.[1].length || 0;
|
|
592
|
+
minIndent = Math.min(minIndent, indent);
|
|
593
|
+
}
|
|
594
|
+
if (minIndent === Infinity) minIndent = 0;
|
|
595
|
+
let formatted;
|
|
596
|
+
if (minIndent > 0) formatted = lines.map((line) => line.slice(minIndent)).join("\n");
|
|
597
|
+
else {
|
|
598
|
+
let restIndent = Infinity;
|
|
599
|
+
for (let i = 1; i < lines.length; i++) if (lines[i].trim()) {
|
|
600
|
+
const indent = lines[i].match(/^(\s*)/)?.[1].length || 0;
|
|
601
|
+
restIndent = Math.min(restIndent, indent);
|
|
602
|
+
}
|
|
603
|
+
if (restIndent === Infinity || restIndent === 0) formatted = lines.join("\n");
|
|
604
|
+
else formatted = lines.map((line, i) => i === 0 ? line : line.slice(restIndent)).join("\n");
|
|
605
|
+
}
|
|
606
|
+
return "```" + lang + "\n" + formatted + "```";
|
|
607
|
+
});
|
|
608
|
+
sendJson({
|
|
609
|
+
...doc,
|
|
610
|
+
markdown
|
|
611
|
+
});
|
|
612
|
+
} else sendJson({
|
|
613
|
+
markdown: "",
|
|
614
|
+
title: art.metadata.title,
|
|
615
|
+
variant_count: art.variants.length
|
|
616
|
+
});
|
|
617
|
+
} catch (e) {
|
|
618
|
+
sendCaughtError(sendError, e);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
/** GET /api/arts/:path/variants/:name/a11y */
|
|
622
|
+
//#endregion
|
|
623
|
+
//#region src/art-order.ts
|
|
624
|
+
function sortedArts(arts) {
|
|
625
|
+
return [...arts].sort(compareArtFiles);
|
|
626
|
+
}
|
|
627
|
+
function compareArtFiles(a, b) {
|
|
628
|
+
const order = compareOrder(a.metadata.order, b.metadata.order);
|
|
629
|
+
if (order !== 0) return order;
|
|
630
|
+
const title = a.metadata.title.localeCompare(b.metadata.title, void 0, { numeric: true });
|
|
631
|
+
if (title !== 0) return title;
|
|
632
|
+
return a.path.localeCompare(b.path, void 0, { numeric: true });
|
|
633
|
+
}
|
|
634
|
+
function compareOrder(a, b) {
|
|
635
|
+
if (a === void 0 && b === void 0) return 0;
|
|
636
|
+
if (a === void 0) return 1;
|
|
637
|
+
if (b === void 0) return -1;
|
|
638
|
+
return a - b;
|
|
639
|
+
}
|
|
640
|
+
//#endregion
|
|
641
|
+
//#region src/static-data.ts
|
|
642
|
+
function staticPreviewId(artPath, variantName) {
|
|
643
|
+
return crypto.createHash("sha256").update(artPath).update("\0").update(variantName).digest("hex").slice(0, 20);
|
|
644
|
+
}
|
|
645
|
+
function joinUrlPath(basePath, ...segments) {
|
|
646
|
+
return `${basePath === "/" ? "" : basePath.replace(/\/+$/, "")}/${segments.map((segment) => segment.replace(/^\/+|\/+$/g, "")).filter(Boolean).join("/")}`;
|
|
647
|
+
}
|
|
648
|
+
async function createStaticGalleryPayload(ctx) {
|
|
649
|
+
const apiCtx = createApiContext(ctx);
|
|
650
|
+
const sourceArts = sortedArts(ctx.artFiles.values());
|
|
651
|
+
const arts = [];
|
|
652
|
+
const previews = Object.create(null);
|
|
653
|
+
const details = {};
|
|
654
|
+
const snapshotIdentities = Object.create(null);
|
|
655
|
+
const identityRoot = resolveStaticIdentityRoot(ctx.config.root, sourceArts.map((art) => art.path), ctx.projectRoot);
|
|
656
|
+
const publicPaths = /* @__PURE__ */ new Map();
|
|
657
|
+
for (const art of sourceArts) publicPaths.set(art.path, resolveStaticArtIdentity(art.path, identityRoot));
|
|
658
|
+
for (const sourceArt of sourceArts) {
|
|
659
|
+
const publicPath = publicPaths.get(sourceArt.path);
|
|
660
|
+
const art = publicArtInfo(sourceArt, publicPath, identityRoot);
|
|
661
|
+
arts.push(art);
|
|
662
|
+
snapshotIdentities[publicPath] = publicPath;
|
|
663
|
+
previews[publicPath] = Object.create(null);
|
|
664
|
+
for (const variant of art.variants) {
|
|
665
|
+
const id = staticPreviewId(publicPath, variant.name);
|
|
666
|
+
previews[publicPath][variant.name] = joinUrlPath(ctx.basePath, "preview", `${id}.html`);
|
|
667
|
+
}
|
|
668
|
+
details[publicPath] = {
|
|
669
|
+
source: withPublicSourcePath(await captureJson((sendJson, sendError) => {
|
|
670
|
+
return handleArtSource(apiCtx, artMatch(sourceArt.path), sendJson, sendError);
|
|
671
|
+
}, {
|
|
672
|
+
source: "",
|
|
673
|
+
path: sourceArt.path
|
|
674
|
+
}), publicPath),
|
|
675
|
+
palette: await captureJson((sendJson, sendError) => {
|
|
676
|
+
return handleArtPalette(apiCtx, artMatch(sourceArt.path), sendJson, sendError);
|
|
677
|
+
}, emptyPalette(art)),
|
|
678
|
+
analysis: await captureJson((sendJson, sendError) => {
|
|
679
|
+
return handleArtAnalysis(apiCtx, artMatch(sourceArt.path), sendJson, sendError);
|
|
680
|
+
}, {
|
|
681
|
+
props: [],
|
|
682
|
+
emits: []
|
|
683
|
+
}),
|
|
684
|
+
docs: await captureJson((sendJson, sendError) => {
|
|
685
|
+
return handleArtDocs(apiCtx, artMatch(sourceArt.path), sendJson, sendError);
|
|
686
|
+
}, emptyDocs(art))
|
|
687
|
+
};
|
|
688
|
+
}
|
|
689
|
+
const tokens = await captureJson((sendJson) => handleTokensGet(apiCtx, sendJson), emptyTokens());
|
|
690
|
+
const tokenUsage = await captureJson((sendJson) => handleTokensUsage(apiCtx, sendJson), {});
|
|
691
|
+
return {
|
|
692
|
+
arts,
|
|
693
|
+
snapshotIdentityVersion: 1,
|
|
694
|
+
snapshotIdentities,
|
|
695
|
+
previews,
|
|
696
|
+
details,
|
|
697
|
+
tokens: withPublicTokenPath(tokens, identityRoot),
|
|
698
|
+
tokenUsage: withPublicUsagePaths(tokenUsage, publicPaths)
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
function resolveStaticIdentityRoot(viteRoot, artPaths, projectRoot) {
|
|
702
|
+
if (projectRoot) return path.resolve(projectRoot);
|
|
703
|
+
let root = path.resolve(viteRoot);
|
|
704
|
+
for (const artPath of artPaths) {
|
|
705
|
+
const directory = path.dirname(path.resolve(artPath));
|
|
706
|
+
while (!isPathInside(root, directory)) {
|
|
707
|
+
const parent = path.dirname(root);
|
|
708
|
+
if (parent === root) throw new Error(`Art is outside the Vite root on another filesystem: ${artPath}. Configure projectRoot.`);
|
|
709
|
+
root = parent;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
return root;
|
|
713
|
+
}
|
|
714
|
+
function resolveStaticArtIdentity(artPath, identityRoot) {
|
|
715
|
+
return resolveSnapshotIdentity(artPath, identityRoot);
|
|
716
|
+
}
|
|
717
|
+
function publicArtInfo(art, publicPath, identityRoot) {
|
|
718
|
+
return {
|
|
719
|
+
...art,
|
|
720
|
+
path: publicPath,
|
|
721
|
+
...art.componentPath ? { componentPath: publicSourceIdentity(art.componentPath, identityRoot) } : {}
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
function withPublicSourcePath(value, publicPath) {
|
|
725
|
+
if (!isRecord(value) || typeof value.path !== "string") return value;
|
|
726
|
+
return {
|
|
727
|
+
...value,
|
|
728
|
+
path: publicPath
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
function withPublicTokenPath(value, identityRoot) {
|
|
732
|
+
if (!isRecord(value) || !isRecord(value.meta) || typeof value.meta.filePath !== "string") return value;
|
|
733
|
+
if (!path.isAbsolute(value.meta.filePath)) return value;
|
|
734
|
+
return {
|
|
735
|
+
...value,
|
|
736
|
+
meta: {
|
|
737
|
+
...value.meta,
|
|
738
|
+
filePath: publicSourceIdentity(value.meta.filePath, identityRoot)
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
function withPublicUsagePaths(value, publicPaths) {
|
|
743
|
+
if (!isRecord(value)) return value;
|
|
744
|
+
return Object.fromEntries(Object.entries(value).map(([token, entries]) => [token, Array.isArray(entries) ? entries.map((entry) => isRecord(entry) && typeof entry.artPath === "string" ? {
|
|
745
|
+
...entry,
|
|
746
|
+
artPath: publicPaths.get(entry.artPath) ?? path.basename(entry.artPath)
|
|
747
|
+
} : entry) : entries]));
|
|
748
|
+
}
|
|
749
|
+
function publicSourceIdentity(sourcePath, identityRoot) {
|
|
750
|
+
const relative = path.relative(identityRoot, path.resolve(sourcePath));
|
|
751
|
+
if (relative === "" || relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) return path.basename(sourcePath);
|
|
752
|
+
return relative.split(path.sep).join("/");
|
|
753
|
+
}
|
|
754
|
+
function isRecord(value) {
|
|
755
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
756
|
+
}
|
|
757
|
+
function isPathInside(root, candidate) {
|
|
758
|
+
const relative = path.relative(root, candidate);
|
|
759
|
+
return relative === "" || relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
|
|
760
|
+
}
|
|
761
|
+
function createApiContext(ctx) {
|
|
762
|
+
return {
|
|
763
|
+
config: ctx.config,
|
|
764
|
+
artFiles: ctx.artFiles,
|
|
765
|
+
scanRoots: ctx.scanRoots,
|
|
766
|
+
tokensPath: ctx.tokensPath,
|
|
767
|
+
projectRoot: ctx.projectRoot,
|
|
768
|
+
basePath: ctx.basePath,
|
|
769
|
+
resolvedPreviewCss: ctx.resolvedPreviewCss,
|
|
770
|
+
resolvedPreviewSetup: ctx.resolvedPreviewSetup,
|
|
771
|
+
devSessionToken: ctx.devSessionToken,
|
|
772
|
+
processArtFile: async (filePath) => {
|
|
773
|
+
await fs.promises.access(filePath);
|
|
774
|
+
},
|
|
775
|
+
getDevServerPort: () => 5173
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
async function captureJson(run, fallback) {
|
|
779
|
+
let captured = fallback;
|
|
780
|
+
const sendJson = (data) => {
|
|
781
|
+
captured = data;
|
|
782
|
+
};
|
|
783
|
+
const sendError = (message, status = 500) => {
|
|
784
|
+
captured = {
|
|
785
|
+
error: message,
|
|
786
|
+
status
|
|
787
|
+
};
|
|
788
|
+
};
|
|
789
|
+
await run(sendJson, sendError);
|
|
790
|
+
return captured;
|
|
791
|
+
}
|
|
792
|
+
function artMatch(artPath) {
|
|
793
|
+
return ["", encodeURIComponent(artPath)];
|
|
794
|
+
}
|
|
795
|
+
function emptyPalette(art) {
|
|
796
|
+
return {
|
|
797
|
+
title: art.metadata.title,
|
|
798
|
+
controls: [],
|
|
799
|
+
groups: [],
|
|
800
|
+
json: "{}",
|
|
801
|
+
typescript: ""
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
function emptyDocs(art) {
|
|
805
|
+
return {
|
|
806
|
+
markdown: "",
|
|
807
|
+
title: art.metadata.title,
|
|
808
|
+
variant_count: art.variants.length
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
function emptyTokens() {
|
|
812
|
+
return {
|
|
813
|
+
categories: [],
|
|
814
|
+
tokenMap: {},
|
|
815
|
+
meta: {
|
|
816
|
+
filePath: "",
|
|
817
|
+
tokenCount: 0,
|
|
818
|
+
primitiveCount: 0,
|
|
819
|
+
semanticCount: 0
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
}
|
|
823
|
+
//#endregion
|
|
824
|
+
//#region src/static-base.ts
|
|
825
|
+
const TEXT_GALLERY_ASSET_EXT = /\.(?:css|html|js|map|mjs)$/u;
|
|
826
|
+
function rewriteGalleryBase(source, basePath) {
|
|
827
|
+
return source.replaceAll("/__musea__/", `${basePath.replace(/\/?$/, "/")}`);
|
|
828
|
+
}
|
|
829
|
+
function rewriteGalleryTextAssetBase(source, relativePath, basePath) {
|
|
830
|
+
if (!TEXT_GALLERY_ASSET_EXT.test(relativePath)) return source;
|
|
831
|
+
return rewriteGalleryBase(Buffer.from(source).toString("utf-8"), basePath);
|
|
832
|
+
}
|
|
833
|
+
function rewriteDevGalleryAsset(source, relativePath, basePath) {
|
|
834
|
+
return basePath === "/__musea__" ? source : rewriteGalleryTextAssetBase(source, relativePath, basePath);
|
|
835
|
+
}
|
|
836
|
+
function publicBasePathFromViteBase(viteBase, basePath) {
|
|
837
|
+
return viteBase && viteBase !== "/" && viteBase !== "./" ? joinUrlPath(viteBase, basePath) : basePath;
|
|
838
|
+
}
|
|
839
|
+
//#endregion
|
|
840
|
+
//#region src/vrt/report-identity.ts
|
|
841
|
+
/** Reserve one qualified namespace and compare legacy names on case-insensitive filesystems. */
|
|
842
|
+
function legacyName(identity) {
|
|
843
|
+
const basename = path.posix.basename(identity, ".art.vue");
|
|
844
|
+
return /^[a-z0-9._-]{1,200}$/i.test(basename) && !/^art-[0-9a-f]{64}$/i.test(basename) ? `vrt-${basename}` : void 0;
|
|
845
|
+
}
|
|
846
|
+
/** The complete discovered Art set decides collisions, even for a single-Art request. */
|
|
847
|
+
function resolveArtReportTarget(artPath, artPaths, projectRoot, reportDir) {
|
|
848
|
+
const identities = Array.from(artPaths, (item) => resolveSnapshotIdentity(item, projectRoot));
|
|
849
|
+
const identity = resolveSnapshotIdentity(artPath, projectRoot);
|
|
850
|
+
if (!identities.includes(identity)) throw new Error(`Report Art not found: ${artPath}`);
|
|
851
|
+
const candidates = identities.map(legacyName);
|
|
852
|
+
const counts = /* @__PURE__ */ new Map();
|
|
853
|
+
for (const name of candidates) if (name) counts.set(name.toLowerCase(), (counts.get(name.toLowerCase()) ?? 0) + 1);
|
|
854
|
+
const names = identities.map((item, index) => {
|
|
855
|
+
const candidate = candidates[index];
|
|
856
|
+
const colliding = candidate && counts.get(candidate.toLowerCase()) > 1;
|
|
857
|
+
return candidate && !colliding ? candidate : `vrt-art-${createHash("sha256").update(JSON.stringify([1, item])).digest("hex")}`;
|
|
858
|
+
});
|
|
859
|
+
if (new Set(names.map((name) => name.toLowerCase())).size !== names.length) throw new Error("Ambiguous VRT report identities");
|
|
860
|
+
const base = names[identities.indexOf(identity)];
|
|
861
|
+
return {
|
|
862
|
+
identity,
|
|
863
|
+
jsonReportPath: path.join(reportDir, `${base}-report.json`),
|
|
864
|
+
htmlReportPath: path.join(reportDir, `${base}-report.html`)
|
|
865
|
+
};
|
|
866
|
+
}
|
|
867
|
+
async function existsRegularFile(file) {
|
|
868
|
+
try {
|
|
869
|
+
if (!(await lstat(file)).isFile()) throw new Error(`VRT report is not a regular file: ${file}`);
|
|
870
|
+
return true;
|
|
871
|
+
} catch (error) {
|
|
872
|
+
if (error.code === "ENOENT") return false;
|
|
873
|
+
throw error;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
/** Fail before capture or writes; legacy adoption requires every recorded result to have this owner. */
|
|
877
|
+
async function assertArtReportOwnership(target, projectRoot) {
|
|
878
|
+
const refusal = `Ambiguous VRT report ownership: ${target.jsonReportPath}. Archive or move the existing reports before retrying.`;
|
|
879
|
+
const names = [path.basename(target.jsonReportPath), path.basename(target.htmlReportPath)];
|
|
880
|
+
let existingNames;
|
|
881
|
+
try {
|
|
882
|
+
existingNames = await readdir(path.dirname(target.jsonReportPath));
|
|
883
|
+
} catch (error) {
|
|
884
|
+
if (error.code === "ENOENT") return;
|
|
885
|
+
throw error;
|
|
886
|
+
}
|
|
887
|
+
if (existingNames.some((item) => names.some((name) => item !== name && item.toLowerCase() === name.toLowerCase()))) throw new Error(refusal);
|
|
888
|
+
const jsonExists = await existsRegularFile(target.jsonReportPath);
|
|
889
|
+
const htmlExists = await existsRegularFile(target.htmlReportPath);
|
|
890
|
+
if (!jsonExists && !htmlExists) return;
|
|
891
|
+
if (!jsonExists) throw new Error(refusal);
|
|
892
|
+
try {
|
|
893
|
+
const report = JSON.parse(await readFile(target.jsonReportPath, "utf8"));
|
|
894
|
+
if (!report || typeof report !== "object" || Array.isArray(report)) throw new Error(refusal);
|
|
895
|
+
if (Object.hasOwn(report, "reportOwner")) {
|
|
896
|
+
const owner = report.reportOwner;
|
|
897
|
+
if (!owner || typeof owner !== "object" || owner.version !== 1 || owner.artIdentity !== target.identity) throw new Error(refusal);
|
|
898
|
+
} else {
|
|
899
|
+
const results = report.results;
|
|
900
|
+
if (!Array.isArray(results) || !results.length || results.some((item) => !item || typeof item !== "object" || typeof item.artPath !== "string" || resolveSnapshotIdentity(item.artPath, projectRoot) !== target.identity)) throw new Error(refusal);
|
|
901
|
+
}
|
|
902
|
+
} catch {
|
|
903
|
+
throw new Error(refusal);
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
/** Keep ownership portable when a report directory moves to another machine. */
|
|
907
|
+
function attachArtReportOwner(json, target) {
|
|
908
|
+
return JSON.stringify({
|
|
909
|
+
...JSON.parse(json),
|
|
910
|
+
reportOwner: {
|
|
911
|
+
version: 1,
|
|
912
|
+
artIdentity: target.identity
|
|
913
|
+
}
|
|
914
|
+
}, null, 2);
|
|
915
|
+
}
|
|
916
|
+
//#endregion
|
|
917
|
+
export { handleTokensUsage as S, handleArtPalette as _, rewriteDevGalleryAsset as a, handleTokensGet as b, createStaticGalleryPayload as c, resolveStaticIdentityRoot as d, staticPreviewId as f, handleArtSource as g, handleArtDocs as h, publicBasePathFromViteBase as i, joinUrlPath as l, handleArtAnalysis as m, attachArtReportOwner as n, rewriteGalleryBase as o, sortedArts as p, resolveArtReportTarget as r, rewriteGalleryTextAssetBase as s, assertArtReportOwnership as t, resolveStaticArtIdentity as u, handleTokensCreate as v, handleTokensUpdate as x, handleTokensDelete as y };
|
|
918
|
+
|
|
919
|
+
//# sourceMappingURL=report-identity-CvU6XOLJ.mjs.map
|