@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
mjs 76 lines 2.91 KB
Raw
1 import * as NodeChildProcess from "node:child_process";
2 import * as NodeFS from "node:fs";
3 import * as NodePath from "node:path";
4 import * as NodeURL from "node:url";
5 import * as NodeUtil from "node:util";
6
7 // oxlint-disable-next-line t3code/no-global-process-runtime -- The Swift compiler targets the actual host; this script has no Effect runtime.
8 const hostArch = process.arch;
9 // oxlint-disable-next-line t3code/no-global-process-runtime -- The Speech framework exists only in the macOS SDK.
10 const hostPlatform = process.platform;
11
12 const { values } = NodeUtil.parseArgs({
13 options: { output: { type: "string" }, arch: { type: "string", default: hostArch } },
14 });
15 // Packaging passes --output and must not ship without the helper; dev builds only lose the option.
16 const required = values.output !== undefined;
17
18 if (hostPlatform !== "darwin") {
19 if (required) throw new Error("The Apple Speech helper can only be built on macOS.");
20 } else {
21 const targets = { arm64: ["arm64"], x64: ["x86_64"], universal: ["arm64", "x86_64"] }[
22 values.arch
23 ];
24 if (targets === undefined) throw new Error(`Unsupported macOS architecture: ${values.arch}`);
25 const root = NodeURL.fileURLToPath(new URL("../../../native/apple-speech/", import.meta.url));
26 const sources = ["AppleSpeech.swift", "AppleSpeechEngine.swift"].map((file) =>
27 NodePath.resolve(root, file),
28 );
29 const output = values.output ?? NodePath.resolve(root, "build", values.arch, "t3-apple-speech");
30 let current = false;
31 try {
32 current =
33 NodeFS.statSync(output).mtimeMs >=
34 Math.max(
35 ...sources.map((source) => NodeFS.statSync(source).mtimeMs),
36 NodeFS.statSync(NodeURL.fileURLToPath(import.meta.url)).mtimeMs,
37 );
38 } catch {}
39 if (!current) {
40 NodeFS.mkdirSync(NodePath.dirname(output), { recursive: true });
41 const temporary = `${output}.${process.pid}.tmp`;
42 const slices = targets.map((target) => `${temporary}.${target}`);
43 try {
44 targets.forEach((target, index) =>
45 NodeChildProcess.execFileSync(
46 "xcrun",
47 [
48 "swiftc",
49 "-O",
50 "-swift-version",
51 "5",
52 "-parse-as-library",
53 "-target",
54 `${target}-apple-macos26.0`,
55 ...sources,
56 "-o",
57 slices[index],
58 ],
59 { stdio: "inherit" },
60 ),
61 );
62 NodeChildProcess.execFileSync("xcrun", ["lipo", "-create", ...slices, "-output", temporary], {
63 stdio: "inherit",
64 });
65 NodeFS.renameSync(temporary, output);
66 } catch (cause) {
67 if (required)
68 throw new Error("Building the Apple Speech helper requires Xcode 26 or later.", { cause });
69 console.warn(
70 "Skipping the Apple Speech helper (needs Xcode 26 or later); on-device Mac dictation is unavailable in this build.",
71 );
72 } finally {
73 for (const file of [temporary, ...slices]) NodeFS.rmSync(file, { force: true });
74 }
75 }
76 }