@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.
Files changed (89) hide show
  1. package/assets/pano-bootstrap.css +17903 -0
  2. package/bin/__tests__/new/e2e.test.js +111 -0
  3. package/bin/__tests__/new/helpers.js +76 -0
  4. package/bin/__tests__/new/new.test.js +472 -0
  5. package/bin/new.js +530 -0
  6. package/bin/pano-plugin.js +162 -0
  7. package/bin/templates/plugin/README.md +30 -0
  8. package/bin/templates/plugin/_gitignore +15 -0
  9. package/bin/templates/plugin/build.gradle.kts +389 -0
  10. package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.jar +0 -0
  11. package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.properties +7 -0
  12. package/bin/templates/plugin/gradle.properties +55 -0
  13. package/bin/templates/plugin/gradlew +249 -0
  14. package/bin/templates/plugin/gradlew.bat +92 -0
  15. package/bin/templates/plugin/package.json.tpl +14 -0
  16. package/bin/templates/plugin/rollup.config.js +2 -0
  17. package/bin/templates/plugin/settings.gradle.kts +5 -0
  18. package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/__CLASS__Plugin.kt +50 -0
  19. package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/frontend/ExampleFallbackPage.kt +28 -0
  20. package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/routes/GetHelloAPI.kt +19 -0
  21. package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/LicenseGuard.kt +30 -0
  22. package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/PluginLicenseClient.kt +144 -0
  23. package/bin/templates/plugin/src/main/resources/config.conf +1 -0
  24. package/bin/templates/plugin/src/main/resources/frontend-targets.json +1 -0
  25. package/bin/templates/plugin/src/main/resources/locales/en-US.json +3 -0
  26. package/bin/templates/plugin/src/main/resources/logo.png +0 -0
  27. package/bin/templates/plugin/src/theme/views/HelloPage.svelte +26 -0
  28. package/bin/ui.js +96 -0
  29. package/package.json +48 -4
  30. package/src/config.js +191 -0
  31. package/src/controller/__tests__/controller.test.js +647 -0
  32. package/src/controller/index.js +604 -0
  33. package/src/dev.js +208 -0
  34. package/src/index.js +22 -0
  35. package/src/rollup/__tests__/controllers/controllers.test.js +418 -0
  36. package/src/rollup/__tests__/controllers/fixture.js +143 -0
  37. package/src/rollup/__tests__/controllers/golden/controllers.json +34 -0
  38. package/src/rollup/__tests__/controllers/golden/controllers.types.js +69 -0
  39. package/src/rollup/__tests__/controllers/golden/pano-plugin.json +16 -0
  40. package/src/rollup/__tests__/controllers/golden/pano-plugin.lock.json +27 -0
  41. package/src/rollup/__tests__/controllers/golden/pano-plugin.with-views.json +54 -0
  42. package/src/rollup/__tests__/preset/preset.test.js +418 -0
  43. package/src/rollup/__tests__/rules/_build.js +39 -0
  44. package/src/rollup/__tests__/rules/_fixture.js +83 -0
  45. package/src/rollup/__tests__/rules/helpers.test.js +148 -0
  46. package/src/rollup/__tests__/rules/rules.test.js +259 -0
  47. package/src/rollup/__tests__/rules/stamp.test.js +113 -0
  48. package/src/rollup/__tests__/samples/samples.test.js +481 -0
  49. package/src/rollup/__tests__/seams/config.test.js +78 -0
  50. package/src/rollup/__tests__/seams/lock.test.js +124 -0
  51. package/src/rollup/__tests__/seams/sdk-seams.test.js +93 -0
  52. package/src/rollup/__tests__/seams/stubs.test.js +52 -0
  53. package/src/rollup/__tests__/views/helpers.js +82 -0
  54. package/src/rollup/__tests__/views/views.test.js +522 -0
  55. package/src/rollup/__tests__/widgets/helpers.js +117 -0
  56. package/src/rollup/__tests__/widgets/widgets.test.js +422 -0
  57. package/src/rollup/cli-build.js +90 -0
  58. package/src/rollup/cli-check.js +280 -0
  59. package/src/rollup/cli-classes.js +75 -0
  60. package/src/rollup/client-gen.js +67 -0
  61. package/src/rollup/controllers.js +434 -0
  62. package/src/rollup/entry.js +250 -0
  63. package/src/rollup/helpers.js +193 -0
  64. package/src/rollup/index.js +601 -0
  65. package/src/rollup/integrate.js +205 -0
  66. package/src/rollup/lock.js +230 -0
  67. package/src/rollup/meta.js +479 -0
  68. package/src/rollup/rules.js +950 -0
  69. package/src/rollup/samples.js +667 -0
  70. package/src/rollup/stamp.js +410 -0
  71. package/src/rollup/types.js +248 -0
  72. package/src/rollup/views.js +1205 -0
  73. package/src/rollup/widgets.js +1000 -0
  74. package/src/styles/__tests__/fallback/__snapshots__/build-fallback-css.test.js.snap +173 -0
  75. package/src/styles/__tests__/fallback/build-fallback-css.test.js +526 -0
  76. package/src/styles/__tests__/fallback/fixtures/base.css +247 -0
  77. package/src/styles/__tests__/fallback/plugins.test.js +301 -0
  78. package/src/styles/__tests__/lint/check-styles.test.js +423 -0
  79. package/src/styles/__tests__/lint/fixtures/ProductCard.golden.svelte +13 -0
  80. package/src/styles/__tests__/lint/fixtures/ProductCard.in.svelte +13 -0
  81. package/src/styles/__tests__/lint/semantic-classes.test.js +364 -0
  82. package/src/styles/bootstrap-classes.js +155 -0
  83. package/src/styles/check-styles.js +626 -0
  84. package/src/styles/fallback-css.js +1182 -0
  85. package/src/styles/plugin-css.js +237 -0
  86. package/src/styles/scan.js +206 -0
  87. package/src/styles/semantic-classes.js +436 -0
  88. package/src/styles/tokens.map.js +53 -0
  89. package/README.md +0 -3
@@ -0,0 +1,280 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { pathToFileURL } from "node:url";
5
+ import { fileURLToPath } from "node:url";
6
+ import { loadConfig } from "../config.js";
7
+ import { checkStyles } from "../styles/check-styles.js";
8
+ import { analyzeProject, resolveProject, viewImportsMode } from "./rules.js";
9
+ import { listControllerFiles } from "./controllers.js";
10
+ import { enumerateControllers, lockSectionOf } from "./meta.js";
11
+ import { diffSection, readLock } from "./lock.js";
12
+ import { buildLockSection, scanViews } from "./views.js";
13
+ import { checkSvelteVersion, findPackageDir } from "./index.js";
14
+ import { createClassPolicy } from "./integrate.js";
15
+
16
+ /**
17
+ * `pano-plugin check [--strict] [--styles badge]` (doc 02 section 8).
18
+ *
19
+ * Runs, without writing anything into the plugin folder:
20
+ * - the build rules V1-V3 and V5 (migration mode turns V1 and V5 into warnings),
21
+ * - the lock comparison for views and controllers (a promise removed without a raised number),
22
+ * - the style lint of doc 03 (`core`, or `badge` with `--styles badge`),
23
+ * - the Svelte version pin of the sdk,
24
+ * - `pano-api check-paths` of the sdk (skipped, with a note, when no sdk is found),
25
+ * - the notice for an old hand-copied `rollup.config.js`.
26
+ *
27
+ * `--strict` makes warnings fail the check as well. The last line is always
28
+ * `public (readable): N views, M helpers · closed: K controllers`.
29
+ */
30
+
31
+ /**
32
+ * @typedef {object} CheckOptions
33
+ * @property {string} [root] plugin root, default the current directory
34
+ * @property {boolean} [strict] warnings fail the check
35
+ * @property {'core' | 'badge'} [styles] lint level, default `core`
36
+ * @property {(line: string) => void} [out] stdout
37
+ * @property {(line: string) => void} [err] stderr
38
+ */
39
+
40
+ /**
41
+ * The sdk folder: `PANO_SDK_DIR`, the installed `@panomc/sdk`, or, in the theme-core workspace, the
42
+ * sibling package of the kit.
43
+ *
44
+ * @param {string} root
45
+ * @param {string | null} configured
46
+ * @returns {string | null}
47
+ */
48
+ export function findSdkDir(root, configured) {
49
+ if (configured) return configured;
50
+
51
+ const installed = findPackageDir(root, "@panomc/sdk");
52
+
53
+ if (installed) return installed;
54
+
55
+ const sibling = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../sdk");
56
+
57
+ try {
58
+ const pkg = JSON.parse(fs.readFileSync(path.join(sibling, "package.json"), "utf8"));
59
+
60
+ if (pkg.name === "@panomc/sdk") return sibling;
61
+ } catch {
62
+ // not in the workspace
63
+ }
64
+
65
+ return null;
66
+ }
67
+
68
+ /**
69
+ * Runs `fn` and returns what it printed with `console.warn`.
70
+ *
71
+ * @template T
72
+ * @param {() => T} fn
73
+ * @returns {{ result?: T, error?: Error, warnings: string[] }}
74
+ */
75
+ function captureWarnings(fn) {
76
+ const original = console.warn;
77
+ /** @type {string[]} */
78
+ const warnings = [];
79
+
80
+ console.warn = (...args) => {
81
+ warnings.push(args.join(" ").replace(/^\[pano\] WARNING: /, ""));
82
+ };
83
+
84
+ try {
85
+ return { result: fn(), warnings };
86
+ } catch (error) {
87
+ return { error: /** @type {Error} */ (error), warnings };
88
+ } finally {
89
+ console.warn = original;
90
+ }
91
+ }
92
+
93
+ /**
94
+ * @param {CheckOptions} [options]
95
+ * @returns {Promise<number>} the exit code: 0 passed, 1 problems found
96
+ */
97
+ export async function runCheck(options = {}) {
98
+ const root = path.resolve(options.root ?? process.cwd());
99
+ const out = options.out ?? ((line) => process.stdout.write(`${line}\n`));
100
+ const err = options.err ?? ((line) => process.stderr.write(`${line}\n`));
101
+ const level = options.styles ?? "core";
102
+ /** @type {string[]} */
103
+ const errors = [];
104
+ /** @type {string[]} */
105
+ const warnings = [];
106
+ /** @type {string[]} */
107
+ const notes = [];
108
+ let counts = { views: 0, helpers: 0, controllers: 0 };
109
+
110
+ /** @param {string} message */
111
+ const fail = (message) => errors.push(String(message).replace(/^\[pano-plugin\] /, ""));
112
+
113
+ let config;
114
+
115
+ try {
116
+ config = await loadConfig(root);
117
+ } catch (error) {
118
+ fail(/** @type {Error} */ (error).message);
119
+ }
120
+
121
+ if (config) {
122
+ const sdkDir = findSdkDir(root, config.sdkDir);
123
+
124
+ // ---- Svelte pin ---------------------------------------------------------------------------
125
+ {
126
+ const { error, warnings: found } = captureWarnings(() => checkSvelteVersion({ root, sdkDir }));
127
+
128
+ warnings.push(...found);
129
+ if (error) fail(error.message);
130
+ }
131
+
132
+ // ---- V1-V3, V5 -----------------------------------------------------------------------------
133
+ const project = await resolveProject({
134
+ root,
135
+ viewDirs: config.viewDirs,
136
+ namespace: config.namespace,
137
+ });
138
+ const analysis = await analyzeProject(project);
139
+ const mode = viewImportsMode();
140
+
141
+ for (const violation of analysis.violations) {
142
+ if (mode === "warn" && (violation.rule === "V1" || violation.rule === "V5")) {
143
+ warnings.push(`${violation.message} (PANO_VIEW_IMPORTS=warn: migration mode, this will be an error)`);
144
+ } else {
145
+ fail(violation.message);
146
+ }
147
+ }
148
+
149
+ counts = {
150
+ views: analysis.views.size,
151
+ helpers: new Set([...analysis.helpers.values()].flat()).size,
152
+ controllers: listControllerFiles(project.controllersDir).length,
153
+ };
154
+
155
+ // ---- lock comparison -------------------------------------------------------------------------
156
+ /** @type {Awaited<ReturnType<typeof scanViews>> | null} */
157
+ let scan = null;
158
+
159
+ try {
160
+ scan = await scanViews({ dirs: config.viewDirs, namespace: config.namespace, pluginId: config.pluginId, root });
161
+ warnings.push(...scan.warnings);
162
+ } catch (error) {
163
+ fail(/** @type {Error} */ (error).message);
164
+ }
165
+
166
+ let lock = { format: 1 };
167
+
168
+ try {
169
+ lock = readLock(root);
170
+ } catch (error) {
171
+ fail(/** @type {Error} */ (error).message);
172
+ }
173
+
174
+ if (scan) {
175
+ const found = scan;
176
+ const policy = createClassPolicy({ root, ns: config.namespace, scan: () => found, styles: config.styles });
177
+ const { breaking } = diffSection(
178
+ "views",
179
+ lock.views,
180
+ buildLockSection(scan, lock.views, (info) => policy.infoOf(info).classes),
181
+ );
182
+
183
+ for (const message of breaking) fail(message);
184
+ }
185
+
186
+ if (counts.controllers > 0) {
187
+ const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "pano-check-"));
188
+
189
+ try {
190
+ const enumeration = await enumerateControllers({
191
+ root,
192
+ outDir: temporary,
193
+ pluginId: config.pluginId,
194
+ namespace: config.namespace,
195
+ });
196
+ const { breaking } = diffSection("controllers", lock.controllers, lockSectionOf(enumeration));
197
+
198
+ for (const message of breaking) fail(message);
199
+ } catch (error) {
200
+ fail(/** @type {Error} */ (error).message);
201
+ } finally {
202
+ fs.rmSync(temporary, { recursive: true, force: true });
203
+ }
204
+ }
205
+
206
+ // ---- style lint ------------------------------------------------------------------------------
207
+ try {
208
+ const findings = checkStyles({
209
+ root,
210
+ ns: config.namespace,
211
+ views: scan
212
+ ? [...scan.views.values()].map((info) => ({ name: info.name, file: info.rel }))
213
+ : [],
214
+ styleAttrAllow: config.styles.styleAttrAllow,
215
+ dynamicClassAllow: config.styles.dynamicClassAllow,
216
+ safelist: config.styles.safelist,
217
+ level,
218
+ });
219
+
220
+ for (const finding of findings) {
221
+ const text = `${finding.file}:${finding.line} [${finding.rule}] ${finding.message}`;
222
+
223
+ if (finding.level === "error") fail(text);
224
+ else warnings.push(text);
225
+ }
226
+ } catch (error) {
227
+ fail(/** @type {Error} */ (error).message);
228
+ }
229
+
230
+ // ---- API paths -----------------------------------------------------------------------------
231
+ const checkPathsFile = sdkDir ? path.join(sdkDir, "bin/api/check-paths.mjs") : null;
232
+
233
+ if (checkPathsFile && fs.existsSync(checkPathsFile)) {
234
+ try {
235
+ const { checkPaths } = await import(pathToFileURL(checkPathsFile).href);
236
+ const result = checkPaths({ root, pluginId: config.pluginId });
237
+
238
+ for (const message of result.errors) warnings.push(`pano-api check-paths: ${message}`);
239
+ for (const problem of result.problems) fail(`${problem} — fix the path (pano-api check-paths ${path.relative(process.cwd(), root) || "."} lists them all)`);
240
+ } catch (error) {
241
+ warnings.push(`pano-api check-paths could not run (${/** @type {Error} */ (error).message})`);
242
+ }
243
+ } else {
244
+ notes.push("pano-api check-paths skipped: no @panomc/sdk found (set PANO_SDK_DIR or install @panomc/sdk)");
245
+ }
246
+
247
+ // ---- old hand-copied rollup.config.js ---------------------------------------------------------
248
+ const rollupConfig = path.join(root, "rollup.config.js");
249
+
250
+ if (fs.existsSync(rollupConfig)) {
251
+ const text = fs.readFileSync(rollupConfig, "utf8");
252
+
253
+ if (!text.includes("@panomc/plugin-kit")) {
254
+ warnings.push(
255
+ "rollup.config.js is an old hand-copied config and gets none of the views, controllers or contract files — replace it with: import { panoPlugin } from '@panomc/plugin-kit/rollup'; export default panoPlugin();",
256
+ );
257
+ }
258
+ }
259
+ }
260
+
261
+ // ---- report ----------------------------------------------------------------------------------
262
+ const strict = options.strict === true;
263
+
264
+ for (const message of errors) err(`error: ${message}`);
265
+ for (const message of warnings) err(`${strict ? "error (--strict)" : "warning"}: ${message}`);
266
+ for (const message of notes) out(`note: ${message}`);
267
+
268
+ const failing = errors.length + (strict ? warnings.length : 0);
269
+
270
+ out(
271
+ failing === 0
272
+ ? `check passed (${warnings.length} warning${warnings.length === 1 ? "" : "s"})`
273
+ : `check failed: ${failing} problem${failing === 1 ? "" : "s"}`,
274
+ );
275
+ out(
276
+ `public (readable): ${counts.views} view${counts.views === 1 ? "" : "s"}, ${counts.helpers} helper${counts.helpers === 1 ? "" : "s"} · closed: ${counts.controllers} controller${counts.controllers === 1 ? "" : "s"}`,
277
+ );
278
+
279
+ return failing === 0 ? 0 : 1;
280
+ }
@@ -0,0 +1,75 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { loadConfig } from "../config.js";
4
+ import { addPartClasses, addRootClass } from "../styles/semantic-classes.js";
5
+ import { listFiles } from "./rules.js";
6
+
7
+ /**
8
+ * `pano-plugin classes [--fix]` (doc 03 section 1.2).
9
+ *
10
+ * Lists the semantic classes the sources are missing: the root class of every view and the part
11
+ * classes of elements with a Bootstrap component class. `--fix` writes them into the sources, so the
12
+ * root class is committed too (the `badge` lint wants it there). Idempotent: a second run lists nothing.
13
+ *
14
+ * @param {{ root?: string, fix?: boolean, out?: (line: string) => void, err?: (line: string) => void }} [options]
15
+ * @returns {Promise<number>} 0 when done (or nothing to add), 1 when the config or a source is broken
16
+ */
17
+ export async function runClasses(options = {}) {
18
+ const root = path.resolve(options.root ?? process.cwd());
19
+ const out = options.out ?? ((line) => process.stdout.write(`${line}\n`));
20
+ const err = options.err ?? ((line) => process.stderr.write(`${line}\n`));
21
+ let config;
22
+
23
+ try {
24
+ config = await loadConfig(root);
25
+ } catch (error) {
26
+ err(`error: ${String(/** @type {Error} */ (error).message).replace(/^\[pano-plugin\] /, "")}`);
27
+
28
+ return 1;
29
+ }
30
+
31
+ const dirs = config.viewDirs.map((dir) => path.resolve(root, dir));
32
+ const files = listFiles(dirs, (file) => file.endsWith(".svelte"));
33
+ let total = 0;
34
+ let failed = false;
35
+
36
+ for (const file of files) {
37
+ const view = path.basename(file, ".svelte");
38
+ const relative = path.relative(root, file).split(path.sep).join("/");
39
+ const source = fs.readFileSync(file, "utf8");
40
+
41
+ try {
42
+ const root_ = addRootClass(source, { ns: config.namespace, view });
43
+ const parts = addPartClasses(root_.code, { ns: config.namespace, view });
44
+
45
+ for (const message of root_.warnings) err(`warning: ${relative}: ${message}`);
46
+
47
+ const names = [
48
+ ...root_.added.map((name) => ({ line: 0, class: name })),
49
+ ...parts.added,
50
+ ];
51
+
52
+ for (const item of names) {
53
+ out(`${relative}${item.line ? `:${item.line}` : ""} + ${item.class}`);
54
+ }
55
+
56
+ total += names.length;
57
+
58
+ if (options.fix && parts.code !== source) fs.writeFileSync(file, parts.code);
59
+ } catch (error) {
60
+ failed = true;
61
+ err(`error: ${relative}: ${/** @type {Error} */ (error).message}`);
62
+ }
63
+ }
64
+
65
+ if (total === 0 && !failed) out("classes: nothing to add");
66
+ else if (!failed) {
67
+ out(
68
+ options.fix
69
+ ? `classes: ${total} class${total === 1 ? "" : "es"} written`
70
+ : `classes: ${total} class${total === 1 ? "" : "es"} to add — run pano-plugin classes --fix to write them`,
71
+ );
72
+ }
73
+
74
+ return failed ? 1 : 0;
75
+ }
@@ -0,0 +1,67 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+
4
+ /**
5
+ * Client generation hook run in the client build (doc 06 section 2.4).
6
+ *
7
+ * When the plugin repo has `api/openapi.json`, runs the `pano-client generate` engine into `<root>/client/`
8
+ * (index.js, types.js, operations.js, openapi.json) and writes `client/package.json` only when it is missing
9
+ * (name `pano-client-<ns>`). No snapshot: does nothing. Output is deterministic and rewritten only when it
10
+ * changed.
11
+ *
12
+ * @param {{ pluginId: string, namespace: string, root?: string, outDir?: string, side?: string }} options
13
+ * `root`: plugin root (default `process.cwd()`); `outDir`: the client folder (default `<root>/client`)
14
+ * @returns {import('rollup').Plugin}
15
+ */
16
+ export function panoClientGen(options) {
17
+ let done = false;
18
+
19
+ return {
20
+ name: "pano-client-gen",
21
+ async buildStart() {
22
+ if (done) return;
23
+ done = true;
24
+
25
+ const root = path.resolve(options.root ?? process.cwd());
26
+ const input = path.join(root, "api", "openapi.json");
27
+
28
+ if (!fs.existsSync(input)) return;
29
+
30
+ const out = path.resolve(root, "client");
31
+ const { generateClient } = await loadGenerator();
32
+ const gen = generateClient(fs.readFileSync(input, "utf8"));
33
+
34
+ for (const warning of gen.warnings) this.warn(`[pano-plugin] client: ${warning}`);
35
+
36
+ for (const [name, content] of Object.entries(gen.files)) writeIfChanged(path.join(out, name), content);
37
+
38
+ const manifest = path.join(out, "package.json");
39
+
40
+ if (!fs.existsSync(manifest)) {
41
+ const pkg = {
42
+ name: `pano-client-${options.namespace}`,
43
+ version: "0.0.0",
44
+ private: true,
45
+ type: "module",
46
+ description: `Generated Pano API client of the ${options.pluginId} plugin.`,
47
+ exports: { ".": "./index.js", "./types": "./types.js", "./operations": "./operations.js", "./openapi.json": "./openapi.json" },
48
+ files: ["*.js", "openapi.json"],
49
+ };
50
+
51
+ fs.writeFileSync(manifest, JSON.stringify(pkg, null, 2) + "\n");
52
+ }
53
+ },
54
+ };
55
+ }
56
+
57
+ /** @param {string} file @param {string} text */
58
+ function writeIfChanged(file, text) {
59
+ if (fs.existsSync(file) && fs.readFileSync(file, "utf8") === text) return;
60
+
61
+ fs.mkdirSync(path.dirname(file), { recursive: true });
62
+ fs.writeFileSync(file, text);
63
+ }
64
+
65
+ async function loadGenerator() {
66
+ return await import("@panomc/client-gen");
67
+ }