@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/pe.ts ADDED
@@ -0,0 +1,429 @@
1
+ // The PE side of `sk build`: a pure-TS reader for the header fields and resource-tree facts the
2
+ // stamper needs, plus bun:ffi bindings to the Win32 resource-update API that does the writing.
3
+ //
4
+ // Win32 rather than a PE writer of our own: `UpdateResourceW` rebuilds `.rsrc`, moves `.reloc` behind
5
+ // it and fixes every header field that moves. A pure-TS writer is the road to stamping on another OS.
6
+ // The reader is file-backed and page-cached: everything asked of it is in the first few KB or in
7
+ // `.rsrc`'s directory tables.
8
+
9
+ import { dlopen, FFIType, ptr, toArrayBuffer } from "bun:ffi";
10
+ import { closeSync, fstatSync, openSync, readSync, writeSync } from "node:fs";
11
+
12
+ const CHUNK = 64 * 1024;
13
+
14
+ class FileReader {
15
+ readonly fd: number;
16
+ readonly size: number;
17
+ #cache = new Map<number, Uint8Array>();
18
+
19
+ constructor(path: string) {
20
+ this.fd = openSync(path, "r");
21
+ this.size = fstatSync(this.fd).size;
22
+ }
23
+
24
+ #chunk(index: number): Uint8Array {
25
+ let c = this.#cache.get(index);
26
+ if (!c) {
27
+ const start = index * CHUNK;
28
+ const len = Math.max(0, Math.min(CHUNK, this.size - start));
29
+ c = new Uint8Array(len);
30
+ if (len) readSync(this.fd, c, 0, len, start);
31
+ this.#cache.set(index, c);
32
+ }
33
+ return c;
34
+ }
35
+
36
+ bytes(off: number, len: number): Uint8Array {
37
+ if (off < 0 || off + len > this.size) {
38
+ throw new Error(`PE: read [${off}, +${len}) outside the ${this.size}-byte file`);
39
+ }
40
+ const out = new Uint8Array(len);
41
+ let done = 0;
42
+ while (done < len) {
43
+ const pos = off + done;
44
+ const c = this.#chunk(Math.floor(pos / CHUNK));
45
+ const inChunk = pos % CHUNK;
46
+ const n = Math.min(len - done, c.length - inChunk);
47
+ out.set(c.subarray(inChunk, inChunk + n), done);
48
+ done += n;
49
+ }
50
+ return out;
51
+ }
52
+
53
+ u16(off: number): number {
54
+ const b = this.bytes(off, 2);
55
+ return b[0]! | (b[1]! << 8);
56
+ }
57
+
58
+ u32(off: number): number {
59
+ const b = this.bytes(off, 4);
60
+ return (b[0]! | (b[1]! << 8) | (b[2]! << 16) | (b[3]! << 24)) >>> 0;
61
+ }
62
+
63
+ close() {
64
+ closeSync(this.fd);
65
+ }
66
+ }
67
+
68
+ export const RT = {
69
+ ICON: 3,
70
+ RCDATA: 10,
71
+ GROUP_ICON: 14,
72
+ VERSION: 16,
73
+ MANIFEST: 24,
74
+ } as const;
75
+
76
+ export const SUBSYSTEM = { GUI: 2, CONSOLE: 3 } as const;
77
+
78
+ export interface Section {
79
+ name: string;
80
+ va: number;
81
+ vsize: number;
82
+ raw: number;
83
+ rawSize: number;
84
+ }
85
+
86
+ export interface ResourceEntry {
87
+ type: number | string;
88
+ name: number | string;
89
+ lang: number;
90
+ rva: number;
91
+ size: number;
92
+ fileOffset: number;
93
+ }
94
+
95
+ export interface PeInfo {
96
+ is64: boolean;
97
+ machine: number;
98
+ subsystem: number;
99
+ subsystemOffset: number;
100
+ timeDateStamp: number;
101
+ sizeOfImage: number;
102
+ sections: Section[];
103
+ /** The certificate table. Its "RVA" is a FILE offset: the one data directory the loader never maps, so a signature can sit after the image. */
104
+ security: { offset: number; size: number };
105
+ securityEntryOffset: number;
106
+ resources: ResourceEntry[];
107
+ overlay: number;
108
+ }
109
+
110
+ export function readPe(path: string): PeInfo {
111
+ const r = new FileReader(path);
112
+ try {
113
+ if (r.u16(0) !== 0x5a4d) throw new Error(`${path}: not an executable (no MZ header)`);
114
+ const pe = r.u32(0x3c);
115
+ if (r.u32(pe) !== 0x4550) throw new Error(`${path}: not a PE image`);
116
+ const coff = pe + 4;
117
+ const machine = r.u16(coff);
118
+ const nSections = r.u16(coff + 2);
119
+ const timeDateStamp = r.u32(coff + 4);
120
+ const optSize = r.u16(coff + 16);
121
+ const opt = coff + 20;
122
+ const magic = r.u16(opt);
123
+ if (magic !== 0x20b && magic !== 0x10b)
124
+ throw new Error(`${path}: unknown optional-header magic 0x${magic.toString(16)}`);
125
+ const is64 = magic === 0x20b;
126
+ const sizeOfImage = r.u32(opt + 56);
127
+ const subsystemOffset = opt + 68;
128
+ const subsystem = r.u16(subsystemOffset);
129
+ const ddCount = r.u32(opt + (is64 ? 108 : 92));
130
+ const ddOff = opt + (is64 ? 112 : 96);
131
+ const dd = (i: number) =>
132
+ i < ddCount ? { rva: r.u32(ddOff + i * 8), size: r.u32(ddOff + i * 8 + 4) } : { rva: 0, size: 0 };
133
+
134
+ const sections: Section[] = [];
135
+ const secOff = opt + optSize;
136
+ let rawEnd = 0;
137
+ for (let i = 0; i < nSections; i++) {
138
+ const o = secOff + i * 40;
139
+ const nameBytes = r.bytes(o, 8);
140
+ const name = new TextDecoder("latin1").decode(nameBytes).replace(/\0+$/, "");
141
+ const s = { name, vsize: r.u32(o + 8), va: r.u32(o + 12), rawSize: r.u32(o + 16), raw: r.u32(o + 20) };
142
+ sections.push(s);
143
+ rawEnd = Math.max(rawEnd, s.raw + s.rawSize);
144
+ }
145
+ const rvaToOff = (rva: number): number => {
146
+ for (const s of sections) {
147
+ if (rva >= s.va && rva < s.va + Math.max(s.vsize, s.rawSize)) return rva - s.va + s.raw;
148
+ }
149
+ throw new Error(`${path}: RVA 0x${rva.toString(16)} is in no section`);
150
+ };
151
+
152
+ const security = dd(4);
153
+ const resources: ResourceEntry[] = [];
154
+ const res = dd(2);
155
+ if (res.size) {
156
+ const base = rvaToOff(res.rva);
157
+ const nameAt = (off: number) => {
158
+ const len = r.u16(base + off);
159
+ return new TextDecoder("utf-16le").decode(r.bytes(base + off + 2, len * 2));
160
+ };
161
+ const walk = (dirOff: number, trail: (number | string)[]) => {
162
+ const nNamed = r.u16(base + dirOff + 12);
163
+ const nId = r.u16(base + dirOff + 14);
164
+ for (let i = 0; i < nNamed + nId; i++) {
165
+ const e = base + dirOff + 16 + i * 8;
166
+ const nm = r.u32(e);
167
+ const off = r.u32(e + 4);
168
+ const label: number | string = nm & 0x80000000 ? nameAt(nm & 0x7fffffff) : nm;
169
+ if (off & 0x80000000) {
170
+ if (trail.length >= 2) throw new Error(`${path}: resource tree deeper than type/name/lang`);
171
+ walk(off & 0x7fffffff, [...trail, label]);
172
+ } else {
173
+ const rva = r.u32(base + off);
174
+ const size = r.u32(base + off + 4);
175
+ resources.push({
176
+ type: trail[0]!,
177
+ name: trail[1]!,
178
+ lang: typeof label === "number" ? label : 0,
179
+ rva,
180
+ size,
181
+ fileOffset: rvaToOff(rva),
182
+ });
183
+ }
184
+ }
185
+ };
186
+ walk(0, []);
187
+ }
188
+
189
+ return {
190
+ is64,
191
+ machine,
192
+ subsystem,
193
+ subsystemOffset,
194
+ timeDateStamp,
195
+ sizeOfImage,
196
+ sections,
197
+ security: { offset: security.rva, size: security.size },
198
+ securityEntryOffset: ddOff + 4 * 8,
199
+ resources,
200
+ overlay: r.size - rawEnd,
201
+ };
202
+ } finally {
203
+ r.close();
204
+ }
205
+ }
206
+
207
+ export function readFileRange(path: string, offset: number, len: number): Uint8Array {
208
+ const r = new FileReader(path);
209
+ try {
210
+ return r.bytes(offset, len);
211
+ } finally {
212
+ r.close();
213
+ }
214
+ }
215
+
216
+ export function patchFile(path: string, offset: number, bytes: Uint8Array) {
217
+ const fd = openSync(path, "r+");
218
+ try {
219
+ writeSync(fd, bytes, 0, bytes.length, offset);
220
+ } finally {
221
+ closeSync(fd);
222
+ }
223
+ }
224
+
225
+ export function setSubsystem(path: string, subsystem: number) {
226
+ const pe = readPe(path);
227
+ patchFile(path, pe.subsystemOffset, new Uint8Array([subsystem & 0xff, subsystem >> 8]));
228
+ }
229
+
230
+ // Every pointer is a u64, every string a NUL-terminated UTF-16LE buffer, and an integer resource id is the id as a pointer.
231
+
232
+ const u64 = FFIType.u64;
233
+ const i32 = FFIType.i32;
234
+ const u32 = FFIType.u32;
235
+
236
+ function loadKernel32() {
237
+ return dlopen("kernel32.dll", {
238
+ BeginUpdateResourceW: { args: [u64, i32], returns: u64 },
239
+ UpdateResourceW: { args: [u64, u64, u64, FFIType.u16, u64, u32], returns: i32 },
240
+ EndUpdateResourceW: { args: [u64, i32], returns: i32 },
241
+ GetLastError: { args: [], returns: u32 },
242
+ LoadLibraryExW: { args: [u64, u64, u32], returns: u64 },
243
+ FreeLibrary: { args: [u64], returns: i32 },
244
+ FindResourceW: { args: [u64, u64, u64], returns: u64 },
245
+ LoadResource: { args: [u64, u64], returns: u64 },
246
+ LockResource: { args: [u64], returns: u64 },
247
+ SizeofResource: { args: [u64, u64], returns: u32 },
248
+ }).symbols;
249
+ }
250
+ function loadShell32() {
251
+ return dlopen("shell32.dll", {
252
+ ExtractIconExW: { args: [u64, i32, u64, u64, u32], returns: u32 },
253
+ }).symbols;
254
+ }
255
+ function loadVersion() {
256
+ return dlopen("version.dll", {
257
+ GetFileVersionInfoSizeW: { args: [u64, u64], returns: u32 },
258
+ GetFileVersionInfoW: { args: [u64, u32, u32, u64], returns: i32 },
259
+ VerQueryValueW: { args: [u64, u64, u64, u64], returns: i32 },
260
+ }).symbols;
261
+ }
262
+
263
+ let k32: ReturnType<typeof loadKernel32> | null = null;
264
+ let sh32: ReturnType<typeof loadShell32> | null = null;
265
+ let verdll: ReturnType<typeof loadVersion> | null = null;
266
+ const kernel32 = () => (k32 ??= loadKernel32());
267
+ const shell32 = () => (sh32 ??= loadShell32());
268
+ const version = () => (verdll ??= loadVersion());
269
+
270
+ export function requireWindows(what: string) {
271
+ if (process.platform !== "win32") {
272
+ throw new Error(
273
+ `${what} uses the Win32 resource API and runs on Windows only; stamping on another OS is not supported.`,
274
+ );
275
+ }
276
+ }
277
+
278
+ /** NUL-terminated UTF-16LE. The buffer must outlive the call — keep a reference. */
279
+ export const wide = (s: string): Uint8Array => new Uint8Array(Buffer.from(s + "\0", "utf16le"));
280
+ const P = (b: Uint8Array) => BigInt(ptr(b));
281
+
282
+ export const ERROR_SHARING_VIOLATION = 32;
283
+ export const ERROR_LOCK_VIOLATION = 33;
284
+ export const ERROR_ACCESS_DENIED = 5;
285
+
286
+ export class Win32Error extends Error {
287
+ constructor(
288
+ readonly fn: string,
289
+ readonly code: number,
290
+ ) {
291
+ super(
292
+ `${fn} failed with Win32 error ${code}${code === ERROR_SHARING_VIOLATION ? " (sharing violation — another process has the file open)" : ""}`,
293
+ );
294
+ }
295
+ }
296
+
297
+ export type ResId = number | string;
298
+
299
+ /** One `BeginUpdateResourceW` ... `EndUpdateResourceW` transaction. String buffers handed to Win32 stay alive on `this` until `commit`: `UpdateResourceW` makes no promise about when it reads the name. */
300
+ export class ResourceUpdate {
301
+ #h: bigint;
302
+ #keep: Uint8Array[] = [];
303
+ #done = false;
304
+
305
+ constructor(readonly path: string) {
306
+ requireWindows("stamping an exe");
307
+ const p = wide(path);
308
+ this.#keep.push(p);
309
+ const h = kernel32().BeginUpdateResourceW(P(p), 0);
310
+ if (!h) throw new Win32Error("BeginUpdateResourceW", kernel32().GetLastError());
311
+ this.#h = BigInt(h);
312
+ }
313
+
314
+ #id(id: ResId): bigint {
315
+ if (typeof id === "number") return BigInt(id);
316
+ const b = wide(id);
317
+ this.#keep.push(b);
318
+ return P(b);
319
+ }
320
+
321
+ set(type: ResId, name: ResId, lang: number, data: Uint8Array) {
322
+ this.#keep.push(data);
323
+ const ok = kernel32().UpdateResourceW(
324
+ this.#h,
325
+ this.#id(type),
326
+ this.#id(name),
327
+ lang,
328
+ data.length ? P(data) : 0n,
329
+ data.length,
330
+ );
331
+ if (!ok) throw new Win32Error(`UpdateResourceW(${type}/${name}/${lang})`, kernel32().GetLastError());
332
+ }
333
+
334
+ remove(type: ResId, name: ResId, lang: number) {
335
+ const ok = kernel32().UpdateResourceW(this.#h, this.#id(type), this.#id(name), lang, 0n, 0);
336
+ if (!ok) throw new Win32Error(`UpdateResourceW(delete ${type}/${name}/${lang})`, kernel32().GetLastError());
337
+ }
338
+
339
+ commit() {
340
+ if (this.#done) return;
341
+ this.#done = true;
342
+ const ok = kernel32().EndUpdateResourceW(this.#h, 0);
343
+ this.#keep = [];
344
+ if (!ok) throw new Win32Error("EndUpdateResourceW", kernel32().GetLastError());
345
+ }
346
+
347
+ discard() {
348
+ if (this.#done) return;
349
+ this.#done = true;
350
+ kernel32().EndUpdateResourceW(this.#h, 1);
351
+ this.#keep = [];
352
+ }
353
+ }
354
+
355
+ const LOAD_LIBRARY_AS_DATAFILE = 0x2;
356
+ const LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x20;
357
+
358
+ export function loadResourceCopy(path: string, type: ResId, name: ResId, maxBytes = Infinity): Uint8Array | null {
359
+ requireWindows("reading an exe's resources");
360
+ const k = kernel32();
361
+ const p = wide(path);
362
+ const mod = k.LoadLibraryExW(P(p), 0n, LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
363
+ if (!mod) throw new Win32Error(`LoadLibraryExW(${path})`, k.GetLastError());
364
+ try {
365
+ const keep: Uint8Array[] = [];
366
+ const id = (v: ResId) => {
367
+ if (typeof v === "number") return BigInt(v);
368
+ const b = wide(v);
369
+ keep.push(b);
370
+ return P(b);
371
+ };
372
+ const res = k.FindResourceW(mod, id(name), id(type));
373
+ if (!res) return null;
374
+ const hg = k.LoadResource(mod, res);
375
+ if (!hg) throw new Win32Error("LoadResource", k.GetLastError());
376
+ const at = k.LockResource(hg);
377
+ const size = k.SizeofResource(mod, res);
378
+ if (!at) throw new Win32Error("LockResource", k.GetLastError());
379
+ const n = Math.min(size, maxBytes);
380
+ return new Uint8Array(toArrayBuffer(Number(at), 0, n).slice(0));
381
+ } finally {
382
+ k.FreeLibrary(mod);
383
+ }
384
+ }
385
+
386
+ export function shellIconCount(path: string): number {
387
+ requireWindows("counting icons");
388
+ const p = wide(path);
389
+ return shell32().ExtractIconExW(P(p), -1, 0n, 0n, 0);
390
+ }
391
+
392
+ export function versionString(path: string, key: string, table = "040904b0"): string | null {
393
+ requireWindows("reading VERSIONINFO");
394
+ const v = version();
395
+ const p = wide(path);
396
+ const size = v.GetFileVersionInfoSizeW(P(p), 0n);
397
+ if (!size) return null;
398
+ const buf = new Uint8Array(size);
399
+ if (!v.GetFileVersionInfoW(P(p), 0, size, P(buf))) return null;
400
+ const outPtr = new Uint8Array(8);
401
+ const outLen = new Uint8Array(4);
402
+ const q = wide(`\\StringFileInfo\\${table}\\${key}`);
403
+ if (!v.VerQueryValueW(P(buf), P(q), P(outPtr), P(outLen))) return null;
404
+ const at = Number(new DataView(outPtr.buffer).getBigUint64(0, true));
405
+ const chars = new DataView(outLen.buffer).getUint32(0, true);
406
+ if (!at || !chars) return "";
407
+ const raw = new Uint8Array(toArrayBuffer(at, 0, chars * 2).slice(0));
408
+ return new TextDecoder("utf-16le").decode(raw).replace(/\0+$/, "");
409
+ }
410
+
411
+ export function fixedFileVersion(path: string): string | null {
412
+ requireWindows("reading VERSIONINFO");
413
+ const v = version();
414
+ const p = wide(path);
415
+ const size = v.GetFileVersionInfoSizeW(P(p), 0n);
416
+ if (!size) return null;
417
+ const buf = new Uint8Array(size);
418
+ if (!v.GetFileVersionInfoW(P(p), 0, size, P(buf))) return null;
419
+ const outPtr = new Uint8Array(8);
420
+ const outLen = new Uint8Array(4);
421
+ const q = wide("\\");
422
+ if (!v.VerQueryValueW(P(buf), P(q), P(outPtr), P(outLen))) return null;
423
+ const at = Number(new DataView(outPtr.buffer).getBigUint64(0, true));
424
+ const ffi = new DataView(toArrayBuffer(at, 0, 52).slice(0));
425
+ if (ffi.getUint32(0, true) !== 0xfeef04bd) return null;
426
+ const ms = ffi.getUint32(8, true);
427
+ const ls = ffi.getUint32(12, true);
428
+ return `${ms >>> 16}.${ms & 0xffff}.${ls >>> 16}.${ls & 0xffff}`;
429
+ }
@@ -0,0 +1,74 @@
1
+ // The OS a `sk build` packages for, and where that OS keeps its hosts. Everything that differs per
2
+ // OS by NAME (cargo target triple, host file name, cache folder) is one row of this table, so
3
+ // host.ts and dev.ts ask for a path instead of spelling one. What a platform produces is
4
+ // build-app.ts's `PACKAGERS`. Any OS that is neither macOS nor Linux keeps the Windows row.
5
+
6
+ import { homedir } from "node:os";
7
+ import { join, resolve } from "node:path";
8
+ import { ConfigError } from "./config";
9
+
10
+ export type Platform = "windows" | "macos" | "linux";
11
+ export const PLATFORMS: readonly Platform[] = ["windows", "macos", "linux"];
12
+
13
+ export function currentPlatform(): Platform {
14
+ return process.platform === "darwin" ? "macos" : process.platform === "linux" ? "linux" : "windows";
15
+ }
16
+
17
+ export function parsePlatform(value: unknown): Platform {
18
+ const v = typeof value === "string" ? value.trim().toLowerCase() : "";
19
+ if (v === "windows" || v === "win32" || v === "win") return "windows";
20
+ if (v === "macos" || v === "darwin" || v === "mac") return "macos";
21
+ if (v === "linux") return "linux";
22
+ throw new ConfigError(`--platform ${JSON.stringify(value)}: expected one of ${PLATFORMS.join(", ")}`);
23
+ }
24
+
25
+ export interface PlatformHosts {
26
+ platform: Platform;
27
+ triple: string;
28
+ exeFile: string;
29
+ cachedFile: string;
30
+ cargoTarget: string[];
31
+ cacheRoot(): string;
32
+ }
33
+
34
+ export function hostsFor(platform: Platform, arch: string = process.arch): PlatformHosts {
35
+ if (platform === "macos") {
36
+ const triple = arch === "x64" ? "x86_64-apple-darwin" : "aarch64-apple-darwin";
37
+ return {
38
+ platform,
39
+ triple,
40
+ exeFile: "sk-app",
41
+ cachedFile: "sk-host",
42
+ // .cargo/config.toml pins the Windows target, and a project config wins over the user's.
43
+ cargoTarget: ["--target", triple],
44
+ cacheRoot: () => join(homedir(), "Library", "Caches", "skathi", "hosts"),
45
+ };
46
+ }
47
+ if (platform === "linux") {
48
+ const triple = arch === "arm64" ? "aarch64-unknown-linux-gnu" : "x86_64-unknown-linux-gnu";
49
+ return {
50
+ platform,
51
+ triple,
52
+ exeFile: "sk-app",
53
+ cachedFile: "sk-host",
54
+ cargoTarget: ["--target", triple],
55
+ cacheRoot: () => join(process.env.XDG_CACHE_HOME || join(homedir(), ".cache"), "skathi", "hosts"),
56
+ };
57
+ }
58
+ return {
59
+ platform,
60
+ triple: "x86_64-pc-windows-msvc",
61
+ exeFile: "sk-app.exe",
62
+ cachedFile: "sk-host.exe",
63
+ cargoTarget: [],
64
+ cacheRoot: () => join(process.env.LOCALAPPDATA || join(homedir(), "AppData", "Local"), "skathi", "hosts"),
65
+ };
66
+ }
67
+
68
+ /** Where cargo puts the checkout's host: under CARGO_TARGET_DIR when that is set, as cargo does. */
69
+ export function repoHostExe(repoRoot: string, hosts: PlatformHosts, profile: "release" | "debug"): string {
70
+ const target = process.env.CARGO_TARGET_DIR
71
+ ? resolve(repoRoot, process.env.CARGO_TARGET_DIR)
72
+ : join(repoRoot, "target");
73
+ return join(target, hosts.triple, profile, hosts.exeFile);
74
+ }