@skathi-native/cli 0.0.0 → 0.1.0-beta.1
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/aot-inputs.ts +294 -0
- package/ascii.ts +397 -0
- package/build.ts +1236 -0
- package/check/builtins.json +1219 -0
- package/check/builtins.ts +112 -0
- package/check/codes.ts +178 -0
- package/check/command.ts +105 -0
- package/check/config.ts +76 -0
- package/check/finding.ts +30 -0
- package/check/graph.ts +69 -0
- package/check/highlight.ts +40 -0
- package/check/index.ts +308 -0
- package/check/packages.ts +54 -0
- package/check/program.ts +209 -0
- package/check/report.ts +219 -0
- package/check/rules.ts +307 -0
- package/check/schema.ts +69 -0
- package/check/typing.ts +703 -0
- package/cli/assets.ts +114 -0
- package/cli/build-app.ts +587 -0
- package/cli/config.ts +646 -0
- package/cli/create.ts +411 -0
- package/cli/dev.ts +92 -0
- package/cli/elf.ts +142 -0
- package/cli/host-fetch.ts +178 -0
- package/cli/host.ts +378 -0
- package/cli/icon.ts +303 -0
- package/cli/macos-bundle.ts +451 -0
- package/cli/pe.ts +429 -0
- package/cli/platform.ts +74 -0
- package/cli/sk.ts +356 -0
- package/cli/skpk.ts +268 -0
- package/cli/stamp.ts +254 -0
- package/cli/term.ts +176 -0
- package/cli/versioninfo.ts +123 -0
- package/css-prefixes.ts +118 -0
- package/devtools/apps.ts +68 -0
- package/devtools/commands.ts +860 -0
- package/devtools/ctl.ts +166 -0
- package/package.json +51 -3
- package/prod.ts +701 -0
- package/runtime-path.ts +24 -0
- package/tailwind.ts +76 -0
- package/vitecompat.ts +1151 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// Prebuilt bare hosts from the Skathi GitHub release of the CLI's own version, so `sk build`
|
|
2
|
+
// needs neither a checkout nor a Rust toolchain. The release (.github/workflows/release.yml,
|
|
3
|
+
// scripts/release-hosts.ts) carries one gzipped host per platform, arch and variant, plus
|
|
4
|
+
// `hosts.json`, which names each with its sha256 and its `--sk-host-info`.
|
|
5
|
+
//
|
|
6
|
+
// Assets are read through the GitHub API, so a private repository works with GH_TOKEN or
|
|
7
|
+
// GITHUB_TOKEN set. SK_HOST_REPO (owner/name) and SK_HOST_API point elsewhere, a fork or a mirror.
|
|
8
|
+
|
|
9
|
+
import { chmodSync, mkdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
import { partsLeftOut, VARIANTS, type Variant } from "./config";
|
|
12
|
+
import {
|
|
13
|
+
CLI_VERSION,
|
|
14
|
+
cachedHost,
|
|
15
|
+
HostError,
|
|
16
|
+
resolveHost,
|
|
17
|
+
type HostInfo,
|
|
18
|
+
type ResolvedHost,
|
|
19
|
+
type ResolveOptions,
|
|
20
|
+
} from "./host";
|
|
21
|
+
import { currentPlatform, type Platform } from "./platform";
|
|
22
|
+
|
|
23
|
+
export const MANIFEST_NAME = "hosts.json";
|
|
24
|
+
export const DEFAULT_REPO = "argon-chat/skathi";
|
|
25
|
+
|
|
26
|
+
export interface HostManifestEntry {
|
|
27
|
+
platform: Platform;
|
|
28
|
+
arch: string;
|
|
29
|
+
variant: Variant;
|
|
30
|
+
/** The release asset: the host, gzipped. */
|
|
31
|
+
file: string;
|
|
32
|
+
/** Of the host itself, not of the gzip. */
|
|
33
|
+
sha256: string;
|
|
34
|
+
size: number;
|
|
35
|
+
info: HostInfo;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface HostManifest {
|
|
39
|
+
skathi: string;
|
|
40
|
+
hosts: HostManifestEntry[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const hostAssetName = (platform: Platform, arch: string, variant: Variant): string =>
|
|
44
|
+
`sk-host-${platform}-${arch}-${variant}${platform === "windows" ? ".exe" : ""}.gz`;
|
|
45
|
+
|
|
46
|
+
export const sha256 = (bytes: Uint8Array): string =>
|
|
47
|
+
new Bun.CryptoHasher("sha256").update(new Uint8Array(bytes)).digest("hex");
|
|
48
|
+
|
|
49
|
+
interface ReleaseAsset {
|
|
50
|
+
name: string;
|
|
51
|
+
url: string;
|
|
52
|
+
size: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface FetchOptions {
|
|
56
|
+
variant: Variant;
|
|
57
|
+
version?: string;
|
|
58
|
+
platform?: Platform;
|
|
59
|
+
arch?: string;
|
|
60
|
+
log?: (line: string) => void;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function source() {
|
|
64
|
+
const repo = process.env.SK_HOST_REPO || DEFAULT_REPO;
|
|
65
|
+
const api = (process.env.SK_HOST_API || "https://api.github.com").replace(/\/+$/, "");
|
|
66
|
+
const token = process.env.GH_TOKEN || process.env.GITHUB_TOKEN;
|
|
67
|
+
const headers: Record<string, string> = { "User-Agent": "skathi-cli", "X-GitHub-Api-Version": "2022-11-28" };
|
|
68
|
+
if (token) headers.Authorization = `Bearer ${token}`;
|
|
69
|
+
return { repo, api, headers };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function download(asset: ReleaseAsset, headers: Record<string, string>): Promise<Uint8Array<ArrayBuffer>> {
|
|
73
|
+
const res = await fetch(asset.url, { headers: { ...headers, Accept: "application/octet-stream" } });
|
|
74
|
+
if (!res.ok) throw new HostError(`downloading ${asset.name}: HTTP ${res.status}`);
|
|
75
|
+
return new Uint8Array(await res.arrayBuffer());
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Downloads the host for `variant` from the release of `version` into the local cache, checked against `hosts.json`. */
|
|
79
|
+
export async function fetchHost(options: FetchOptions): Promise<{ path: string; info: HostInfo }> {
|
|
80
|
+
const version = options.version ?? CLI_VERSION;
|
|
81
|
+
const platform = options.platform ?? currentPlatform();
|
|
82
|
+
const arch = options.arch ?? process.arch;
|
|
83
|
+
const log = options.log ?? (() => {});
|
|
84
|
+
const { repo, api, headers } = source();
|
|
85
|
+
|
|
86
|
+
const tag = `v${version}`;
|
|
87
|
+
const res = await fetch(`${api}/repos/${repo}/releases/tags/${tag}`, {
|
|
88
|
+
headers: { ...headers, Accept: "application/vnd.github+json" },
|
|
89
|
+
});
|
|
90
|
+
if (res.status === 404) {
|
|
91
|
+
throw new HostError(
|
|
92
|
+
`github.com/${repo} has no ${tag} release` +
|
|
93
|
+
(headers.Authorization ? "" : " (a private repository needs GH_TOKEN or GITHUB_TOKEN)"),
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
if (!res.ok) throw new HostError(`github.com/${repo} release ${tag}: HTTP ${res.status}`);
|
|
97
|
+
const assets = ((await res.json()) as { assets?: ReleaseAsset[] }).assets ?? [];
|
|
98
|
+
const asset = (name: string) => assets.find((a) => a.name === name);
|
|
99
|
+
|
|
100
|
+
const manifestAsset = asset(MANIFEST_NAME);
|
|
101
|
+
if (!manifestAsset) throw new HostError(`release ${tag} of github.com/${repo} has no ${MANIFEST_NAME}`);
|
|
102
|
+
const manifest = JSON.parse(new TextDecoder().decode(await download(manifestAsset, headers))) as HostManifest;
|
|
103
|
+
const entry = manifest.hosts.find((h) => h.platform === platform && h.arch === arch && h.variant === options.variant);
|
|
104
|
+
if (!entry) {
|
|
105
|
+
const offered = manifest.hosts
|
|
106
|
+
.filter((h) => h.platform === platform && h.arch === arch)
|
|
107
|
+
.map((h) => h.variant)
|
|
108
|
+
.join(", ");
|
|
109
|
+
throw new HostError(
|
|
110
|
+
`release ${tag} has no ${options.variant} host for ${platform}-${arch}` +
|
|
111
|
+
(offered ? ` (it has: ${offered}); other variants are built from a checkout with sk host build` : ""),
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
const hostAsset = asset(entry.file);
|
|
115
|
+
if (!hostAsset) throw new HostError(`release ${tag} lists ${entry.file} in ${MANIFEST_NAME} but does not carry it`);
|
|
116
|
+
|
|
117
|
+
log(`downloading ${entry.file} (${(hostAsset.size / 1048576).toFixed(1)} MB) from github.com/${repo} ${tag}`);
|
|
118
|
+
const bytes = Bun.gunzipSync(await download(hostAsset, headers));
|
|
119
|
+
if (bytes.length !== entry.size || sha256(bytes) !== entry.sha256) {
|
|
120
|
+
throw new HostError(`${entry.file} does not match the sha256 in ${MANIFEST_NAME}; nothing was cached`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const dest = cachedHost(version, options.variant, platform);
|
|
124
|
+
mkdirSync(dirname(dest), { recursive: true });
|
|
125
|
+
const tmp = `${dest}.partial`;
|
|
126
|
+
writeFileSync(tmp, bytes);
|
|
127
|
+
if (process.platform !== "win32") chmodSync(tmp, 0o755);
|
|
128
|
+
rmSync(dest, { force: true });
|
|
129
|
+
renameSync(tmp, dest);
|
|
130
|
+
writeFileSync(join(dirname(dest), "host.json"), JSON.stringify(entry.info, null, 2) + "\n");
|
|
131
|
+
log(`cached ${options.variant} host ${version} → ${dest}`);
|
|
132
|
+
return { path: dest, info: entry.info };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The published variant that can stand in for `variant`: devtools exactly as asked, and every other
|
|
137
|
+
* part the app may use, with the fewest extra. A host with a part the app does not use only costs size.
|
|
138
|
+
*/
|
|
139
|
+
export function standIn(variant: Variant, published: readonly Variant[] = VARIANTS): Variant | null {
|
|
140
|
+
const off = new Set(partsLeftOut(variant));
|
|
141
|
+
const fits = published.filter((v) => {
|
|
142
|
+
const o = partsLeftOut(v);
|
|
143
|
+
return o.includes("devtools") === off.has("devtools") && o.every((p) => off.has(p));
|
|
144
|
+
});
|
|
145
|
+
return fits.sort((a, b) => partsLeftOut(b).length - partsLeftOut(a).length)[0] ?? null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* {@link resolveHost}, and when it finds nothing for this OS and no host was named, a download into the
|
|
150
|
+
* cache first. A variant no release carries falls back to its {@link standIn}, with `substitute` saying
|
|
151
|
+
* so. SK_HOST_OFFLINE=1 turns the download off.
|
|
152
|
+
*/
|
|
153
|
+
export async function resolveOrFetchHost(options: ResolveOptions): Promise<ResolvedHost & { substitute?: string }> {
|
|
154
|
+
try {
|
|
155
|
+
return resolveHost(options);
|
|
156
|
+
} catch (e) {
|
|
157
|
+
const named = options.host ?? process.env.SK_HOST;
|
|
158
|
+
const cross = (options.platform ?? currentPlatform()) !== currentPlatform();
|
|
159
|
+
if (!(e instanceof HostError) || named || cross || process.env.SK_HOST_OFFLINE === "1") throw e;
|
|
160
|
+
const instead = VARIANTS.includes(options.variant) ? null : standIn(options.variant);
|
|
161
|
+
if (instead) {
|
|
162
|
+
const host = await resolveOrFetchHost({ ...options, variant: instead });
|
|
163
|
+
const extra = partsLeftOut(instead).length < partsLeftOut(options.variant).length;
|
|
164
|
+
return {
|
|
165
|
+
...host,
|
|
166
|
+
substitute:
|
|
167
|
+
`no prebuilt ${options.variant} host: using ${instead}` +
|
|
168
|
+
(extra ? ", which also carries parts this app leaves out (sk host build makes an exact one)" : ""),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
try {
|
|
172
|
+
await fetchHost({ variant: options.variant, ...(options.log && { log: options.log }) });
|
|
173
|
+
} catch (f) {
|
|
174
|
+
throw new HostError(`${e.message}\n(downloading it failed too: ${(f as Error).message})`);
|
|
175
|
+
}
|
|
176
|
+
return resolveHost(options);
|
|
177
|
+
}
|
|
178
|
+
}
|
package/cli/host.ts
ADDED
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
// Where `sk build` gets the prebuilt bare host it stamps: `cargo build --release -p sk-app
|
|
2
|
+
// --features prod` (the whole engine, no app), built once per Skathi version and variant. A
|
|
3
|
+
// variant names the engine parts left out (config.ts `OPTIONAL`).
|
|
4
|
+
//
|
|
5
|
+
// Resolution, first hit wins: 1. `--host <exe>` or `SK_HOST`; 2. in-repo
|
|
6
|
+
// target/x86_64-pc-windows-msvc/release/sk-app.exe, when that is a prod host of this variant and
|
|
7
|
+
// version; 3. the local cache (`sk host add <exe>`, `sk host build`, or a download from this
|
|
8
|
+
// version's GitHub release: host-fetch.ts); 4. an error naming them. Paths per OS are in platform.ts.
|
|
9
|
+
//
|
|
10
|
+
// A host for another OS cannot be run to ask what it is: only `--host` / `SK_HOST` names one, its
|
|
11
|
+
// format is sniffed, and what it is comes from a `host.json` beside it or is taken on trust, with a warning.
|
|
12
|
+
//
|
|
13
|
+
// A host and a bundle must come from the same Skathi version (the bundle carries the JS half of
|
|
14
|
+
// the `__sk_*` protocol): the CLI refuses a host whose `--sk-host-info` disagrees
|
|
15
|
+
// (`--allow-host-mismatch` is for maintainers).
|
|
16
|
+
|
|
17
|
+
import { spawnSync } from "node:child_process";
|
|
18
|
+
import {
|
|
19
|
+
chmodSync,
|
|
20
|
+
copyFileSync,
|
|
21
|
+
existsSync,
|
|
22
|
+
mkdirSync,
|
|
23
|
+
readdirSync,
|
|
24
|
+
readFileSync,
|
|
25
|
+
renameSync,
|
|
26
|
+
rmSync,
|
|
27
|
+
statSync,
|
|
28
|
+
writeFileSync,
|
|
29
|
+
} from "node:fs";
|
|
30
|
+
import { homedir } from "node:os";
|
|
31
|
+
import { dirname, join, resolve } from "node:path";
|
|
32
|
+
import { cargoFeatureArgs, OPTIONAL, partsLeftOut, type Variant } from "./config";
|
|
33
|
+
import { appendedPayloadOf, elfMachineOf } from "./elf";
|
|
34
|
+
import { bundlePayloadPath, machOArchs } from "./macos-bundle";
|
|
35
|
+
import { readPe, RT } from "./pe";
|
|
36
|
+
import { currentPlatform, hostsFor, repoHostExe, type Platform } from "./platform";
|
|
37
|
+
import { PAYLOAD_NAME } from "./stamp";
|
|
38
|
+
|
|
39
|
+
export const PKG_DIR = resolve(import.meta.dir, "..");
|
|
40
|
+
export const CLI_VERSION: string = JSON.parse(readFileSync(join(PKG_DIR, "package.json"), "utf8")).version;
|
|
41
|
+
|
|
42
|
+
export const REPO_ROOT: string | null = (() => {
|
|
43
|
+
const root = resolve(PKG_DIR, "../..");
|
|
44
|
+
return existsSync(join(root, "Cargo.toml")) && existsSync(join(root, "crates", "sk-app")) ? root : null;
|
|
45
|
+
})();
|
|
46
|
+
|
|
47
|
+
export const REPO_RELEASE_EXE = REPO_ROOT ? repoHostExe(REPO_ROOT, hostsFor(currentPlatform()), "release") : null;
|
|
48
|
+
|
|
49
|
+
export interface HostInfo {
|
|
50
|
+
mode: "prod" | "dev";
|
|
51
|
+
skathi: string;
|
|
52
|
+
crate: string;
|
|
53
|
+
variant: Variant;
|
|
54
|
+
devtools: boolean;
|
|
55
|
+
features: string[];
|
|
56
|
+
jscPin: string;
|
|
57
|
+
payloadFormat: number;
|
|
58
|
+
profile: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface ResolvedHost {
|
|
62
|
+
exe: string;
|
|
63
|
+
info: HostInfo;
|
|
64
|
+
source: "--host" | "SK_HOST" | "repo" | "cache";
|
|
65
|
+
unverified?: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class HostError extends Error {}
|
|
69
|
+
|
|
70
|
+
/** True when `exe` already carries an app (asking it for host info would launch it): a stamped resource on Windows, `Contents/Resources/app.skpk` on macOS, an appended payload on Linux. */
|
|
71
|
+
export function isStamped(exe: string): boolean {
|
|
72
|
+
const bundled = bundlePayloadPath(exe);
|
|
73
|
+
if (bundled && existsSync(bundled)) return true;
|
|
74
|
+
if (appendedPayloadOf(exe)) return true;
|
|
75
|
+
try {
|
|
76
|
+
return readPe(exe).resources.some((r) => r.type === RT.RCDATA && r.name === PAYLOAD_NAME);
|
|
77
|
+
} catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function formatProblem(exe: string, platform: Platform): string | null {
|
|
83
|
+
if (platform === "windows") return null;
|
|
84
|
+
if (platform === "linux") {
|
|
85
|
+
const machine = elfMachineOf(exe);
|
|
86
|
+
if (!machine) return `${exe} is not an ELF executable — a Linux build needs a Linux sk-app`;
|
|
87
|
+
if (machine !== "x86_64" && machine !== "aarch64") return `${exe} is an ELF for ${machine}, not x86_64 or aarch64`;
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
const archs = machOArchs(exe);
|
|
91
|
+
if (!archs) return `${exe} is not a Mach-O executable — a macOS bundle needs a macOS sk-app`;
|
|
92
|
+
if (!archs.some((a) => a === "arm64" || a === "x86_64"))
|
|
93
|
+
return `${exe} is a Mach-O for ${archs.join(", ")}, not arm64 or x86_64`;
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function crossHostInfo(
|
|
98
|
+
exe: string,
|
|
99
|
+
variant: Variant,
|
|
100
|
+
platform: Platform,
|
|
101
|
+
): { info: HostInfo; unverified: string } {
|
|
102
|
+
const problem = formatProblem(exe, platform);
|
|
103
|
+
if (problem) throw new HostError(problem);
|
|
104
|
+
const sidecar = join(dirname(exe), "host.json");
|
|
105
|
+
if (existsSync(sidecar)) {
|
|
106
|
+
try {
|
|
107
|
+
const info = JSON.parse(readFileSync(sidecar, "utf8")) as HostInfo;
|
|
108
|
+
if (info.mode && info.skathi && info.variant) {
|
|
109
|
+
return { info, unverified: `${platform} host described by ${sidecar}, not run (this is ${process.platform})` };
|
|
110
|
+
}
|
|
111
|
+
} catch {}
|
|
112
|
+
}
|
|
113
|
+
const off = partsLeftOut(variant);
|
|
114
|
+
const info: HostInfo = {
|
|
115
|
+
mode: "prod",
|
|
116
|
+
skathi: CLI_VERSION,
|
|
117
|
+
crate: "unknown",
|
|
118
|
+
variant,
|
|
119
|
+
devtools: !off.includes("devtools"),
|
|
120
|
+
features: ["prod", ...OPTIONAL.filter((p) => p !== "devtools" && !off.includes(p))],
|
|
121
|
+
jscPin: "unknown",
|
|
122
|
+
payloadFormat: 1,
|
|
123
|
+
profile: "unknown",
|
|
124
|
+
};
|
|
125
|
+
return {
|
|
126
|
+
info,
|
|
127
|
+
unverified:
|
|
128
|
+
`a ${platform} host cannot be run on ${process.platform} to check it — ${exe} is taken to be a prod ${variant} host ` +
|
|
129
|
+
`of Skathi ${CLI_VERSION}. Put its \`--sk-host-info\` output in ${sidecar} to have it checked.`,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function hostInfo(exe: string): HostInfo {
|
|
134
|
+
if (!existsSync(exe)) throw new HostError(`${exe} does not exist`);
|
|
135
|
+
if (isStamped(exe)) {
|
|
136
|
+
throw new HostError(`${exe} is a packaged app, not a bare host (it already carries a SKPAYLOAD)`);
|
|
137
|
+
}
|
|
138
|
+
const r = spawnSync(exe, ["--sk-host-info"], { encoding: "utf8", timeout: 30_000, windowsHide: true });
|
|
139
|
+
if (r.error) throw new HostError(`${exe} could not be run: ${r.error.message}`);
|
|
140
|
+
const line = (r.stdout ?? "").trim().split(/\r?\n/).pop() ?? "";
|
|
141
|
+
try {
|
|
142
|
+
const info = JSON.parse(line) as HostInfo;
|
|
143
|
+
if (!info.mode || !info.skathi) throw new Error("missing fields");
|
|
144
|
+
return info;
|
|
145
|
+
} catch {
|
|
146
|
+
throw new HostError(
|
|
147
|
+
`${exe} did not answer --sk-host-info (exit ${r.status}). Hosts older than this CLI do not know the flag — ` +
|
|
148
|
+
`rebuild it.\n${(r.stderr ?? "").trim().split(/\r?\n/).slice(-5).join("\n")}`,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function cacheRoot(platform: Platform = currentPlatform()): string {
|
|
154
|
+
return hostsFor(platform).cacheRoot();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export const cachedHost = (version: string, variant: Variant, platform: Platform = currentPlatform()) =>
|
|
158
|
+
join(cacheRoot(platform), version, variant, hostsFor(platform).cachedFile);
|
|
159
|
+
|
|
160
|
+
/** The host `sk dev` runs: every part, built without `prod`, so it reads a dev dist and hot-reloads. Cached and released beside the prod variants under this name. */
|
|
161
|
+
export const DEV_HOST: Variant = "dev";
|
|
162
|
+
|
|
163
|
+
export function checkHost(exe: string, info: HostInfo, variant: Variant | null, allowMismatch: boolean): string | null {
|
|
164
|
+
const mismatch =
|
|
165
|
+
info.skathi !== CLI_VERSION && !allowMismatch
|
|
166
|
+
? `${exe} is Skathi ${info.skathi}, this CLI is ${CLI_VERSION} — the bundle carries the JS half of the ` +
|
|
167
|
+
`host protocol, so the two must match (--allow-host-mismatch overrides, for maintainers)`
|
|
168
|
+
: null;
|
|
169
|
+
if (variant === DEV_HOST) {
|
|
170
|
+
return info.mode === "dev"
|
|
171
|
+
? mismatch
|
|
172
|
+
: `${exe} is a ${info.mode} host — sk dev needs one built without --features prod`;
|
|
173
|
+
}
|
|
174
|
+
if (info.mode !== "prod") return `${exe} is a ${info.mode} host — sk build needs one built with --features prod`;
|
|
175
|
+
if (variant && info.variant !== variant) return `${exe} is the ${info.variant} variant, not ${variant}`;
|
|
176
|
+
if (!partsLeftOut(variant ?? info.variant).includes("webgpu") && !info.features?.includes("webgpu")) {
|
|
177
|
+
return `${exe} lacks native WebGPU required by ${variant} — rebuild or replace the cached host`;
|
|
178
|
+
}
|
|
179
|
+
return mismatch;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function newestHostSource(): number {
|
|
183
|
+
if (!REPO_ROOT) return 0;
|
|
184
|
+
let newest = 0;
|
|
185
|
+
const visit = (dir: string) => {
|
|
186
|
+
let names: string[];
|
|
187
|
+
try {
|
|
188
|
+
names = readdirSync(dir);
|
|
189
|
+
} catch {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
for (const name of names) {
|
|
193
|
+
if (name === "node_modules" || name === "target" || name.startsWith(".")) continue;
|
|
194
|
+
const abs = join(dir, name);
|
|
195
|
+
let st;
|
|
196
|
+
try {
|
|
197
|
+
st = statSync(abs);
|
|
198
|
+
} catch {
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (st.isDirectory()) visit(abs);
|
|
202
|
+
else if (/\.(rs|toml|ts|vue|json)$/.test(name)) newest = Math.max(newest, st.mtimeMs);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
visit(join(REPO_ROOT, "crates"));
|
|
206
|
+
visit(join(REPO_ROOT, "apps", "devtools"));
|
|
207
|
+
visit(join(REPO_ROOT, "packages", "runtime", "src"));
|
|
208
|
+
for (const f of ["Cargo.toml", "Cargo.lock"]) {
|
|
209
|
+
try {
|
|
210
|
+
newest = Math.max(newest, statSync(join(REPO_ROOT, f)).mtimeMs);
|
|
211
|
+
} catch {}
|
|
212
|
+
}
|
|
213
|
+
return newest;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export interface ResolveOptions {
|
|
217
|
+
host?: string | undefined;
|
|
218
|
+
variant: Variant;
|
|
219
|
+
allowMismatch?: boolean | undefined;
|
|
220
|
+
platform?: Platform | undefined;
|
|
221
|
+
log?: (line: string) => void;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function resolveHost(options: ResolveOptions): ResolvedHost {
|
|
225
|
+
const log = options.log ?? (() => {});
|
|
226
|
+
const allow = options.allowMismatch ?? false;
|
|
227
|
+
const platform = options.platform ?? currentPlatform();
|
|
228
|
+
const cross = platform !== currentPlatform();
|
|
229
|
+
const explicit = options.host ?? process.env.SK_HOST;
|
|
230
|
+
if (explicit) {
|
|
231
|
+
const exe = resolve(explicit);
|
|
232
|
+
const source = options.host ? "--host" : "SK_HOST";
|
|
233
|
+
if (cross) {
|
|
234
|
+
if (!existsSync(exe)) throw new HostError(`${exe} does not exist`);
|
|
235
|
+
if (isStamped(exe))
|
|
236
|
+
throw new HostError(`${exe} is a packaged app, not a bare host (it already carries an app payload)`);
|
|
237
|
+
const { info, unverified } = crossHostInfo(exe, options.variant, platform);
|
|
238
|
+
const problem = checkHost(exe, info, options.variant, allow);
|
|
239
|
+
if (problem) throw new HostError(problem);
|
|
240
|
+
return { exe, info, source, unverified };
|
|
241
|
+
}
|
|
242
|
+
const format = existsSync(exe) ? formatProblem(exe, platform) : null;
|
|
243
|
+
if (format) throw new HostError(format);
|
|
244
|
+
const info = hostInfo(exe);
|
|
245
|
+
const problem = checkHost(exe, info, options.variant, allow);
|
|
246
|
+
if (problem) throw new HostError(problem);
|
|
247
|
+
return { exe, info, source };
|
|
248
|
+
}
|
|
249
|
+
if (cross) {
|
|
250
|
+
throw new HostError(
|
|
251
|
+
`a ${platform} build on ${process.platform} needs the host named: --host <path to a ${platform} sk-app> ` +
|
|
252
|
+
`(or SK_HOST). This machine's checkout and cache hold ${currentPlatform()} hosts.`,
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const tried: string[] = [];
|
|
257
|
+
if (REPO_RELEASE_EXE && existsSync(REPO_RELEASE_EXE)) {
|
|
258
|
+
try {
|
|
259
|
+
const info = hostInfo(REPO_RELEASE_EXE);
|
|
260
|
+
const problem = checkHost(REPO_RELEASE_EXE, info, options.variant, allow);
|
|
261
|
+
if (!problem) {
|
|
262
|
+
if (statSync(REPO_RELEASE_EXE).mtimeMs < newestHostSource()) {
|
|
263
|
+
log(
|
|
264
|
+
`warning: ${REPO_RELEASE_EXE} is older than the sources it is built from — ` +
|
|
265
|
+
"`bun packages/cli/cli/sk.ts host build` rebuilds it",
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
return { exe: REPO_RELEASE_EXE, info, source: "repo" };
|
|
269
|
+
}
|
|
270
|
+
tried.push(`in-repo: ${problem}`);
|
|
271
|
+
} catch (e) {
|
|
272
|
+
tried.push(`in-repo: ${(e as Error).message}`);
|
|
273
|
+
}
|
|
274
|
+
} else if (REPO_RELEASE_EXE) {
|
|
275
|
+
tried.push(`in-repo: ${REPO_RELEASE_EXE} has not been built`);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const cached = cachedHost(CLI_VERSION, options.variant);
|
|
279
|
+
if (existsSync(cached)) {
|
|
280
|
+
const info = hostInfo(cached);
|
|
281
|
+
const problem = checkHost(cached, info, options.variant, allow);
|
|
282
|
+
if (!problem) return { exe: cached, info, source: "cache" };
|
|
283
|
+
tried.push(`cache: ${problem}`);
|
|
284
|
+
} else {
|
|
285
|
+
tried.push(`cache: ${cached} does not exist`);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
throw new HostError(
|
|
289
|
+
`no ${options.variant} Skathi ${CLI_VERSION} host found.\n` +
|
|
290
|
+
tried.map((t) => ` - ${t}`).join("\n") +
|
|
291
|
+
"\nSupply one of:\n" +
|
|
292
|
+
` ${`--host <${hostsFor(platform).exeFile}>`.padEnd(21)}(or SK_HOST=<exe>) a prod host built from this version\n` +
|
|
293
|
+
` sk host fetch download it from the Skathi ${CLI_VERSION} release\n` +
|
|
294
|
+
" sk host add <exe> import one into the local cache\n" +
|
|
295
|
+
(REPO_ROOT
|
|
296
|
+
? options.variant === "standard" || options.variant === "no-devtools"
|
|
297
|
+
? " sk host build build both variants from this checkout (cargo)\n"
|
|
298
|
+
: ` sk host build --variant ${options.variant} build this one from this checkout (cargo)\n`
|
|
299
|
+
: ""),
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export function addHost(exe: string, log: (line: string) => void = () => {}): { path: string; info: HostInfo } {
|
|
304
|
+
const src = resolve(exe);
|
|
305
|
+
const info = hostInfo(src);
|
|
306
|
+
const problem = checkHost(src, info, null, true);
|
|
307
|
+
if (problem) throw new HostError(problem);
|
|
308
|
+
const dest = cachedHost(info.skathi, info.variant);
|
|
309
|
+
mkdirSync(dirname(dest), { recursive: true });
|
|
310
|
+
const tmp = `${dest}.partial`;
|
|
311
|
+
copyFileSync(src, tmp);
|
|
312
|
+
// A POSIX host must stay runnable whatever mode the source had.
|
|
313
|
+
if (process.platform !== "win32") chmodSync(tmp, 0o755);
|
|
314
|
+
rmSync(dest, { force: true });
|
|
315
|
+
renameSync(tmp, dest);
|
|
316
|
+
writeFileSync(join(dirname(dest), "host.json"), JSON.stringify(info, null, 2) + "\n");
|
|
317
|
+
log(`cached ${info.variant} host ${info.skathi} (${info.profile}) → ${dest}`);
|
|
318
|
+
return { path: dest, info };
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function cargo(): string {
|
|
322
|
+
const which = Bun.which("cargo");
|
|
323
|
+
if (which) return which;
|
|
324
|
+
const home = process.env.CARGO_HOME ?? join(process.env.USERPROFILE ?? homedir(), ".cargo");
|
|
325
|
+
const exe = join(home, "bin", process.platform === "win32" ? "cargo.exe" : "cargo");
|
|
326
|
+
if (existsSync(exe)) return exe;
|
|
327
|
+
throw new HostError("cargo is not on PATH — building a host needs the Rust toolchain (sk build does not)");
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** The cargo command line for `variant`, plus `extra` features (`native-inspector` for a release host). */
|
|
331
|
+
export function hostCargoArgs(variant: Variant, extra: string[] = []): string[] {
|
|
332
|
+
const args = variant === DEV_HOST ? [] : cargoFeatureArgs(variant);
|
|
333
|
+
const at = args.indexOf("--features");
|
|
334
|
+
if (extra.length) {
|
|
335
|
+
if (at < 0) args.push("--features", extra.join(","));
|
|
336
|
+
else args[at + 1] = [args[at + 1], ...extra].join(",");
|
|
337
|
+
}
|
|
338
|
+
return ["build", "--release", "-p", "sk-app", ...hostsFor(currentPlatform()).cargoTarget, ...args];
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export interface CargoExtras {
|
|
342
|
+
features?: string[];
|
|
343
|
+
env?: Record<string, string>;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** MAINTAINERS ONLY, in a checkout: cargo-builds the bare host for `variant` into the checkout's target/; returns its path. */
|
|
347
|
+
export function cargoBuildHost(
|
|
348
|
+
variant: Variant,
|
|
349
|
+
log: (line: string) => void = console.log,
|
|
350
|
+
extras: CargoExtras = {},
|
|
351
|
+
): { exe: string; ms: number } {
|
|
352
|
+
if (!REPO_ROOT || !REPO_RELEASE_EXE) {
|
|
353
|
+
throw new HostError("sk host build runs from a Skathi checkout; elsewhere, use sk host fetch or sk host add <exe>");
|
|
354
|
+
}
|
|
355
|
+
const args = hostCargoArgs(variant, extras.features);
|
|
356
|
+
log(`cargo ${args.join(" ")}`);
|
|
357
|
+
const t0 = performance.now();
|
|
358
|
+
const r = spawnSync(cargo(), args, {
|
|
359
|
+
cwd: REPO_ROOT,
|
|
360
|
+
stdio: "inherit",
|
|
361
|
+
env: { ...process.env, ...extras.env },
|
|
362
|
+
});
|
|
363
|
+
if (r.status !== 0) throw new HostError(`cargo ${args.join(" ")} failed (exit ${r.status})`);
|
|
364
|
+
return { exe: REPO_RELEASE_EXE, ms: Math.round(performance.now() - t0) };
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** MAINTAINERS ONLY, in a checkout: builds the bare host for `variant` and imports it into the cache. */
|
|
368
|
+
export function buildHost(
|
|
369
|
+
variant: Variant,
|
|
370
|
+
log: (line: string) => void = console.log,
|
|
371
|
+
): { path: string; info: HostInfo; ms: number } {
|
|
372
|
+
const { exe, ms } = cargoBuildHost(variant, log);
|
|
373
|
+
const added = addHost(exe, log);
|
|
374
|
+
if (added.info.variant !== variant) {
|
|
375
|
+
throw new HostError(`built the ${variant} variant but the exe reports ${added.info.variant}`);
|
|
376
|
+
}
|
|
377
|
+
return { ...added, ms };
|
|
378
|
+
}
|