@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,479 @@
|
|
|
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 { nullHost } from "../controller/index.js";
|
|
6
|
+
import { readLock, writeLockSection, diffSection } from "./lock.js";
|
|
7
|
+
import { loadConfig } from "../config.js";
|
|
8
|
+
import {
|
|
9
|
+
ensureControllersBuilt,
|
|
10
|
+
resetControllersBuild,
|
|
11
|
+
resolvePaths,
|
|
12
|
+
writeIfChanged,
|
|
13
|
+
} from "./controllers.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Key enumeration of the built controllers and the package index (doc 02 section 5.2).
|
|
17
|
+
*
|
|
18
|
+
* Nothing here is typed by the author: the build imports the fresh `controllers.mjs`, calls
|
|
19
|
+
* `create(name, nullHost, {})` and reads the keys of the state and of the actions. It writes
|
|
20
|
+
*
|
|
21
|
+
* contract/controllers.json { "<ns>/<name>": { version, scope, state, actions, types } }
|
|
22
|
+
* pano-plugin.json the package index (format, pluginId, namespace, version, sdk, views, styles, badges)
|
|
23
|
+
*
|
|
24
|
+
* and keeps the `controllers` section of `pano-plugin.lock.json`: a key removed without a raised
|
|
25
|
+
* `version` fails `build` and is one warning line in watch.
|
|
26
|
+
*
|
|
27
|
+
* THE SHARED BUILD CONTEXT. The other kit plugins (views, helpers, samples, styles, widgets) tell the
|
|
28
|
+
* package index what they found by writing to the context of the plugin root:
|
|
29
|
+
*
|
|
30
|
+
* import { getBuildContext } from "./meta.js";
|
|
31
|
+
* const ctx = getBuildContext(root);
|
|
32
|
+
* ctx.views.ProductCard = { samples: ["filled"], controllers: { "market/cart": 1 }, helpers: ["lib/sale.js"],
|
|
33
|
+
* roots: ["div"], classes: ["market-product-card"] }; // merged, extra keys (attrs) are kept
|
|
34
|
+
* ctx.styles = { fallback: "client/fallback.css", own: "client/plugin.css", hash: "9f2c1a7e", icons: true };
|
|
35
|
+
* ctx.badges.semanticClasses = true; // controllers is computed here
|
|
36
|
+
* ctx.viewImports = "warn"; // migration mode
|
|
37
|
+
*
|
|
38
|
+
* The context lives on `globalThis` under `Symbol.for("pano.plugin-kit.build-context")` (a Map keyed
|
|
39
|
+
* by the absolute plugin root), so two copies of the kit still share it. View names that are only in
|
|
40
|
+
* `contract/views.json` (written by panoViews) are listed with empty entries.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
const CONTEXT_KEY = Symbol.for("pano.plugin-kit.build-context");
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @typedef {object} ViewInfo
|
|
47
|
+
* @property {string[]} [samples] state names of the view's samples file
|
|
48
|
+
* @property {Record<string, number>} [controllers] literal controller names used by the view, with their versions
|
|
49
|
+
* @property {string[]} [helpers] helper closure, paths under `src/theme`
|
|
50
|
+
* @property {string[]} [roots] root element tags
|
|
51
|
+
* @property {string[]} [classes] semantic classes of the view
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @typedef {object} BuildContext
|
|
56
|
+
* @property {Record<string, ViewInfo & Record<string, any>>} views by view name (`ProductCard`, not `market:ProductCard`)
|
|
57
|
+
* @property {{ fallback?: string, own?: string, hash?: string, icons?: boolean } | null} styles
|
|
58
|
+
* @property {{ samples?: boolean, semanticClasses?: boolean, widgets?: boolean, openapi?: boolean }} badges
|
|
59
|
+
* @property {'warn' | undefined} viewImports
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The shared build context of one plugin root (created on first use).
|
|
64
|
+
*
|
|
65
|
+
* @param {string} [root] plugin root, default the current directory
|
|
66
|
+
* @returns {BuildContext}
|
|
67
|
+
*/
|
|
68
|
+
export function getBuildContext(root = process.cwd()) {
|
|
69
|
+
const store = (globalThis[CONTEXT_KEY] ??= new Map());
|
|
70
|
+
const key = path.resolve(root);
|
|
71
|
+
|
|
72
|
+
if (!store.has(key)) {
|
|
73
|
+
store.set(key, { views: {}, styles: null, badges: {}, viewImports: undefined });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return store.get(key);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* `cart` -> `Cart`, `cartItems` -> `CartItems`.
|
|
81
|
+
*
|
|
82
|
+
* @param {string} name
|
|
83
|
+
* @returns {string}
|
|
84
|
+
*/
|
|
85
|
+
export function pascal(name) {
|
|
86
|
+
return name.charAt(0).toUpperCase() + name.slice(1);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Names of the typedefs of a controller in `controllers.types.js` (declared by the author, else generated).
|
|
91
|
+
*
|
|
92
|
+
* @param {string} name controller name without namespace
|
|
93
|
+
* @returns {{ state: string, actions: string }}
|
|
94
|
+
*/
|
|
95
|
+
export function typeNames(name) {
|
|
96
|
+
return { state: `${pascal(name)}State`, actions: `${pascal(name)}Actions` };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @typedef {object} EnumeratedController
|
|
101
|
+
* @property {number} version
|
|
102
|
+
* @property {'app' | 'instance'} scope
|
|
103
|
+
* @property {string[]} state keys of the initial state
|
|
104
|
+
* @property {string[]} actions names of the actions
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @typedef {object} Enumeration
|
|
109
|
+
* @property {string} namespace
|
|
110
|
+
* @property {string} pluginId
|
|
111
|
+
* @property {Record<string, EnumeratedController>} controllers by short name, sorted
|
|
112
|
+
*/
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @typedef {object} MetaOptions
|
|
116
|
+
* @property {string} [pluginId] default: `pluginId` of gradle.properties
|
|
117
|
+
* @property {string} [namespace] default: derived like the rest of the kit
|
|
118
|
+
* @property {string} [outDir] package folder, default `src/main/resources/plugin-ui`
|
|
119
|
+
* @property {string} [root] plugin root, default the current directory
|
|
120
|
+
* @property {string} [dir] controllers folder, default `src/theme/controllers`
|
|
121
|
+
* @property {string} [version] plugin version for pano-plugin.json, default: package.json, else 0.0.0
|
|
122
|
+
* @property {number} [sdk] panoSdk level the package was built for, default 2
|
|
123
|
+
* @property {boolean} [watch] treat the build as a watch build (default: rollup's `watchMode`)
|
|
124
|
+
*/
|
|
125
|
+
|
|
126
|
+
/** @type {Map<string, Promise<Enumeration>>} */
|
|
127
|
+
const enumerations = new Map();
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param {MetaOptions} options
|
|
131
|
+
* @returns {string}
|
|
132
|
+
*/
|
|
133
|
+
function enumerationKey(options) {
|
|
134
|
+
const { root, dir, outDir } = resolvePaths(options);
|
|
135
|
+
|
|
136
|
+
return [root, dir, outDir].join("\0");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Imports the fresh controllers.mjs and reads the keys of every controller. Shared by the plugins of
|
|
141
|
+
* one build cycle.
|
|
142
|
+
*
|
|
143
|
+
* @param {MetaOptions} options
|
|
144
|
+
* @returns {Promise<Enumeration>}
|
|
145
|
+
*/
|
|
146
|
+
export function enumerateControllers(options) {
|
|
147
|
+
const key = enumerationKey(options);
|
|
148
|
+
let promise = enumerations.get(key);
|
|
149
|
+
|
|
150
|
+
if (!promise) {
|
|
151
|
+
promise = doEnumerate(options);
|
|
152
|
+
promise.catch(() => {});
|
|
153
|
+
enumerations.set(key, promise);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return promise;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Starts a new build cycle for the enumeration (the controllers build is reset by panoControllers).
|
|
161
|
+
*
|
|
162
|
+
* @param {MetaOptions} options
|
|
163
|
+
*/
|
|
164
|
+
export function resetEnumeration(options) {
|
|
165
|
+
enumerations.delete(enumerationKey(options));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Imports a copy of the file under a name never used before: some runtimes (Bun) ignore a query string
|
|
170
|
+
* in their module cache, and the file is self-contained, so it runs anywhere.
|
|
171
|
+
*
|
|
172
|
+
* @param {string} file
|
|
173
|
+
* @returns {Promise<any>}
|
|
174
|
+
*/
|
|
175
|
+
async function importFresh(file) {
|
|
176
|
+
const folder = fs.mkdtempSync(path.join(os.tmpdir(), "pano-kit-controllers-"));
|
|
177
|
+
const copy = path.join(folder, "controllers.mjs");
|
|
178
|
+
|
|
179
|
+
try {
|
|
180
|
+
fs.copyFileSync(file, copy);
|
|
181
|
+
|
|
182
|
+
return await import(pathToFileURL(copy).href);
|
|
183
|
+
} finally {
|
|
184
|
+
fs.rmSync(folder, { recursive: true, force: true });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @param {MetaOptions} options
|
|
190
|
+
* @returns {Promise<Enumeration>}
|
|
191
|
+
*/
|
|
192
|
+
async function doEnumerate(options) {
|
|
193
|
+
const built = await ensureControllersBuilt(options);
|
|
194
|
+
let { pluginId, namespace } = options;
|
|
195
|
+
|
|
196
|
+
if (!pluginId || !namespace) {
|
|
197
|
+
const config = await loadConfig(resolvePaths(options).root);
|
|
198
|
+
|
|
199
|
+
pluginId = pluginId || config.pluginId;
|
|
200
|
+
namespace = namespace || config.namespace;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** @type {Enumeration} */
|
|
204
|
+
const result = { namespace, pluginId, controllers: {} };
|
|
205
|
+
|
|
206
|
+
if (!built.file) return result;
|
|
207
|
+
|
|
208
|
+
const mod = await importFresh(built.file);
|
|
209
|
+
|
|
210
|
+
for (const name of Object.keys(mod.controllers).sort()) {
|
|
211
|
+
const definition = mod.controllers[name];
|
|
212
|
+
let controller;
|
|
213
|
+
|
|
214
|
+
try {
|
|
215
|
+
controller = mod.create(name, nullHost, {});
|
|
216
|
+
|
|
217
|
+
result.controllers[name] = {
|
|
218
|
+
version: definition.version,
|
|
219
|
+
scope: definition.scope,
|
|
220
|
+
state: Object.keys(controller.get() ?? {}),
|
|
221
|
+
actions: Object.keys(controller.actions ?? {}),
|
|
222
|
+
};
|
|
223
|
+
} catch (error) {
|
|
224
|
+
throw new Error(
|
|
225
|
+
`[pano-plugin] ${namespace}/${name}: cannot read its keys on the null host (${error?.message ?? error}) — state() and actions() must run without the network or the browser; do that work in start() or inside an action`,
|
|
226
|
+
);
|
|
227
|
+
} finally {
|
|
228
|
+
try {
|
|
229
|
+
controller?.destroy();
|
|
230
|
+
} catch {
|
|
231
|
+
// enumeration only
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return result;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The `controllers` section of the lock file: `{ "<ns>/<name>": { version, state, actions } }`.
|
|
241
|
+
*
|
|
242
|
+
* @param {Enumeration} enumeration
|
|
243
|
+
* @returns {Record<string, { version: number, state: string[], actions: string[] }>}
|
|
244
|
+
*/
|
|
245
|
+
export function lockSectionOf(enumeration) {
|
|
246
|
+
/** @type {Record<string, any>} */
|
|
247
|
+
const section = {};
|
|
248
|
+
|
|
249
|
+
for (const [name, info] of Object.entries(enumeration.controllers)) {
|
|
250
|
+
section[`${enumeration.namespace}/${name}`] = {
|
|
251
|
+
version: info.version,
|
|
252
|
+
state: info.state,
|
|
253
|
+
actions: info.actions,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return section;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* `contract/controllers.json` content.
|
|
262
|
+
*
|
|
263
|
+
* @param {Enumeration} enumeration
|
|
264
|
+
* @returns {Record<string, any>}
|
|
265
|
+
*/
|
|
266
|
+
export function controllersContract(enumeration) {
|
|
267
|
+
/** @type {Record<string, any>} */
|
|
268
|
+
const contract = {};
|
|
269
|
+
|
|
270
|
+
for (const [name, info] of Object.entries(enumeration.controllers)) {
|
|
271
|
+
contract[`${enumeration.namespace}/${name}`] = {
|
|
272
|
+
version: info.version,
|
|
273
|
+
scope: info.scope,
|
|
274
|
+
state: info.state,
|
|
275
|
+
actions: info.actions,
|
|
276
|
+
types: typeNames(name),
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return contract;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Compares with the committed lock and updates it.
|
|
285
|
+
*
|
|
286
|
+
* @param {Enumeration} enumeration
|
|
287
|
+
* @param {{ root: string, watch: boolean, warn: (message: string) => void }} context
|
|
288
|
+
* @returns {{ written: boolean, breaking: string[] }}
|
|
289
|
+
* @throws {Error} outside watch mode when a key was removed without a raised version
|
|
290
|
+
*/
|
|
291
|
+
export function checkControllerLock(enumeration, { root, watch, warn }) {
|
|
292
|
+
const next = lockSectionOf(enumeration);
|
|
293
|
+
const previous = readLock(root).controllers;
|
|
294
|
+
const { breaking } = diffSection("controllers", previous, next);
|
|
295
|
+
|
|
296
|
+
if (breaking.length > 0) {
|
|
297
|
+
if (!watch) {
|
|
298
|
+
throw new Error(
|
|
299
|
+
`[pano-plugin] ${breaking.join("\n[pano-plugin] ")}\n[pano-plugin] (never published? delete pano-plugin.lock.json)`,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
for (const message of breaking) warn(`[pano-plugin] ${message}`);
|
|
304
|
+
|
|
305
|
+
return { written: false, breaking };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (watch) return { written: false, breaking };
|
|
309
|
+
|
|
310
|
+
return { written: writeLockSection(root, "controllers", next), breaking };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* @param {Record<string, any>} value
|
|
315
|
+
* @returns {Record<string, any>}
|
|
316
|
+
*/
|
|
317
|
+
function sortedObject(value) {
|
|
318
|
+
return Object.fromEntries(Object.entries(value).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* @param {string} root
|
|
323
|
+
* @param {string | undefined} given
|
|
324
|
+
* @returns {string}
|
|
325
|
+
*/
|
|
326
|
+
function versionOf(root, given) {
|
|
327
|
+
if (given) return given;
|
|
328
|
+
|
|
329
|
+
try {
|
|
330
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8"));
|
|
331
|
+
|
|
332
|
+
if (typeof pkg.version === "string" && pkg.version) return pkg.version;
|
|
333
|
+
} catch {
|
|
334
|
+
// no package.json
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return "0.0.0";
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* View names written by panoViews to `contract/views.json` (keys are `<ns>:<Name>`).
|
|
342
|
+
*
|
|
343
|
+
* @param {string} outDir
|
|
344
|
+
* @returns {string[] | null} null when the file does not exist
|
|
345
|
+
*/
|
|
346
|
+
function viewNamesOnDisk(outDir) {
|
|
347
|
+
const file = path.join(outDir, "contract", "views.json");
|
|
348
|
+
|
|
349
|
+
if (!fs.existsSync(file)) return null;
|
|
350
|
+
|
|
351
|
+
try {
|
|
352
|
+
const json = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
353
|
+
|
|
354
|
+
return Object.keys(json.views ?? {}).map((id) => id.slice(id.indexOf(":") + 1));
|
|
355
|
+
} catch {
|
|
356
|
+
return [];
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Builds the content of `pano-plugin.json`.
|
|
362
|
+
*
|
|
363
|
+
* @param {MetaOptions} options
|
|
364
|
+
* @param {Enumeration} enumeration
|
|
365
|
+
* @param {BuildContext} context
|
|
366
|
+
* @returns {Record<string, any>}
|
|
367
|
+
*/
|
|
368
|
+
export function packageIndex(options, enumeration, context) {
|
|
369
|
+
const { root, outDir } = resolvePaths(options);
|
|
370
|
+
const names = new Set(Object.keys(context.views));
|
|
371
|
+
const onDisk = viewNamesOnDisk(outDir);
|
|
372
|
+
|
|
373
|
+
for (const name of onDisk ?? []) names.add(name);
|
|
374
|
+
|
|
375
|
+
/** @type {Record<string, any>} */
|
|
376
|
+
const views = {};
|
|
377
|
+
|
|
378
|
+
for (const name of [...names].sort()) {
|
|
379
|
+
const entry = { samples: [], controllers: {}, helpers: [], roots: [], classes: [] };
|
|
380
|
+
|
|
381
|
+
Object.assign(entry, context.views[name] ?? {});
|
|
382
|
+
entry.controllers = sortedObject(entry.controllers ?? {});
|
|
383
|
+
views[name] = entry;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const hasControllers = Object.keys(enumeration.controllers).length > 0;
|
|
387
|
+
/** @type {Record<string, any>} */
|
|
388
|
+
const index = {
|
|
389
|
+
format: 1,
|
|
390
|
+
pluginId: enumeration.pluginId,
|
|
391
|
+
namespace: enumeration.namespace,
|
|
392
|
+
version: versionOf(root, options.version),
|
|
393
|
+
sdk: options.sdk ?? 2,
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
if (context.viewImports === "warn" || process.env.PANO_VIEW_IMPORTS === "warn") {
|
|
397
|
+
index.viewImports = "warn";
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (onDisk || Object.keys(context.views).length > 0) index.contract = "contract/views.json";
|
|
401
|
+
if (hasControllers) index.controllers = "contract/controllers.json";
|
|
402
|
+
|
|
403
|
+
index.views = views;
|
|
404
|
+
|
|
405
|
+
if (context.styles) index.styles = context.styles;
|
|
406
|
+
|
|
407
|
+
index.badges = {
|
|
408
|
+
controllers: hasControllers,
|
|
409
|
+
samples: context.badges.samples === true,
|
|
410
|
+
semanticClasses: context.badges.semanticClasses === true,
|
|
411
|
+
widgets: context.badges.widgets === true,
|
|
412
|
+
openapi: context.badges.openapi === true,
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
return index;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Writes `pano-plugin.json` from the context as it is right now. Exported so a command can
|
|
420
|
+
* refresh the index without a rollup run.
|
|
421
|
+
*
|
|
422
|
+
* @param {MetaOptions} options
|
|
423
|
+
* @returns {Promise<boolean>} true when the file changed
|
|
424
|
+
*/
|
|
425
|
+
export async function writePackageIndex(options) {
|
|
426
|
+
const { root, outDir } = resolvePaths(options);
|
|
427
|
+
const enumeration = await enumerateControllers(options);
|
|
428
|
+
const index = packageIndex(options, enumeration, getBuildContext(root));
|
|
429
|
+
|
|
430
|
+
return writeIfChanged(path.join(outDir, "pano-plugin.json"), JSON.stringify(index, null, 2) + "\n");
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Key enumeration of the built controllers, `contract/controllers.json`, the controllers lock
|
|
435
|
+
* section and `pano-plugin.json` (doc 02 section 5.2). List it in the same configuration as
|
|
436
|
+
* `panoControllers` (the preset does): `controllers.json` and the lock are handled in `writeBundle`,
|
|
437
|
+
* `pano-plugin.json` in `closeBundle`, after the other plugins of the configuration had their say.
|
|
438
|
+
*
|
|
439
|
+
* @param {MetaOptions} options
|
|
440
|
+
* @returns {import('rollup').Plugin}
|
|
441
|
+
*/
|
|
442
|
+
export function panoMeta(options) {
|
|
443
|
+
const { root, outDir } = resolvePaths(options);
|
|
444
|
+
|
|
445
|
+
return {
|
|
446
|
+
name: "pano-meta",
|
|
447
|
+
buildStart() {
|
|
448
|
+
resetControllersBuild(options);
|
|
449
|
+
resetEnumeration(options);
|
|
450
|
+
},
|
|
451
|
+
async writeBundle() {
|
|
452
|
+
const enumeration = await enumerateControllers(options);
|
|
453
|
+
const watch = options.watch ?? Boolean(this.meta?.watchMode);
|
|
454
|
+
|
|
455
|
+
checkControllerLock(enumeration, {
|
|
456
|
+
root,
|
|
457
|
+
watch,
|
|
458
|
+
warn: (message) => this.warn(message),
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
const file = path.join(outDir, "contract", "controllers.json");
|
|
462
|
+
|
|
463
|
+
if (Object.keys(enumeration.controllers).length > 0) {
|
|
464
|
+
writeIfChanged(file, JSON.stringify(controllersContract(enumeration), null, 2) + "\n");
|
|
465
|
+
} else {
|
|
466
|
+
fs.rmSync(file, { force: true });
|
|
467
|
+
}
|
|
468
|
+
},
|
|
469
|
+
async closeBundle() {
|
|
470
|
+
try {
|
|
471
|
+
await enumerateControllers(options);
|
|
472
|
+
} catch {
|
|
473
|
+
return; // writeBundle reported it
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
await writePackageIndex(options);
|
|
477
|
+
},
|
|
478
|
+
};
|
|
479
|
+
}
|