@sirenodeck/sirenodeck 0.2.5 → 0.2.7
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/dist/capture-file-C11RW9NA.mjs +47 -0
- package/dist/darwin-B1ECvuGg.mjs +15 -0
- package/dist/darwin-Bs7hyOFv.mjs +78 -0
- package/dist/darwin-Cm-RiXMK.mjs +56 -0
- package/dist/darwin-DCTL6wDT.mjs +39 -0
- package/dist/darwin-RFhh5cL5.mjs +122 -0
- package/dist/install-yAoVCzK1.mjs +198 -0
- package/dist/linux-BDSwIJbc.mjs +577 -0
- package/dist/linux-CS_pCg0V.mjs +158 -0
- package/dist/linux-Dq07aXcR.mjs +119 -0
- package/dist/linux-OyPmXbFE.mjs +51 -0
- package/dist/live-metrics-CMf02ncg.mjs +975 -0
- package/dist/main.mjs +170 -10
- package/dist/spawn-IWeEQPpd.mjs +69 -0
- package/dist/windows-CRWdUgCn.mjs +45 -0
- package/dist/windows-CTAfiwO_.mjs +361 -0
- package/dist/windows-DeS97h4f.mjs +74 -0
- package/dist/windows-zhWiv_h8.mjs +55 -0
- package/package.json +2 -2
- package/src/cli/commands/run.ts +28 -1
- package/src/cli/commands/start.ts +17 -0
- package/src/cli/startup-display.ts +1 -1
- package/src/core/settings.ts +5 -3
- package/src/device/black-frame.ts +2 -7
- package/src/outputClient/real.ts +2 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
//#region src/system/providers/active-app/capture-file.ts
|
|
3
|
+
const sameSnapshot = (left, right) => left?.name === right?.name && left?.windowTitle === right?.windowTitle && left?.processId === right?.processId;
|
|
4
|
+
const readSnapshot = (path) => {
|
|
5
|
+
try {
|
|
6
|
+
const value = JSON.parse(readFileSync(path, "utf8"));
|
|
7
|
+
if (value === null) return null;
|
|
8
|
+
if (typeof value !== "object" || Array.isArray(value)) return null;
|
|
9
|
+
const snapshot = value;
|
|
10
|
+
if (typeof snapshot["name"] !== "string") return null;
|
|
11
|
+
const windowTitle = snapshot["windowTitle"];
|
|
12
|
+
const processId = snapshot["processId"];
|
|
13
|
+
return {
|
|
14
|
+
name: snapshot["name"],
|
|
15
|
+
windowTitle: typeof windowTitle === "string" ? windowTitle : null,
|
|
16
|
+
processId: typeof processId === "number" && Number.isFinite(processId) ? processId : null
|
|
17
|
+
};
|
|
18
|
+
} catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const createCaptureFileActiveAppProvider = (path) => {
|
|
23
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
24
|
+
let last = readSnapshot(path);
|
|
25
|
+
const interval = setInterval(() => {
|
|
26
|
+
const next = readSnapshot(path);
|
|
27
|
+
if (sameSnapshot(last, next)) return;
|
|
28
|
+
last = next;
|
|
29
|
+
for (const subscriber of subscribers) subscriber(next);
|
|
30
|
+
}, 100);
|
|
31
|
+
return {
|
|
32
|
+
async getActive() {
|
|
33
|
+
last = readSnapshot(path);
|
|
34
|
+
return last;
|
|
35
|
+
},
|
|
36
|
+
subscribe(handler) {
|
|
37
|
+
subscribers.add(handler);
|
|
38
|
+
return () => subscribers.delete(handler);
|
|
39
|
+
},
|
|
40
|
+
async stop() {
|
|
41
|
+
clearInterval(interval);
|
|
42
|
+
subscribers.clear();
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
//#endregion
|
|
47
|
+
export { createCaptureFileActiveAppProvider };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/system/providers/notification/darwin.ts
|
|
2
|
+
const escapeForAppleScript = (value) => value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
|
|
3
|
+
const createDarwinNotificationProvider = async (deps) => {
|
|
4
|
+
return { async notify(args) {
|
|
5
|
+
const script = `display notification "${escapeForAppleScript(args.body)}" with title "${escapeForAppleScript(args.title)}"`;
|
|
6
|
+
try {
|
|
7
|
+
if ((await deps.executor.run("osascript", ["-e", script])).exitCode !== 0) {}
|
|
8
|
+
} catch {}
|
|
9
|
+
if (args.sound === true && deps.soundPath !== void 0) try {
|
|
10
|
+
await deps.executor.run("afplay", [deps.soundPath]);
|
|
11
|
+
} catch {}
|
|
12
|
+
} };
|
|
13
|
+
};
|
|
14
|
+
//#endregion
|
|
15
|
+
export { createDarwinNotificationProvider };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
//#region src/system/providers/active-app/darwin.ts
|
|
2
|
+
const DEFAULT_POLL_MS = 1e3;
|
|
3
|
+
const APPLE_SCRIPT_GET_ACTIVE = `tell application "System Events"
|
|
4
|
+
set p to first process whose frontmost is true
|
|
5
|
+
set n to name of p
|
|
6
|
+
set u to unix id of p
|
|
7
|
+
try
|
|
8
|
+
set w to name of window 1 of p
|
|
9
|
+
on error
|
|
10
|
+
set w to ""
|
|
11
|
+
end try
|
|
12
|
+
return n & linefeed & u & linefeed & w
|
|
13
|
+
end tell`;
|
|
14
|
+
const parseOutput = (raw) => {
|
|
15
|
+
if (raw.trim().length === 0) return null;
|
|
16
|
+
const lines = raw.replace(/\r\n?/g, "\n").split("\n");
|
|
17
|
+
const name = (lines[0] ?? "").trim();
|
|
18
|
+
if (name.length === 0) return null;
|
|
19
|
+
const pid = Number.parseInt((lines[1] ?? "").trim(), 10);
|
|
20
|
+
return {
|
|
21
|
+
name,
|
|
22
|
+
title: lines.slice(2).join("\n").trim(),
|
|
23
|
+
pid: Number.isFinite(pid) ? pid : null
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
const createDarwinActiveAppProvider = async (deps) => {
|
|
27
|
+
const pollMs = deps.pollIntervalMs ?? DEFAULT_POLL_MS;
|
|
28
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
29
|
+
let last = null;
|
|
30
|
+
let stopped = false;
|
|
31
|
+
const snapshot = async () => {
|
|
32
|
+
try {
|
|
33
|
+
const result = await deps.executor.run("osascript", ["-e", APPLE_SCRIPT_GET_ACTIVE], { timeoutMs: 3e3 });
|
|
34
|
+
if (result.exitCode !== 0) return last;
|
|
35
|
+
const parsed = parseOutput(result.stdout);
|
|
36
|
+
if (parsed === null) return last;
|
|
37
|
+
return {
|
|
38
|
+
name: parsed.name,
|
|
39
|
+
windowTitle: parsed.title || null,
|
|
40
|
+
processId: parsed.pid
|
|
41
|
+
};
|
|
42
|
+
} catch (err) {
|
|
43
|
+
deps.logger.warn({ err }, "active-app: osascript failed");
|
|
44
|
+
return last;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
let interval = null;
|
|
48
|
+
interval = setInterval(() => {
|
|
49
|
+
if (stopped) return;
|
|
50
|
+
snapshot().then((s) => {
|
|
51
|
+
if (stopped) return;
|
|
52
|
+
if (!(last !== null && s !== null && last.name === s.name && last.windowTitle === s.windowTitle && last.processId === s.processId)) {
|
|
53
|
+
last = s;
|
|
54
|
+
for (const h of subscribers) h(s);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}, pollMs);
|
|
58
|
+
return {
|
|
59
|
+
async getActive() {
|
|
60
|
+
return snapshot();
|
|
61
|
+
},
|
|
62
|
+
subscribe(handler) {
|
|
63
|
+
subscribers.add(handler);
|
|
64
|
+
return () => {
|
|
65
|
+
subscribers.delete(handler);
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
async stop() {
|
|
69
|
+
stopped = true;
|
|
70
|
+
if (interval !== null) {
|
|
71
|
+
clearInterval(interval);
|
|
72
|
+
interval = null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
//#endregion
|
|
78
|
+
export { createDarwinActiveAppProvider };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
//#region src/system/providers/session/darwin.ts
|
|
2
|
+
const toState = (locked) => locked ? "locked" : "unlocked";
|
|
3
|
+
const IOREG_ARGS = [
|
|
4
|
+
"-n",
|
|
5
|
+
"Root",
|
|
6
|
+
"-d1",
|
|
7
|
+
"-a"
|
|
8
|
+
];
|
|
9
|
+
const parseScreenLocked = (raw) => /<key>CGSSessionScreenIsLocked<\/key>\s*<true\/>/.test(raw);
|
|
10
|
+
const createDarwinSessionProvider = async (deps) => {
|
|
11
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
12
|
+
let state = "unknown";
|
|
13
|
+
let stopped = false;
|
|
14
|
+
const pollMs = deps.pollIntervalMs ?? 5e3;
|
|
15
|
+
const tick = async () => {
|
|
16
|
+
try {
|
|
17
|
+
const result = await deps.executor.run("ioreg", [...IOREG_ARGS], { timeoutMs: 2e3 });
|
|
18
|
+
if (result.exitCode !== 0) return state;
|
|
19
|
+
return toState(parseScreenLocked(result.stdout));
|
|
20
|
+
} catch (err) {
|
|
21
|
+
deps.logger.debug({ err }, "session: ioreg failed");
|
|
22
|
+
return state;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
state = await tick();
|
|
26
|
+
let interval = setInterval(() => {
|
|
27
|
+
if (stopped) return;
|
|
28
|
+
tick().then((s) => {
|
|
29
|
+
if (stopped) return;
|
|
30
|
+
if (s !== state) {
|
|
31
|
+
state = s;
|
|
32
|
+
for (const l of listeners) l(state);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}, pollMs);
|
|
36
|
+
return {
|
|
37
|
+
getState() {
|
|
38
|
+
return state;
|
|
39
|
+
},
|
|
40
|
+
subscribe(handler) {
|
|
41
|
+
listeners.add(handler);
|
|
42
|
+
return () => {
|
|
43
|
+
listeners.delete(handler);
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
async stop() {
|
|
47
|
+
stopped = true;
|
|
48
|
+
if (interval !== null) {
|
|
49
|
+
clearInterval(interval);
|
|
50
|
+
interval = null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
//#endregion
|
|
56
|
+
export { createDarwinSessionProvider };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { a as withTimeout, o as ProviderError } from "./main.mjs";
|
|
2
|
+
//#region src/system/providers/clipboard/darwin.ts
|
|
3
|
+
const createDarwinClipboardProvider = (options) => {
|
|
4
|
+
const { executor, logger, timeoutMs: timeoutMsOption } = options;
|
|
5
|
+
const timeoutMs = timeoutMsOption ?? 500;
|
|
6
|
+
let disposed = false;
|
|
7
|
+
const stop = async () => {
|
|
8
|
+
disposed = true;
|
|
9
|
+
};
|
|
10
|
+
const writeText = async (text) => {
|
|
11
|
+
if (disposed) throw new Error("Clipboard provider is disposed");
|
|
12
|
+
const escaped = text.replace(/'/g, "'\\''");
|
|
13
|
+
try {
|
|
14
|
+
const result = await withTimeout(executor.run("sh", ["-c", `export LC_CTYPE=UTF-8; printf '%s' '${escaped}' | pbcopy`], { timeoutMs }), timeoutMs + 500);
|
|
15
|
+
if (result.exitCode !== 0) {
|
|
16
|
+
logger.warn({ stderr: result.stderr }, "clipboard: pbcopy failed");
|
|
17
|
+
throw new ProviderError("EXEC_FAILED", `clipboard write failed: ${result.stderr || "unknown error"}`);
|
|
18
|
+
}
|
|
19
|
+
} catch (err) {
|
|
20
|
+
if (err instanceof ProviderError) throw err;
|
|
21
|
+
throw new ProviderError("EXEC_FAILED", `clipboard write failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const readText = async () => {
|
|
25
|
+
if (disposed) throw new Error("Clipboard provider is disposed");
|
|
26
|
+
try {
|
|
27
|
+
const result = await withTimeout(executor.run("sh", ["-c", "export LC_CTYPE=UTF-8; pbpaste"], { timeoutMs }), timeoutMs + 500);
|
|
28
|
+
if (result.exitCode === 0) return result.stdout;
|
|
29
|
+
} catch {}
|
|
30
|
+
return "";
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
writeText,
|
|
34
|
+
readText,
|
|
35
|
+
stop
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
//#endregion
|
|
39
|
+
export { createDarwinClipboardProvider as t };
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { a as withTimeout, o as ProviderError, s as parseCombo } from "./main.mjs";
|
|
2
|
+
import { t as createDarwinClipboardProvider } from "./darwin-DCTL6wDT.mjs";
|
|
3
|
+
//#region src/system/providers/key-macro/darwin.ts
|
|
4
|
+
const MOD_OSASCRIPT = /* @__PURE__ */ new Map([
|
|
5
|
+
["ctrl", "control down"],
|
|
6
|
+
["meta", "command down"],
|
|
7
|
+
["super", "command down"],
|
|
8
|
+
["hyper", "command down"],
|
|
9
|
+
["alt", "option down"],
|
|
10
|
+
["shift", "shift down"]
|
|
11
|
+
]);
|
|
12
|
+
const escapeOsascriptString = (s) => s.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
|
|
13
|
+
const isPureAscii = (text) => /^[\x20-\x7E]*$/.test(text);
|
|
14
|
+
const CMD_V_ARGS = ["-e", `tell application "System Events" to keystroke "v" using {command down}`];
|
|
15
|
+
const CLIPBOARD_PRE_DELAY_MS = 60;
|
|
16
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
17
|
+
const isAccessibilityDenied = (stderr) => stderr.includes("(-1719)") || stderr.includes("(1002)") || stderr.includes("not allowed assistive access") || stderr.includes("not allowed to send keystrokes");
|
|
18
|
+
const ACCESSIBILITY_ERROR = "macOS blocked the keystroke: this app has no Accessibility permission. Open System Settings → Privacy & Security → Accessibility, enable the app running sirenodeck (your terminal, or SirenoDeck.app), then restart it.";
|
|
19
|
+
const KEY_ALIASES = /* @__PURE__ */ new Map([
|
|
20
|
+
["plus", "+"],
|
|
21
|
+
["minus", "-"],
|
|
22
|
+
["equal", "="],
|
|
23
|
+
["comma", ","],
|
|
24
|
+
["period", "."]
|
|
25
|
+
]);
|
|
26
|
+
const KEY_CODES = /* @__PURE__ */ new Map([
|
|
27
|
+
["Return", 36],
|
|
28
|
+
["Enter", 36],
|
|
29
|
+
["Tab", 48],
|
|
30
|
+
["space", 49],
|
|
31
|
+
["Delete", 51],
|
|
32
|
+
["BackSpace", 51],
|
|
33
|
+
["Escape", 53],
|
|
34
|
+
["Home", 115],
|
|
35
|
+
["Page_Up", 116],
|
|
36
|
+
["End", 119],
|
|
37
|
+
["Page_Down", 121],
|
|
38
|
+
["Left", 123],
|
|
39
|
+
["Right", 124],
|
|
40
|
+
["Down", 125],
|
|
41
|
+
["Up", 126],
|
|
42
|
+
["Insert", 114],
|
|
43
|
+
["Clear", 71],
|
|
44
|
+
["F1", 122],
|
|
45
|
+
["F2", 120],
|
|
46
|
+
["F3", 99],
|
|
47
|
+
["F4", 118],
|
|
48
|
+
["F5", 96],
|
|
49
|
+
["F6", 97],
|
|
50
|
+
["F7", 98],
|
|
51
|
+
["F8", 100],
|
|
52
|
+
["F9", 101],
|
|
53
|
+
["F10", 109],
|
|
54
|
+
["F11", 103],
|
|
55
|
+
["F12", 111],
|
|
56
|
+
["F13", 105],
|
|
57
|
+
["F14", 107],
|
|
58
|
+
["F15", 113],
|
|
59
|
+
["F16", 106],
|
|
60
|
+
["F17", 64],
|
|
61
|
+
["F18", 79],
|
|
62
|
+
["F19", 80],
|
|
63
|
+
["F20", 90]
|
|
64
|
+
]);
|
|
65
|
+
const buildComboArgs = (input) => {
|
|
66
|
+
const parsed = parseCombo(input);
|
|
67
|
+
if (parsed === null) return null;
|
|
68
|
+
const modList = ((parsed.key === "plus" || parsed.key === "minus") && !parsed.mods.includes("shift") ? [...parsed.mods, "shift"] : parsed.mods).map((m) => MOD_OSASCRIPT.get(m)).filter((m) => m !== void 0).join(", ");
|
|
69
|
+
const using = modList.length > 0 ? ` using {${modList}}` : "";
|
|
70
|
+
const code = KEY_CODES.get(parsed.key);
|
|
71
|
+
if (code !== void 0) return ["-e", `tell application "System Events" to key code ${code}${using}`];
|
|
72
|
+
const key = KEY_ALIASES.get(parsed.key) ?? parsed.key;
|
|
73
|
+
return ["-e", `tell application "System Events" to keystroke "${escapeOsascriptString(key)}"${using}`];
|
|
74
|
+
};
|
|
75
|
+
const buildLiteralArgs = (input) => {
|
|
76
|
+
return ["-e", `tell application "System Events" to keystroke "${escapeOsascriptString(input)}"`];
|
|
77
|
+
};
|
|
78
|
+
const createDarwinKeyMacroProvider = async (deps) => {
|
|
79
|
+
deps.logger.info("Darwin key-macro provider initialised (osascript; non-ASCII pastes via pbcopy)");
|
|
80
|
+
const timeoutMs = deps.timeoutMs ?? 500;
|
|
81
|
+
const clipboard = createDarwinClipboardProvider({
|
|
82
|
+
executor: deps.executor,
|
|
83
|
+
logger: deps.logger
|
|
84
|
+
});
|
|
85
|
+
const runOsascript = async (args) => {
|
|
86
|
+
let result;
|
|
87
|
+
try {
|
|
88
|
+
result = await withTimeout(deps.executor.run("osascript", [...args]), timeoutMs + 2500);
|
|
89
|
+
} catch (err) {
|
|
90
|
+
throw new ProviderError("EXEC_FAILED", `osascript failed: ${err.message ?? "unknown"}`);
|
|
91
|
+
}
|
|
92
|
+
if (result.exitCode === 0) return;
|
|
93
|
+
const stderr = result.stderr.trim();
|
|
94
|
+
if (isAccessibilityDenied(stderr)) throw new ProviderError("NOT_AVAILABLE", ACCESSIBILITY_ERROR);
|
|
95
|
+
throw new ProviderError("EXEC_FAILED", `osascript exited ${result.exitCode}: ${stderr}`);
|
|
96
|
+
};
|
|
97
|
+
return {
|
|
98
|
+
async sendKey(input) {
|
|
99
|
+
const comboArgs = buildComboArgs(input);
|
|
100
|
+
if (comboArgs !== null) {
|
|
101
|
+
await runOsascript(comboArgs);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (isPureAscii(input)) {
|
|
105
|
+
await runOsascript(buildLiteralArgs(input));
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
deps.logger.info({
|
|
109
|
+
step: "keyMacro.nonAscii.start",
|
|
110
|
+
textPreview: input.slice(0, 24)
|
|
111
|
+
}, "keyMacro: typing non-ASCII via pbcopy + cmd+v");
|
|
112
|
+
await clipboard.writeText(input);
|
|
113
|
+
await sleep(CLIPBOARD_PRE_DELAY_MS);
|
|
114
|
+
await runOsascript(CMD_V_ARGS);
|
|
115
|
+
},
|
|
116
|
+
async stop() {
|
|
117
|
+
await clipboard.stop();
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
//#endregion
|
|
122
|
+
export { createDarwinKeyMacroProvider };
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { dirname, join } from "node:path";
|
|
2
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { platform as platform$1 } from "node:process";
|
|
5
|
+
import { execSync } from "node:child_process";
|
|
6
|
+
//#region src/cli/commands/service/render-template.ts
|
|
7
|
+
const currentOS = () => {
|
|
8
|
+
const p = platform$1;
|
|
9
|
+
if (p === "darwin") return "darwin";
|
|
10
|
+
if (p === "win32") return "win32";
|
|
11
|
+
return "linux";
|
|
12
|
+
};
|
|
13
|
+
const renderSystemd = (vars, options) => {
|
|
14
|
+
const lines = [
|
|
15
|
+
"[Unit]",
|
|
16
|
+
`Description=${vars.description}`,
|
|
17
|
+
"After=network-online.target",
|
|
18
|
+
"Wants=network-online.target",
|
|
19
|
+
"",
|
|
20
|
+
"[Service]",
|
|
21
|
+
"Type=simple",
|
|
22
|
+
`ExecStart=${vars.execStart}`,
|
|
23
|
+
`Restart=${vars.restartPolicy}`,
|
|
24
|
+
"RestartSec=5",
|
|
25
|
+
`WorkingDirectory=${vars.workingDirectory}`
|
|
26
|
+
];
|
|
27
|
+
if (vars.user !== void 0) lines.push(`User=${vars.user}`);
|
|
28
|
+
if (vars.group !== void 0) lines.push(`Group=${vars.group}`);
|
|
29
|
+
lines.push("Environment=NODE_ENV=production");
|
|
30
|
+
lines.push("");
|
|
31
|
+
lines.push("[Install]");
|
|
32
|
+
lines.push(`WantedBy=${options.userLevel ? "default.target" : "multi-user.target"}`);
|
|
33
|
+
return `${lines.join("\n")}\n`;
|
|
34
|
+
};
|
|
35
|
+
const splitExec = (execStart) => {
|
|
36
|
+
const tokens = [];
|
|
37
|
+
let buf = "";
|
|
38
|
+
let quote = null;
|
|
39
|
+
for (const ch of execStart) if (quote !== null) {
|
|
40
|
+
if (ch === quote) quote = null;
|
|
41
|
+
else buf += ch;
|
|
42
|
+
} else if (ch === "\"" || ch === "'") quote = ch;
|
|
43
|
+
else if (ch === " ") {
|
|
44
|
+
if (buf.length > 0) {
|
|
45
|
+
tokens.push(buf);
|
|
46
|
+
buf = "";
|
|
47
|
+
}
|
|
48
|
+
} else buf += ch;
|
|
49
|
+
if (buf.length > 0) tokens.push(buf);
|
|
50
|
+
return tokens;
|
|
51
|
+
};
|
|
52
|
+
const darwinPathEntries = (program) => {
|
|
53
|
+
const entries = [
|
|
54
|
+
...program !== void 0 ? [dirname(program)] : [],
|
|
55
|
+
"/opt/homebrew/bin",
|
|
56
|
+
"/usr/local/bin",
|
|
57
|
+
"/usr/bin",
|
|
58
|
+
"/bin",
|
|
59
|
+
"/usr/sbin",
|
|
60
|
+
"/sbin"
|
|
61
|
+
];
|
|
62
|
+
return [...new Set(entries)].join(":");
|
|
63
|
+
};
|
|
64
|
+
const renderDarwinPlist = (vars) => {
|
|
65
|
+
const [program, ...restArgs] = splitExec(vars.execStart);
|
|
66
|
+
return `${[
|
|
67
|
+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
|
|
68
|
+
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">",
|
|
69
|
+
"<plist version=\"1.0\">",
|
|
70
|
+
"<dict>",
|
|
71
|
+
" <key>Label</key>",
|
|
72
|
+
` <string>${vars.name}</string>`,
|
|
73
|
+
" <key>ProgramArguments</key>",
|
|
74
|
+
" <array>",
|
|
75
|
+
...program !== void 0 ? [` <string>${program}</string>`] : [],
|
|
76
|
+
...restArgs.map((arg) => ` <string>${arg}</string>`),
|
|
77
|
+
" </array>",
|
|
78
|
+
" <key>RunAtLoad</key>",
|
|
79
|
+
" <true/>",
|
|
80
|
+
" <key>KeepAlive</key>",
|
|
81
|
+
` <${vars.restartPolicy === "always" ? "true" : "false"}/>`,
|
|
82
|
+
" <key>EnvironmentVariables</key>",
|
|
83
|
+
" <dict>",
|
|
84
|
+
" <key>PATH</key>",
|
|
85
|
+
` <string>${darwinPathEntries(program)}</string>`,
|
|
86
|
+
" <key>LANG</key>",
|
|
87
|
+
" <string>en_US.UTF-8</string>",
|
|
88
|
+
" <key>LC_CTYPE</key>",
|
|
89
|
+
" <string>UTF-8</string>",
|
|
90
|
+
" </dict>",
|
|
91
|
+
" <key>WorkingDirectory</key>",
|
|
92
|
+
` <string>${vars.workingDirectory}</string>`,
|
|
93
|
+
" <key>StandardOutPath</key>",
|
|
94
|
+
` <string>${vars.logPath}</string>`,
|
|
95
|
+
" <key>StandardErrorPath</key>",
|
|
96
|
+
` <string>${vars.logPath}</string>`,
|
|
97
|
+
"</dict>",
|
|
98
|
+
"</plist>"
|
|
99
|
+
].join("\n")}\n`;
|
|
100
|
+
};
|
|
101
|
+
const renderWindowsSvc = (vars) => {
|
|
102
|
+
return `@echo off
|
|
103
|
+
REM ${vars.description}
|
|
104
|
+
sc create ${vars.name} binPath= "${vars.execStart}" start= auto
|
|
105
|
+
sc description ${vars.name} "${vars.description}"
|
|
106
|
+
sc failure "${vars.name}" reset= 86400 actions= restart/5000/restart/10000/restart/30000
|
|
107
|
+
`;
|
|
108
|
+
};
|
|
109
|
+
const renderTemplate = (os, vars, options) => {
|
|
110
|
+
switch (os) {
|
|
111
|
+
case "linux": return renderSystemd(vars, options);
|
|
112
|
+
case "darwin": return renderDarwinPlist(vars);
|
|
113
|
+
case "win32": return renderWindowsSvc(vars);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/cli/commands/service/install.ts
|
|
118
|
+
const DAEMON_NAME = "sirenodeck";
|
|
119
|
+
const SERVICE_NAME = `${DAEMON_NAME}.service`;
|
|
120
|
+
const getExecStart = () => {
|
|
121
|
+
const script = process.argv[1];
|
|
122
|
+
if (script === void 0) return "sirenodeck start";
|
|
123
|
+
return `"${process.execPath}" "${script}" start`;
|
|
124
|
+
};
|
|
125
|
+
const getTemplateVars = () => {
|
|
126
|
+
const home = homedir();
|
|
127
|
+
return {
|
|
128
|
+
name: DAEMON_NAME,
|
|
129
|
+
displayName: "Sireno Deck",
|
|
130
|
+
description: "Sireno Deck daemon service",
|
|
131
|
+
execStart: getExecStart(),
|
|
132
|
+
restartPolicy: "always",
|
|
133
|
+
workingDirectory: home,
|
|
134
|
+
logPath: join(home, "Library", "Logs", `${DAEMON_NAME}.log`)
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
const ensureDir = (dir, mode) => {
|
|
138
|
+
if (!existsSync(dir)) mkdirSync(dir, {
|
|
139
|
+
recursive: true,
|
|
140
|
+
mode
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
const installService = async (options) => {
|
|
144
|
+
const { logger, system = false } = options;
|
|
145
|
+
const os = currentOS();
|
|
146
|
+
const vars = getTemplateVars();
|
|
147
|
+
const userLevel = !system;
|
|
148
|
+
if (os === "linux") {
|
|
149
|
+
const unitDir = userLevel ? join(homedir(), ".config", "systemd", "user") : "/etc/systemd/system";
|
|
150
|
+
const unitPath = join(unitDir, SERVICE_NAME);
|
|
151
|
+
const content = renderTemplate(os, {
|
|
152
|
+
...vars,
|
|
153
|
+
...userLevel ? {} : {
|
|
154
|
+
user: "root",
|
|
155
|
+
group: "root"
|
|
156
|
+
}
|
|
157
|
+
}, { userLevel });
|
|
158
|
+
try {
|
|
159
|
+
if (userLevel) ensureDir(unitDir, 493);
|
|
160
|
+
writeFileSync(unitPath, content, { mode: 420 });
|
|
161
|
+
logger.info({ path: unitPath }, "install: systemd unit installed");
|
|
162
|
+
const systemctl = userLevel ? "systemctl --user" : "systemctl";
|
|
163
|
+
execSync(`${systemctl} daemon-reload`, { stdio: "ignore" });
|
|
164
|
+
logger.info({ userLevel }, "install: systemd daemon reloaded");
|
|
165
|
+
execSync(`${systemctl} enable ${SERVICE_NAME}`, { stdio: "ignore" });
|
|
166
|
+
logger.info({
|
|
167
|
+
service: SERVICE_NAME,
|
|
168
|
+
userLevel
|
|
169
|
+
}, "install: enabled");
|
|
170
|
+
} catch (err) {
|
|
171
|
+
logger.error({
|
|
172
|
+
err,
|
|
173
|
+
userLevel
|
|
174
|
+
}, userLevel ? "install: failed to install user-level systemd service" : "install: failed to install system systemd service (needs root)");
|
|
175
|
+
process.exitCode = 1;
|
|
176
|
+
}
|
|
177
|
+
} else if (os === "darwin") {
|
|
178
|
+
const plistDir = join(homedir(), "Library", "LaunchAgents");
|
|
179
|
+
const plistPath = join(plistDir, `${DAEMON_NAME}.plist`);
|
|
180
|
+
const logDir = join(homedir(), "Library", "Logs");
|
|
181
|
+
try {
|
|
182
|
+
ensureDir(plistDir, 493);
|
|
183
|
+
ensureDir(logDir, 493);
|
|
184
|
+
const content = renderTemplate(os, vars, { userLevel });
|
|
185
|
+
writeFileSync(plistPath, content, { mode: 420 });
|
|
186
|
+
logger.info({ path: plistPath }, "install: launchd plist installed");
|
|
187
|
+
logger.info({ service: SERVICE_NAME }, "install: plist written — bootstrap happens on start");
|
|
188
|
+
} catch (err) {
|
|
189
|
+
logger.error({ err }, "install: failed to install launchd service");
|
|
190
|
+
process.exitCode = 1;
|
|
191
|
+
}
|
|
192
|
+
} else {
|
|
193
|
+
logger.error("install: native service installation not supported on this platform");
|
|
194
|
+
process.exitCode = 1;
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
//#endregion
|
|
198
|
+
export { installService };
|