@skathi-native/cli 0.0.0 → 0.1.0-beta.2

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 (81) hide show
  1. package/aot-inputs.ts +294 -0
  2. package/ascii.ts +397 -0
  3. package/build.ts +1236 -0
  4. package/check/builtins.json +1219 -0
  5. package/check/builtins.ts +112 -0
  6. package/check/codes.ts +178 -0
  7. package/check/command.ts +105 -0
  8. package/check/config.ts +76 -0
  9. package/check/finding.ts +30 -0
  10. package/check/graph.ts +69 -0
  11. package/check/highlight.ts +40 -0
  12. package/check/index.ts +308 -0
  13. package/check/packages.ts +54 -0
  14. package/check/program.ts +209 -0
  15. package/check/report.ts +219 -0
  16. package/check/rules.ts +307 -0
  17. package/check/schema.ts +69 -0
  18. package/check/typing.ts +703 -0
  19. package/cli/assets.ts +114 -0
  20. package/cli/build-app.ts +587 -0
  21. package/cli/config.ts +646 -0
  22. package/cli/create.ts +327 -0
  23. package/cli/dev.ts +92 -0
  24. package/cli/elf.ts +142 -0
  25. package/cli/help.ts +160 -0
  26. package/cli/host-fetch.ts +178 -0
  27. package/cli/host.ts +378 -0
  28. package/cli/icon.ts +303 -0
  29. package/cli/macos-bundle.ts +451 -0
  30. package/cli/pe.ts +429 -0
  31. package/cli/platform.ts +74 -0
  32. package/cli/sk.ts +290 -0
  33. package/cli/skpk.ts +268 -0
  34. package/cli/stamp.ts +254 -0
  35. package/cli/term.ts +176 -0
  36. package/cli/versioninfo.ts +123 -0
  37. package/css-prefixes.ts +118 -0
  38. package/devtools/apps.ts +68 -0
  39. package/devtools/commands.ts +860 -0
  40. package/devtools/ctl.ts +166 -0
  41. package/package.json +52 -3
  42. package/prod.ts +701 -0
  43. package/runtime-path.ts +24 -0
  44. package/tailwind.ts +76 -0
  45. package/templates/minimal/assets/hello.txt +1 -0
  46. package/templates/minimal/src/App.vue +142 -0
  47. package/templates/minimal/src/main.ts +4 -0
  48. package/templates/showcase/assets/hello.txt +1 -0
  49. package/templates/showcase/assets/media/aurora.png +0 -0
  50. package/templates/showcase/assets/media/clip.mp4 +0 -0
  51. package/templates/showcase/assets/media/sticker.tgs +0 -0
  52. package/templates/showcase/src/App.vue +128 -0
  53. package/templates/showcase/src/components/Card.vue +41 -0
  54. package/templates/showcase/src/components/Icon.vue +29 -0
  55. package/templates/showcase/src/components/Missing.vue +26 -0
  56. package/templates/showcase/src/components/Page.vue +41 -0
  57. package/templates/showcase/src/components/Titlebar.vue +128 -0
  58. package/templates/showcase/src/engine.ts +33 -0
  59. package/templates/showcase/src/icons.ts +22 -0
  60. package/templates/showcase/src/main.ts +7 -0
  61. package/templates/showcase/src/pages/AudioPage.vue +124 -0
  62. package/templates/showcase/src/pages/FilesPage.vue +91 -0
  63. package/templates/showcase/src/pages/FormsPage.vue +139 -0
  64. package/templates/showcase/src/pages/GraphicsPage.vue +156 -0
  65. package/templates/showcase/src/pages/HomePage.vue +141 -0
  66. package/templates/showcase/src/pages/LocalePage.vue +71 -0
  67. package/templates/showcase/src/pages/MediaPage.vue +85 -0
  68. package/templates/showcase/src/pages/MotionPage.vue +199 -0
  69. package/templates/showcase/src/pages/NetworkPage.vue +103 -0
  70. package/templates/showcase/src/pages/PermissionsPage.vue +80 -0
  71. package/templates/showcase/src/pages/StoragePage.vue +117 -0
  72. package/templates/showcase/src/pages/StylesPage.vue +199 -0
  73. package/templates/showcase/src/pages/SystemPage.vue +89 -0
  74. package/templates/showcase/src/pages/TrayPage.vue +66 -0
  75. package/templates/showcase/src/pages/WindowPage.vue +128 -0
  76. package/templates/showcase/src/product.ts +2 -0
  77. package/templates/showcase/src/router.ts +149 -0
  78. package/templates/showcase/src/stores/counter.ts +9 -0
  79. package/templates/showcase/src/stores/desktop.ts +115 -0
  80. package/templates/showcase/src/styles.css +140 -0
  81. package/vitecompat.ts +1151 -0
package/cli/config.ts ADDED
@@ -0,0 +1,646 @@
1
+ // `sk.config.json`, as `sk build` reads it. Keys beyond the dev builder's (name, window, devtools):
2
+ //
3
+ // "name" identity: %LOCALAPPDATA%\<name>\storage, the devtools slot
4
+ // "productName", "version" display name (default window.title ?? name); default: package.json version
5
+ // "httpCache": { "maxMB": 256 } disk HTTP cache cap; 0 or false = none
6
+ // "id", "singleInstance", "raiseOnSecondInstance", "protocols" launch identity and URL schemes
7
+ // "assets", "publicDir" what is packaged (relative to the app root); Vite's public/, keyed relative to it
8
+ // "packs": [{ "name": "fonts", "match": ["**/*.woff2"] }, { "name": "assets" }]
9
+ // assets in their own .skpk files, in order; no "match" takes the rest
10
+ // "define", "aliases" Vite's `define`; a bare module name -> the app's own stand-in file
11
+ // "windows": { exeName, icon, company, copyright, description, console,
12
+ // without | features | host } engine parts left out / taken / a whole variant by name
13
+ // "macos": { bundleId, exeName, icon, category, minimumSystemVersion, copyright,
14
+ // microphoneUsageDescription, cameraUsageDescription, sign }
15
+ //
16
+ // Unknown keys under `windows` and `macos` are ERRORS (a typo'd "icno" must not ship an iconless
17
+ // exe silently); unknown top-level keys are warnings, since the file is shared with other tools.
18
+
19
+ import { existsSync, readFileSync, statSync } from "node:fs";
20
+ import { basename, dirname, isAbsolute, join, resolve, sep } from "node:path";
21
+ import type { Platform } from "./platform";
22
+ import { numericVersion } from "./versioninfo";
23
+
24
+ /**
25
+ * The parts of the engine a host can be built WITHOUT: the same list and order as `OPTIONAL` in
26
+ * crates/sk-app/src/payload.rs. `devtools` is the listener, inspector and F12; the rest are cargo features of sk-app.
27
+ */
28
+ export const OPTIONAL = ["devtools", "audio", "clipboard", "storages", "indexeddb", "webgpu"] as const;
29
+ export type Part = (typeof OPTIONAL)[number];
30
+
31
+ /** A host's name: `standard`, else the parts left out as `no-<part>` joined by `+` in {@link OPTIONAL}'s order. The host cache is keyed by it. */
32
+ export type Variant = string;
33
+ export const VARIANTS: Variant[] = ["standard", "no-devtools"];
34
+
35
+ const isPart = (p: unknown): p is Part => typeof p === "string" && (OPTIONAL as readonly string[]).includes(p);
36
+
37
+ export function variantFor(without: Iterable<Part>): Variant {
38
+ const off = new Set(without);
39
+ const names = OPTIONAL.filter((p) => off.has(p)).map((p) => `no-${p}`);
40
+ return names.length ? names.join("+") : "standard";
41
+ }
42
+
43
+ export function partsLeftOut(variant: Variant): Part[] {
44
+ if (variant === "standard") return [];
45
+ const off = new Set<Part>();
46
+ for (const name of variant.split("+")) {
47
+ const p = name.startsWith("no-") ? name.slice(3) : "";
48
+ if (!isPart(p)) {
49
+ throw new ConfigError(
50
+ `${JSON.stringify(variant)} is not a host variant — "standard", or the parts left out as no-<part> joined by "+" ` +
51
+ `(parts: ${OPTIONAL.join(", ")})`,
52
+ );
53
+ }
54
+ off.add(p);
55
+ }
56
+ return OPTIONAL.filter((p) => off.has(p));
57
+ }
58
+
59
+ /** A list of part names: `"audio,clipboard"` or an array; spaces separate too (PowerShell passes an unquoted `audio,clipboard` as `"audio clipboard"`). */
60
+ export function parseParts(list: unknown, where: string): Part[] {
61
+ const items = typeof list === "string" ? list.split(/[\s,]+/).filter(Boolean) : list;
62
+ if (!Array.isArray(items)) throw new ConfigError(`${where} must be a list of parts (${OPTIONAL.join(", ")})`);
63
+ const out = new Set<Part>();
64
+ for (const p of items) {
65
+ if (!isPart(p))
66
+ throw new ConfigError(`${where}: ${JSON.stringify(p)} is not a part — known parts are ${OPTIONAL.join(", ")}`);
67
+ out.add(p);
68
+ }
69
+ return OPTIONAL.filter((p) => out.has(p));
70
+ }
71
+
72
+ /** The variant a selection asks for (`host` is the legacy whole-variant spelling). */
73
+ export function selectVariant(
74
+ sel: { features?: Part[] | undefined; without?: Part[] | undefined; host?: Variant | undefined },
75
+ where: string,
76
+ ): Variant {
77
+ const given = [sel.features && "features", sel.without && "without", sel.host && "host"].filter(Boolean);
78
+ if (given.length > 1) throw new ConfigError(`${where}: give one of ${given.join(" / ")}, not several`);
79
+ if (sel.features) return variantFor(OPTIONAL.filter((p) => !sel.features!.includes(p)));
80
+ if (sel.without) return variantFor(sel.without);
81
+ if (sel.host) return variantFor(partsLeftOut(sel.host));
82
+ return "standard";
83
+ }
84
+
85
+ export function variantWarnings(variant: Variant): string[] {
86
+ const off = new Set(partsLeftOut(variant));
87
+ const out: string[] = [];
88
+ if (!off.has("indexeddb") && off.has("storages")) {
89
+ out.push("the host has indexedDB but no storages: its databases are in memory and nothing survives a restart");
90
+ }
91
+ return out;
92
+ }
93
+
94
+ export function cargoFeatureArgs(variant: Variant): string[] {
95
+ const off = partsLeftOut(variant);
96
+ const features = ["prod", ...(off.includes("devtools") ? ["no-devtools"] : [])];
97
+ const defaults = OPTIONAL.filter((p) => p !== "devtools");
98
+ if (!off.some((p) => p !== "devtools")) return ["--features", features.join(",")];
99
+ return ["--no-default-features", "--features", [...features, ...defaults.filter((p) => !off.includes(p))].join(",")];
100
+ }
101
+
102
+ export interface WindowsConfig {
103
+ exeName?: string | undefined;
104
+ icon?: string | undefined;
105
+ company?: string | undefined;
106
+ copyright?: string | undefined;
107
+ description?: string | undefined;
108
+ console?: boolean;
109
+ host?: Variant;
110
+ features?: Part[];
111
+ without?: Part[];
112
+ variant: Variant;
113
+ /** Whether the config chose the engine parts (`host`, `features` or `without`). */
114
+ selected?: boolean;
115
+ }
116
+
117
+ /** `"macos"`, defaults applied. Read on every platform, so a typo fails a Windows build too. */
118
+ export interface MacConfig {
119
+ bundleId: string;
120
+ bundleIdFrom: "macos.bundleId" | "id";
121
+ exeName: string;
122
+ iconPath: string | null;
123
+ category: string | null;
124
+ minimumSystemVersion: string;
125
+ copyright: string | null;
126
+ microphoneUsageDescription: string | null;
127
+ cameraUsageDescription: string | null;
128
+ /** A codesign identity; `"-"` signs ad hoc; `false` leaves the bundle unsigned. `undefined`: ad hoc wherever `codesign` exists. */
129
+ sign: string | false | undefined;
130
+ }
131
+
132
+ export const MACOS_MINIMUM = "13.0";
133
+
134
+ export interface AppConfig {
135
+ appRoot: string;
136
+ entry: string;
137
+ configPath: string | null;
138
+ raw: Record<string, unknown>;
139
+ name: string;
140
+ productName: string;
141
+ version: string;
142
+ versionFrom: "sk.config.json" | "package.json" | "default";
143
+ assets: string[];
144
+ assetsDeclared: boolean;
145
+ publicDir: string | null;
146
+ packs: PackConfig[];
147
+ windows: WindowsConfig;
148
+ macos: MacConfig;
149
+ exeName: string;
150
+ iconPath: string | null;
151
+ id: string;
152
+ launch: LaunchConfig;
153
+ /** The OSes `sk publish` packages for; null: this OS only. */
154
+ platforms: Platform[] | null;
155
+ warnings: string[];
156
+ }
157
+
158
+ /** One asset pack: `<name>.skpk` beside the payload, holding the assets whose key matches `match` (none: every asset no earlier pack took). */
159
+ export interface PackConfig {
160
+ name: string;
161
+ match: string[] | null;
162
+ }
163
+
164
+ const PACK_NAME = /^[a-z0-9][a-z0-9_-]{0,63}$/;
165
+
166
+ function parsePacks(raw: unknown): PackConfig[] {
167
+ if (raw === undefined) return [];
168
+ const bad = (why: string) => new ConfigError(`sk.config.json "packs": ${why}`);
169
+ if (!Array.isArray(raw)) throw bad('must be an array of { "name": …, "match"?: [globs] }');
170
+ const seen = new Set<string>();
171
+ return raw.map((p, i) => {
172
+ if (!p || typeof p !== "object" || Array.isArray(p)) throw bad(`entry ${i} must be an object`);
173
+ const { name, match, ...rest } = p as Record<string, unknown>;
174
+ const extra = Object.keys(rest).filter((k) => !k.startsWith("$"));
175
+ if (extra.length)
176
+ throw bad(
177
+ `entry ${i} has unknown key(s) ${extra.map((k) => JSON.stringify(k)).join(", ")} — known keys are name, match`,
178
+ );
179
+ if (typeof name !== "string" || !PACK_NAME.test(name) || name === "app") {
180
+ throw bad(
181
+ `entry ${i}: "name" must be lowercase letters, digits, "-" or "_" (and not "app") — it names the file <name>.skpk`,
182
+ );
183
+ }
184
+ if (seen.has(name)) throw bad(`two packs are named ${JSON.stringify(name)}`);
185
+ seen.add(name);
186
+ if (
187
+ match !== undefined &&
188
+ (!Array.isArray(match) || !match.length || match.some((m) => typeof m !== "string" || !m.trim()))
189
+ ) {
190
+ throw bad(
191
+ `${JSON.stringify(name)}: "match" must be a non-empty array of globs over asset keys, or left out to take the rest`,
192
+ );
193
+ }
194
+ return { name, match: match === undefined ? null : (match as string[]).map((m) => m.trim()) };
195
+ });
196
+ }
197
+
198
+ export interface LaunchConfig {
199
+ id: string;
200
+ singleInstance: boolean;
201
+ raiseOnSecondInstance: boolean;
202
+ protocols: string[];
203
+ }
204
+
205
+ const TOP_LEVEL = new Set([
206
+ "name",
207
+ "platforms",
208
+ "productName",
209
+ "version",
210
+ "window",
211
+ "devtools",
212
+ "httpCache",
213
+ "assets",
214
+ "publicDir",
215
+ "packs",
216
+ "define",
217
+ "aliases",
218
+ "check",
219
+ "windows",
220
+ "macos",
221
+ "id",
222
+ "singleInstance",
223
+ "raiseOnSecondInstance",
224
+ "protocols",
225
+ ]);
226
+
227
+ const SCHEME = /^[a-z][a-z0-9+.-]{0,62}$/;
228
+ /** Schemes an app may never claim; the host refuses them too, this makes it a build error. */
229
+ const RESERVED_SCHEMES = new Set([
230
+ "http",
231
+ "https",
232
+ "file",
233
+ "ftp",
234
+ "mailto",
235
+ "tel",
236
+ "data",
237
+ "blob",
238
+ "javascript",
239
+ "about",
240
+ "vbscript",
241
+ "search-ms",
242
+ ]);
243
+ /** An id names a kernel object and a registry value: keep it to a safe alphabet. */
244
+ const ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
245
+
246
+ function readLaunch(raw: Record<string, unknown>, name: string): LaunchConfig {
247
+ const bool = (k: string, dflt: boolean): boolean => {
248
+ const v = raw[k];
249
+ if (v === undefined) return dflt;
250
+ if (typeof v !== "boolean") throw new ConfigError(`sk.config.json "${k}" must be true or false`);
251
+ return v;
252
+ };
253
+ let id = name;
254
+ if (raw.id !== undefined) {
255
+ if (typeof raw.id !== "string" || !ID.test(raw.id.trim())) {
256
+ throw new ConfigError(
257
+ 'sk.config.json "id" must be a reverse-DNS style identifier (letters, digits, ".", "_", "-"), e.g. "gl.argon.notes"',
258
+ );
259
+ }
260
+ id = raw.id.trim();
261
+ }
262
+ const protocols: string[] = [];
263
+ if (raw.protocols !== undefined) {
264
+ if (!Array.isArray(raw.protocols))
265
+ throw new ConfigError('sk.config.json "protocols" must be an array of scheme names');
266
+ for (const p of raw.protocols) {
267
+ const s = typeof p === "string" ? p.trim().toLowerCase().replace(/:\/*$/, "") : "";
268
+ if (!SCHEME.test(s)) {
269
+ throw new ConfigError(
270
+ `sk.config.json "protocols": ${JSON.stringify(p)} is not a URL scheme (a letter, then letters, digits, "+", "-" or ".")`,
271
+ );
272
+ }
273
+ if (RESERVED_SCHEMES.has(s) || s.startsWith("ms-")) {
274
+ throw new ConfigError(
275
+ `sk.config.json "protocols": "${s}" belongs to the browser or to Windows and cannot be registered by an app`,
276
+ );
277
+ }
278
+ if (!protocols.includes(s)) protocols.push(s);
279
+ }
280
+ }
281
+ return {
282
+ id,
283
+ singleInstance: bool("singleInstance", false),
284
+ raiseOnSecondInstance: bool("raiseOnSecondInstance", true),
285
+ protocols,
286
+ };
287
+ }
288
+ const WINDOWS_KEYS = new Set([
289
+ "exeName",
290
+ "icon",
291
+ "company",
292
+ "copyright",
293
+ "description",
294
+ "console",
295
+ "host",
296
+ "features",
297
+ "without",
298
+ ]);
299
+ const MACOS_KEYS = new Set([
300
+ "bundleId",
301
+ "exeName",
302
+ "icon",
303
+ "category",
304
+ "minimumSystemVersion",
305
+ "copyright",
306
+ "microphoneUsageDescription",
307
+ "cameraUsageDescription",
308
+ "sign",
309
+ ]);
310
+ const BUNDLE_ID = /^[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)+$/;
311
+ const CATEGORY = /^public\.app-category\.[a-z]+(-[a-z]+)*$/;
312
+
313
+ function readMacos(
314
+ raw: unknown,
315
+ appRoot: string,
316
+ fallback: { id: string; productName: string; name: string; icon: string | null; copyright?: string | undefined },
317
+ warnings: string[],
318
+ ): MacConfig {
319
+ if (raw !== undefined && (!raw || typeof raw !== "object" || Array.isArray(raw))) {
320
+ throw new ConfigError('sk.config.json "macos" must be an object');
321
+ }
322
+ const m = (raw ?? {}) as Record<string, unknown>;
323
+ for (const key of Object.keys(m)) {
324
+ if (key.startsWith("$")) continue;
325
+ if (!MACOS_KEYS.has(key)) {
326
+ throw new ConfigError(
327
+ `sk.config.json "macos": unknown key ${JSON.stringify(key)} — known keys are ${[...MACOS_KEYS].join(", ")}`,
328
+ );
329
+ }
330
+ }
331
+ const where = (k: string) => `sk.config.json macos.${k}`;
332
+
333
+ const explicitId = str(m.bundleId, where("bundleId"));
334
+ if (explicitId !== undefined && !BUNDLE_ID.test(explicitId)) {
335
+ throw new ConfigError(
336
+ `sk.config.json "macos.bundleId": ${JSON.stringify(explicitId)} is not a bundle identifier — reverse-DNS, ` +
337
+ 'letters, digits, "-" and "." only, e.g. "gl.argon.notes"',
338
+ );
339
+ }
340
+ // "id" allows "_", which a bundle identifier does not.
341
+ const bundleId = explicitId ?? fallback.id.replace(/_/g, "-");
342
+
343
+ let category: string | null = null;
344
+ const cat = str(m.category, where("category"));
345
+ if (cat !== undefined) {
346
+ category = cat.startsWith("public.app-category.") ? cat : `public.app-category.${cat}`;
347
+ if (!CATEGORY.test(category)) {
348
+ throw new ConfigError(
349
+ `sk.config.json "macos.category": ${JSON.stringify(cat)} is not an app category — e.g. "developer-tools" ` +
350
+ 'or "public.app-category.social-networking" (Apple\'s LSApplicationCategoryType list)',
351
+ );
352
+ }
353
+ }
354
+
355
+ const min = str(m.minimumSystemVersion, where("minimumSystemVersion")) ?? MACOS_MINIMUM;
356
+ if (!/^\d+(\.\d+){0,2}$/.test(min)) {
357
+ throw new ConfigError(
358
+ `sk.config.json "macos.minimumSystemVersion": ${JSON.stringify(min)} is not a version like "13.0"`,
359
+ );
360
+ }
361
+ const [maj = 0, minor = 0] = min.split(".").map(Number);
362
+ const [wantMaj = 0, wantMinor = 0] = MACOS_MINIMUM.split(".").map(Number);
363
+ if (maj < wantMaj || (maj === wantMaj && minor < wantMinor)) {
364
+ warnings.push(
365
+ `sk.config.json "macos.minimumSystemVersion" ${min} is below ${MACOS_MINIMUM}, which the host is built for — ` +
366
+ "an older macOS will offer to open the app and then fail to load it",
367
+ );
368
+ }
369
+
370
+ let sign: MacConfig["sign"];
371
+ if (m.sign !== undefined) {
372
+ if (m.sign === false) sign = false;
373
+ else if (typeof m.sign === "string" && m.sign.trim()) sign = m.sign.trim();
374
+ else {
375
+ throw new ConfigError(
376
+ 'sk.config.json "macos.sign" must be a codesign identity, "-" (ad hoc) or false (leave unsigned)',
377
+ );
378
+ }
379
+ }
380
+
381
+ let iconPath = fallback.icon;
382
+ const icon = str(m.icon, where("icon"));
383
+ if (icon) {
384
+ iconPath = isAbsolute(icon) ? icon : join(appRoot, icon);
385
+ if (!existsSync(iconPath)) throw new ConfigError(`sk.config.json "macos.icon": ${iconPath} does not exist`);
386
+ }
387
+
388
+ const exeName =
389
+ sanitizeExeName(str(m.exeName, where("exeName")) ?? fallback.productName) ||
390
+ sanitizeExeName(fallback.name) ||
391
+ "app";
392
+ return {
393
+ bundleId,
394
+ bundleIdFrom: explicitId ? "macos.bundleId" : "id",
395
+ exeName,
396
+ iconPath,
397
+ category,
398
+ minimumSystemVersion: min,
399
+ copyright: str(m.copyright, where("copyright")) ?? fallback.copyright ?? null,
400
+ microphoneUsageDescription: str(m.microphoneUsageDescription, where("microphoneUsageDescription")) ?? null,
401
+ cameraUsageDescription: str(m.cameraUsageDescription, where("cameraUsageDescription")) ?? null,
402
+ sign,
403
+ };
404
+ }
405
+ const ENTRY_CANDIDATES = ["src/main.ts", "src/main.js", "src/main.mts", "main.ts", "main.js"];
406
+
407
+ export class ConfigError extends Error {}
408
+
409
+ export function resolveApp(target: string): { appRoot: string; entry: string } {
410
+ const abs = resolve(target);
411
+ if (!existsSync(abs)) throw new ConfigError(`${target}: no such file or directory`);
412
+ if (statSync(abs).isDirectory()) {
413
+ for (const rel of ENTRY_CANDIDATES) {
414
+ const entry = join(abs, rel);
415
+ if (existsSync(entry)) return { appRoot: abs, entry };
416
+ }
417
+ throw new ConfigError(
418
+ `${target}: no entry module — looked for ${ENTRY_CANDIDATES.join(", ")}. Pass the entry file instead.`,
419
+ );
420
+ }
421
+ if (basename(abs) === "sk.config.json") return resolveApp(dirname(abs));
422
+ const srcDir = dirname(abs);
423
+ const appRoot = srcDir.endsWith(`${sep}src`) ? dirname(srcDir) : srcDir;
424
+ return { appRoot, entry: abs };
425
+ }
426
+
427
+ export function sanitizeExeName(name: string): string {
428
+ let s = name
429
+ .replace(/[<>:"/\\|?*\u0000-\u001f]/g, "")
430
+ .trim()
431
+ .replace(/[. ]+$/, "");
432
+ if (/^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/i.test(s)) s += "-app";
433
+ return s.slice(0, 120);
434
+ }
435
+
436
+ function readJson(path: string): Record<string, unknown> {
437
+ let text: string;
438
+ try {
439
+ text = readFileSync(path, "utf8");
440
+ } catch (e) {
441
+ throw new ConfigError(`${path}: ${(e as Error).message}`);
442
+ }
443
+ try {
444
+ const v = JSON.parse(text);
445
+ if (!v || typeof v !== "object" || Array.isArray(v)) throw new Error("not a JSON object");
446
+ return v as Record<string, unknown>;
447
+ } catch (e) {
448
+ throw new ConfigError(`${path}: ${(e as Error).message}`);
449
+ }
450
+ }
451
+
452
+ const str = (v: unknown, where: string): string | undefined => {
453
+ if (v === undefined) return undefined;
454
+ if (typeof v !== "string") throw new ConfigError(`${where} must be a string`);
455
+ return v.trim() || undefined;
456
+ };
457
+
458
+ /** A config override changes configuration only; entry and relative assets keep the target's app root. */
459
+ export function loadAppConfig(
460
+ target: string,
461
+ options: { configPath?: string | undefined; platform?: Platform | undefined } = {},
462
+ ): AppConfig {
463
+ const { appRoot, entry } = resolveApp(target);
464
+ if (options.configPath !== undefined && !options.configPath.trim()) throw new ConfigError("--config needs a path");
465
+ const configPath = options.configPath === undefined ? join(appRoot, "sk.config.json") : resolve(options.configPath);
466
+ const has = existsSync(configPath);
467
+ if (options.configPath !== undefined && !has) throw new ConfigError(`${configPath}: no such config file`);
468
+ const raw = has ? readJson(configPath) : {};
469
+ const where = (k: string) => `sk.config.json ${k}`;
470
+ const warnings: string[] = [];
471
+
472
+ for (const key of Object.keys(raw)) {
473
+ if (key.startsWith("$") || TOP_LEVEL.has(key)) continue;
474
+ warnings.push(`sk.config.json: unknown key ${JSON.stringify(key)} (ignored by sk build)`);
475
+ }
476
+
477
+ const name = str(raw.name, where('"name"')) ?? basename(appRoot);
478
+ const launch = readLaunch(raw, name);
479
+ if (launch.protocols.length && !launch.singleInstance) {
480
+ warnings.push(
481
+ 'sk.config.json declares "protocols" without "singleInstance": every link to them will start a new instance of the app',
482
+ );
483
+ }
484
+ const win = raw.window && typeof raw.window === "object" ? (raw.window as Record<string, unknown>) : {};
485
+ const title = typeof win.title === "string" && win.title.trim() ? win.title.trim() : undefined;
486
+ const productName = str(raw.productName, where('"productName"')) ?? title ?? name;
487
+
488
+ let version = str(raw.version, where('"version"'));
489
+ let versionFrom: AppConfig["versionFrom"] = "sk.config.json";
490
+ if (!version) {
491
+ const pkgPath = join(appRoot, "package.json");
492
+ const pkgVersion = existsSync(pkgPath) ? readJson(pkgPath).version : undefined;
493
+ if (typeof pkgVersion === "string" && pkgVersion.trim()) {
494
+ version = pkgVersion.trim();
495
+ versionFrom = "package.json";
496
+ } else {
497
+ version = "0.0.0";
498
+ versionFrom = "default";
499
+ warnings.push('no version: set sk.config.json "version" (or package.json "version") — stamping 0.0.0');
500
+ }
501
+ }
502
+ try {
503
+ numericVersion(version);
504
+ } catch (e) {
505
+ throw new ConfigError(`${versionFrom} "version": ${(e as Error).message}`);
506
+ }
507
+
508
+ let assets = ["assets"];
509
+ const assetsDeclared = raw.assets !== undefined;
510
+ if (assetsDeclared) {
511
+ if (!Array.isArray(raw.assets) || raw.assets.some((a) => typeof a !== "string" || !a.trim())) {
512
+ throw new ConfigError('sk.config.json "assets" must be an array of non-empty strings (directories or globs)');
513
+ }
514
+ assets = (raw.assets as string[]).map((a) => a.trim());
515
+ }
516
+
517
+ // Vite's `publicDir`, opt-in here: on by default it would re-key a package that already ships a public/ directory.
518
+ let publicDir: string | null = null;
519
+ if (raw.publicDir !== undefined && raw.publicDir !== false) {
520
+ const p =
521
+ typeof raw.publicDir === "string"
522
+ ? raw.publicDir.trim().replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/+$/, "")
523
+ : "";
524
+ if (!p || p.startsWith("/") || /^[a-zA-Z]:/.test(p) || p.split("/").includes("..")) {
525
+ throw new ConfigError(
526
+ 'sk.config.json "publicDir" must be a directory relative to the app root, inside it (or false)',
527
+ );
528
+ }
529
+ const abs = join(appRoot, p);
530
+ if (!existsSync(abs) || !statSync(abs).isDirectory()) {
531
+ warnings.push(
532
+ `sk.config.json "publicDir": ${JSON.stringify(p)} is not a directory — nothing is packaged from it`,
533
+ );
534
+ }
535
+ publicDir = p;
536
+ }
537
+ const packs = parsePacks(raw.packs);
538
+ if (raw.define !== undefined && (!raw.define || typeof raw.define !== "object" || Array.isArray(raw.define))) {
539
+ throw new ConfigError('sk.config.json "define" must be an object of { "NAME": value }');
540
+ }
541
+
542
+ const windows: WindowsConfig = { variant: "standard" };
543
+ if (raw.windows !== undefined) {
544
+ if (!raw.windows || typeof raw.windows !== "object" || Array.isArray(raw.windows)) {
545
+ throw new ConfigError('sk.config.json "windows" must be an object');
546
+ }
547
+ const w = raw.windows as Record<string, unknown>;
548
+ for (const key of Object.keys(w)) {
549
+ if (key.startsWith("$")) continue;
550
+ if (!WINDOWS_KEYS.has(key)) {
551
+ throw new ConfigError(
552
+ `sk.config.json "windows": unknown key ${JSON.stringify(key)} — known keys are ${[...WINDOWS_KEYS].join(", ")}`,
553
+ );
554
+ }
555
+ }
556
+ windows.exeName = str(w.exeName, where("windows.exeName"));
557
+ windows.icon = str(w.icon, where("windows.icon"));
558
+ windows.company = str(w.company, where("windows.company"));
559
+ windows.copyright = str(w.copyright, where("windows.copyright"));
560
+ windows.description = str(w.description, where("windows.description"));
561
+ if (w.console !== undefined) {
562
+ if (typeof w.console !== "boolean")
563
+ throw new ConfigError('sk.config.json "windows.console" must be true or false');
564
+ windows.console = w.console;
565
+ }
566
+ if (w.host !== undefined) {
567
+ if (typeof w.host !== "string")
568
+ throw new ConfigError('sk.config.json "windows.host" must be a host variant, e.g. "no-devtools"');
569
+ try {
570
+ partsLeftOut(w.host);
571
+ } catch (e) {
572
+ throw new ConfigError(`sk.config.json "windows.host": ${(e as Error).message}`);
573
+ }
574
+ windows.host = w.host;
575
+ }
576
+ if (w.features !== undefined) windows.features = parseParts(w.features, where("windows.features"));
577
+ if (w.without !== undefined) windows.without = parseParts(w.without, where("windows.without"));
578
+ windows.variant = selectVariant(windows, where('"windows"'));
579
+ windows.selected = windows.host !== undefined || windows.features !== undefined || windows.without !== undefined;
580
+ }
581
+
582
+ if (raw.httpCache !== undefined && raw.httpCache !== false) {
583
+ const c = raw.httpCache as Record<string, unknown> | null;
584
+ const mb = c && typeof c === "object" ? c.maxMB : undefined;
585
+ if (typeof mb !== "number" || !(mb >= 0)) {
586
+ throw new ConfigError('sk.config.json "httpCache" must be false or { "maxMB": <MB, 0 = no cache> }');
587
+ }
588
+ }
589
+
590
+ const exeName = sanitizeExeName(windows.exeName ?? productName) || sanitizeExeName(name) || "app";
591
+ let iconPath: string | null = null;
592
+ if (windows.icon) {
593
+ iconPath = isAbsolute(windows.icon) ? windows.icon : join(appRoot, windows.icon);
594
+ if (!existsSync(iconPath)) throw new ConfigError(`sk.config.json "windows.icon": ${iconPath} does not exist`);
595
+ }
596
+ const macos = readMacos(
597
+ raw.macos,
598
+ appRoot,
599
+ { id: launch.id, productName, name, icon: iconPath, copyright: windows.copyright },
600
+ warnings,
601
+ );
602
+ if (options.platform === "macos") {
603
+ if (!macos.iconPath) {
604
+ warnings.push(
605
+ 'no icon: set sk.config.json "macos.icon" (or "windows.icon") to a square .png or an .icns — the app gets the generic macOS icon',
606
+ );
607
+ }
608
+ } else if (options.platform !== "linux" && !windows.icon) {
609
+ warnings.push(
610
+ 'no icon: set sk.config.json "windows.icon" to an .ico or a square .png — the exe gets the default Windows icon',
611
+ );
612
+ }
613
+
614
+ return {
615
+ appRoot,
616
+ entry,
617
+ configPath: has ? configPath : null,
618
+ raw,
619
+ name,
620
+ productName,
621
+ version,
622
+ versionFrom,
623
+ assets,
624
+ assetsDeclared,
625
+ publicDir,
626
+ packs,
627
+ windows,
628
+ macos,
629
+ exeName,
630
+ iconPath,
631
+ id: launch.id,
632
+ launch,
633
+ platforms: readPlatforms(raw.platforms),
634
+ warnings,
635
+ };
636
+ }
637
+
638
+ const PLATFORM_NAMES: readonly Platform[] = ["windows", "macos", "linux"];
639
+
640
+ function readPlatforms(v: unknown): Platform[] | null {
641
+ if (v === undefined) return null;
642
+ if (!Array.isArray(v) || !v.length || !v.every((p): p is Platform => PLATFORM_NAMES.includes(p as Platform))) {
643
+ throw new ConfigError(`sk.config.json "platforms" must be a non-empty list of ${PLATFORM_NAMES.join(", ")}`);
644
+ }
645
+ return [...new Set(v)];
646
+ }