@panomc/plugin-kit 0.0.0-stage → 1.0.0-dev.61

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 +46 -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 +72 -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,205 @@
1
+ import { addRootClass, rootClassOf, SemanticClassCollisionError } from "../styles/semantic-classes.js";
2
+ import { checkStyles } from "../styles/check-styles.js";
3
+ import { getBuildContext } from "./meta.js";
4
+
5
+ /**
6
+ * The glue between the kit plugins of one `panoPlugin()` build (TC-31).
7
+ *
8
+ * The plugins of doc 02's file map each know one thing about a view: panoViews its contract,
9
+ * panoRules the import mode, panoHelpers the helper closure, panoStamp the controller versions,
10
+ * the semantic-class step its root classes. `pano-plugin.json` wants all of it in one entry per
11
+ * view. This file holds the two pieces that put it together:
12
+ *
13
+ * - `viewClassInfo` / `lockClassesOf`: what the root-class step does to one view source, and the
14
+ * classes the lock keeps for it (root class always; every own class once the plugin holds the
15
+ * `semanticClasses` badge);
16
+ * - `panoBuildContext`: a rollup plugin that copies what the other plugins found (their shared
17
+ * `context` object) into the build context that `panoMeta` reads when it writes the index.
18
+ */
19
+
20
+ /** @param {string} text */
21
+ const escapeRe = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
22
+
23
+ /**
24
+ * What `addRootClass` makes of one view source.
25
+ *
26
+ * - `code`: the source with the root class (the readable copy in `contract/src`)
27
+ * - `roots`: tag names of the root elements (empty when the view has no element root)
28
+ * - `rootClass`: `<ns>-<view-kebab>`
29
+ * - `warnings`: the "no root element" message, when it applies
30
+ *
31
+ * A collision with a Bootstrap / FontAwesome class throws (the build fails with the fix). A
32
+ * source Svelte cannot parse is left alone: the compiler reports it with better positions.
33
+ *
34
+ * @param {string} source
35
+ * @param {{ ns: string, view: string }} ctx
36
+ * @returns {{ code: string, roots: string[], rootClass: string, warnings: string[] }}
37
+ */
38
+ export function viewClassInfo(source, ctx) {
39
+ const rootClass = rootClassOf(ctx.ns, ctx.view);
40
+
41
+ try {
42
+ const result = addRootClass(source, ctx);
43
+
44
+ return { code: result.code, roots: result.roots, rootClass, warnings: result.warnings };
45
+ } catch (error) {
46
+ if (error instanceof SemanticClassCollisionError) throw error;
47
+
48
+ return { code: source, roots: [], rootClass, warnings: [] };
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Every class of the view that follows the plugin convention: the root class and its parts
54
+ * (`min-hello-page`, `min-hello-page__title`), sorted.
55
+ *
56
+ * @param {string} source source after the root-class step
57
+ * @param {string} rootClass
58
+ * @returns {string[]}
59
+ */
60
+ export function ownClassesOf(source, rootClass) {
61
+ const found = new Set();
62
+ const pattern = new RegExp(`(?<![\\w-])${escapeRe(rootClass)}(?:__[a-z0-9-]+)?(?![\\w-])`, "g");
63
+
64
+ for (const match of source.matchAll(pattern)) found.add(match[0]);
65
+
66
+ return [...found].sort();
67
+ }
68
+
69
+ /**
70
+ * Whether the plugin passes the style lint at `badge` level, which is what sets
71
+ * `badges.semanticClasses` (doc 03 section 3). Never throws: a lint that cannot run is "no".
72
+ *
73
+ * @param {{ root: string, ns: string, viewsDir?: string, views: { name: string, file: string }[], styles?: Record<string, any> }} input
74
+ * @returns {boolean}
75
+ */
76
+ export function passesBadge({ root, ns, viewsDir, views, styles = {} }) {
77
+ if (views.length === 0) return false;
78
+
79
+ try {
80
+ return checkStyles({
81
+ root,
82
+ ns,
83
+ viewsDir,
84
+ views,
85
+ styleAttrAllow: styles.styleAttrAllow,
86
+ dynamicClassAllow: styles.dynamicClassAllow,
87
+ safelist: styles.safelist,
88
+ level: "badge",
89
+ }).every((finding) => finding.level !== "error");
90
+ } catch {
91
+ return false;
92
+ }
93
+ }
94
+
95
+ /**
96
+ * @typedef {object} ClassPolicyOptions
97
+ * @property {string} root plugin root (absolute)
98
+ * @property {string} ns plugin namespace
99
+ * @property {() => ({ views: Map<string, { name: string, file: string, source: string }> } | null)} scan the panoViews scan
100
+ * @property {Record<string, any>} [styles] `styles` of pano.plugin.js
101
+ * @property {string} [viewsDir] folder the style lint walks, default `src/theme`
102
+ */
103
+
104
+ /**
105
+ * The class rules of one build, decided once per scan: whether the plugin holds the
106
+ * `semanticClasses` badge, and the classes of every view that the lock and the package index keep
107
+ * (root class always, its parts too once the badge is held).
108
+ *
109
+ * @param {ClassPolicyOptions} options
110
+ * @returns {{ badge(): boolean, infoOf(info: { source: string, name: string }): { roots: string[], classes: string[], code: string } }}
111
+ */
112
+ export function createClassPolicy({ root, ns, scan, styles, viewsDir }) {
113
+ /** @type {{ scan: any, badge: boolean } | null} */
114
+ let cached = null;
115
+
116
+ const badge = () => {
117
+ const found = scan();
118
+
119
+ if (!found) return false;
120
+
121
+ if (!cached || cached.scan !== found) {
122
+ cached = {
123
+ scan: found,
124
+ badge: passesBadge({
125
+ root,
126
+ ns,
127
+ viewsDir,
128
+ views: [...found.views.values()].map((info) => ({ name: info.name, file: info.file })),
129
+ styles,
130
+ }),
131
+ };
132
+ }
133
+
134
+ return cached.badge;
135
+ };
136
+
137
+ return {
138
+ badge,
139
+
140
+ infoOf(info) {
141
+ const classInfo = viewClassInfo(info.source, { ns, view: info.name });
142
+
143
+ if (classInfo.roots.length === 0) return { roots: [], classes: [], code: classInfo.code };
144
+
145
+ return {
146
+ roots: classInfo.roots,
147
+ classes: badge() ? ownClassesOf(classInfo.code, classInfo.rootClass) : [classInfo.rootClass],
148
+ code: classInfo.code,
149
+ };
150
+ },
151
+ };
152
+ }
153
+
154
+ /**
155
+ * @typedef {object} BuildContextOptions
156
+ * @property {string} root plugin root (absolute)
157
+ * @property {Record<string, any>} context the object handed to panoRules, panoHelpers and panoStamp
158
+ * @property {() => ({ views: Map<string, { name: string, file: string, source: string }> } | null)} scan the panoViews scan
159
+ * @property {ReturnType<typeof createClassPolicy>} policy
160
+ */
161
+
162
+ /**
163
+ * Copies what the other kit plugins found into the shared build context of the plugin root
164
+ * (`getBuildContext`), so `panoMeta` writes it into `pano-plugin.json`:
165
+ *
166
+ * views.<Name> = { controllers, helpers, roots, classes } (samples: a later unit)
167
+ * badges.semanticClasses (the style lint at `badge` level)
168
+ * viewImports (migration mode)
169
+ *
170
+ * Runs in `writeBundle`, which is after every compile (the stamp step has seen all views) and before
171
+ * `panoMeta` writes the index in `closeBundle`.
172
+ *
173
+ * @param {BuildContextOptions} options
174
+ * @returns {import('rollup').Plugin}
175
+ */
176
+ export function panoBuildContext({ root, context, scan, policy }) {
177
+ return {
178
+ name: "pano-build-context",
179
+
180
+ writeBundle() {
181
+ const found = scan();
182
+
183
+ if (!found) return;
184
+
185
+ const shared = getBuildContext(root);
186
+ const usage = context.controllerUsage ?? new Map();
187
+ const helpers = context.helpers ?? new Map();
188
+
189
+ for (const info of found.views.values()) {
190
+ const { roots, classes } = policy.infoOf(info);
191
+
192
+ shared.views[info.name] = {
193
+ ...(shared.views[info.name] ?? {}),
194
+ controllers: { ...(usage.get(info.name) ?? {}) },
195
+ helpers: [...(helpers.get(info.name) ?? [])],
196
+ roots,
197
+ classes,
198
+ };
199
+ }
200
+
201
+ shared.badges.semanticClasses = policy.badge();
202
+ shared.viewImports = context.viewImports === "warn" ? "warn" : undefined;
203
+ },
204
+ };
205
+ }
@@ -0,0 +1,230 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+
4
+ /**
5
+ * `pano-plugin.lock.json` (committed, written by non-watch builds), doc 01 section 2
6
+ * and doc 02 section 5.2:
7
+ *
8
+ * {
9
+ * "format": 1,
10
+ * "views": { "<ns>:<Name>": { contract, props: {name: {required}}, slots, hooks, classes? } },
11
+ * "controllers": { "<ns>/<name>": { version, state: [..], actions: [..] } }
12
+ * }
13
+ *
14
+ * A section records what a view or controller promised the last time its number
15
+ * (`contract` / `version`) was raised. A breaking change without a raised number is
16
+ * reported by `diffSection`; additions only update the lock.
17
+ */
18
+
19
+ export const LOCK_FILE = "pano-plugin.lock.json";
20
+ export const LOCK_FORMAT = 1;
21
+
22
+ /**
23
+ * @typedef {{ format: number, views?: Record<string, any>, controllers?: Record<string, any> } & Record<string, any>} Lock
24
+ */
25
+
26
+ /**
27
+ * @typedef {object} SectionDiff
28
+ * @property {boolean} changed the section differs from the stored one (a rewrite is needed)
29
+ * @property {string[]} breaking one message per breaking change that did not raise its number
30
+ */
31
+
32
+ /**
33
+ * @param {string} root plugin root
34
+ * @returns {string}
35
+ */
36
+ export function lockPath(root) {
37
+ return path.join(root, LOCK_FILE);
38
+ }
39
+
40
+ /**
41
+ * Reads the lock file. A missing file is `{ format: 1 }`; an unreadable or malformed
42
+ * file throws with the fix.
43
+ *
44
+ * @param {string} root plugin root
45
+ * @returns {Lock}
46
+ */
47
+ export function readLock(root) {
48
+ const file = lockPath(root);
49
+
50
+ if (!fs.existsSync(file)) return { format: LOCK_FORMAT };
51
+
52
+ try {
53
+ const lock = JSON.parse(fs.readFileSync(file, "utf8"));
54
+
55
+ if (typeof lock !== "object" || lock === null || Array.isArray(lock)) {
56
+ throw new Error("not an object");
57
+ }
58
+
59
+ return { format: LOCK_FORMAT, ...lock };
60
+ } catch (error) {
61
+ throw new Error(
62
+ `[pano-plugin] ${LOCK_FILE} is not valid JSON (${error.message}): fix it, or delete it if this plugin was never published`,
63
+ );
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Recursively sorts object keys so the file is stable across builds (array order is kept).
69
+ *
70
+ * @param {any} value
71
+ * @returns {any}
72
+ */
73
+ function sortKeys(value) {
74
+ if (Array.isArray(value)) return value.map(sortKeys);
75
+
76
+ if (value && typeof value === "object") {
77
+ return Object.fromEntries(
78
+ Object.keys(value)
79
+ .sort()
80
+ .map((key) => [key, sortKeys(value[key])]),
81
+ );
82
+ }
83
+
84
+ return value;
85
+ }
86
+
87
+ /**
88
+ * Replaces one section of the lock file and keeps the others. Keys are sorted, the file
89
+ * ends with a newline, and nothing is written when the content would not change.
90
+ *
91
+ * @param {string} root plugin root
92
+ * @param {string} section `views` | `controllers` | any future section
93
+ * @param {Record<string, any>} data
94
+ * @returns {boolean} true when the file was written
95
+ */
96
+ export function writeLockSection(root, section, data) {
97
+ const lock = readLock(root);
98
+ const next = sortKeys({ ...lock, format: LOCK_FORMAT, [section]: data });
99
+ const text = JSON.stringify(next, null, 2) + "\n";
100
+ const file = lockPath(root);
101
+
102
+ if (fs.existsSync(file) && fs.readFileSync(file, "utf8") === text) return false;
103
+
104
+ fs.writeFileSync(file, text);
105
+
106
+ return true;
107
+ }
108
+
109
+ /**
110
+ * @param {any} value
111
+ * @returns {string[]}
112
+ */
113
+ function list(value) {
114
+ return Array.isArray(value) ? value : [];
115
+ }
116
+
117
+ /**
118
+ * Breaking changes of one view entry, in the order of doc 01 section 2: a prop removed,
119
+ * a required prop added, `slots` / `hooks` changed, a class removed (when the lock holds
120
+ * classes, i.e. the plugin has the `semanticClasses` badge).
121
+ *
122
+ * @param {string} id
123
+ * @param {any} before
124
+ * @param {any} after
125
+ * @returns {string[]} problems, without the "set contract" tail
126
+ */
127
+ function breakingViewChanges(id, before, after) {
128
+ const problems = [];
129
+ const beforeProps = before.props ?? {};
130
+ const afterProps = after.props ?? {};
131
+
132
+ for (const name of Object.keys(beforeProps)) {
133
+ if (!(name in afterProps)) problems.push(`prop "${name}" removed`);
134
+ }
135
+
136
+ for (const name of Object.keys(afterProps)) {
137
+ if (!(name in beforeProps) && afterProps[name]?.required) {
138
+ problems.push(`required prop "${name}" added`);
139
+ }
140
+ }
141
+
142
+ for (const key of ["slots", "hooks"]) {
143
+ if (JSON.stringify(list(before[key])) !== JSON.stringify(list(after[key]))) {
144
+ problems.push(`${key} changed`);
145
+ }
146
+ }
147
+
148
+ if (Array.isArray(before.classes)) {
149
+ for (const name of before.classes) {
150
+ if (!list(after.classes).includes(name)) problems.push(`class ${name} removed`);
151
+ }
152
+ }
153
+
154
+ return problems;
155
+ }
156
+
157
+ /**
158
+ * @param {string} id
159
+ * @param {any} before
160
+ * @param {any} after
161
+ * @returns {string[]}
162
+ */
163
+ function breakingControllerChanges(id, before, after) {
164
+ const problems = [];
165
+
166
+ for (const key of list(before.state)) {
167
+ if (!list(after.state).includes(key)) problems.push(`state '${key}' removed`);
168
+ }
169
+
170
+ for (const key of list(before.actions)) {
171
+ if (!list(after.actions).includes(key)) problems.push(`action '${key}' removed`);
172
+ }
173
+
174
+ return problems;
175
+ }
176
+
177
+ /**
178
+ * Compares the stored section with the freshly computed one.
179
+ *
180
+ * `breaking` holds one message for every entry whose contract (views) or version
181
+ * (controllers) was not raised although something it promised is gone:
182
+ *
183
+ * views: `market:ProductCard: prop "settings" removed — set contract: 3 in ProductCard.svelte (never published? delete pano-plugin.lock.json)`
184
+ * controllers: `market/cart: action 'retry' removed — set version: 2 in src/theme/controllers/cart.js`
185
+ *
186
+ * Added entries and added optional keys are never breaking; they only set `changed`.
187
+ * An entry missing from `next` (a deleted view or controller) is not breaking either.
188
+ *
189
+ * @param {"views" | "controllers" | string} section
190
+ * @param {Record<string, any> | null | undefined} previous the stored section (null / undefined = none yet)
191
+ * @param {Record<string, any>} next the freshly computed section
192
+ * @returns {SectionDiff}
193
+ */
194
+ export function diffSection(section, previous, next) {
195
+ const changed = JSON.stringify(sortKeys(previous ?? null)) !== JSON.stringify(sortKeys(next));
196
+ /** @type {string[]} */
197
+ const breaking = [];
198
+
199
+ if (!previous) return { changed, breaking };
200
+
201
+ const numberKey = section === "controllers" ? "version" : "contract";
202
+
203
+ for (const [id, after] of Object.entries(next)) {
204
+ const before = previous[id];
205
+
206
+ if (!before) continue;
207
+
208
+ const oldNumber = Number(before[numberKey] ?? 1);
209
+ const newNumber = Number(after[numberKey] ?? 1);
210
+
211
+ if (newNumber > oldNumber) continue;
212
+
213
+ const problems =
214
+ section === "controllers"
215
+ ? breakingControllerChanges(id, before, after)
216
+ : breakingViewChanges(id, before, after);
217
+
218
+ for (const problem of problems) {
219
+ const raised = oldNumber + 1;
220
+
221
+ breaking.push(
222
+ section === "controllers"
223
+ ? `${id}: ${problem} — set version: ${raised} in src/theme/controllers/${id.split("/").pop()}.js`
224
+ : `${id}: ${problem} — set contract: ${raised} in ${id.split(":").pop()}.svelte (never published? delete ${LOCK_FILE})`,
225
+ );
226
+ }
227
+ }
228
+
229
+ return { changed, breaking };
230
+ }