@glazeen/mod-sdk 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +221 -2
- package/bin/bearbar-mod.js +3 -0
- package/client/bb.css +58 -0
- package/client/bb.js +157 -0
- package/dist/bb-JW737zq0.d.ts +266 -0
- package/dist/cli.js +457 -0
- package/dist/global.d.ts +19 -0
- package/dist/global.js +0 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +267 -0
- package/docs/api.md +229 -0
- package/docs/guide.md +200 -0
- package/docs/manifest.md +187 -0
- package/docs/runtime.md +144 -0
- package/docs/styling.md +118 -0
- package/package.json +66 -3
- package/schema/bearbar.mod.schema.json +276 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { basename, join, resolve } from "node:path";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
// src/manifest.ts
|
|
6
|
+
const WIDGET_SIZES = [
|
|
7
|
+
"s",
|
|
8
|
+
"m",
|
|
9
|
+
"l"
|
|
10
|
+
];
|
|
11
|
+
/** Mod ids: 2-64 chars of letters, digits, ".", "_" or "-", starting with a letter or digit. */
|
|
12
|
+
const MOD_ID_PATTERN = /^[a-z0-9][a-z0-9._-]{1,63}$/i;
|
|
13
|
+
// src/validate.ts
|
|
14
|
+
/** "path: message", or just the message for the root. */
|
|
15
|
+
function formatIssue(issue) {
|
|
16
|
+
return issue.path ? `${issue.path}: ${issue.message}` : issue.message;
|
|
17
|
+
}
|
|
18
|
+
const PERMISSION_PATTERN = /^(system|open-url|net:(\*\.)?[a-z0-9-]+(\.[a-z0-9-]+)*)$/i;
|
|
19
|
+
const SEMVER = /^\d+\.\d+\.\d+(-[0-9a-z.-]+)?(\+[0-9a-z.-]+)?$/i;
|
|
20
|
+
const MANIFEST_KEYS = [
|
|
21
|
+
"$schema",
|
|
22
|
+
"id",
|
|
23
|
+
"name",
|
|
24
|
+
"version",
|
|
25
|
+
"apiVersion",
|
|
26
|
+
"description",
|
|
27
|
+
"author",
|
|
28
|
+
"category",
|
|
29
|
+
"sizes",
|
|
30
|
+
"tile",
|
|
31
|
+
"panel",
|
|
32
|
+
"panelSize",
|
|
33
|
+
"permissions",
|
|
34
|
+
"settings"
|
|
35
|
+
];
|
|
36
|
+
const BASE_FIELD_KEYS = [
|
|
37
|
+
"type",
|
|
38
|
+
"label",
|
|
39
|
+
"description",
|
|
40
|
+
"visibleWhen",
|
|
41
|
+
"default"
|
|
42
|
+
];
|
|
43
|
+
const FIELD_KEYS = {
|
|
44
|
+
text: [...BASE_FIELD_KEYS, "placeholder"],
|
|
45
|
+
number: [
|
|
46
|
+
...BASE_FIELD_KEYS,
|
|
47
|
+
"min",
|
|
48
|
+
"max",
|
|
49
|
+
"step"
|
|
50
|
+
],
|
|
51
|
+
toggle: BASE_FIELD_KEYS,
|
|
52
|
+
select: [...BASE_FIELD_KEYS, "options"]
|
|
53
|
+
};
|
|
54
|
+
const FIELD_TYPES = Object.keys(FIELD_KEYS);
|
|
55
|
+
const DEFAULT_TYPE = {
|
|
56
|
+
text: "string",
|
|
57
|
+
select: "string",
|
|
58
|
+
number: "number",
|
|
59
|
+
toggle: "boolean"
|
|
60
|
+
};
|
|
61
|
+
const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
62
|
+
const isNum = (v) => typeof v === "number" && Number.isFinite(v);
|
|
63
|
+
const isPositive = (v) => isNum(v) && v > 0;
|
|
64
|
+
const json = (v) => JSON.stringify(v);
|
|
65
|
+
/** True for an .html file inside the mod folder: relative, no "..", no drive or scheme. */
|
|
66
|
+
function isPagePath(p) {
|
|
67
|
+
return typeof p === "string" && !/^([a-z][a-z0-9+.-]*:|[\\/])/i.test(p) && !p.split(/[\\/]/).includes("..") && /.\.html$/i.test(p);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Checks a parsed bearbar.mod.json and reports every problem, with a path to each.
|
|
71
|
+
*
|
|
72
|
+
* Stricter than the dock, which loads what it can and fills in defaults: use it in tooling
|
|
73
|
+
* and CI to catch mistakes early. It checks the manifest only; the `bearbar-mod validate`
|
|
74
|
+
* command also checks that the pages exist.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* const result = validateManifest(JSON.parse(text));
|
|
78
|
+
* if (!result.ok) for (const e of result.errors) console.error(formatIssue(e));
|
|
79
|
+
*/
|
|
80
|
+
function validateManifest(input) {
|
|
81
|
+
const errors = [];
|
|
82
|
+
const warnings = [];
|
|
83
|
+
const error = (path, message) => errors.push({
|
|
84
|
+
path,
|
|
85
|
+
message
|
|
86
|
+
});
|
|
87
|
+
const warn = (path, message) => warnings.push({
|
|
88
|
+
path,
|
|
89
|
+
message
|
|
90
|
+
});
|
|
91
|
+
if (!isObj(input)) return {
|
|
92
|
+
ok: false,
|
|
93
|
+
errors: [{
|
|
94
|
+
path: "",
|
|
95
|
+
message: "must be a JSON object"
|
|
96
|
+
}],
|
|
97
|
+
warnings
|
|
98
|
+
};
|
|
99
|
+
const m = input;
|
|
100
|
+
for (const key of Object.keys(m)) if (!MANIFEST_KEYS.includes(key)) warn(key, `unknown property${suggest(key, MANIFEST_KEYS)}`);
|
|
101
|
+
if (m.id === void 0) error("id", "is required");
|
|
102
|
+
else if (typeof m.id !== "string" || !MOD_ID_PATTERN.test(m.id)) error("id", "must be 2-64 letters, digits, \".\", \"_\" or \"-\", starting with a letter or digit");
|
|
103
|
+
if (m.name === void 0) error("name", "is required");
|
|
104
|
+
else if (typeof m.name !== "string" || m.name.trim() === "") error("name", "must be a non-empty string");
|
|
105
|
+
if (m.version === void 0) error("version", "is required");
|
|
106
|
+
else if (typeof m.version !== "string" || m.version.trim() === "") error("version", "must be a non-empty string");
|
|
107
|
+
else if (!SEMVER.test(m.version)) warn("version", `${json(m.version)} is not a semantic version like "1.0.0"`);
|
|
108
|
+
if (m.apiVersion !== void 0) {
|
|
109
|
+
if (typeof m.apiVersion !== "number" || !Number.isInteger(m.apiVersion) || m.apiVersion < 1) error("apiVersion", "must be a positive integer");
|
|
110
|
+
else if (m.apiVersion > 1) warn("apiVersion", `${m.apiVersion} is newer than this SDK supports (1)`);
|
|
111
|
+
}
|
|
112
|
+
for (const key of ["description", "author"]) if (m[key] !== void 0 && typeof m[key] !== "string") error(key, "must be a string");
|
|
113
|
+
if (m.category !== void 0) warn("category", "is ignored: mods are always listed under Mods");
|
|
114
|
+
if (m.tile === void 0) error("tile", "is required");
|
|
115
|
+
else if (!isPagePath(m.tile)) error("tile", "must be an .html file inside the mod folder");
|
|
116
|
+
if (m.panel !== void 0 && !isPagePath(m.panel)) error("panel", "must be an .html file inside the mod folder");
|
|
117
|
+
if (m.sizes !== void 0) {
|
|
118
|
+
if (!isObj(m.sizes) || Object.keys(m.sizes).length === 0) error("sizes", "must be an object with at least one of s, m, l");
|
|
119
|
+
else for (const [size, units] of Object.entries(m.sizes)) if (!WIDGET_SIZES.includes(size)) error(`sizes.${size}`, "unknown size (use s, m or l)");
|
|
120
|
+
else if (!isPositive(units)) error(`sizes.${size}`, "must be a positive number");
|
|
121
|
+
}
|
|
122
|
+
if (m.panelSize !== void 0) {
|
|
123
|
+
if (!isObj(m.panelSize) || !isPositive(m.panelSize.width) || !isPositive(m.panelSize.height)) error("panelSize", "must be { width, height } with positive numbers");
|
|
124
|
+
else for (const key of Object.keys(m.panelSize)) if (key !== "width" && key !== "height") error(`panelSize.${key}`, "unknown property");
|
|
125
|
+
if (m.panel === void 0) warn("panelSize", "is set but there is no panel");
|
|
126
|
+
}
|
|
127
|
+
if (m.permissions !== void 0) {
|
|
128
|
+
if (!Array.isArray(m.permissions)) error("permissions", "must be an array");
|
|
129
|
+
else {
|
|
130
|
+
const seen = /* @__PURE__ */ new Set();
|
|
131
|
+
m.permissions.forEach((p, i) => {
|
|
132
|
+
if (typeof p !== "string" || !PERMISSION_PATTERN.test(p)) error(`permissions[${i}]`, `unknown permission ${json(p)} (use "net:<host>", "system" or "open-url")`);
|
|
133
|
+
else if (seen.has(p.toLowerCase())) error(`permissions[${i}]`, `duplicate ${json(p)}`);
|
|
134
|
+
else seen.add(p.toLowerCase());
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (m.settings !== void 0) {
|
|
139
|
+
if (!isObj(m.settings)) error("settings", "must be an object");
|
|
140
|
+
else for (const [key, field] of Object.entries(m.settings)) validateField(`settings.${key}`, field, m.settings, error, warn);
|
|
141
|
+
}
|
|
142
|
+
return errors.length ? {
|
|
143
|
+
ok: false,
|
|
144
|
+
errors,
|
|
145
|
+
warnings
|
|
146
|
+
} : {
|
|
147
|
+
ok: true,
|
|
148
|
+
manifest: m,
|
|
149
|
+
errors: [],
|
|
150
|
+
warnings
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function validateField(path, f, all, error, warn) {
|
|
154
|
+
if (!isObj(f)) {
|
|
155
|
+
error(path, "must be an object");
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const type = f.type;
|
|
159
|
+
if (!FIELD_TYPES.includes(type)) {
|
|
160
|
+
error(`${path}.type`, `must be one of ${FIELD_TYPES.join(", ")}`);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const allowed = FIELD_KEYS[type];
|
|
164
|
+
for (const key of Object.keys(f)) if (!allowed.includes(key)) error(`${path}.${key}`, `not allowed on a ${type} field${suggest(key, allowed)}`);
|
|
165
|
+
if (typeof f.label !== "string") error(`${path}.label`, "must be a string");
|
|
166
|
+
if (f.description !== void 0 && typeof f.description !== "string") error(`${path}.description`, "must be a string");
|
|
167
|
+
const vw = f.visibleWhen;
|
|
168
|
+
if (vw !== void 0) {
|
|
169
|
+
if (!isObj(vw) || typeof vw.field !== "string" || ![
|
|
170
|
+
"string",
|
|
171
|
+
"number",
|
|
172
|
+
"boolean"
|
|
173
|
+
].includes(typeof vw.equals) || Object.keys(vw).some((k) => k !== "field" && k !== "equals")) error(`${path}.visibleWhen`, "must be { field, equals }");
|
|
174
|
+
else if (!(vw.field in all) || `settings.${vw.field}` === path) error(`${path}.visibleWhen.field`, `${json(vw.field)} is not another setting`);
|
|
175
|
+
}
|
|
176
|
+
const def = f.default;
|
|
177
|
+
if (def !== void 0 && typeof def !== DEFAULT_TYPE[type]) error(`${path}.default`, `must be a ${DEFAULT_TYPE[type]}`);
|
|
178
|
+
if (type === "text" && f.placeholder !== void 0 && typeof f.placeholder !== "string") error(`${path}.placeholder`, "must be a string");
|
|
179
|
+
if (type === "number") {
|
|
180
|
+
for (const key of [
|
|
181
|
+
"min",
|
|
182
|
+
"max",
|
|
183
|
+
"step"
|
|
184
|
+
]) if (f[key] !== void 0 && !isNum(f[key])) error(`${path}.${key}`, "must be a number");
|
|
185
|
+
const { min, max, step } = f;
|
|
186
|
+
if (isNum(step) && step <= 0) error(`${path}.step`, "must be greater than 0");
|
|
187
|
+
if (isNum(min) && isNum(max) && min > max) error(`${path}.min`, "is greater than max");
|
|
188
|
+
if (isNum(def) && (isNum(min) && def < min || isNum(max) && def > max)) warn(`${path}.default`, "is outside min..max");
|
|
189
|
+
}
|
|
190
|
+
if (type === "select") {
|
|
191
|
+
const options = f.options;
|
|
192
|
+
if (!Array.isArray(options) || options.length === 0) error(`${path}.options`, "must be a non-empty array");
|
|
193
|
+
else {
|
|
194
|
+
const values = /* @__PURE__ */ new Set();
|
|
195
|
+
options.forEach((o, i) => {
|
|
196
|
+
if (!isObj(o) || typeof o.value !== "string" || typeof o.label !== "string" || Object.keys(o).some((k) => k !== "value" && k !== "label")) error(`${path}.options[${i}]`, "must be { value, label } strings");
|
|
197
|
+
else if (values.has(o.value)) error(`${path}.options[${i}].value`, `duplicate ${json(o.value)}`);
|
|
198
|
+
else values.add(o.value);
|
|
199
|
+
});
|
|
200
|
+
if (typeof def === "string" && values.size && !values.has(def)) error(`${path}.default`, `${json(def)} is not one of the option values`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/** ' (did you mean "x"?)' when a known key is a likely typo of `key`. */
|
|
205
|
+
function suggest(key, known) {
|
|
206
|
+
let best = "";
|
|
207
|
+
let bestDist = 3;
|
|
208
|
+
for (const k of known) {
|
|
209
|
+
const d = distance(key.toLowerCase(), k.toLowerCase());
|
|
210
|
+
if (d < bestDist) [best, bestDist] = [k, d];
|
|
211
|
+
}
|
|
212
|
+
return best ? ` (did you mean ${json(best)}?)` : "";
|
|
213
|
+
}
|
|
214
|
+
/** Levenshtein distance. */
|
|
215
|
+
function distance(a, b) {
|
|
216
|
+
let prev = Array.from({ length: b.length + 1 }, (_, j) => j);
|
|
217
|
+
for (let i = 1; i <= a.length; i++) {
|
|
218
|
+
const cur = [i];
|
|
219
|
+
for (let j = 1; j <= b.length; j++) cur[j] = Math.min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
220
|
+
prev = cur;
|
|
221
|
+
}
|
|
222
|
+
return prev[b.length];
|
|
223
|
+
}
|
|
224
|
+
// src/cli.ts
|
|
225
|
+
/**
|
|
226
|
+
* bearbar-mod: validate and scaffold bearbar mods.
|
|
227
|
+
*
|
|
228
|
+
* bearbar-mod validate [dir...] a mod folder, or a folder of mods (default ".")
|
|
229
|
+
* bearbar-mod init [dir] create a mod in dir (default ".")
|
|
230
|
+
*/
|
|
231
|
+
const MANIFEST = "bearbar.mod.json";
|
|
232
|
+
const SCHEMA_URL = "https://unpkg.com/@glazeen/mod-sdk/schema/bearbar.mod.schema.json";
|
|
233
|
+
const HELP = `bearbar-mod 0.1.0: tools for bearbar dock mods
|
|
234
|
+
|
|
235
|
+
Usage:
|
|
236
|
+
bearbar-mod validate [dir...] Check mods: a mod folder, or a folder of mods (default ".")
|
|
237
|
+
--strict Fail on warnings too
|
|
238
|
+
--json Print the results as JSON
|
|
239
|
+
bearbar-mod init [dir] Create a mod in dir (default "."), without overwriting files
|
|
240
|
+
--name <name> Display name (default: from the folder name)
|
|
241
|
+
|
|
242
|
+
-h, --help Show this help
|
|
243
|
+
-v, --version Show the version
|
|
244
|
+
|
|
245
|
+
Docs: https://github.com/glazeen/bearbar/blob/main/packages/mod-sdk/docs/guide.md`;
|
|
246
|
+
const color = process.stdout.isTTY && !process.env.NO_COLOR;
|
|
247
|
+
const paint = (code) => (s) => color ? `\x1b[${code}m${s}\x1b[0m` : s;
|
|
248
|
+
const red = paint(31);
|
|
249
|
+
const yellow = paint(33);
|
|
250
|
+
const green = paint(32);
|
|
251
|
+
const dim = paint(2);
|
|
252
|
+
function main(argv) {
|
|
253
|
+
const { values, positionals } = parseArgs({
|
|
254
|
+
args: argv,
|
|
255
|
+
allowPositionals: true,
|
|
256
|
+
options: {
|
|
257
|
+
strict: { type: "boolean" },
|
|
258
|
+
json: { type: "boolean" },
|
|
259
|
+
name: { type: "string" },
|
|
260
|
+
help: {
|
|
261
|
+
type: "boolean",
|
|
262
|
+
short: "h"
|
|
263
|
+
},
|
|
264
|
+
version: {
|
|
265
|
+
type: "boolean",
|
|
266
|
+
short: "v"
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
const [command, ...rest] = positionals;
|
|
271
|
+
if (values.version) return print("0.1.0");
|
|
272
|
+
if (values.help || !command) return print(HELP);
|
|
273
|
+
if (command === "validate") return validate(rest.length ? rest : ["."], values);
|
|
274
|
+
if (command === "init") return init(rest[0] ?? ".", values.name);
|
|
275
|
+
console.error(`Unknown command "${command}".\n\n${HELP}`);
|
|
276
|
+
return 2;
|
|
277
|
+
}
|
|
278
|
+
function print(text) {
|
|
279
|
+
console.log(text);
|
|
280
|
+
return 0;
|
|
281
|
+
}
|
|
282
|
+
function validate(roots, opts) {
|
|
283
|
+
const reports = [];
|
|
284
|
+
for (const root of roots) {
|
|
285
|
+
const dir = resolve(root);
|
|
286
|
+
if (!isDir(dir)) {
|
|
287
|
+
console.error(`${root}: not a folder`);
|
|
288
|
+
return 2;
|
|
289
|
+
}
|
|
290
|
+
if (existsSync(join(dir, MANIFEST))) reports.push(checkMod(dir));
|
|
291
|
+
else for (const name of readdirSync(dir).sort()) {
|
|
292
|
+
const sub = join(dir, name);
|
|
293
|
+
if (isDir(sub) && existsSync(join(sub, MANIFEST))) reports.push(checkMod(sub));
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
const failed = reports.filter((r) => r.errors.length || opts.strict && r.warnings.length);
|
|
297
|
+
if (opts.json) console.log(JSON.stringify({
|
|
298
|
+
mods: reports,
|
|
299
|
+
failed: failed.length
|
|
300
|
+
}, null, 2));
|
|
301
|
+
else {
|
|
302
|
+
for (const r of reports) {
|
|
303
|
+
const bad = failed.includes(r);
|
|
304
|
+
const label = r.id && r.id !== basename(r.dir) ? `${basename(r.dir)} (${r.id})` : r.id;
|
|
305
|
+
console.log(`${bad ? red("✗") : green("✓")} ${label ?? basename(r.dir)} ${dim(r.dir)}`);
|
|
306
|
+
for (const e of r.errors) console.log(` ${red("error")} ${formatIssue(e)}`);
|
|
307
|
+
for (const w of r.warnings) console.log(` ${yellow("warning")} ${formatIssue(w)}`);
|
|
308
|
+
}
|
|
309
|
+
if (reports.length === 0) console.log(`No ${MANIFEST} found in ${roots.join(", ")}.`);
|
|
310
|
+
else console.log(`\n${reports.length} mod(s) checked, ${failed.length} failed.`);
|
|
311
|
+
}
|
|
312
|
+
return reports.length === 0 ? 1 : failed.length ? 1 : 0;
|
|
313
|
+
}
|
|
314
|
+
/** The manifest checks plus what needs the file system: pages exist and load nothing remote. */
|
|
315
|
+
function checkMod(dir) {
|
|
316
|
+
let raw;
|
|
317
|
+
try {
|
|
318
|
+
raw = JSON.parse(readFileSync(join(dir, MANIFEST), "utf8").replace(/^/, ""));
|
|
319
|
+
} catch (err) {
|
|
320
|
+
return {
|
|
321
|
+
dir,
|
|
322
|
+
errors: [{
|
|
323
|
+
path: "",
|
|
324
|
+
message: `invalid JSON: ${err.message}`
|
|
325
|
+
}],
|
|
326
|
+
warnings: []
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
const result = validateManifest(raw);
|
|
330
|
+
const report = {
|
|
331
|
+
dir,
|
|
332
|
+
id: typeof raw?.id === "string" ? raw.id : void 0,
|
|
333
|
+
errors: [...result.errors],
|
|
334
|
+
warnings: [...result.warnings]
|
|
335
|
+
};
|
|
336
|
+
if (!result.ok) return report;
|
|
337
|
+
for (const key of ["tile", "panel"]) {
|
|
338
|
+
const page = result.manifest[key];
|
|
339
|
+
if (page === void 0) continue;
|
|
340
|
+
const file = join(dir, page);
|
|
341
|
+
if (!existsSync(file)) {
|
|
342
|
+
report.errors.push({
|
|
343
|
+
path: key,
|
|
344
|
+
message: `file not found: ${page}`
|
|
345
|
+
});
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
for (const url of remoteUrls(readFileSync(file, "utf8"))) report.warnings.push({
|
|
349
|
+
path: key,
|
|
350
|
+
message: `${page} loads ${url}, which the mod sandbox blocks: copy the file into the mod folder, or use bb.fetch for data`
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
return report;
|
|
354
|
+
}
|
|
355
|
+
/** URLs of remote scripts, styles, images and fonts in an HTML page. */
|
|
356
|
+
function remoteUrls(html) {
|
|
357
|
+
const urls = /* @__PURE__ */ new Set();
|
|
358
|
+
const source = html.replace(/<!--[\s\S]*?-->/g, "");
|
|
359
|
+
for (const m of source.matchAll(/<(?:script|img|link|source|iframe|video|audio)\b[^>]*?\s(?:src|href)\s*=\s*["']?((?:https?:)?\/\/[^"'\s>]+)/gi)) urls.add(m[1]);
|
|
360
|
+
for (const m of source.matchAll(/url\(\s*["']?((?:https?:)?\/\/[^"')\s]+)/gi)) urls.add(m[1]);
|
|
361
|
+
for (const m of source.matchAll(/@import\s+["']((?:https?:)?\/\/[^"']+)/gi)) urls.add(m[1]);
|
|
362
|
+
return [...urls];
|
|
363
|
+
}
|
|
364
|
+
function init(target, name) {
|
|
365
|
+
const dir = resolve(target);
|
|
366
|
+
const folder = basename(dir);
|
|
367
|
+
const id = folder.toLowerCase().replace(/[^a-z0-9._-]+/g, "-").replace(/^[^a-z0-9]+|-+$/g, "").slice(0, 64) || "my-mod";
|
|
368
|
+
const display = name ?? (folder.replace(/[-_.]+/g, " ").replace(/^\w/, (c) => c.toUpperCase()) || "My mod");
|
|
369
|
+
const files = {
|
|
370
|
+
[MANIFEST]: `${JSON.stringify({
|
|
371
|
+
$schema: SCHEMA_URL,
|
|
372
|
+
id: id.length >= 2 ? id : `${id}-mod`,
|
|
373
|
+
name: display,
|
|
374
|
+
version: "0.1.0",
|
|
375
|
+
description: "What this widget shows, in one line.",
|
|
376
|
+
sizes: {
|
|
377
|
+
s: 1,
|
|
378
|
+
m: 2
|
|
379
|
+
},
|
|
380
|
+
tile: "tile.html",
|
|
381
|
+
permissions: [],
|
|
382
|
+
settings: { label: {
|
|
383
|
+
type: "text",
|
|
384
|
+
label: "Label",
|
|
385
|
+
default: "Hello"
|
|
386
|
+
} }
|
|
387
|
+
}, null, 2)}\n`,
|
|
388
|
+
"tile.html": `<!doctype html>
|
|
389
|
+
<html lang="en">
|
|
390
|
+
<head>
|
|
391
|
+
<meta charset="utf-8" />
|
|
392
|
+
<!-- The dock injects the bb runtime and a base stylesheet into this page. -->
|
|
393
|
+
</head>
|
|
394
|
+
<body>
|
|
395
|
+
<div class="bb-tile">
|
|
396
|
+
<div class="bb-stack">
|
|
397
|
+
<span class="bb-label" id="label"></span>
|
|
398
|
+
<span class="bb-value" id="value">…</span>
|
|
399
|
+
</div>
|
|
400
|
+
</div>
|
|
401
|
+
<script src="tile.js"><\/script>
|
|
402
|
+
</body>
|
|
403
|
+
</html>
|
|
404
|
+
`,
|
|
405
|
+
"tile.js": `// @ts-check
|
|
406
|
+
/// <reference types="@glazeen/mod-sdk/global" />
|
|
407
|
+
// Editor completion for \`bb\`: npm install --save-dev @glazeen/mod-sdk
|
|
408
|
+
|
|
409
|
+
const $ = (/** @type {string} */ id) => /** @type {HTMLElement} */ (document.getElementById(id));
|
|
410
|
+
|
|
411
|
+
function render() {
|
|
412
|
+
$("label").textContent = String(bb.config.label ?? "");
|
|
413
|
+
$("value").textContent = bb.preview ? "42" : new Date().toLocaleTimeString([], { timeStyle: "short" });
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
bb.ready.then(() => {
|
|
417
|
+
render();
|
|
418
|
+
bb.on("config", render);
|
|
419
|
+
setInterval(render, 30_000);
|
|
420
|
+
});
|
|
421
|
+
`
|
|
422
|
+
};
|
|
423
|
+
mkdirSync(dir, { recursive: true });
|
|
424
|
+
const written = [];
|
|
425
|
+
const skipped = [];
|
|
426
|
+
for (const [file, content] of Object.entries(files)) {
|
|
427
|
+
const path = join(dir, file);
|
|
428
|
+
if (existsSync(path)) skipped.push(file);
|
|
429
|
+
else {
|
|
430
|
+
writeFileSync(path, content);
|
|
431
|
+
written.push(file);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
for (const f of written) console.log(`${green("created")} ${f}`);
|
|
435
|
+
for (const f of skipped) console.log(`${yellow("skipped")} ${f} ${dim("(already exists)")}`);
|
|
436
|
+
console.log(`
|
|
437
|
+
Next:
|
|
438
|
+
1. Copy or link this folder into %APPDATA%\\bearbar\\mods
|
|
439
|
+
(right-click the dock > Add widget… > Mods folder).
|
|
440
|
+
2. Add the widget from Add widget > Mods. Saving a file reloads it.
|
|
441
|
+
3. Check it with: npx @glazeen/mod-sdk validate ${target === "." ? "" : target}`.trimEnd());
|
|
442
|
+
return 0;
|
|
443
|
+
}
|
|
444
|
+
function isDir(p) {
|
|
445
|
+
try {
|
|
446
|
+
return statSync(p).isDirectory();
|
|
447
|
+
} catch {
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
try {
|
|
452
|
+
process.exitCode = main(process.argv.slice(2));
|
|
453
|
+
} catch (err) {
|
|
454
|
+
console.error(err.message);
|
|
455
|
+
process.exitCode = 2;
|
|
456
|
+
}
|
|
457
|
+
export {};
|
package/dist/global.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declares the `bb` global. Reference it from a plain-JS mod page script:
|
|
3
|
+
*
|
|
4
|
+
* /// <reference types="@glazeen/mod-sdk/global" />
|
|
5
|
+
*
|
|
6
|
+
* or add "@glazeen/mod-sdk/global" to `compilerOptions.types` in tsconfig/jsconfig.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Declares the `bb` global. Reference it from a plain-JS mod page script:
|
|
10
|
+
*
|
|
11
|
+
* /// <reference types="@glazeen/mod-sdk/global" />
|
|
12
|
+
*
|
|
13
|
+
* or add "@glazeen/mod-sdk/global" to `compilerOptions.types` in tsconfig/jsconfig.
|
|
14
|
+
*/
|
|
15
|
+
import { t as Bb } from "./bb-JW737zq0.js";
|
|
16
|
+
declare global {
|
|
17
|
+
/** The bearbar mod API, injected by the dock. */
|
|
18
|
+
var bb: Bb;
|
|
19
|
+
}
|
package/dist/global.js
ADDED
|
File without changes
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { A as WidgetCategory, C as SettingField, D as ToggleField, E as TextField, M as defineManifest, O as WIDGET_CATEGORIES, S as SelectOption, T as SettingsSchema, _ as MOD_ID_PATTERN, a as HeadersLike, b as Permission, c as STORAGE_QUOTA, d as THEME_TOKENS, f as Theme, g as InferConfig, h as API_VERSION, i as BbResponse, j as WidgetSize, k as WIDGET_SIZES, l as Surface, m as ThemeToken, n as BbEvents, o as Register, p as ThemeAttrs, r as BbFetchInit, s as RegisteredConfig, t as Bb, u as SystemStats, v as ModManifest, w as SettingFieldBase, x as SelectField, y as NumberField } from "./bb-JW737zq0.js";
|
|
2
|
+
/** A problem found in a manifest. */
|
|
3
|
+
export interface ManifestIssue {
|
|
4
|
+
/** Where the problem is, e.g. "settings.unit.default" or "permissions[1]". Empty for the root. */
|
|
5
|
+
path: string;
|
|
6
|
+
message: string;
|
|
7
|
+
}
|
|
8
|
+
export type ValidationResult = {
|
|
9
|
+
ok: true;
|
|
10
|
+
manifest: ModManifest;
|
|
11
|
+
errors: [];
|
|
12
|
+
warnings: ManifestIssue[];
|
|
13
|
+
} | {
|
|
14
|
+
ok: false;
|
|
15
|
+
manifest?: undefined;
|
|
16
|
+
errors: ManifestIssue[];
|
|
17
|
+
warnings: ManifestIssue[];
|
|
18
|
+
};
|
|
19
|
+
/** "path: message", or just the message for the root. */
|
|
20
|
+
export declare function formatIssue(issue: ManifestIssue): string;
|
|
21
|
+
/**
|
|
22
|
+
* Checks a parsed bearbar.mod.json and reports every problem, with a path to each.
|
|
23
|
+
*
|
|
24
|
+
* Stricter than the dock, which loads what it can and fills in defaults: use it in tooling
|
|
25
|
+
* and CI to catch mistakes early. It checks the manifest only; the `bearbar-mod validate`
|
|
26
|
+
* command also checks that the pages exist.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const result = validateManifest(JSON.parse(text));
|
|
30
|
+
* if (!result.ok) for (const e of result.errors) console.error(formatIssue(e));
|
|
31
|
+
*/
|
|
32
|
+
export declare function validateManifest(input: unknown): ValidationResult;
|
|
33
|
+
export { API_VERSION, Bb, BbEvents, BbFetchInit, BbResponse, HeadersLike, InferConfig, MOD_ID_PATTERN, ModManifest, NumberField, Permission, Register, RegisteredConfig, STORAGE_QUOTA, SelectField, SelectOption, SettingField, SettingFieldBase, SettingsSchema, Surface, SystemStats, THEME_TOKENS, TextField, Theme, ThemeAttrs, ThemeToken, ToggleField, WIDGET_CATEGORIES, WIDGET_SIZES, WidgetCategory, WidgetSize, defineManifest };
|