@panomc/plugin-kit 0.0.0-stage → 1.0.0-dev.62
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/assets/pano-bootstrap.css +17903 -0
- package/bin/__tests__/new/e2e.test.js +111 -0
- package/bin/__tests__/new/helpers.js +76 -0
- package/bin/__tests__/new/new.test.js +472 -0
- package/bin/new.js +530 -0
- package/bin/pano-plugin.js +162 -0
- package/bin/templates/plugin/README.md +30 -0
- package/bin/templates/plugin/_gitignore +15 -0
- package/bin/templates/plugin/build.gradle.kts +389 -0
- package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.properties +7 -0
- package/bin/templates/plugin/gradle.properties +55 -0
- package/bin/templates/plugin/gradlew +249 -0
- package/bin/templates/plugin/gradlew.bat +92 -0
- package/bin/templates/plugin/package.json.tpl +14 -0
- package/bin/templates/plugin/rollup.config.js +2 -0
- package/bin/templates/plugin/settings.gradle.kts +5 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/__CLASS__Plugin.kt +50 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/frontend/ExampleFallbackPage.kt +28 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/routes/GetHelloAPI.kt +19 -0
- package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/LicenseGuard.kt +30 -0
- package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/PluginLicenseClient.kt +144 -0
- package/bin/templates/plugin/src/main/resources/config.conf +1 -0
- package/bin/templates/plugin/src/main/resources/frontend-targets.json +1 -0
- package/bin/templates/plugin/src/main/resources/locales/en-US.json +3 -0
- package/bin/templates/plugin/src/main/resources/logo.png +0 -0
- package/bin/templates/plugin/src/theme/views/HelloPage.svelte +26 -0
- package/bin/ui.js +96 -0
- package/package.json +48 -4
- package/src/config.js +191 -0
- package/src/controller/__tests__/controller.test.js +647 -0
- package/src/controller/index.js +604 -0
- package/src/dev.js +208 -0
- package/src/index.js +22 -0
- package/src/rollup/__tests__/controllers/controllers.test.js +418 -0
- package/src/rollup/__tests__/controllers/fixture.js +143 -0
- package/src/rollup/__tests__/controllers/golden/controllers.json +34 -0
- package/src/rollup/__tests__/controllers/golden/controllers.types.js +69 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.json +16 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.lock.json +27 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.with-views.json +54 -0
- package/src/rollup/__tests__/preset/preset.test.js +418 -0
- package/src/rollup/__tests__/rules/_build.js +39 -0
- package/src/rollup/__tests__/rules/_fixture.js +83 -0
- package/src/rollup/__tests__/rules/helpers.test.js +148 -0
- package/src/rollup/__tests__/rules/rules.test.js +259 -0
- package/src/rollup/__tests__/rules/stamp.test.js +113 -0
- package/src/rollup/__tests__/samples/samples.test.js +481 -0
- package/src/rollup/__tests__/seams/config.test.js +78 -0
- package/src/rollup/__tests__/seams/lock.test.js +124 -0
- package/src/rollup/__tests__/seams/sdk-seams.test.js +93 -0
- package/src/rollup/__tests__/seams/stubs.test.js +52 -0
- package/src/rollup/__tests__/views/helpers.js +82 -0
- package/src/rollup/__tests__/views/views.test.js +522 -0
- package/src/rollup/__tests__/widgets/helpers.js +117 -0
- package/src/rollup/__tests__/widgets/widgets.test.js +422 -0
- package/src/rollup/cli-build.js +90 -0
- package/src/rollup/cli-check.js +280 -0
- package/src/rollup/cli-classes.js +75 -0
- package/src/rollup/client-gen.js +67 -0
- package/src/rollup/controllers.js +434 -0
- package/src/rollup/entry.js +250 -0
- package/src/rollup/helpers.js +193 -0
- package/src/rollup/index.js +601 -0
- package/src/rollup/integrate.js +205 -0
- package/src/rollup/lock.js +230 -0
- package/src/rollup/meta.js +479 -0
- package/src/rollup/rules.js +950 -0
- package/src/rollup/samples.js +667 -0
- package/src/rollup/stamp.js +410 -0
- package/src/rollup/types.js +248 -0
- package/src/rollup/views.js +1205 -0
- package/src/rollup/widgets.js +1000 -0
- package/src/styles/__tests__/fallback/__snapshots__/build-fallback-css.test.js.snap +173 -0
- package/src/styles/__tests__/fallback/build-fallback-css.test.js +526 -0
- package/src/styles/__tests__/fallback/fixtures/base.css +247 -0
- package/src/styles/__tests__/fallback/plugins.test.js +301 -0
- package/src/styles/__tests__/lint/check-styles.test.js +423 -0
- package/src/styles/__tests__/lint/fixtures/ProductCard.golden.svelte +13 -0
- package/src/styles/__tests__/lint/fixtures/ProductCard.in.svelte +13 -0
- package/src/styles/__tests__/lint/semantic-classes.test.js +364 -0
- package/src/styles/bootstrap-classes.js +155 -0
- package/src/styles/check-styles.js +626 -0
- package/src/styles/fallback-css.js +1182 -0
- package/src/styles/plugin-css.js +237 -0
- package/src/styles/scan.js +206 -0
- package/src/styles/semantic-classes.js +436 -0
- package/src/styles/tokens.map.js +53 -0
- package/README.md +0 -3
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import MagicString from "magic-string";
|
|
4
|
+
import {
|
|
5
|
+
controllerFiles,
|
|
6
|
+
lineOf,
|
|
7
|
+
parsePrograms,
|
|
8
|
+
resolveProject,
|
|
9
|
+
walk,
|
|
10
|
+
} from "./rules.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Version stamping (doc 02 section 2): a plugin's own default views always match the
|
|
14
|
+
* controllers they ship with, so the build adds the current version to every literal
|
|
15
|
+
* controller call of a compiled view:
|
|
16
|
+
*
|
|
17
|
+
* plugin('market').require('cart') -> plugin('market').require('cart', { version: 1 })
|
|
18
|
+
*
|
|
19
|
+
* Only the compiled output is stamped; the readable copy in `contract/src` is the author's
|
|
20
|
+
* source and stays as written. Recognised calls: `.require(name)`, `.use(name)` and
|
|
21
|
+
* `.load(name)` on `plugin(...)` or on a variable (or destructured name) bound to it, and
|
|
22
|
+
* `useController('<ns>/<name>')`. A controller name that is not a string literal is a build
|
|
23
|
+
* error because the version cannot be known. Calls on another namespace are left alone.
|
|
24
|
+
*
|
|
25
|
+
* The plugin exposes `api.usage` for `pano-plugin.json`: a Map of view name -> object of
|
|
26
|
+
* `'<ns>/<name>': version` for the controllers that view uses.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
const METHODS = new Set(["require", "use", "load"]);
|
|
30
|
+
const CONTROLLERS_MODULE = "@panomc/sdk/controllers";
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @typedef {object} StampOptions
|
|
34
|
+
* @property {Record<string, number>} [controllers] controller name (`cart` or `market/cart`) -> version; default: read from the controller files
|
|
35
|
+
* @property {string} [root] plugin root, default the current directory
|
|
36
|
+
* @property {string[]} [viewDirs] view folders relative to `root`
|
|
37
|
+
* @property {string} [themeDir] default `src/theme`
|
|
38
|
+
* @property {string} [controllersDir] default `<themeDir>/controllers`
|
|
39
|
+
* @property {string} [namespace] own namespace
|
|
40
|
+
* @property {Record<string, any>} [context] shared build context: gets `controllerUsage`
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Reads `{ name: version }` from the `defineController({ name, version })` call of every
|
|
45
|
+
* public controller file. A controller without a literal version is left out.
|
|
46
|
+
*
|
|
47
|
+
* @param {import('./rules.js').KitProject} project
|
|
48
|
+
* @returns {Promise<Record<string, number>>}
|
|
49
|
+
*/
|
|
50
|
+
export async function readControllerVersions(project) {
|
|
51
|
+
/** @type {Record<string, number>} */
|
|
52
|
+
const versions = {};
|
|
53
|
+
|
|
54
|
+
for (const file of controllerFiles(project)) {
|
|
55
|
+
const source = fs.readFileSync(file, "utf8");
|
|
56
|
+
const programs = await parsePrograms(file, source);
|
|
57
|
+
|
|
58
|
+
walk(programs, (node) => {
|
|
59
|
+
if (
|
|
60
|
+
node.type !== "CallExpression" ||
|
|
61
|
+
node.callee.type !== "Identifier" ||
|
|
62
|
+
node.callee.name !== "defineController" ||
|
|
63
|
+
node.arguments[0]?.type !== "ObjectExpression"
|
|
64
|
+
) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** @type {Record<string, any>} */
|
|
69
|
+
const literals = {};
|
|
70
|
+
|
|
71
|
+
for (const property of node.arguments[0].properties) {
|
|
72
|
+
if (property.type !== "Property" || property.computed) continue;
|
|
73
|
+
|
|
74
|
+
const key = property.key.name ?? property.key.value;
|
|
75
|
+
|
|
76
|
+
if (property.value.type === "Literal") literals[key] = property.value.value;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (Number.isInteger(literals.version)) {
|
|
80
|
+
const name =
|
|
81
|
+
typeof literals.name === "string" ? literals.name : path.basename(file, ".js");
|
|
82
|
+
|
|
83
|
+
versions[name] = literals.version;
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return versions;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The string a call argument names, or null when it is not a literal.
|
|
93
|
+
*
|
|
94
|
+
* @param {any} node
|
|
95
|
+
* @returns {string | null}
|
|
96
|
+
*/
|
|
97
|
+
function literalString(node) {
|
|
98
|
+
if (!node) return null;
|
|
99
|
+
if (node.type === "Literal" && typeof node.value === "string") return node.value;
|
|
100
|
+
if (node.type === "TemplateLiteral" && node.expressions.length === 0) {
|
|
101
|
+
return node.quasis[0].value.cooked ?? null;
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Line (1-based) of a controller call in the author's `.svelte` source. Compiled output has
|
|
108
|
+
* other line numbers, so the call is searched by text: the first `label(<needle>` for a
|
|
109
|
+
* literal name, else the first `label(` whose argument is not a string.
|
|
110
|
+
*
|
|
111
|
+
* @param {string} original
|
|
112
|
+
* @param {string} label
|
|
113
|
+
* @param {string} needle
|
|
114
|
+
* @param {boolean} literal
|
|
115
|
+
* @returns {number | null}
|
|
116
|
+
*/
|
|
117
|
+
export function locateCall(original, label, needle, literal) {
|
|
118
|
+
const escaped = needle.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
119
|
+
const exact = literal
|
|
120
|
+
? new RegExp(`\\b${label}\\(\\s*['"\`]${escaped}['"\`]`)
|
|
121
|
+
: new RegExp(`\\b${label}\\(\\s*${escaped}`);
|
|
122
|
+
let match = exact.exec(original);
|
|
123
|
+
|
|
124
|
+
if (!match && !literal) {
|
|
125
|
+
const loose = new RegExp(`\\b${label}\\((?!\\s*['"\`])`);
|
|
126
|
+
match = loose.exec(original);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return match ? lineOf(original, match.index) : null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Stamps `{ version }` into the controller calls of one compiled view module.
|
|
134
|
+
*
|
|
135
|
+
* @param {object} input
|
|
136
|
+
* @param {string} input.code compiled JavaScript of the view
|
|
137
|
+
* @param {any} input.ast its ESTree program
|
|
138
|
+
* @param {string} input.viewFile display name of the view file, e.g. `ProductCard.svelte`
|
|
139
|
+
* @param {string} input.source text the AST offsets refer to (the same as `code`)
|
|
140
|
+
* @param {string} input.namespace own namespace
|
|
141
|
+
* @param {Map<string, number>} input.versions short controller name -> version
|
|
142
|
+
* @param {(label: string, needle: string, literal: boolean) => number | null} [input.locate] line of a call in the author's source (compiled lines mean nothing to the author); default: the line in `code`
|
|
143
|
+
* @returns {{ code: string, map: any, used: Record<string, number>, errors: string[], warnings: string[] } | null} null when the view has no controller calls
|
|
144
|
+
*/
|
|
145
|
+
export function stampController({ code, ast, viewFile, source, namespace, versions, locate }) {
|
|
146
|
+
/** @type {Set<string>} */
|
|
147
|
+
const pluginLocals = new Set();
|
|
148
|
+
/** @type {Set<string>} */
|
|
149
|
+
const useControllerLocals = new Set();
|
|
150
|
+
|
|
151
|
+
walk(ast, (node) => {
|
|
152
|
+
if (node.type !== "ImportDeclaration" || node.source?.value !== CONTROLLERS_MODULE) return;
|
|
153
|
+
|
|
154
|
+
for (const specifier of node.specifiers) {
|
|
155
|
+
if (specifier.type !== "ImportSpecifier") continue;
|
|
156
|
+
|
|
157
|
+
const imported = specifier.imported.name ?? specifier.imported.value;
|
|
158
|
+
|
|
159
|
+
if (imported === "plugin") pluginLocals.add(specifier.local.name);
|
|
160
|
+
else if (imported === "useController") useControllerLocals.add(specifier.local.name);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
if (pluginLocals.size === 0 && useControllerLocals.size === 0) return null;
|
|
165
|
+
|
|
166
|
+
/** @param {any} node */
|
|
167
|
+
const isPluginCall = (node) =>
|
|
168
|
+
node?.type === "CallExpression" &&
|
|
169
|
+
node.callee.type === "Identifier" &&
|
|
170
|
+
pluginLocals.has(node.callee.name);
|
|
171
|
+
|
|
172
|
+
/** @type {Map<string, string | null>} variable -> namespace literal of the plugin(...) it holds */
|
|
173
|
+
const pluginVars = new Map();
|
|
174
|
+
/** @type {Map<string, string | null>} destructured method local -> namespace literal */
|
|
175
|
+
const methodLocals = new Map();
|
|
176
|
+
|
|
177
|
+
walk(ast, (node) => {
|
|
178
|
+
if (node.type !== "VariableDeclarator" || !isPluginCall(node.init)) return;
|
|
179
|
+
|
|
180
|
+
const ns = literalString(node.init.arguments[0]);
|
|
181
|
+
|
|
182
|
+
if (node.id.type === "Identifier") {
|
|
183
|
+
pluginVars.set(node.id.name, ns);
|
|
184
|
+
} else if (node.id.type === "ObjectPattern") {
|
|
185
|
+
for (const property of node.id.properties) {
|
|
186
|
+
if (
|
|
187
|
+
property.type === "Property" &&
|
|
188
|
+
!property.computed &&
|
|
189
|
+
METHODS.has(property.key.name ?? property.key.value) &&
|
|
190
|
+
property.value.type === "Identifier"
|
|
191
|
+
) {
|
|
192
|
+
methodLocals.set(property.value.name, ns);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const s = new MagicString(code);
|
|
199
|
+
/** @type {Record<string, number>} */
|
|
200
|
+
const used = {};
|
|
201
|
+
/** @type {string[]} */
|
|
202
|
+
const errors = [];
|
|
203
|
+
/** @type {string[]} */
|
|
204
|
+
const warnings = [];
|
|
205
|
+
let sawCall = false;
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* @param {any} node
|
|
209
|
+
* @param {string} label
|
|
210
|
+
* @param {string} needle
|
|
211
|
+
* @param {boolean} literal
|
|
212
|
+
*/
|
|
213
|
+
const at = (node, label, needle, literal) => {
|
|
214
|
+
const line = locate ? locate(label, needle, literal) : lineOf(source, node.start);
|
|
215
|
+
return line === null ? viewFile : `${viewFile}:${line}`;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
walk(ast, (node) => {
|
|
219
|
+
if (node.type !== "CallExpression") return;
|
|
220
|
+
|
|
221
|
+
const callee = node.callee;
|
|
222
|
+
/** @type {{ label: string, ns: string | null | undefined } | null} */
|
|
223
|
+
let hit = null;
|
|
224
|
+
let fullName = false;
|
|
225
|
+
|
|
226
|
+
if (
|
|
227
|
+
callee.type === "MemberExpression" &&
|
|
228
|
+
!callee.computed &&
|
|
229
|
+
callee.property.type === "Identifier" &&
|
|
230
|
+
METHODS.has(callee.property.name)
|
|
231
|
+
) {
|
|
232
|
+
if (isPluginCall(callee.object)) {
|
|
233
|
+
hit = { label: callee.property.name, ns: literalString(callee.object.arguments[0]) };
|
|
234
|
+
} else if (callee.object.type === "Identifier" && pluginVars.has(callee.object.name)) {
|
|
235
|
+
hit = { label: callee.property.name, ns: pluginVars.get(callee.object.name) };
|
|
236
|
+
}
|
|
237
|
+
} else if (callee.type === "Identifier" && methodLocals.has(callee.name)) {
|
|
238
|
+
hit = { label: callee.name, ns: methodLocals.get(callee.name) };
|
|
239
|
+
} else if (callee.type === "Identifier" && useControllerLocals.has(callee.name)) {
|
|
240
|
+
hit = { label: callee.name, ns: undefined };
|
|
241
|
+
fullName = true;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (!hit) return;
|
|
245
|
+
|
|
246
|
+
sawCall = true;
|
|
247
|
+
|
|
248
|
+
const [nameArg, optionsArg] = node.arguments;
|
|
249
|
+
const name = literalString(nameArg);
|
|
250
|
+
|
|
251
|
+
if (name === null) {
|
|
252
|
+
const written = nameArg ? source.slice(nameArg.start, nameArg.end) : "";
|
|
253
|
+
errors.push(
|
|
254
|
+
`${at(node, hit.label, written, false)} ${hit.label}(${written}) — controller names must be string literals so the build can stamp their version`,
|
|
255
|
+
);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
let own = name;
|
|
260
|
+
|
|
261
|
+
if (fullName) {
|
|
262
|
+
const slash = name.indexOf("/");
|
|
263
|
+
|
|
264
|
+
if (slash < 0) return;
|
|
265
|
+
if (name.slice(0, slash) !== namespace) return;
|
|
266
|
+
|
|
267
|
+
own = name.slice(slash + 1);
|
|
268
|
+
} else if (hit.ns === null || (hit.ns !== undefined && hit.ns !== namespace)) {
|
|
269
|
+
// another plugin's controller, or a namespace the build cannot read: not ours to stamp
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const version = versions.get(own);
|
|
274
|
+
|
|
275
|
+
if (version === undefined) {
|
|
276
|
+
warnings.push(
|
|
277
|
+
`${at(node, hit.label, name, true)} ${hit.label}('${name}') — no controller named ${own} in the controllers folder, so no version is stamped`,
|
|
278
|
+
);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
used[`${namespace}/${own}`] = version;
|
|
283
|
+
|
|
284
|
+
if (!optionsArg) {
|
|
285
|
+
s.appendLeft(nameArg.end, `, { version: ${version} }`);
|
|
286
|
+
} else if (optionsArg.type === "ObjectExpression") {
|
|
287
|
+
const hasVersion = optionsArg.properties.some(
|
|
288
|
+
(property) =>
|
|
289
|
+
property.type === "Property" &&
|
|
290
|
+
!property.computed &&
|
|
291
|
+
(property.key.name ?? property.key.value) === "version",
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
if (hasVersion) return;
|
|
295
|
+
|
|
296
|
+
const last = optionsArg.properties[optionsArg.properties.length - 1];
|
|
297
|
+
|
|
298
|
+
if (last) s.appendLeft(last.end, `, version: ${version}`);
|
|
299
|
+
else s.appendLeft(optionsArg.start + 1, ` version: ${version} `);
|
|
300
|
+
} else {
|
|
301
|
+
s.appendLeft(optionsArg.start, "{ ...(");
|
|
302
|
+
s.appendRight(optionsArg.end, `), version: ${version} }`);
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
if (!sawCall) return null;
|
|
307
|
+
|
|
308
|
+
return {
|
|
309
|
+
code: s.toString(),
|
|
310
|
+
map: s.generateMap({ hires: true }),
|
|
311
|
+
used,
|
|
312
|
+
errors,
|
|
313
|
+
warnings,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Stamps { version: N } into literal controller calls of compiled views (doc 02 section 2).
|
|
319
|
+
*
|
|
320
|
+
* @param {StampOptions} [options]
|
|
321
|
+
* @returns {import('rollup').Plugin & { api: { readonly usage: Map<string, Record<string, number>>, readonly versions: Map<string, number> } }}
|
|
322
|
+
*/
|
|
323
|
+
export function panoStamp(options = {}) {
|
|
324
|
+
/** @type {import('./rules.js').KitProject | null} */
|
|
325
|
+
let project = null;
|
|
326
|
+
/** @type {Map<string, number>} */
|
|
327
|
+
let versions = new Map();
|
|
328
|
+
/** @type {Map<string, Record<string, number>>} */
|
|
329
|
+
const usage = new Map();
|
|
330
|
+
|
|
331
|
+
const api = {
|
|
332
|
+
get usage() {
|
|
333
|
+
return usage;
|
|
334
|
+
},
|
|
335
|
+
get versions() {
|
|
336
|
+
return versions;
|
|
337
|
+
},
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
/** @param {string} name */
|
|
341
|
+
const shortName = (name) => (name.includes("/") ? name.slice(name.indexOf("/") + 1) : name);
|
|
342
|
+
|
|
343
|
+
return {
|
|
344
|
+
name: "pano-stamp",
|
|
345
|
+
api,
|
|
346
|
+
|
|
347
|
+
async buildStart() {
|
|
348
|
+
project = await resolveProject(options);
|
|
349
|
+
|
|
350
|
+
const source = options.controllers ?? (await readControllerVersions(project));
|
|
351
|
+
|
|
352
|
+
versions = new Map(Object.entries(source).map(([name, version]) => [shortName(name), version]));
|
|
353
|
+
|
|
354
|
+
if (options.context) options.context.controllerUsage = usage;
|
|
355
|
+
},
|
|
356
|
+
|
|
357
|
+
transform: {
|
|
358
|
+
order: "post",
|
|
359
|
+
handler(code, id) {
|
|
360
|
+
if (!project || !id.endsWith(".svelte")) return null;
|
|
361
|
+
|
|
362
|
+
const file = path.resolve(id);
|
|
363
|
+
|
|
364
|
+
if (!project.viewDirs.some((dir) => file.startsWith(dir + path.sep))) return null;
|
|
365
|
+
|
|
366
|
+
const viewFile = path.basename(file);
|
|
367
|
+
let ast;
|
|
368
|
+
|
|
369
|
+
try {
|
|
370
|
+
ast = this.parse(code);
|
|
371
|
+
} catch (error) {
|
|
372
|
+
this.error(
|
|
373
|
+
`${viewFile}: pano-stamp needs compiled JavaScript but found something else (${/** @type {Error} */ (error).message}); the Svelte compiler must run before it`,
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
let original = "";
|
|
378
|
+
|
|
379
|
+
try {
|
|
380
|
+
original = fs.readFileSync(file, "utf8");
|
|
381
|
+
} catch {
|
|
382
|
+
// virtual or generated view: lines fall back to the compiled output
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const result = stampController({
|
|
386
|
+
code,
|
|
387
|
+
ast,
|
|
388
|
+
viewFile,
|
|
389
|
+
source: code,
|
|
390
|
+
namespace: project.namespace,
|
|
391
|
+
versions,
|
|
392
|
+
locate: original ? (label, needle, literal) => locateCall(original, label, needle, literal) : undefined,
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
usage.set(path.basename(file, ".svelte"), result?.used ?? {});
|
|
396
|
+
|
|
397
|
+
if (!result) return null;
|
|
398
|
+
|
|
399
|
+
for (const warning of result.warnings) this.warn(warning);
|
|
400
|
+
|
|
401
|
+
if (result.errors.length === 1) this.error(result.errors[0]);
|
|
402
|
+
if (result.errors.length > 1) {
|
|
403
|
+
this.error(`${result.errors.length} controller calls cannot be stamped:\n ${result.errors.join("\n ")}`);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
return { code: result.code, map: result.map };
|
|
407
|
+
},
|
|
408
|
+
},
|
|
409
|
+
};
|
|
410
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { resetControllersBuild, resolvePaths, writeIfChanged } from "./controllers.js";
|
|
5
|
+
import { enumerateControllers, resetEnumeration, typeNames } from "./meta.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* JSDoc typedef module (doc 02 section 5.3, decision 66).
|
|
9
|
+
*
|
|
10
|
+
* `src/theme/controllers/types.js` holds only `@typedef` comments and `export {};`. It is copied
|
|
11
|
+
* verbatim to `contract/controllers.types.js`, then a generated block is appended:
|
|
12
|
+
*
|
|
13
|
+
* - the `@template` typedefs `Controller`, `ControllerHost` (taken from the controller core of this kit,
|
|
14
|
+
* so they cannot drift) and `ApiResult`, inlined, because the file imports nothing;
|
|
15
|
+
* - a fallback `<Name>State` / `<Name>Actions` from the enumerated keys for each controller lacking them;
|
|
16
|
+
* - `@typedef {{ 'market/cart': Controller<CartState, CartActions>, ... }} Controllers`.
|
|
17
|
+
*
|
|
18
|
+
* No `types.js` = fallbacks only.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const CORE_FILE = fileURLToPath(new URL("../controller/index.js", import.meta.url));
|
|
22
|
+
|
|
23
|
+
export const GENERATED_MARKER =
|
|
24
|
+
"// ---- generated by @panomc/plugin-kit, do not edit below this line ----";
|
|
25
|
+
|
|
26
|
+
const API_RESULT = `/**
|
|
27
|
+
* What \`host.request\` resolves, and what an action returns when it can fail: the parsed body, or the error envelope.
|
|
28
|
+
* @template [T=object]
|
|
29
|
+
* @typedef {T | { error: { code: string, message?: string, details?: object, fields?: Record<string, string> } }} ApiResult
|
|
30
|
+
*/`;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Replaces every comment by blanks (newlines kept) so what is left is code.
|
|
34
|
+
*
|
|
35
|
+
* @param {string} text
|
|
36
|
+
* @returns {string}
|
|
37
|
+
*/
|
|
38
|
+
function blankComments(text) {
|
|
39
|
+
return text.replace(/\/\*[\s\S]*?\*\/|\/\/[^\n]*/g, (comment) => comment.replace(/[^\n]/g, " "));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Checks that a types.js holds nothing but comments and `export {}`.
|
|
44
|
+
*
|
|
45
|
+
* @param {string} text file content
|
|
46
|
+
* @param {string} [file] shown in the error
|
|
47
|
+
* @throws {Error} naming the first statement and its line
|
|
48
|
+
*/
|
|
49
|
+
export function validateTypesSource(text, file = "src/theme/controllers/types.js") {
|
|
50
|
+
const code = blankComments(text);
|
|
51
|
+
const leftover = code.replace(/^\s*export\s*\{\s*\}\s*;?[ \t]*$/gm, (match) => match.replace(/[^\n]/g, " "));
|
|
52
|
+
const match = /\S/.exec(leftover);
|
|
53
|
+
|
|
54
|
+
if (!match) return;
|
|
55
|
+
|
|
56
|
+
const line = leftover.slice(0, match.index).split("\n").length;
|
|
57
|
+
const snippet = leftover.split("\n")[line - 1].trim().slice(0, 60);
|
|
58
|
+
|
|
59
|
+
throw new Error(
|
|
60
|
+
`[pano-plugin] ${file}:${line} has code (${snippet}) — types.js may hold only @typedef comments and export {}; move the code to a controller or a view helper`,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Names declared by `@typedef` / `@callback` comments.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} text
|
|
68
|
+
* @returns {Set<string>}
|
|
69
|
+
*/
|
|
70
|
+
export function declaredTypeNames(text) {
|
|
71
|
+
const names = new Set();
|
|
72
|
+
const pattern = /@(?:typedef|callback)\b/g;
|
|
73
|
+
let match;
|
|
74
|
+
|
|
75
|
+
while ((match = pattern.exec(text))) {
|
|
76
|
+
let index = match.index + match[0].length;
|
|
77
|
+
|
|
78
|
+
while (/\s|\*/.test(text[index] ?? "")) index += 1;
|
|
79
|
+
|
|
80
|
+
if (text[index] === "{") {
|
|
81
|
+
let depth = 0;
|
|
82
|
+
|
|
83
|
+
for (; index < text.length; index += 1) {
|
|
84
|
+
if (text[index] === "{") depth += 1;
|
|
85
|
+
else if (text[index] === "}") {
|
|
86
|
+
depth -= 1;
|
|
87
|
+
|
|
88
|
+
if (depth === 0) {
|
|
89
|
+
index += 1;
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
while (/\s|\*/.test(text[index] ?? "")) index += 1;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const name = /^[A-Za-z_$][\w$]*/.exec(text.slice(index));
|
|
99
|
+
|
|
100
|
+
if (name) names.add(name[0]);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return names;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @param {string} key
|
|
108
|
+
* @returns {string}
|
|
109
|
+
*/
|
|
110
|
+
function property(key) {
|
|
111
|
+
return /^[A-Za-z_$][\w$]*$/.test(key) ? key : JSON.stringify(key);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @param {string[]} members already formatted `key: type` members
|
|
116
|
+
* @returns {string}
|
|
117
|
+
*/
|
|
118
|
+
function objectType(members) {
|
|
119
|
+
return members.length === 0 ? "{}" : `{ ${members.join(", ")} }`;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The typedef comments of the controller core that the generated block inlines.
|
|
124
|
+
*
|
|
125
|
+
* @returns {string[]} comment blocks: Controller, ControllerHost
|
|
126
|
+
*/
|
|
127
|
+
function coreTypedefs() {
|
|
128
|
+
const source = fs.readFileSync(CORE_FILE, "utf8");
|
|
129
|
+
const blocks = source.match(/\/\*\*[\s\S]*?\*\//g) ?? [];
|
|
130
|
+
const wanted = ["Controller", "ControllerHost"];
|
|
131
|
+
|
|
132
|
+
return wanted.map((name) => {
|
|
133
|
+
const block = blocks.find((item) => new RegExp(`@typedef \\{Object\\} ${name}\\s`).test(item));
|
|
134
|
+
|
|
135
|
+
if (!block) {
|
|
136
|
+
throw new Error(`[pano-plugin] the controller core has no @typedef ${name}; the kit is damaged, reinstall @panomc/plugin-kit`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return block;
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Builds the content of `contract/controllers.types.js`.
|
|
145
|
+
*
|
|
146
|
+
* @param {{ namespace: string, controllers: Record<string, { state: string[], actions: string[] }>, authored?: string | null }} input
|
|
147
|
+
* `authored` = content of the author's types.js (already validated), null when there is none
|
|
148
|
+
* @returns {string}
|
|
149
|
+
*/
|
|
150
|
+
export function buildTypesModule({ namespace, controllers, authored = null }) {
|
|
151
|
+
const names = Object.keys(controllers).sort();
|
|
152
|
+
const known = declaredTypeNames(authored ?? "");
|
|
153
|
+
const hasExport = authored !== null && /^\s*export\s*\{\s*\}\s*;?/m.test(blankComments(authored));
|
|
154
|
+
const parts = [];
|
|
155
|
+
|
|
156
|
+
if (authored !== null) parts.push(authored.replace(/\s+$/, "") + "\n");
|
|
157
|
+
|
|
158
|
+
const [controllerDef, hostDef] = coreTypedefs();
|
|
159
|
+
const generated = [GENERATED_MARKER, controllerDef, hostDef, API_RESULT];
|
|
160
|
+
|
|
161
|
+
for (const name of names) {
|
|
162
|
+
const { state, actions } = typeNames(name);
|
|
163
|
+
const info = controllers[name];
|
|
164
|
+
|
|
165
|
+
if (!known.has(state)) {
|
|
166
|
+
generated.push(
|
|
167
|
+
`/**\n * State of ${namespace}/${name}, generated from the keys of its initial state (declare ${state} in types.js to type it).\n * @typedef {${objectType(info.state.map((key) => `${property(key)}: any`))}} ${state}\n */`,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (!known.has(actions)) {
|
|
172
|
+
generated.push(
|
|
173
|
+
`/**\n * Actions of ${namespace}/${name}, generated from its action names (declare ${actions} in types.js to type them).\n * @typedef {${objectType(info.actions.map((key) => `${property(key)}: (...args: any[]) => any`))}} ${actions}\n */`,
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const members = names.map((name) => {
|
|
179
|
+
const { state, actions } = typeNames(name);
|
|
180
|
+
|
|
181
|
+
return `'${namespace}/${name}': Controller<${state}, ${actions}>`;
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
generated.push(
|
|
185
|
+
`/**\n * Every public controller of ${namespace}, by full name.\n * @typedef {${objectType(members)}} Controllers\n */`,
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
parts.push(generated.join("\n\n") + "\n");
|
|
189
|
+
|
|
190
|
+
if (!hasExport) parts.push("export {};\n");
|
|
191
|
+
|
|
192
|
+
return parts.join("\n");
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @typedef {object} TypesOptions
|
|
197
|
+
* @property {string} [root]
|
|
198
|
+
* @property {string} [dir] controllers folder, default `src/theme/controllers`
|
|
199
|
+
* @property {string} [outDir] package folder, default `src/main/resources/plugin-ui`
|
|
200
|
+
* @property {string} [pluginId] needed with `namespace` when this plugin runs without panoMeta
|
|
201
|
+
* @property {string} [namespace]
|
|
202
|
+
*/
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Copies src/theme/controllers/types.js to contract/controllers.types.js (only @typedef comments
|
|
206
|
+
* and `export {}` allowed) and appends the generated block and the fallback typedefs. List it in the
|
|
207
|
+
* same configuration as `panoControllers` and `panoMeta`; it writes in `writeBundle`.
|
|
208
|
+
*
|
|
209
|
+
* @param {TypesOptions} [options]
|
|
210
|
+
* @returns {import('rollup').Plugin}
|
|
211
|
+
*/
|
|
212
|
+
export function panoTypes(options = {}) {
|
|
213
|
+
const { dir, outDir } = resolvePaths(options);
|
|
214
|
+
const typesFile = path.join(dir, "types.js");
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
name: "pano-types",
|
|
218
|
+
buildStart() {
|
|
219
|
+
resetControllersBuild(options);
|
|
220
|
+
resetEnumeration(/** @type {any} */ (options));
|
|
221
|
+
|
|
222
|
+
if (fs.existsSync(typesFile)) this.addWatchFile(typesFile);
|
|
223
|
+
},
|
|
224
|
+
async writeBundle() {
|
|
225
|
+
const output = path.join(outDir, "contract", "controllers.types.js");
|
|
226
|
+
const authoredText = fs.existsSync(typesFile) ? fs.readFileSync(typesFile, "utf8") : null;
|
|
227
|
+
|
|
228
|
+
if (authoredText !== null) validateTypesSource(authoredText, path.relative(resolvePaths(options).root, typesFile).split(path.sep).join("/"));
|
|
229
|
+
|
|
230
|
+
const enumeration = await enumerateControllers(/** @type {any} */ (options));
|
|
231
|
+
|
|
232
|
+
if (Object.keys(enumeration.controllers).length === 0 && authoredText === null) {
|
|
233
|
+
fs.rmSync(output, { force: true });
|
|
234
|
+
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
writeIfChanged(
|
|
239
|
+
output,
|
|
240
|
+
buildTypesModule({
|
|
241
|
+
namespace: enumeration.namespace,
|
|
242
|
+
controllers: enumeration.controllers,
|
|
243
|
+
authored: authoredText,
|
|
244
|
+
}),
|
|
245
|
+
);
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
}
|