@scrylog/cli 0.1.1 → 0.1.3
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/package.json +1 -1
- package/src/service/manager.ts +51 -11
package/package.json
CHANGED
package/src/service/manager.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { existsSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
-
import {
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { dirname, isAbsolute, join } from "node:path";
|
|
3
4
|
import { getResolvedConfigPath } from "../shared/config";
|
|
4
5
|
import {
|
|
5
6
|
ensureParentDir,
|
|
@@ -9,7 +10,7 @@ import {
|
|
|
9
10
|
getSystemdUnitPath,
|
|
10
11
|
getUserUid,
|
|
11
12
|
} from "../shared/paths";
|
|
12
|
-
import { IS_LINUX, IS_MAC, IS_WSL } from "../shared/platform";
|
|
13
|
+
import { IS_LINUX, IS_MAC, IS_WSL, whichCommand } from "../shared/platform";
|
|
13
14
|
import { maybeRunCommand } from "../shared/utils";
|
|
14
15
|
|
|
15
16
|
export interface ServiceStatus {
|
|
@@ -52,13 +53,18 @@ function daemonScriptPath(): string {
|
|
|
52
53
|
return join(import.meta.dir, "../daemon/index.ts");
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
function
|
|
56
|
+
export function resolveDaemonExec(configPath?: string, daemonPath = whichCommand("scrylogd")): string[] {
|
|
56
57
|
const resolvedConfigPath = getResolvedConfigPath(configPath);
|
|
58
|
+
if (daemonPath) {
|
|
59
|
+
return [daemonPath, "--config", resolvedConfigPath];
|
|
60
|
+
}
|
|
57
61
|
return [process.execPath, "run", daemonScriptPath(), "--config", resolvedConfigPath];
|
|
58
62
|
}
|
|
59
63
|
|
|
60
64
|
export function renderLaunchAgentPlist(configPath?: string): string {
|
|
61
|
-
const
|
|
65
|
+
const args = resolveDaemonExec(configPath);
|
|
66
|
+
const programArguments = args.map((arg) => ` <string>${xmlEscape(arg)}</string>`).join("\n");
|
|
67
|
+
const path = launchdPath(args);
|
|
62
68
|
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
63
69
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
64
70
|
<plist version="1.0">
|
|
@@ -67,12 +73,13 @@ export function renderLaunchAgentPlist(configPath?: string): string {
|
|
|
67
73
|
<string>${getServiceLabel()}</string>
|
|
68
74
|
<key>ProgramArguments</key>
|
|
69
75
|
<array>
|
|
70
|
-
|
|
71
|
-
<string>${runArg}</string>
|
|
72
|
-
<string>${scriptPath}</string>
|
|
73
|
-
<string>${configFlag}</string>
|
|
74
|
-
<string>${resolvedConfigPath}</string>
|
|
76
|
+
${programArguments}
|
|
75
77
|
</array>
|
|
78
|
+
<key>EnvironmentVariables</key>
|
|
79
|
+
<dict>
|
|
80
|
+
<key>PATH</key>
|
|
81
|
+
<string>${xmlEscape(path)}</string>
|
|
82
|
+
</dict>
|
|
76
83
|
<key>RunAtLoad</key>
|
|
77
84
|
<true/>
|
|
78
85
|
<key>KeepAlive</key>
|
|
@@ -83,13 +90,13 @@ export function renderLaunchAgentPlist(configPath?: string): string {
|
|
|
83
90
|
}
|
|
84
91
|
|
|
85
92
|
export function renderSystemdUserUnit(configPath?: string): string {
|
|
86
|
-
const
|
|
93
|
+
const args = resolveDaemonExec(configPath);
|
|
87
94
|
return `[Unit]
|
|
88
95
|
Description=scrylog daemon
|
|
89
96
|
After=default.target
|
|
90
97
|
|
|
91
98
|
[Service]
|
|
92
|
-
ExecStart=${
|
|
99
|
+
ExecStart=${args.map(shellQuote).join(" ")}
|
|
93
100
|
Restart=on-failure
|
|
94
101
|
RestartSec=3
|
|
95
102
|
|
|
@@ -240,3 +247,36 @@ class UnsupportedServiceManager implements ServiceManager {
|
|
|
240
247
|
function shellQuote(value: string): string {
|
|
241
248
|
return `'${value.replace(/'/g, `'"'"'`)}'`;
|
|
242
249
|
}
|
|
250
|
+
|
|
251
|
+
function launchdPath(args: string[]): string {
|
|
252
|
+
const commandPath = args[0];
|
|
253
|
+
const candidates = [
|
|
254
|
+
commandPath && isAbsolute(commandPath) ? dirname(commandPath) : undefined,
|
|
255
|
+
isAbsolute(process.execPath) ? dirname(process.execPath) : undefined,
|
|
256
|
+
process.env.PATH,
|
|
257
|
+
join(homedir(), ".bun", "bin"),
|
|
258
|
+
"/opt/homebrew/bin",
|
|
259
|
+
"/usr/local/bin",
|
|
260
|
+
"/usr/bin",
|
|
261
|
+
"/bin",
|
|
262
|
+
"/usr/sbin",
|
|
263
|
+
"/sbin",
|
|
264
|
+
];
|
|
265
|
+
|
|
266
|
+
const paths = new Set<string>();
|
|
267
|
+
for (const candidate of candidates) {
|
|
268
|
+
for (const part of candidate?.split(":") ?? []) {
|
|
269
|
+
if (part) paths.add(part);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return Array.from(paths).join(":");
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function xmlEscape(value: string): string {
|
|
276
|
+
return value
|
|
277
|
+
.replace(/&/g, "&")
|
|
278
|
+
.replace(/</g, "<")
|
|
279
|
+
.replace(/>/g, ">")
|
|
280
|
+
.replace(/"/g, """)
|
|
281
|
+
.replace(/'/g, "'");
|
|
282
|
+
}
|