@taco-ai/cli 0.1.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/LICENSE +21 -0
- package/README.md +27 -0
- package/bin/taco.cjs +56 -0
- package/dist/taco.mjs +104 -0
- package/lib/index.ts +102 -0
- package/lib/install.ts +85 -0
- package/lib/installHelpers.ts +140 -0
- package/lib/installId.ts +75 -0
- package/lib/installLaunchd.ts +194 -0
- package/lib/installSchtasks.ts +127 -0
- package/lib/paths.ts +100 -0
- package/lib/sidecarLauncher.ts +179 -0
- package/lib/start.ts +381 -0
- package/lib/startLock.ts +109 -0
- package/lib/status.ts +74 -0
- package/lib/stop.ts +108 -0
- package/lib/uninstall.ts +25 -0
- package/lib/uninstallLaunchd.ts +36 -0
- package/lib/uninstallSchtasks.ts +25 -0
- package/lib/upgrade.ts +254 -0
- package/lib/upgradeApply.ts +178 -0
- package/lib/upgradeLogger.ts +32 -0
- package/lib/upgradeMarker.ts +69 -0
- package/lib/upgradePlatform.ts +37 -0
- package/lib/upgradeTypes.ts +40 -0
- package/package.json +39 -0
package/lib/install.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/** Register the sidecar daemon with the host's per-user service manager.
|
|
2
|
+
* Linux is unsupported because no single user-level manager covers the
|
|
3
|
+
* supported distro matrix without administrator setup. */
|
|
4
|
+
|
|
5
|
+
import { findPlatformPkg, type PlatformPkgPaths } from "./installHelpers.ts";
|
|
6
|
+
import { installLaunchd as runInstallLaunchd } from "./installLaunchd.ts";
|
|
7
|
+
import { installSchtasks as runInstallSchtasks } from "./installSchtasks.ts";
|
|
8
|
+
import {
|
|
9
|
+
controlSocketPath,
|
|
10
|
+
defaultRuntimeDir,
|
|
11
|
+
ensureDirs,
|
|
12
|
+
ndjsonSocketPath,
|
|
13
|
+
TACO_HOME,
|
|
14
|
+
} from "./paths.ts";
|
|
15
|
+
|
|
16
|
+
export interface InstallOptions {
|
|
17
|
+
/** Override $TACO_HOME (defaults to env / ~/.taco). */
|
|
18
|
+
tacoHome?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface InstallResult {
|
|
22
|
+
/** Service manager that ended up owning the daemon. */
|
|
23
|
+
serviceManager: "launchd" | "schtasks";
|
|
24
|
+
/** Absolute path to the wrapper script the service invokes. */
|
|
25
|
+
wrapperPath: string;
|
|
26
|
+
/** Absolute path to the launchd plist (macOS only). */
|
|
27
|
+
plistPath?: string;
|
|
28
|
+
/** Name of the schtasks task (Windows only). */
|
|
29
|
+
taskName?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface PlatformPaths {
|
|
33
|
+
tacoHome: string;
|
|
34
|
+
pkg: PlatformPkgPaths;
|
|
35
|
+
/** NDJSON socket path the wrapper will export as TACO_SOCKET. */
|
|
36
|
+
socket: string;
|
|
37
|
+
/** Control socket path the wrapper will export as TACO_CONTROL_SOCKET. */
|
|
38
|
+
control: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Top-level entry point for `taco install`. Throws on unsupported platforms
|
|
42
|
+
* or when no platform pkg is installed (the CLI surfaces the message). */
|
|
43
|
+
export async function installCommand(opts: InstallOptions = {}): Promise<InstallResult> {
|
|
44
|
+
const tacoHome = opts.tacoHome ?? TACO_HOME;
|
|
45
|
+
// Installed services always own the release runtime, not a caller's
|
|
46
|
+
// debug-only TACO_RUNTIME_DIR override.
|
|
47
|
+
const runtimeDir = defaultRuntimeDir(tacoHome);
|
|
48
|
+
await ensureDirs(tacoHome, runtimeDir);
|
|
49
|
+
|
|
50
|
+
const pkg = findPlatformPkg();
|
|
51
|
+
if (!pkg) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
"no @taco-ai/sidecar-<platform> bundle installed. " +
|
|
54
|
+
"Run `pnpm install` on a supported platform to install the " +
|
|
55
|
+
"optional dep, then retry `taco install`.",
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
// Stale-bundle guard: a pre-daemon bundle will boot into stdio mode,
|
|
59
|
+
// launchd will keep "running" it (exit 0 keeps KeepAlive happy), and
|
|
60
|
+
// the desktop's hello wait times out 5s later with no useful breadcrumb.
|
|
61
|
+
// Fail fast here so the fix (rebuild + reinstall) is obvious instead of
|
|
62
|
+
// buried in a silent daemon that never opens its socket.
|
|
63
|
+
if (!pkg.daemonMode) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`stale sidecar bundle at ${pkg.bundle}: missing daemon-mode support. ` +
|
|
66
|
+
"Rebuild the runtime (`pnpm --filter @taco-ai/sidecar package:runtime`) " +
|
|
67
|
+
"and re-run `taco install`.",
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const paths: PlatformPaths = {
|
|
72
|
+
tacoHome,
|
|
73
|
+
pkg,
|
|
74
|
+
socket: ndjsonSocketPath(runtimeDir),
|
|
75
|
+
control: controlSocketPath(runtimeDir),
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
if (process.platform === "darwin") {
|
|
79
|
+
return runInstallLaunchd({ ...paths, logDir: `${tacoHome}/logs` });
|
|
80
|
+
}
|
|
81
|
+
if (process.platform === "win32") {
|
|
82
|
+
return runInstallSchtasks({ ...paths });
|
|
83
|
+
}
|
|
84
|
+
throw new Error(`unsupported platform: ${process.platform}`);
|
|
85
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers shared by `taco install` / `taco uninstall` — platform-pkg discovery
|
|
3
|
+
* and a small `execFile` wrapper. Platform-specific dispatch (plist rendering,
|
|
4
|
+
* schtasks invocation) lives in `install.ts` / `uninstall.ts` so each file
|
|
5
|
+
* stays under the 1000-line cap.
|
|
6
|
+
*
|
|
7
|
+
* The platform pkg is found the same way `sidecarLauncher.ts`'s prod path
|
|
8
|
+
* does: walk the optional deps via `createRequire` and look for one whose
|
|
9
|
+
* `manifest.json` declares the right Node binary. We do NOT copy the pkg to
|
|
10
|
+
* $TACO_HOME — launchd / schtasks invoke a wrapper that points at the
|
|
11
|
+
* pkg's existing install location, so a `pnpm update` of the platform pkg
|
|
12
|
+
* propagates without re-running `taco install`.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { spawn } from "node:child_process";
|
|
16
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
17
|
+
import { createRequire } from "node:module";
|
|
18
|
+
import { dirname, join } from "node:path";
|
|
19
|
+
import { PLATFORM_KEYS } from "./upgradePlatform.ts";
|
|
20
|
+
|
|
21
|
+
export interface PlatformPkgPaths {
|
|
22
|
+
/** Absolute path to the @taco-ai/sidecar-<platform>/ package directory. */
|
|
23
|
+
pkgDir: string;
|
|
24
|
+
/** Absolute path to the bundled `taco-sidecar-node` (or .exe on Windows). */
|
|
25
|
+
nodeBin: string;
|
|
26
|
+
/** Absolute path to the bundled sidecar entry (`lib/index.mjs`). */
|
|
27
|
+
bundle: string;
|
|
28
|
+
/** Absolute path that should be exported as TACO_SIDECAR_RESOURCES — the
|
|
29
|
+
* directory containing `agents/` and `skills/`. Currently equals pkgDir. */
|
|
30
|
+
resources: string;
|
|
31
|
+
/** True when the bundle was built with daemon-mode support (manifest
|
|
32
|
+
* advertises `daemonMode: true`). A missing/false value means the
|
|
33
|
+
* bundle is stale (pre-daemon) — `taco install` must refuse to
|
|
34
|
+
* register a daemon that will silently boot into stdio mode. */
|
|
35
|
+
daemonMode: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Locate the @taco-ai/sidecar-<platform>/lib/index.mjs + bundled node binary.
|
|
39
|
+
* Returns null when no matching pkg is installed (the caller surfaces a
|
|
40
|
+
* clear error — install needs the bundle, dev mode does not). */
|
|
41
|
+
export function findPlatformPkg(): PlatformPkgPaths | null {
|
|
42
|
+
const configured = [
|
|
43
|
+
process.env.TACO_SIDECAR_NODE,
|
|
44
|
+
process.env.TACO_SIDECAR_BUNDLE,
|
|
45
|
+
process.env.TACO_SIDECAR_RESOURCES,
|
|
46
|
+
];
|
|
47
|
+
if (configured.every((value) => value !== undefined)) {
|
|
48
|
+
const [nodeBin, bundle, resources] = configured as [string, string, string];
|
|
49
|
+
if (existsSync(nodeBin) && existsSync(bundle)) {
|
|
50
|
+
// Env override: no manifest to consult, so grep the bundle
|
|
51
|
+
// directly. Slow (~4MB read) but install is not hot-path.
|
|
52
|
+
return {
|
|
53
|
+
pkgDir: resources,
|
|
54
|
+
nodeBin,
|
|
55
|
+
bundle,
|
|
56
|
+
resources,
|
|
57
|
+
daemonMode: bundleHasDaemonMode(bundle),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const req = createRequire(import.meta.url);
|
|
63
|
+
for (const key of PLATFORM_KEYS) {
|
|
64
|
+
try {
|
|
65
|
+
const pkgJsonPath = req.resolve(`@taco-ai/sidecar-${key}/package.json`);
|
|
66
|
+
const pkgDir = dirname(pkgJsonPath);
|
|
67
|
+
const manifestPath = join(pkgDir, "manifest.json");
|
|
68
|
+
if (!existsSync(manifestPath)) continue;
|
|
69
|
+
const manifest = JSON.parse(readFileSync(manifestPath, "utf8")) as {
|
|
70
|
+
target?: string;
|
|
71
|
+
daemonMode?: boolean;
|
|
72
|
+
};
|
|
73
|
+
const nodeBin = join(
|
|
74
|
+
pkgDir,
|
|
75
|
+
"bin",
|
|
76
|
+
manifest.target?.endsWith("-pc-windows-msvc")
|
|
77
|
+
? "taco-sidecar-node.exe"
|
|
78
|
+
: "taco-sidecar-node",
|
|
79
|
+
);
|
|
80
|
+
if (!existsSync(nodeBin)) continue;
|
|
81
|
+
const bundle = join(pkgDir, "lib", "index.mjs");
|
|
82
|
+
if (!existsSync(bundle)) continue;
|
|
83
|
+
// Trust the manifest's flag when present; fall back to a bundle
|
|
84
|
+
// grep for older packages that predate the field.
|
|
85
|
+
const daemonMode = manifest.daemonMode === true ? true : bundleHasDaemonMode(bundle);
|
|
86
|
+
return { pkgDir, nodeBin, bundle, resources: pkgDir, daemonMode };
|
|
87
|
+
} catch {
|
|
88
|
+
// not installed / wrong platform — try next
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Cheap daemon-mode probe: read the bundle and look for the env var the
|
|
95
|
+
* runtime gates on. Older manifests lack the `daemonMode` field, so
|
|
96
|
+
* install must fall back to this rather than reject. Bundle read is
|
|
97
|
+
* ~4MB — fine for `taco install`, not something to do per-RPC. Exported
|
|
98
|
+
* for unit tests; production callers should reach for `findPlatformPkg`
|
|
99
|
+
* which consults the manifest first. */
|
|
100
|
+
export function bundleHasDaemonMode(bundlePath: string): boolean {
|
|
101
|
+
try {
|
|
102
|
+
return readFileSync(bundlePath, "utf8").includes("TACO_DAEMON_MODE");
|
|
103
|
+
} catch {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface ExecResult {
|
|
109
|
+
code: number;
|
|
110
|
+
stdout: string;
|
|
111
|
+
stderr: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Run an external command and capture its output. Rejects on non-zero exit
|
|
115
|
+
* unless `opts.allowFailure` is true (used by `uninstall` for `launchctl
|
|
116
|
+
* unload` — it returns non-zero when the agent isn't loaded, which is fine). */
|
|
117
|
+
export function execFile(
|
|
118
|
+
program: string,
|
|
119
|
+
args: readonly string[],
|
|
120
|
+
opts: { allowFailure?: boolean } = {},
|
|
121
|
+
): Promise<ExecResult> {
|
|
122
|
+
return new Promise((resolve, reject) => {
|
|
123
|
+
const child = spawn(program, args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
124
|
+
const chunks: Buffer[] = [];
|
|
125
|
+
const errChunks: Buffer[] = [];
|
|
126
|
+
child.stdout.on("data", (c: Buffer) => chunks.push(c));
|
|
127
|
+
child.stderr.on("data", (c: Buffer) => errChunks.push(c));
|
|
128
|
+
child.once("error", (err) => reject(err));
|
|
129
|
+
child.once("close", (code) => {
|
|
130
|
+
const stdout = Buffer.concat(chunks).toString("utf8");
|
|
131
|
+
const stderr = Buffer.concat(errChunks).toString("utf8");
|
|
132
|
+
const result: ExecResult = { code: code ?? -1, stdout, stderr };
|
|
133
|
+
if (code === 0 || opts.allowFailure === true) {
|
|
134
|
+
resolve(result);
|
|
135
|
+
} else {
|
|
136
|
+
reject(new Error(`${program} ${args.join(" ")} exited ${code}: ${stderr.trim()}`));
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
}
|
package/lib/installId.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/** Mirror of `packages/sidecar/src/lib/installId.ts::computeInstallId`.
|
|
2
|
+
*
|
|
3
|
+
* The CLI cannot import from `@taco-ai/sidecar` (only the bundled platform
|
|
4
|
+
* pkg ships at runtime, not the TS source), so we duplicate the algorithm.
|
|
5
|
+
* Both sides MUST stay byte-for-byte identical — any drift means the
|
|
6
|
+
* desktop reap path will skip a daemon it owns (or kill one it doesn't).
|
|
7
|
+
* The unit test in `tests/installId.test.ts` enforces identical output
|
|
8
|
+
* against a fixed corpus of inputs.
|
|
9
|
+
*/
|
|
10
|
+
import { createHash } from "node:crypto";
|
|
11
|
+
|
|
12
|
+
export function computeInstallId(resourcesRoot: string, tacoHome: string): string {
|
|
13
|
+
const h = createHash("sha256");
|
|
14
|
+
h.update(resourcesRoot);
|
|
15
|
+
h.update("\0");
|
|
16
|
+
h.update(tacoHome);
|
|
17
|
+
return h.digest("hex").slice(0, 16);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Parse the contents of $TACO_HOME/run/sidecar.pid.
|
|
21
|
+
*
|
|
22
|
+
* Two on-disk formats are accepted:
|
|
23
|
+
* - **JSON record** (current, written by the daemon post-PR-A): a JSON
|
|
24
|
+
* object with `{version, pid, install_id, started_at}`. The version
|
|
25
|
+
* field exists so future schema changes can fail loudly rather than
|
|
26
|
+
* silently misparse old files.
|
|
27
|
+
* - **Legacy bare-int** (pre-PR-A): a single decimal pid string. Kept
|
|
28
|
+
* readable so a desktop upgrade doesn't strand an already-running
|
|
29
|
+
* daemon's pid file — the next reap on the new code will overwrite
|
|
30
|
+
* it once a fresh daemon starts.
|
|
31
|
+
*
|
|
32
|
+
* Returns null when the file is absent, unparseable, or yields a
|
|
33
|
+
* non-finite pid. Callers must treat null as "no claim to ownership" and
|
|
34
|
+
* not signal/kill anything. */
|
|
35
|
+
export interface ParsedPidFile {
|
|
36
|
+
pid: number;
|
|
37
|
+
installId: string | null;
|
|
38
|
+
startedAt: string | null;
|
|
39
|
+
/** Schema version, when the on-disk format is JSON. null for legacy. */
|
|
40
|
+
version: number | null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function parsePidFile(contents: string): ParsedPidFile | null {
|
|
44
|
+
const trimmed = contents.trim();
|
|
45
|
+
if (trimmed.length === 0) return null;
|
|
46
|
+
|
|
47
|
+
// Try JSON first. The version field is the discriminator: a bare-int
|
|
48
|
+
// pid (e.g. "12345\n") is the legacy format; anything starting with
|
|
49
|
+
// "{" is the new JSON record. JSON.parse of a bare int would throw,
|
|
50
|
+
// so a try/catch is cheap insurance against ambiguous input.
|
|
51
|
+
if (trimmed.startsWith("{")) {
|
|
52
|
+
try {
|
|
53
|
+
const parsed = JSON.parse(trimmed) as Record<string, unknown>;
|
|
54
|
+
if (parsed.version !== 1) {
|
|
55
|
+
// Unknown schema — caller can choose to log + unlink rather
|
|
56
|
+
// than act on a record whose shape we don't recognise.
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
const pid = typeof parsed.pid === "number" ? parsed.pid : Number.NaN;
|
|
60
|
+
const installId = typeof parsed.install_id === "string" ? parsed.install_id : null;
|
|
61
|
+
const startedAt = typeof parsed.started_at === "string" ? parsed.started_at : null;
|
|
62
|
+
if (!Number.isFinite(pid) || pid <= 0) return null;
|
|
63
|
+
return { pid, installId, startedAt, version: 1 };
|
|
64
|
+
} catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Legacy fallback. Some pid files may have whitespace or a trailing
|
|
70
|
+
// newline — trim already handled that. parseInt is permissive so we
|
|
71
|
+
// sanity-check with isFinite + positive.
|
|
72
|
+
const pid = Number.parseInt(trimmed, 10);
|
|
73
|
+
if (!Number.isFinite(pid) || pid <= 0) return null;
|
|
74
|
+
return { pid, installId: null, startedAt: null, version: null };
|
|
75
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/** macOS service registration. launchd executes a self-contained wrapper
|
|
2
|
+
* because its plist environment is static; paths are shell-quoted in the
|
|
3
|
+
* wrapper and XML-escaped only in the plist. */
|
|
4
|
+
|
|
5
|
+
import { writeFileSync } from "node:fs";
|
|
6
|
+
import { connect } from "node:net";
|
|
7
|
+
import { homedir } from "node:os";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import type { InstallResult } from "./install.ts";
|
|
10
|
+
import { execFile, type PlatformPkgPaths } from "./installHelpers.ts";
|
|
11
|
+
|
|
12
|
+
/** launchd label — appears in `launchctl list` + the LaunchAgents filename. */
|
|
13
|
+
export const LAUNCHD_LABEL = "com.taco-ai.sidecar";
|
|
14
|
+
|
|
15
|
+
/** LaunchAgent plist path under $HOME. */
|
|
16
|
+
export function launchdPlistPath(): string {
|
|
17
|
+
return join(homedir(), "Library", "LaunchAgents", `${LAUNCHD_LABEL}.plist`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** POSIX wrapper script name under $TACO_HOME/bin/. */
|
|
21
|
+
export function posixWrapperPath(tacoHome: string): string {
|
|
22
|
+
return join(tacoHome, "bin", "taco-sidecar-daemon");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Escape XML special characters in attribute / element values. */
|
|
26
|
+
export function escapeXml(value: string): string {
|
|
27
|
+
return value
|
|
28
|
+
.replace(/&/g, "&")
|
|
29
|
+
.replace(/</g, "<")
|
|
30
|
+
.replace(/>/g, ">")
|
|
31
|
+
.replace(/"/g, """)
|
|
32
|
+
.replace(/'/g, "'");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface PosixWrapperOptions {
|
|
36
|
+
socketPath: string;
|
|
37
|
+
controlSocketPath: string;
|
|
38
|
+
nodeBin: string;
|
|
39
|
+
bundle: string;
|
|
40
|
+
resourcesRoot: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function quoteShell(value: string): string {
|
|
44
|
+
return `'${value.replace(/'/g, "'\\''")}'`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function renderPosixWrapper(opts: PosixWrapperOptions): string {
|
|
48
|
+
return `#!/bin/sh
|
|
49
|
+
# Generated by 'taco install' — do not edit.
|
|
50
|
+
set -e
|
|
51
|
+
|
|
52
|
+
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
|
53
|
+
TACO_HOME=$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)
|
|
54
|
+
export TACO_HOME
|
|
55
|
+
|
|
56
|
+
export TACO_SOCKET=${quoteShell(opts.socketPath)}
|
|
57
|
+
export TACO_CONTROL_SOCKET=${quoteShell(opts.controlSocketPath)}
|
|
58
|
+
export TACO_DAEMON_MODE=1
|
|
59
|
+
export TACO_SIDECAR_RESOURCES=${quoteShell(opts.resourcesRoot)}
|
|
60
|
+
|
|
61
|
+
NODE_BIN=${quoteShell(opts.nodeBin)}
|
|
62
|
+
BUNDLE=${quoteShell(opts.bundle)}
|
|
63
|
+
exec "$NODE_BIN" "$BUNDLE"
|
|
64
|
+
`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface LaunchdPlistOptions {
|
|
68
|
+
wrapperPath: string;
|
|
69
|
+
tacoHome: string;
|
|
70
|
+
logDir: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Generate the launchd plist XML. KeepAlive covers crash + abnormal exit;
|
|
74
|
+
* ThrottleInterval prevents a tight crash loop from eating CPU. */
|
|
75
|
+
export function renderLaunchdPlist(opts: LaunchdPlistOptions): string {
|
|
76
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
77
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
78
|
+
<plist version="1.0">
|
|
79
|
+
<dict>
|
|
80
|
+
<key>Label</key> <string>${LAUNCHD_LABEL}</string>
|
|
81
|
+
<key>ProgramArguments</key>
|
|
82
|
+
<array>
|
|
83
|
+
<string>${escapeXml(opts.wrapperPath)}</string>
|
|
84
|
+
</array>
|
|
85
|
+
<key>RunAtLoad</key> <true/>
|
|
86
|
+
<key>KeepAlive</key>
|
|
87
|
+
<dict>
|
|
88
|
+
<key>SuccessfulExit</key> <false/>
|
|
89
|
+
<key>Crashed</key> <true/>
|
|
90
|
+
</dict>
|
|
91
|
+
<key>ProcessType</key> <string>Background</string>
|
|
92
|
+
<key>Nice</key> <integer>5</integer>
|
|
93
|
+
<key>ThrottleInterval</key><integer>10</integer>
|
|
94
|
+
<key>StandardOutPath</key> <string>${escapeXml(opts.logDir)}/daemon.out.log</string>
|
|
95
|
+
<key>StandardErrorPath</key><string>${escapeXml(opts.logDir)}/daemon.err.log</string>
|
|
96
|
+
<key>EnvironmentVariables</key>
|
|
97
|
+
<dict>
|
|
98
|
+
<key>TACO_HOME</key> <string>${escapeXml(opts.tacoHome)}</string>
|
|
99
|
+
</dict>
|
|
100
|
+
</dict>
|
|
101
|
+
</plist>
|
|
102
|
+
`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface InstallLaunchdInput {
|
|
106
|
+
tacoHome: string;
|
|
107
|
+
pkg: PlatformPkgPaths;
|
|
108
|
+
socket: string;
|
|
109
|
+
control: string;
|
|
110
|
+
logDir: string;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Probe the NDJSON socket until a connect attempt succeeds or the deadline
|
|
114
|
+
* expires. Mirrors the CLI's waitForSocket (start.ts) so launchd and the
|
|
115
|
+
* desktop agree on the "ready" heuristic. */
|
|
116
|
+
export function waitForSocket(path: string, timeoutMs: number): Promise<void> {
|
|
117
|
+
const deadline = Date.now() + timeoutMs;
|
|
118
|
+
return new Promise((resolve, reject) => {
|
|
119
|
+
const attempt = () => {
|
|
120
|
+
const sock = connect(path);
|
|
121
|
+
const finish = (err?: Error) => {
|
|
122
|
+
sock.removeAllListeners();
|
|
123
|
+
sock.destroy();
|
|
124
|
+
if (err) reject(err);
|
|
125
|
+
else resolve();
|
|
126
|
+
};
|
|
127
|
+
sock.once("connect", () => finish());
|
|
128
|
+
sock.once("error", (e) => {
|
|
129
|
+
if (Date.now() >= deadline) {
|
|
130
|
+
finish(new Error(`socket ${path} not ready in ${timeoutMs}ms (${e.message})`));
|
|
131
|
+
} else {
|
|
132
|
+
setTimeout(attempt, 100);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
attempt();
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Run the macOS install: write wrapper + plist, `launchctl load -w`,
|
|
141
|
+
* then wait for the daemon socket to appear. Throws if the load fails or
|
|
142
|
+
* the daemon doesn't come up within 10s. */
|
|
143
|
+
export async function installLaunchd(input: InstallLaunchdInput): Promise<InstallResult> {
|
|
144
|
+
const wrapperPath = posixWrapperPath(input.tacoHome);
|
|
145
|
+
const plistPath = launchdPlistPath();
|
|
146
|
+
|
|
147
|
+
const wrapperContent = renderPosixWrapper({
|
|
148
|
+
socketPath: input.socket,
|
|
149
|
+
controlSocketPath: input.control,
|
|
150
|
+
nodeBin: input.pkg.nodeBin,
|
|
151
|
+
bundle: input.pkg.bundle,
|
|
152
|
+
resourcesRoot: input.pkg.resources,
|
|
153
|
+
});
|
|
154
|
+
writeFileSync(wrapperPath, wrapperContent, { mode: 0o755 });
|
|
155
|
+
|
|
156
|
+
const plistContent = renderLaunchdPlist({
|
|
157
|
+
wrapperPath,
|
|
158
|
+
tacoHome: input.tacoHome,
|
|
159
|
+
logDir: input.logDir,
|
|
160
|
+
});
|
|
161
|
+
writeFileSync(plistPath, plistContent, { mode: 0o644 });
|
|
162
|
+
|
|
163
|
+
// bootout-then-load. `launchctl load -w` on a service that's ALREADY
|
|
164
|
+
// loaded is a no-op in some macOS versions (the service keeps running
|
|
165
|
+
// the old wrapper binary even though the plist on disk changed). We
|
|
166
|
+
// force a bootout first so the new plist + wrapper always take effect.
|
|
167
|
+
// `bootout` on a non-loaded service is exit 3 ("No such process"),
|
|
168
|
+
// which we tolerate.
|
|
169
|
+
try {
|
|
170
|
+
await execFile("launchctl", ["bootout", plistPath], {
|
|
171
|
+
allowFailure: true,
|
|
172
|
+
});
|
|
173
|
+
} catch {
|
|
174
|
+
// Older launchctl versions may not accept bootout without a target
|
|
175
|
+
// domain; the subsequent load still works as before.
|
|
176
|
+
}
|
|
177
|
+
// `-w` persists the load across reboots; without it launchd forgets the
|
|
178
|
+
// agent on the next login and the desktop has to re-install.
|
|
179
|
+
await execFile("launchctl", ["load", "-w", plistPath]);
|
|
180
|
+
|
|
181
|
+
try {
|
|
182
|
+
await waitForSocket(input.socket, 10_000);
|
|
183
|
+
} catch (err) {
|
|
184
|
+
throw new Error(
|
|
185
|
+
`launchctl load succeeded but daemon socket ${input.socket} did not appear: ${err}`,
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
serviceManager: "launchd",
|
|
191
|
+
wrapperPath,
|
|
192
|
+
plistPath,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/** Windows service registration through Task Scheduler. `ONSTART` matches
|
|
2
|
+
* the boot-time daemon contract; the wrapper bakes in the per-user home and
|
|
3
|
+
* runtime paths because schtasks cannot define environment variables. */
|
|
4
|
+
|
|
5
|
+
import { writeFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import type { InstallResult } from "./install.ts";
|
|
8
|
+
import { execFile, type PlatformPkgPaths } from "./installHelpers.ts";
|
|
9
|
+
|
|
10
|
+
/** Scheduled task name — appears in `schtasks /Query /TN` + the wrapper. */
|
|
11
|
+
export const SCHTASKS_NAME = "TacoSidecar";
|
|
12
|
+
|
|
13
|
+
/** Windows wrapper script name under $TACO_HOME\bin\. */
|
|
14
|
+
export function windowsWrapperPath(tacoHome: string): string {
|
|
15
|
+
return join(tacoHome, "bin", "taco-sidecar-daemon.cmd");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Escape characters that cmd treats as syntax (`& | < > ^ " %`) when they
|
|
19
|
+
* appear inside a `set VAR=value` line. Delimiters like `%` and `!` are
|
|
20
|
+
* also escaped for safety (delayed expansion + for-variable substitution
|
|
21
|
+
* can otherwise leak through to the sidecar). */
|
|
22
|
+
export function escapeCmd(value: string): string {
|
|
23
|
+
return value.replace(/[&|<>^"%!]/g, "^$&");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface WindowsWrapperOptions {
|
|
27
|
+
tacoHome: string;
|
|
28
|
+
socketPath: string;
|
|
29
|
+
controlSocketPath: string;
|
|
30
|
+
nodeBin: string;
|
|
31
|
+
bundle: string;
|
|
32
|
+
resourcesRoot: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Generate the Windows .cmd wrapper content. Every value is baked in so
|
|
36
|
+
* schtasks can invoke the .cmd with no args — schtasks /TR passes the
|
|
37
|
+
* script path verbatim. The wrapper resolves TACO_HOME at runtime (with
|
|
38
|
+
* the install-time value as fallback) so re-running `taco install` from
|
|
39
|
+
* a different user still lands the daemon under the right home. */
|
|
40
|
+
export function renderWindowsWrapper(opts: WindowsWrapperOptions): string {
|
|
41
|
+
const tacoHome = escapeCmd(opts.tacoHome);
|
|
42
|
+
const socket = escapeCmd(opts.socketPath);
|
|
43
|
+
const control = escapeCmd(opts.controlSocketPath);
|
|
44
|
+
const nodeBin = escapeCmd(opts.nodeBin);
|
|
45
|
+
const bundle = escapeCmd(opts.bundle);
|
|
46
|
+
const resourcesRoot = escapeCmd(opts.resourcesRoot);
|
|
47
|
+
return `@echo off
|
|
48
|
+
rem Generated by 'taco install' — do not edit.
|
|
49
|
+
rem Invokes the bundled sidecar in daemon mode under schtasks.
|
|
50
|
+
setlocal EnableExtensions EnableDelayedExpansion
|
|
51
|
+
|
|
52
|
+
rem Scheduled tasks do not inherit the installer's environment, so bake the
|
|
53
|
+
rem per-user TACO_HOME into the wrapper.
|
|
54
|
+
set "TACO_HOME=${tacoHome}"
|
|
55
|
+
|
|
56
|
+
rem Socket paths (must match packages/cli/lib/paths.ts on Windows).
|
|
57
|
+
set "TACO_SOCKET=${socket}"
|
|
58
|
+
set "TACO_CONTROL_SOCKET=${control}"
|
|
59
|
+
|
|
60
|
+
rem Daemon-mode env the sidecar bundle reads on startup.
|
|
61
|
+
set "TACO_DAEMON_MODE=1"
|
|
62
|
+
set "TACO_SIDECAR_RESOURCES=${resourcesRoot}"
|
|
63
|
+
|
|
64
|
+
rem Bundled node + sidecar entry (resolved at install time).
|
|
65
|
+
set "NODE_BIN=${nodeBin}"
|
|
66
|
+
set "BUNDLE=${bundle}"
|
|
67
|
+
|
|
68
|
+
"!NODE_BIN!" "!BUNDLE!"
|
|
69
|
+
endlocal
|
|
70
|
+
`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface InstallSchtasksInput {
|
|
74
|
+
tacoHome: string;
|
|
75
|
+
pkg: PlatformPkgPaths;
|
|
76
|
+
socket: string;
|
|
77
|
+
control: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Run the Windows install: write the .cmd wrapper + create the scheduled
|
|
81
|
+
* task via schtasks. We don't wait for the daemon socket (Windows named
|
|
82
|
+
* pipes don't have a stable readiness signal without a probe round-trip
|
|
83
|
+
* matching the sidecar's hello frame); the desktop's `resolve_sidecar`
|
|
84
|
+
* handles reconnect + retry. */
|
|
85
|
+
export async function installSchtasks(input: InstallSchtasksInput): Promise<InstallResult> {
|
|
86
|
+
const wrapperPath = windowsWrapperPath(input.tacoHome);
|
|
87
|
+
|
|
88
|
+
const wrapperContent = renderWindowsWrapper({
|
|
89
|
+
tacoHome: input.tacoHome,
|
|
90
|
+
socketPath: input.socket,
|
|
91
|
+
controlSocketPath: input.control,
|
|
92
|
+
nodeBin: input.pkg.nodeBin,
|
|
93
|
+
bundle: input.pkg.bundle,
|
|
94
|
+
resourcesRoot: input.pkg.resources,
|
|
95
|
+
});
|
|
96
|
+
writeFileSync(wrapperPath, wrapperContent, { mode: 0o755 });
|
|
97
|
+
|
|
98
|
+
// `/SC ONLOGON` so the daemon runs in the user's context — $TACO_HOME
|
|
99
|
+
// under %USERPROFILE%\.taco is a per-user path. `/RL HIGHEST` runs the
|
|
100
|
+
// task with the highest privileges available (still the user). `/F`
|
|
101
|
+
// overwrites any prior registration; re-running `taco install` is the
|
|
102
|
+
// supported update path.
|
|
103
|
+
await execFile("schtasks", [
|
|
104
|
+
"/Create",
|
|
105
|
+
"/SC",
|
|
106
|
+
"ONSTART",
|
|
107
|
+
"/TN",
|
|
108
|
+
SCHTASKS_NAME,
|
|
109
|
+
"/TR",
|
|
110
|
+
`"${wrapperPath}"`,
|
|
111
|
+
"/RL",
|
|
112
|
+
"HIGHEST",
|
|
113
|
+
"/F",
|
|
114
|
+
]);
|
|
115
|
+
|
|
116
|
+
// Kick the task immediately so the user doesn't have to sign out + in
|
|
117
|
+
// to see the install take effect. `/Run` returns once the task is
|
|
118
|
+
// dispatched, not when the daemon is ready — the desktop handles
|
|
119
|
+
// reconnect.
|
|
120
|
+
await execFile("schtasks", ["/Run", "/TN", SCHTASKS_NAME], { allowFailure: true });
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
serviceManager: "schtasks",
|
|
124
|
+
wrapperPath,
|
|
125
|
+
taskName: SCHTASKS_NAME,
|
|
126
|
+
};
|
|
127
|
+
}
|