@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.
- 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 +46 -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 +72 -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
package/src/dev.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import { loadConfig } from "./config.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `pano-plugin dev [--pano <url>]` (doc 02 section 8): `bun run dev` of a plugin.
|
|
8
|
+
*
|
|
9
|
+
* 1. builds the plugin jar once, with `-Pnoui`, when `build/libs` holds none (the UI comes from the
|
|
10
|
+
* watch build below, so the first run does not wait for it);
|
|
11
|
+
* 2. reads `developmentMode` from the site info of the running Pano and, when it is off, prints the
|
|
12
|
+
* panel path of the switch (a plugin's UI is only reloaded from the folder while it is on);
|
|
13
|
+
* 3. starts the rollup watch with `DEV=true` (unminified, Svelte dev mode).
|
|
14
|
+
*
|
|
15
|
+
* Every outside effect is a parameter, so the command is testable without Gradle, a network or rollup.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export const DEFAULT_PANO_URL = "http://localhost:8088";
|
|
19
|
+
export const DEV_MODE_PANEL_PATH = "Panel → Platform Settings → Development Mode";
|
|
20
|
+
|
|
21
|
+
/** Site info paths, newest first (`/api/v1/site-info` after the v1 migration, `/api/site-info` before). */
|
|
22
|
+
const SITE_INFO_PATHS = ["/api/v1/site-info", "/api/site-info"];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The plugin jars under `build/libs`.
|
|
26
|
+
*
|
|
27
|
+
* @param {string} root
|
|
28
|
+
* @returns {string[]}
|
|
29
|
+
*/
|
|
30
|
+
export function builtJars(root) {
|
|
31
|
+
const dir = path.join(root, "build", "libs");
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
return fs
|
|
35
|
+
.readdirSync(dir)
|
|
36
|
+
.filter((name) => name.endsWith(".jar") && !name.endsWith("-plain.jar"))
|
|
37
|
+
.map((name) => path.join(dir, name));
|
|
38
|
+
} catch {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @param {string} url
|
|
45
|
+
* @param {typeof fetch} request
|
|
46
|
+
* @returns {Promise<{ reachable: boolean, developmentMode: boolean | null }>}
|
|
47
|
+
*/
|
|
48
|
+
export async function readDevelopmentMode(url, request = fetch) {
|
|
49
|
+
const base = url.replace(/\/+$/, "");
|
|
50
|
+
|
|
51
|
+
for (const sitePath of SITE_INFO_PATHS) {
|
|
52
|
+
try {
|
|
53
|
+
const response = await request(base + sitePath, { signal: AbortSignal.timeout(3000) });
|
|
54
|
+
|
|
55
|
+
if (!response.ok) continue;
|
|
56
|
+
|
|
57
|
+
const body = /** @type {any} */ (await response.json());
|
|
58
|
+
// before the v1 envelope the fields sit beside `result: "ok"`
|
|
59
|
+
const info = body && typeof body.result === "object" ? body.result : body;
|
|
60
|
+
|
|
61
|
+
return { reachable: true, developmentMode: typeof info?.developmentMode === "boolean" ? info.developmentMode : null };
|
|
62
|
+
} catch {
|
|
63
|
+
return { reachable: false, developmentMode: null };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return { reachable: true, developmentMode: null };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @typedef {object} DevOptions
|
|
72
|
+
* @property {string} [root] plugin root, default the current directory
|
|
73
|
+
* @property {string} [pano] Pano address, default `PANO_URL` or http://localhost:8088
|
|
74
|
+
* @property {(line: string) => void} [out]
|
|
75
|
+
* @property {(line: string) => void} [err]
|
|
76
|
+
* @property {(command: string, args: string[], cwd: string) => number} [run] runs a command with inherited output, returns its exit code
|
|
77
|
+
* @property {typeof fetch} [request]
|
|
78
|
+
* @property {(root: string) => Promise<import('rollup').RollupOptions[]>} [loadConfigs]
|
|
79
|
+
* @property {(configs: import('rollup').RollupOptions[]) => { on: (event: string, handler: (event: any) => void) => any, close: () => Promise<void> | void }} [watch]
|
|
80
|
+
* @property {Promise<void>} [stop] resolves when the watch should end (default: SIGINT / SIGTERM)
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @param {string} command
|
|
85
|
+
* @param {string[]} args
|
|
86
|
+
* @param {string} cwd
|
|
87
|
+
* @returns {number}
|
|
88
|
+
*/
|
|
89
|
+
function defaultRun(command, args, cwd) {
|
|
90
|
+
const result = spawnSync(command, args, { cwd, stdio: "inherit", shell: process.platform === "win32" });
|
|
91
|
+
|
|
92
|
+
return result.status ?? 1;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @param {DevOptions} [options]
|
|
97
|
+
* @returns {Promise<number>}
|
|
98
|
+
*/
|
|
99
|
+
export async function runDev(options = {}) {
|
|
100
|
+
const root = path.resolve(options.root ?? process.cwd());
|
|
101
|
+
const out = options.out ?? ((line) => process.stdout.write(`${line}\n`));
|
|
102
|
+
const err = options.err ?? ((line) => process.stderr.write(`${line}\n`));
|
|
103
|
+
const run = options.run ?? defaultRun;
|
|
104
|
+
const panoUrl = options.pano ?? process.env.PANO_URL ?? DEFAULT_PANO_URL;
|
|
105
|
+
let config;
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
config = await loadConfig(root);
|
|
109
|
+
} catch (error) {
|
|
110
|
+
err(`error: ${String(/** @type {Error} */ (error).message).replace(/^\[pano-plugin\] /, "")}`);
|
|
111
|
+
|
|
112
|
+
return 1;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// 1. the jar
|
|
116
|
+
let builtJar = false;
|
|
117
|
+
|
|
118
|
+
if (builtJars(root).length === 0) {
|
|
119
|
+
const windows = process.platform === "win32";
|
|
120
|
+
const wrapper = windows ? "gradlew.bat" : "gradlew";
|
|
121
|
+
|
|
122
|
+
if (!fs.existsSync(path.join(root, wrapper))) {
|
|
123
|
+
err(`error: no build/libs jar and no ${wrapper} in ${root} — build the plugin once with: gradle build -Pnoui`);
|
|
124
|
+
|
|
125
|
+
return 1;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
out(`dev: no jar in build/libs yet — building ${config.pluginId} once (this takes a few minutes)`);
|
|
129
|
+
|
|
130
|
+
const code = run(windows ? wrapper : `./${wrapper}`, ["build", "-Pnoui"], root);
|
|
131
|
+
|
|
132
|
+
if (code !== 0) {
|
|
133
|
+
err(`error: ./${wrapper} build -Pnoui failed (exit ${code}) — fix the Kotlin build, then run bun run dev again`);
|
|
134
|
+
|
|
135
|
+
return 1;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
builtJar = true;
|
|
139
|
+
out("dev: jar built — restart Pano once so it loads the plugin");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 2. Development Mode
|
|
143
|
+
const mode = await readDevelopmentMode(panoUrl, options.request);
|
|
144
|
+
|
|
145
|
+
if (!mode.reachable) {
|
|
146
|
+
out(`dev: Pano is not reachable at ${panoUrl} (--pano <url> or PANO_URL) — start it${builtJar ? "" : ", and restart it once if this plugin is new"}`);
|
|
147
|
+
} else if (mode.developmentMode === false) {
|
|
148
|
+
out(`dev: Development Mode is off — Pano will not reload this plugin's UI. Switch it on in the panel: ${DEV_MODE_PANEL_PATH}`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 3. the watch
|
|
152
|
+
const previousDev = process.env.DEV;
|
|
153
|
+
|
|
154
|
+
process.env.DEV = "true";
|
|
155
|
+
|
|
156
|
+
/** The command may run inside another process (tests): leave its environment as found. */
|
|
157
|
+
const restoreEnv = () => {
|
|
158
|
+
if (previousDev === undefined) delete process.env.DEV;
|
|
159
|
+
else process.env.DEV = previousDev;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
let configs;
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
configs = await (options.loadConfigs ?? (async (folder) => (await import("./rollup/cli-build.js")).loadRollupConfigs(folder)))(root);
|
|
166
|
+
} catch (error) {
|
|
167
|
+
restoreEnv();
|
|
168
|
+
err(`error: ${String(/** @type {Error} */ (error).message).replace(/^\[pano-plugin\] /, "")}`);
|
|
169
|
+
|
|
170
|
+
return 1;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const watch = options.watch ?? (await import("rollup")).watch;
|
|
174
|
+
const watcher = watch(configs);
|
|
175
|
+
let failed = false;
|
|
176
|
+
|
|
177
|
+
let elapsed = 0;
|
|
178
|
+
|
|
179
|
+
watcher.on("event", (event) => {
|
|
180
|
+
if (event.code === "START") {
|
|
181
|
+
failed = false;
|
|
182
|
+
elapsed = 0;
|
|
183
|
+
} else if (event.code === "BUNDLE_END") {
|
|
184
|
+
elapsed += event.duration ?? 0;
|
|
185
|
+
event.result?.close?.();
|
|
186
|
+
} else if (event.code === "ERROR") {
|
|
187
|
+
failed = true;
|
|
188
|
+
err(`error: ${String(event.error?.message ?? event.error).replace(/^\[pano-plugin\] /, "")}`);
|
|
189
|
+
event.result?.close?.();
|
|
190
|
+
} else if (event.code === "END" && !failed) {
|
|
191
|
+
// all targets (server, client, controllers) are written once rollup says END
|
|
192
|
+
out(`dev: built in ${elapsed} ms`);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
out("dev: watching src/ — press Ctrl+C to stop");
|
|
197
|
+
|
|
198
|
+
await (options.stop ??
|
|
199
|
+
new Promise((resolve) => {
|
|
200
|
+
process.once("SIGINT", () => resolve(undefined));
|
|
201
|
+
process.once("SIGTERM", () => resolve(undefined));
|
|
202
|
+
}));
|
|
203
|
+
|
|
204
|
+
await watcher.close();
|
|
205
|
+
restoreEnv();
|
|
206
|
+
|
|
207
|
+
return failed ? 1 : 0;
|
|
208
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @panomc/plugin-kit: build tooling for Pano plugins (rollup preset `panoPlugin()`,
|
|
3
|
+
* the `pano-plugin` CLI and the controller core). This entry stays free of rollup and
|
|
4
|
+
* svelte imports so it loads anywhere; the preset lives under `@panomc/plugin-kit/rollup`
|
|
5
|
+
* and the controller core under `@panomc/plugin-kit/controller`.
|
|
6
|
+
*/
|
|
7
|
+
export {
|
|
8
|
+
loadConfig,
|
|
9
|
+
namespaceOf,
|
|
10
|
+
readProperty,
|
|
11
|
+
PLUGIN_ID_PREFIX,
|
|
12
|
+
RESERVED_NAMESPACES,
|
|
13
|
+
DEFAULT_VIEW_DIRS,
|
|
14
|
+
} from './config.js';
|
|
15
|
+
export {
|
|
16
|
+
readLock,
|
|
17
|
+
writeLockSection,
|
|
18
|
+
diffSection,
|
|
19
|
+
lockPath,
|
|
20
|
+
LOCK_FILE,
|
|
21
|
+
LOCK_FORMAT,
|
|
22
|
+
} from './rollup/lock.js';
|
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, onTestFinished, test } from "bun:test";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import ts from "typescript";
|
|
6
|
+
import { getBuildContext } from "../../meta.js";
|
|
7
|
+
import { declaredTypeNames, validateTypesSource } from "../../types.js";
|
|
8
|
+
import { OUT, build, cartSource, cleanupFixtures, makeFixture, read, write } from "./fixture.js";
|
|
9
|
+
|
|
10
|
+
const GOLDEN = path.join(import.meta.dir, "golden");
|
|
11
|
+
|
|
12
|
+
/** Compares with golden/<name>; UPDATE_GOLDEN=1 rewrites it. */
|
|
13
|
+
function expectGolden(name, text) {
|
|
14
|
+
const file = path.join(GOLDEN, name);
|
|
15
|
+
|
|
16
|
+
if (process.env.UPDATE_GOLDEN === "1") {
|
|
17
|
+
fs.mkdirSync(GOLDEN, { recursive: true });
|
|
18
|
+
fs.writeFileSync(file, text);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
expect(fs.existsSync(file), `${name} is missing: run with UPDATE_GOLDEN=1`).toBe(true);
|
|
22
|
+
expect(text).toBe(fs.readFileSync(file, "utf8"));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
beforeAll(() => {
|
|
26
|
+
delete process.env.PANO_VIEW_IMPORTS;
|
|
27
|
+
});
|
|
28
|
+
afterAll(cleanupFixtures);
|
|
29
|
+
|
|
30
|
+
const MJS = `${OUT}/controllers/controllers.mjs`;
|
|
31
|
+
|
|
32
|
+
describe("standalone controllers.mjs", () => {
|
|
33
|
+
test("is one minified module with no import, exporting controllers, createHost, create, meta", async () => {
|
|
34
|
+
const root = makeFixture();
|
|
35
|
+
|
|
36
|
+
await build(root);
|
|
37
|
+
|
|
38
|
+
const code = read(root, MJS);
|
|
39
|
+
const scan = new Bun.Transpiler({ loader: "js" }).scan(code);
|
|
40
|
+
|
|
41
|
+
expect(scan.imports).toEqual([]);
|
|
42
|
+
expect(scan.exports.sort()).toEqual(["controllers", "create", "createHost", "meta"]);
|
|
43
|
+
expect(code.split("\n").length).toBeLessThanOrEqual(2);
|
|
44
|
+
expect(code).not.toContain("@typedef");
|
|
45
|
+
expect(code).not.toContain("sub-folders are private code");
|
|
46
|
+
|
|
47
|
+
const mod = await import(path.join(root, MJS));
|
|
48
|
+
|
|
49
|
+
expect(Object.keys(mod.controllers)).toEqual(["cart", "format"]);
|
|
50
|
+
expect(mod.meta).toEqual({
|
|
51
|
+
format: 1,
|
|
52
|
+
pluginId: "pano-plugin-demo",
|
|
53
|
+
namespace: "demo",
|
|
54
|
+
controllers: { cart: { version: 1, scope: "app" }, format: { version: 1, scope: "instance" } },
|
|
55
|
+
});
|
|
56
|
+
expect(mod.createHost).toBeFunction();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("a script imports it from an empty directory and drives a controller", () => {
|
|
60
|
+
const root = makeFixture();
|
|
61
|
+
|
|
62
|
+
return build(root).then(() => {
|
|
63
|
+
const empty = fs.mkdtempSync(path.join(os.tmpdir(), "pano-kit-fixture-empty-"));
|
|
64
|
+
|
|
65
|
+
onTestFinished(() => fs.rmSync(empty, { recursive: true, force: true }));
|
|
66
|
+
|
|
67
|
+
fs.copyFileSync(path.join(root, MJS), path.join(empty, "controllers.mjs"));
|
|
68
|
+
fs.writeFileSync(
|
|
69
|
+
path.join(empty, "run.mjs"),
|
|
70
|
+
`import * as demo from "./controllers.mjs";
|
|
71
|
+
const calls = [];
|
|
72
|
+
const host = demo.createHost({
|
|
73
|
+
baseUrl: "https://example.com",
|
|
74
|
+
messages: { "plugins.pano-plugin-demo.currency": "USD" },
|
|
75
|
+
fetch: async (url) => { calls.push(url); return new Response("{}", { status: 200 }); },
|
|
76
|
+
});
|
|
77
|
+
const cart = demo.create("cart", host);
|
|
78
|
+
const seen = [];
|
|
79
|
+
const off = cart.subscribe((s) => seen.push(s.count));
|
|
80
|
+
await cart.actions.add({ id: 1 });
|
|
81
|
+
off();
|
|
82
|
+
const format = demo.create("format", host);
|
|
83
|
+
console.log(JSON.stringify({
|
|
84
|
+
seen,
|
|
85
|
+
keys: Object.keys(demo).sort(),
|
|
86
|
+
money: await format.actions.money(1500),
|
|
87
|
+
same: demo.create("cart", host) === cart,
|
|
88
|
+
}));
|
|
89
|
+
`,
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const runtimes = [process.execPath, Bun.which("node")].filter(Boolean);
|
|
93
|
+
|
|
94
|
+
for (const runtime of runtimes) {
|
|
95
|
+
const result = Bun.spawnSync([runtime, "run.mjs"], { cwd: empty, stdout: "pipe", stderr: "pipe" });
|
|
96
|
+
|
|
97
|
+
expect(result.stderr.toString(), runtime).toBe("");
|
|
98
|
+
expect(JSON.parse(result.stdout.toString()), runtime).toEqual({
|
|
99
|
+
seen: [0, 4],
|
|
100
|
+
keys: ["controllers", "create", "createHost", "meta"],
|
|
101
|
+
money: "000015 USD",
|
|
102
|
+
same: true,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("create: app scope is one per host, instance scope is new, unknown names throw, destroy frees the slot", async () => {
|
|
109
|
+
const root = makeFixture();
|
|
110
|
+
|
|
111
|
+
await build(root);
|
|
112
|
+
|
|
113
|
+
const mod = await import(path.join(root, MJS) + "?semantics");
|
|
114
|
+
const host = mod.createHost({ baseUrl: "" });
|
|
115
|
+
const other = mod.createHost({ baseUrl: "" });
|
|
116
|
+
const cart = mod.create("cart", host);
|
|
117
|
+
|
|
118
|
+
expect(mod.create("cart", host)).toBe(cart);
|
|
119
|
+
expect(mod.create("cart", other)).not.toBe(cart);
|
|
120
|
+
expect(mod.create("format", host)).not.toBe(mod.create("format", host));
|
|
121
|
+
expect(cart.name).toBe("demo/cart");
|
|
122
|
+
expect(() => mod.create("nope", host)).toThrow("controller 'demo/nope' is not in this package (available: cart, format)");
|
|
123
|
+
|
|
124
|
+
cart.destroy();
|
|
125
|
+
expect(mod.create("cart", host)).not.toBe(cart);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("ctx.use reaches a sibling made on the same host", async () => {
|
|
129
|
+
const root = makeFixture({
|
|
130
|
+
"src/theme/controllers/badge.js": `import { defineController } from '@panomc/plugin-kit/controller';
|
|
131
|
+
export default defineController({
|
|
132
|
+
name: 'badge',
|
|
133
|
+
version: 1,
|
|
134
|
+
actions: (c) => ({ total: () => c.use('cart').get().count }),
|
|
135
|
+
});
|
|
136
|
+
`,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
await build(root);
|
|
140
|
+
|
|
141
|
+
const mod = await import(path.join(root, MJS) + "?use");
|
|
142
|
+
const host = mod.createHost({ baseUrl: "" });
|
|
143
|
+
const badge = mod.create("badge", host);
|
|
144
|
+
|
|
145
|
+
await mod.create("cart", host).actions.add({ id: 1 });
|
|
146
|
+
expect(badge.actions.total()).toBe(4);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("an unchanged second build rewrites nothing, a changed controller is picked up fresh", async () => {
|
|
150
|
+
const root = makeFixture();
|
|
151
|
+
|
|
152
|
+
await build(root);
|
|
153
|
+
|
|
154
|
+
const files = [MJS, `${OUT}/contract/controllers.json`, `${OUT}/contract/controllers.types.js`, `${OUT}/pano-plugin.json`, "pano-plugin.lock.json"];
|
|
155
|
+
const before = files.map((file) => fs.statSync(path.join(root, file)).mtimeMs);
|
|
156
|
+
|
|
157
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
158
|
+
await build(root);
|
|
159
|
+
expect(files.map((file) => fs.statSync(path.join(root, file)).mtimeMs)).toEqual(before);
|
|
160
|
+
|
|
161
|
+
write(root, "src/theme/controllers/cart.js", cartSource({ state: ["status", "lines", "count", "replaceRequest", "coupon"], actions: ["add", "setQuantity", "clear", "retry", "apply"] }));
|
|
162
|
+
await build(root);
|
|
163
|
+
|
|
164
|
+
expect(JSON.parse(read(root, `${OUT}/contract/controllers.json`))["demo/cart"].state).toContain("coupon");
|
|
165
|
+
expect(read(root, `${OUT}/contract/controllers.types.js`)).toContain("apply: (...args: any[]) => any");
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe("contract files", () => {
|
|
170
|
+
test("controllers.json, controllers.types.js and pano-plugin.json match the golden files", async () => {
|
|
171
|
+
const root = makeFixture();
|
|
172
|
+
|
|
173
|
+
await build(root);
|
|
174
|
+
|
|
175
|
+
expectGolden("controllers.json", read(root, `${OUT}/contract/controllers.json`));
|
|
176
|
+
expectGolden("controllers.types.js", read(root, `${OUT}/contract/controllers.types.js`));
|
|
177
|
+
expectGolden("pano-plugin.json", read(root, `${OUT}/pano-plugin.json`));
|
|
178
|
+
expectGolden("pano-plugin.lock.json", read(root, "pano-plugin.lock.json"));
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("pano-plugin.json collects what the other sub-plugins put on the shared build context", async () => {
|
|
182
|
+
const root = makeFixture({
|
|
183
|
+
[`${OUT}/contract/views.json`]: JSON.stringify({ namespace: "demo", views: { "demo:HelloPage": {}, "demo:ProductCard": {} } }),
|
|
184
|
+
});
|
|
185
|
+
const context = getBuildContext(root);
|
|
186
|
+
|
|
187
|
+
context.views.ProductCard = {
|
|
188
|
+
samples: ["filled", "empty"],
|
|
189
|
+
controllers: { "demo/format": 1, "demo/cart": 1 },
|
|
190
|
+
helpers: ["lib/sale.js"],
|
|
191
|
+
roots: ["div"],
|
|
192
|
+
classes: ["demo-product-card"],
|
|
193
|
+
attrs: ["title"],
|
|
194
|
+
};
|
|
195
|
+
context.styles = { fallback: "client/fallback.css", own: "client/plugin.css", hash: "9f2c1a7e", icons: true };
|
|
196
|
+
context.badges.semanticClasses = true;
|
|
197
|
+
context.badges.samples = true;
|
|
198
|
+
context.viewImports = "warn";
|
|
199
|
+
|
|
200
|
+
await build(root);
|
|
201
|
+
|
|
202
|
+
expectGolden("pano-plugin.with-views.json", read(root, `${OUT}/pano-plugin.json`));
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test("a plugin without public controllers has no controllers file, key or stale output", async () => {
|
|
206
|
+
const root = makeFixture();
|
|
207
|
+
|
|
208
|
+
await build(root);
|
|
209
|
+
fs.rmSync(path.join(root, "src/theme/controllers"), { recursive: true });
|
|
210
|
+
await build(root);
|
|
211
|
+
|
|
212
|
+
const index = JSON.parse(read(root, `${OUT}/pano-plugin.json`));
|
|
213
|
+
|
|
214
|
+
expect(index.controllers).toBeUndefined();
|
|
215
|
+
expect(index.badges.controllers).toBe(false);
|
|
216
|
+
expect(fs.existsSync(path.join(root, MJS))).toBe(false);
|
|
217
|
+
expect(fs.existsSync(path.join(root, `${OUT}/contract/controllers.json`))).toBe(false);
|
|
218
|
+
expect(fs.existsSync(path.join(root, `${OUT}/contract/controllers.types.js`))).toBe(false);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test("the generated types module type-checks and keeps an author's typedef", async () => {
|
|
222
|
+
const root = makeFixture();
|
|
223
|
+
|
|
224
|
+
await build(root);
|
|
225
|
+
|
|
226
|
+
const dir = path.join(root, OUT, "contract");
|
|
227
|
+
const check = path.join(dir, "check.js");
|
|
228
|
+
|
|
229
|
+
fs.writeFileSync(
|
|
230
|
+
check,
|
|
231
|
+
`/** @type {import('./controllers.types.js').Controllers['demo/cart']} */
|
|
232
|
+
export let cart = /** @type {any} */ (null);
|
|
233
|
+
export const count = cart.get().count + 1;
|
|
234
|
+
cart.actions.add(1);
|
|
235
|
+
/** @type {import('./controllers.types.js').CartState} */
|
|
236
|
+
export const state = { status: 'idle', lines: [], count: 1 };
|
|
237
|
+
// @ts-expect-error status is declared by the author as 'idle' | 'busy'
|
|
238
|
+
export const bad = /** @type {import('./controllers.types.js').CartState} */ ({ status: 'nope', lines: [], count: 1 });
|
|
239
|
+
/** @type {import('./controllers.types.js').Controllers['demo/format']} */
|
|
240
|
+
export let format = /** @type {any} */ (null);
|
|
241
|
+
format.actions.money(1);
|
|
242
|
+
`,
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
const program = ts.createProgram([check], {
|
|
246
|
+
allowJs: true,
|
|
247
|
+
checkJs: true,
|
|
248
|
+
noEmit: true,
|
|
249
|
+
strict: true,
|
|
250
|
+
target: ts.ScriptTarget.ES2022,
|
|
251
|
+
module: ts.ModuleKind.ESNext,
|
|
252
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
253
|
+
skipLibCheck: true,
|
|
254
|
+
});
|
|
255
|
+
const diagnostics = ts.getPreEmitDiagnostics(program).map((d) => ts.flattenDiagnosticMessageText(d.messageText, "\n"));
|
|
256
|
+
|
|
257
|
+
expect(diagnostics).toEqual([]);
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
describe("types.js validation", () => {
|
|
262
|
+
test("accepts comments and export {} only", () => {
|
|
263
|
+
expect(() => validateTypesSource("")).not.toThrow();
|
|
264
|
+
expect(() => validateTypesSource("/** @typedef {number} A */\n// note\nexport {};\n")).not.toThrow();
|
|
265
|
+
expect(() => validateTypesSource("/** @typedef {import('x').Y} A */\nexport {}\n")).not.toThrow();
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
test("names the first statement and its line", () => {
|
|
269
|
+
expect(() => validateTypesSource("/** @typedef {number} A */\n\nexport const x = 1;\n", "src/theme/controllers/types.js")).toThrow(
|
|
270
|
+
"src/theme/controllers/types.js:3 has code (export const x = 1;) — types.js may hold only @typedef comments and export {}",
|
|
271
|
+
);
|
|
272
|
+
expect(() => validateTypesSource("import x from 'y';\nexport {};")).toThrow("types.js:1 has code");
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
test("a code statement in types.js fails the build", async () => {
|
|
276
|
+
const root = makeFixture({ "src/theme/controllers/types.js": "/** @typedef {number} A */\nexport const x = 1;\n" });
|
|
277
|
+
|
|
278
|
+
await expect(build(root)).rejects.toThrow("src/theme/controllers/types.js:2 has code (export const x = 1;)");
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
test("declaredTypeNames reads nested braces and callbacks", () => {
|
|
282
|
+
const text = `/**
|
|
283
|
+
* @typedef {{ a: { b: number } }} Deep
|
|
284
|
+
* @typedef {Object} Plain
|
|
285
|
+
* @callback Handler
|
|
286
|
+
*/
|
|
287
|
+
/** @typedef Bare */`;
|
|
288
|
+
|
|
289
|
+
expect([...declaredTypeNames(text)].sort()).toEqual(["Bare", "Deep", "Handler", "Plain"]);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
test("without types.js the typedefs are all generated", async () => {
|
|
293
|
+
const root = makeFixture({ "src/theme/controllers/types.js": null });
|
|
294
|
+
|
|
295
|
+
await build(root);
|
|
296
|
+
|
|
297
|
+
const text = read(root, `${OUT}/contract/controllers.types.js`);
|
|
298
|
+
|
|
299
|
+
expect(text).toContain("@typedef {{ status: any, lines: any, count: any, replaceRequest: any }} CartState");
|
|
300
|
+
expect(text.trimEnd().endsWith("export {};")).toBe(true);
|
|
301
|
+
expect(text.match(/^export \{\};$/gm)).toHaveLength(1);
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
describe("build rules of the standalone build", () => {
|
|
306
|
+
test("a framework import fails with the import chain", async () => {
|
|
307
|
+
const root = makeFixture({
|
|
308
|
+
"src/theme/controllers/cart.js": cartSource({ extra: "import { session } from '../stores/session.js';" }),
|
|
309
|
+
"src/theme/stores/session.js": "import { writable } from 'svelte/store';\nexport const session = writable(null);\n",
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
await expect(build(root)).rejects.toThrow(
|
|
313
|
+
"controllers/cart.js -> stores/session.js imports svelte/store — controllers are framework-free; use host.session()",
|
|
314
|
+
);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test("a package that is not installed fails with the fix", async () => {
|
|
318
|
+
const root = makeFixture({ "src/theme/controllers/cart.js": cartSource({ extra: "import 'not-installed';" }) });
|
|
319
|
+
|
|
320
|
+
await expect(build(root)).rejects.toThrow("controllers/cart.js imports 'not-installed', which cannot be resolved");
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test("a default export that is not defineController fails", async () => {
|
|
324
|
+
const root = makeFixture({ "src/theme/controllers/oops.js": "export default { name: 'oops' };\n" });
|
|
325
|
+
|
|
326
|
+
await expect(build(root)).rejects.toThrow("controllers/oops.js: the default export must be defineController");
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
test("two files with one controller name fail", async () => {
|
|
330
|
+
const root = makeFixture({ "src/theme/controllers/cart2.js": cartSource() });
|
|
331
|
+
|
|
332
|
+
await expect(build(root)).rejects.toThrow("the controller name 'cart' is already used by another file");
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test("a controller that touches the network while enumerating is named", async () => {
|
|
336
|
+
const root = makeFixture({
|
|
337
|
+
"src/theme/controllers/cart.js": `import { defineController } from '@panomc/plugin-kit/controller';
|
|
338
|
+
export default defineController({ name: 'cart', version: 1, state: () => ({ a: window.location.href }) });
|
|
339
|
+
`,
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
await expect(build(root)).rejects.toThrow("demo/cart: cannot read its keys on the null host");
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
describe("controllers lock section", () => {
|
|
347
|
+
const LOCKED = (root) => JSON.parse(read(root, "pano-plugin.lock.json")).controllers;
|
|
348
|
+
|
|
349
|
+
test("the first build writes it, added keys update it silently", async () => {
|
|
350
|
+
const root = makeFixture();
|
|
351
|
+
|
|
352
|
+
await build(root);
|
|
353
|
+
expect(LOCKED(root)["demo/cart"].actions).toEqual(["add", "setQuantity", "clear", "retry"]);
|
|
354
|
+
|
|
355
|
+
write(root, "src/theme/controllers/cart.js", cartSource({ actions: ["add", "setQuantity", "clear", "retry", "apply"] }));
|
|
356
|
+
await build(root);
|
|
357
|
+
expect(LOCKED(root)["demo/cart"].actions).toContain("apply");
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
test("removing an action without a new version fails the build", async () => {
|
|
361
|
+
const root = makeFixture();
|
|
362
|
+
|
|
363
|
+
await build(root);
|
|
364
|
+
write(root, "src/theme/controllers/cart.js", cartSource({ actions: ["add", "setQuantity", "clear"] }));
|
|
365
|
+
|
|
366
|
+
await expect(build(root)).rejects.toThrow(
|
|
367
|
+
"demo/cart: action 'retry' removed — set version: 2 in src/theme/controllers/cart.js",
|
|
368
|
+
);
|
|
369
|
+
expect(LOCKED(root)["demo/cart"].actions).toContain("retry");
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
test("removing a state key without a new version fails the build", async () => {
|
|
373
|
+
const root = makeFixture();
|
|
374
|
+
|
|
375
|
+
await build(root);
|
|
376
|
+
write(root, "src/theme/controllers/cart.js", cartSource({ state: ["status", "lines", "count"] }));
|
|
377
|
+
|
|
378
|
+
await expect(build(root)).rejects.toThrow("demo/cart: state 'replaceRequest' removed — set version: 2");
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
test("a raised version accepts the removal and rewrites the lock", async () => {
|
|
382
|
+
const root = makeFixture();
|
|
383
|
+
|
|
384
|
+
await build(root);
|
|
385
|
+
write(root, "src/theme/controllers/cart.js", cartSource({ version: 2, actions: ["add", "setQuantity", "clear"] }));
|
|
386
|
+
await build(root);
|
|
387
|
+
|
|
388
|
+
expect(LOCKED(root)["demo/cart"]).toEqual({ version: 2, state: ["status", "lines", "count", "replaceRequest"], actions: ["add", "setQuantity", "clear"] });
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
test("in watch mode a removal is one warning and the lock is left alone", async () => {
|
|
392
|
+
const root = makeFixture();
|
|
393
|
+
|
|
394
|
+
await build(root);
|
|
395
|
+
|
|
396
|
+
const lock = read(root, "pano-plugin.lock.json");
|
|
397
|
+
const warnings = [];
|
|
398
|
+
|
|
399
|
+
write(root, "src/theme/controllers/cart.js", cartSource({ actions: ["add", "setQuantity", "clear"] }));
|
|
400
|
+
await build(root, { watch: true, warnings });
|
|
401
|
+
|
|
402
|
+
expect(warnings.filter((message) => message.includes("action 'retry' removed"))).toHaveLength(1);
|
|
403
|
+
expect(warnings.join("\n")).toContain("demo/cart: action 'retry' removed — set version: 2 in src/theme/controllers/cart.js");
|
|
404
|
+
expect(read(root, "pano-plugin.lock.json")).toBe(lock);
|
|
405
|
+
expect(JSON.parse(read(root, `${OUT}/contract/controllers.json`))["demo/cart"].actions).not.toContain("retry");
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
test("the lock keeps the views section written by panoViews", async () => {
|
|
409
|
+
const root = makeFixture({ "pano-plugin.lock.json": JSON.stringify({ format: 1, views: { "demo:A": { contract: 1 } } }) });
|
|
410
|
+
|
|
411
|
+
await build(root);
|
|
412
|
+
|
|
413
|
+
const lock = JSON.parse(read(root, "pano-plugin.lock.json"));
|
|
414
|
+
|
|
415
|
+
expect(lock.views).toEqual({ "demo:A": { contract: 1 } });
|
|
416
|
+
expect(Object.keys(lock.controllers)).toEqual(["demo/cart", "demo/format"]);
|
|
417
|
+
});
|
|
418
|
+
});
|