feat(node): add TypeScript sibling project for npm-based install
Introduces @kuns/claude-mailbox under node/, a wire-compatible TypeScript port of the .NET daemon that distributes via the public Gitea npm registry. The .NET project stays in src/ClaudeMailbox/ untouched; users pick whichever flavor they prefer. - node/ project: fastify + @modelcontextprotocol/sdk StreamableHTTPServerTransport + better-sqlite3, schema and wire surface match the C# version (port 47822, X-Mailbox header, MCP tool names, snake_case SQLite columns) - Cross-platform autostart: Scheduled Task (Win, no admin) / Windows Service (Win, --service) / launchd (mac) / systemd --user (linux) - 9/9 vitest tests pass; end-to-end /health + send/check round-trip verified - CI split: existing ci.yml/release.yml renamed to *-dotnet.yml with path filters, new ci-node.yml and release-node.yml publish to Gitea npm registry - install.ps1 / install.sh bootstrap one-liners at repo root; homebrew/ contains a tap formula template - README install section reordered: npm path primary, dotnet publish secondary Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
121
node/src/autostart/darwin.ts
Normal file
121
node/src/autostart/darwin.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import { existsSync, mkdirSync, writeFileSync, unlinkSync, rmSync, readFileSync } from "node:fs";
|
||||
import { join, dirname } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import { run, cliEntry, type AutostartManager, type AutostartInstallOpts } from "./index.js";
|
||||
import { userConfigPath } from "../config.js";
|
||||
|
||||
const LABEL = "dev.kuns.claude-mailbox";
|
||||
|
||||
function plistPath(): string {
|
||||
return join(homedir(), "Library", "LaunchAgents", `${LABEL}.plist`);
|
||||
}
|
||||
|
||||
function logDir(): string {
|
||||
return join(homedir(), "Library", "Logs", "ClaudeMailbox");
|
||||
}
|
||||
|
||||
function ensureConfigSeeded(opts: AutostartInstallOpts): string {
|
||||
const path = userConfigPath();
|
||||
if (!existsSync(path)) {
|
||||
mkdirSync(dirname(path), { recursive: true });
|
||||
const seed: Record<string, unknown> = {};
|
||||
if (opts.port !== undefined) seed.port = opts.port;
|
||||
if (opts.bind !== undefined) seed.bind = opts.bind;
|
||||
if (opts.dbPath !== undefined) seed.dbPath = opts.dbPath;
|
||||
writeFileSync(path, JSON.stringify(seed, null, 2) + "\n", "utf8");
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
function escapeXml(s: string): string {
|
||||
return s
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """);
|
||||
}
|
||||
|
||||
function buildPlist(node: string, script: string, configPath: string): string {
|
||||
mkdirSync(logDir(), { recursive: true });
|
||||
const argv = [node, script, "serve", "--config", configPath];
|
||||
const argsXml = argv
|
||||
.map((a) => ` <string>${escapeXml(a)}</string>`)
|
||||
.join("\n");
|
||||
const stdout = join(logDir(), "stdout.log");
|
||||
const stderr = join(logDir(), "stderr.log");
|
||||
return `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>${LABEL}</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
${argsXml}
|
||||
</array>
|
||||
<key>RunAtLoad</key>
|
||||
<true/>
|
||||
<key>KeepAlive</key>
|
||||
<true/>
|
||||
<key>StandardOutPath</key>
|
||||
<string>${escapeXml(stdout)}</string>
|
||||
<key>StandardErrorPath</key>
|
||||
<string>${escapeXml(stderr)}</string>
|
||||
</dict>
|
||||
</plist>
|
||||
`;
|
||||
}
|
||||
|
||||
export function darwinManager(): AutostartManager {
|
||||
return {
|
||||
mode: "default",
|
||||
async install(opts) {
|
||||
const configPath = ensureConfigSeeded(opts);
|
||||
const { node, script } = cliEntry();
|
||||
const plist = buildPlist(node, script, configPath);
|
||||
const path = plistPath();
|
||||
mkdirSync(dirname(path), { recursive: true });
|
||||
writeFileSync(path, plist, "utf8");
|
||||
run("launchctl", ["unload", path]);
|
||||
const r = run("launchctl", ["load", "-w", path]);
|
||||
if (r.status !== 0) {
|
||||
throw new Error(`launchctl load failed: ${r.stderr || r.stdout}`);
|
||||
}
|
||||
},
|
||||
async uninstall(purge) {
|
||||
const path = plistPath();
|
||||
if (existsSync(path)) {
|
||||
run("launchctl", ["unload", path]);
|
||||
unlinkSync(path);
|
||||
}
|
||||
if (purge) {
|
||||
const cfg = userConfigPath();
|
||||
if (existsSync(cfg)) {
|
||||
try {
|
||||
const parsed = JSON.parse(readFileSync(cfg, "utf8")) as { dbPath?: string };
|
||||
if (parsed.dbPath && existsSync(parsed.dbPath)) {
|
||||
rmSync(parsed.dbPath, { force: true });
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
unlinkSync(cfg);
|
||||
}
|
||||
}
|
||||
},
|
||||
async start() {
|
||||
const r = run("launchctl", ["start", LABEL]);
|
||||
if (r.status !== 0) throw new Error(`launchctl start failed: ${r.stderr || r.stdout}`);
|
||||
},
|
||||
async stop() {
|
||||
run("launchctl", ["stop", LABEL]);
|
||||
},
|
||||
async status() {
|
||||
if (!existsSync(plistPath())) return "NotInstalled";
|
||||
const r = run("launchctl", ["list", LABEL]);
|
||||
if (r.status !== 0) return "Stopped";
|
||||
const pidMatch = r.stdout.match(/"PID"\s*=\s*(\d+)/);
|
||||
return pidMatch ? "Running" : "Stopped";
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user