@jive-sh/skill-sync 0.2.2-latest-windows-x64 → 0.2.2
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/README.md +56 -0
- package/bin/skill-sync +576 -0
- package/bin/skill-sync-daemon +576 -0
- package/dist/daemons/skill-sync-daemon.js +279 -0
- package/dist/daemons/skill-sync-daemon.js.map +1 -0
- package/dist/entrypoints/skill-sync.js +100 -0
- package/dist/entrypoints/skill-sync.js.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/config.js +150 -0
- package/dist/internal/config.js.map +1 -0
- package/dist/internal/folders.js +65 -0
- package/dist/internal/folders.js.map +1 -0
- package/dist/internal/notify.js +50 -0
- package/dist/internal/notify.js.map +1 -0
- package/dist/internal/path.js +36 -0
- package/dist/internal/path.js.map +1 -0
- package/dist/internal/reconcile.js +124 -0
- package/dist/internal/reconcile.js.map +1 -0
- package/dist/internal/sync.js +241 -0
- package/dist/internal/sync.js.map +1 -0
- package/dist/internal/wire.js +13 -0
- package/dist/internal/wire.js.map +1 -0
- package/install.js +576 -0
- package/package.json +39 -11
- package/src/daemons/skill-sync-daemon.ts +276 -0
- package/src/entrypoints/skill-sync.ts +109 -0
- package/src/index.ts +15 -0
- package/src/internal/config.ts +191 -0
- package/src/internal/folders.ts +69 -0
- package/src/internal/notify.ts +53 -0
- package/src/internal/path.ts +32 -0
- package/src/internal/reconcile.ts +166 -0
- package/src/internal/sync.ts +304 -0
- package/src/internal/wire.ts +162 -0
- package/multi-call-binary.exe +0 -4
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# @jive-sh/skill-sync
|
|
2
|
+
|
|
3
|
+
Keeps the skills you choose identical across every Claude folder on every machine you work on.
|
|
4
|
+
|
|
5
|
+
A daemon finds each Claude home on the machine (`~/.claude` and every `~/.claude-<name>`) and syncs the
|
|
6
|
+
`skills` folder in each through a Cloudflare Worker: file index in D1, contents in R2 addressed by hash.
|
|
7
|
+
`skill-sync login` opens `skill-sync.jive.sh` in your browser, where you verify with your jive.sh passkey
|
|
8
|
+
(from any passkey manager, such as Google Password Manager); the service then hands the CLI a per-machine
|
|
9
|
+
token and opens the dashboard. The CLI never handles the passkey.
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
skill-sync login verify with your passkey in the browser and store this machine's token
|
|
13
|
+
skill-sync logout revoke this machine's token
|
|
14
|
+
skill-sync upgrade upgrade with the package manager that installed it
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Everything else — which folders and skills sync, renaming computers, the merge log — is on the
|
|
18
|
+
dashboard.
|
|
19
|
+
|
|
20
|
+
The daemon registers itself to start at login when the package is installed.
|
|
21
|
+
|
|
22
|
+
## The dashboard
|
|
23
|
+
|
|
24
|
+
`https://skill-sync.jive.sh/` shows, live:
|
|
25
|
+
|
|
26
|
+
- **Synced skills**: the shared version of every skill that syncs, and who changed it last.
|
|
27
|
+
- **Reconciliation log**: every merge Claude made — which computer, which folder and file, what
|
|
28
|
+
conflicted and how it was resolved — so you can audit them.
|
|
29
|
+
- **Computers**: each machine (renameable), whether its daemon is running, its Claude folders, and the
|
|
30
|
+
skills in each with their files.
|
|
31
|
+
|
|
32
|
+
Every file shows the first 8 characters of its SHA-256, amber when a folder's copy differs from the synced
|
|
33
|
+
one. The page reloads itself when a new version of the service is deployed.
|
|
34
|
+
|
|
35
|
+
Each folder and each skill has a checkbox. An unchecked folder does not sync at all; an unchecked skill
|
|
36
|
+
stays local to its folder — never sent, never overwritten. Choices are stored on the server, so they apply
|
|
37
|
+
at once and survive a reinstall. Defaults: a new folder syncs; a skill already synced there stays synced;
|
|
38
|
+
a skill that exists only in one folder stays local until you check it; a shared skill a folder does not
|
|
39
|
+
have yet is delivered to it.
|
|
40
|
+
|
|
41
|
+
## Conflicts
|
|
42
|
+
|
|
43
|
+
Every write is a compare-and-swap on the file's current hash, so a change made in two places at once is
|
|
44
|
+
never silently overwritten. When a file changed both locally and on the server — or a folder opts into a
|
|
45
|
+
skill it already has a different copy of — the daemon asks Claude, with no tools, to merge the two (using
|
|
46
|
+
the last agreed version when there is one), writes the merge, pushes it, and logs the explanation to the
|
|
47
|
+
dashboard. Both versions are copied first to `~/.jive/skill-sync/reconciled/`.
|
|
48
|
+
|
|
49
|
+
If Claude is unavailable or the merge looks truncated, the server's version is kept and yours is saved
|
|
50
|
+
beside it as `<file>.local`, with a desktop notification and a "needs attention" state on the dashboard.
|
|
51
|
+
|
|
52
|
+
## Configuration
|
|
53
|
+
|
|
54
|
+
`~/.jive/skill-sync.json` holds the endpoint and the machine name, written on first run.
|
|
55
|
+
`~/.skill-sync-token.json` holds the token alone, at mode 0600, written by `skill-sync login` rather than
|
|
56
|
+
by hand. Merges run under the machine's default `claude` login.
|
package/bin/skill-sync
ADDED
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import {
|
|
5
|
+
chmodSync,
|
|
6
|
+
copyFileSync,
|
|
7
|
+
existsSync,
|
|
8
|
+
linkSync,
|
|
9
|
+
mkdirSync,
|
|
10
|
+
readdirSync,
|
|
11
|
+
readFileSync,
|
|
12
|
+
renameSync,
|
|
13
|
+
rmSync,
|
|
14
|
+
writeFileSync,
|
|
15
|
+
} from "node:fs";
|
|
16
|
+
import { createHash } from "node:crypto";
|
|
17
|
+
import { homedir } from "node:os";
|
|
18
|
+
import { basename, dirname, join } from "node:path";
|
|
19
|
+
import { fileURLToPath } from "node:url";
|
|
20
|
+
|
|
21
|
+
const PLATFORM_MAP = {
|
|
22
|
+
"darwin-arm64": "darwin-arm64",
|
|
23
|
+
"darwin-x64": "darwin-x64",
|
|
24
|
+
"linux-arm64": "linux-arm64",
|
|
25
|
+
"linux-x64": "linux-x64",
|
|
26
|
+
"win32-x64": "windows-x64"
|
|
27
|
+
};
|
|
28
|
+
const PLATFORM_PACKAGES = {
|
|
29
|
+
"darwin-arm64": "@jive-sh/skill-sync-darwin-arm64",
|
|
30
|
+
"darwin-x64": "@jive-sh/skill-sync-darwin-x64",
|
|
31
|
+
"linux-arm64": "@jive-sh/skill-sync-linux-arm64",
|
|
32
|
+
"linux-x64": "@jive-sh/skill-sync-linux-x64",
|
|
33
|
+
"windows-x64": "@jive-sh/skill-sync-windows-x64"
|
|
34
|
+
};
|
|
35
|
+
const PLATFORM_BINARIES = {
|
|
36
|
+
"darwin-arm64": "multi-call-binary",
|
|
37
|
+
"darwin-x64": "multi-call-binary",
|
|
38
|
+
"linux-arm64": "multi-call-binary",
|
|
39
|
+
"linux-x64": "multi-call-binary",
|
|
40
|
+
"windows-x64": "multi-call-binary.exe"
|
|
41
|
+
};
|
|
42
|
+
const COMMANDS = [
|
|
43
|
+
"skill-sync",
|
|
44
|
+
"skill-sync-daemon"
|
|
45
|
+
];
|
|
46
|
+
const PACKAGE_NAME = "@jive-sh/skill-sync";
|
|
47
|
+
// Every src/daemons/ entry. The build refuses to produce a package where this disagrees with the files,
|
|
48
|
+
// so it needs no checking here.
|
|
49
|
+
const DAEMONS = [
|
|
50
|
+
"skill-sync-daemon"
|
|
51
|
+
];
|
|
52
|
+
const DAEMON_ID_PREFIX = "jive-daemon";
|
|
53
|
+
const DAEMON_HOME_DIR = ".jive";
|
|
54
|
+
const DAEMON_REGISTRY_SUBDIR = "daemons";
|
|
55
|
+
const DAEMON_LOG_SUBDIR = "logs";
|
|
56
|
+
const REPAIR_ONLY_FLAG = "--repair-only";
|
|
57
|
+
const BIN_DIR = "bin";
|
|
58
|
+
const INSTALL_SCRIPT = "install.js";
|
|
59
|
+
// Root install.js repairs command targets; bin launchers repair and then execute.
|
|
60
|
+
const IS_INSTALL_SCRIPT = false;
|
|
61
|
+
const WINDOWS_EXECUTABLE_SUFFIX = ".exe";
|
|
62
|
+
const INSTALLED_JSON = "installed.json";
|
|
63
|
+
|
|
64
|
+
const require = createRequire(import.meta.url);
|
|
65
|
+
const launcherPath = fileURLToPath(import.meta.url);
|
|
66
|
+
const launcherDir = dirname(launcherPath);
|
|
67
|
+
const packageDir = IS_INSTALL_SCRIPT ? launcherDir : dirname(launcherDir);
|
|
68
|
+
const commandTargetDir = join(packageDir, BIN_DIR);
|
|
69
|
+
const rootInstallScriptPath = join(packageDir, INSTALL_SCRIPT);
|
|
70
|
+
|
|
71
|
+
function fail(message) {
|
|
72
|
+
console.error(message);
|
|
73
|
+
process.exit(1);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function currentPlatformKey() {
|
|
77
|
+
const platformKey = PLATFORM_MAP[`${process.platform}-${process.arch}`];
|
|
78
|
+
if (!platformKey) {
|
|
79
|
+
fail(`Unsupported platform: ${process.platform}-${process.arch}`);
|
|
80
|
+
}
|
|
81
|
+
return platformKey;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function currentPlatformPackageName() {
|
|
85
|
+
const platformKey = currentPlatformKey();
|
|
86
|
+
const packageName = PLATFORM_PACKAGES[platformKey];
|
|
87
|
+
if (!packageName) {
|
|
88
|
+
fail(`No platform package is configured for ${platformKey}`);
|
|
89
|
+
}
|
|
90
|
+
return packageName;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* When a Jive library contains any number of CLIs (entrypoints or agents), then the base package
|
|
95
|
+
* will have an optional dependency for each possible architecture, with those platform-specific
|
|
96
|
+
* packages containing a platform build of the CLI.
|
|
97
|
+
* @returns the path to the platform binary and the exact version of the platform package.
|
|
98
|
+
*/
|
|
99
|
+
function currentPlatformPackage() {
|
|
100
|
+
const platformKey = currentPlatformKey();
|
|
101
|
+
const binaryFile = PLATFORM_BINARIES[platformKey];
|
|
102
|
+
if (!binaryFile) {
|
|
103
|
+
fail(`No platform binary is configured for ${platformKey}`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const packageName = currentPlatformPackageName();
|
|
107
|
+
try {
|
|
108
|
+
const packageJsonPath = require.resolve(`${packageName}/package.json`);
|
|
109
|
+
const packageDir = dirname(packageJsonPath);
|
|
110
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
|
|
111
|
+
return {
|
|
112
|
+
binaryPath: join(packageDir, binaryFile),
|
|
113
|
+
version: packageJson.version,
|
|
114
|
+
};
|
|
115
|
+
} catch (error) {
|
|
116
|
+
fail(`Could not resolve optional platform package ${packageName}. Reinstall dependencies for ${process.platform}-${process.arch}.`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function commandTargetPath(command) {
|
|
121
|
+
const suffix = process.platform === "win32" ? WINDOWS_EXECUTABLE_SUFFIX : "";
|
|
122
|
+
return join(commandTargetDir, `${command}${suffix}`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function installedJsonPath() {
|
|
126
|
+
return join(commandTargetDir, INSTALLED_JSON);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function windowsTargetsAreCurrent(version) {
|
|
130
|
+
if (process.platform !== "win32") return false;
|
|
131
|
+
if (!COMMANDS.every((command) => existsSync(commandTargetPath(command)))) return false;
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
const installed = JSON.parse(readFileSync(installedJsonPath(), "utf-8"));
|
|
135
|
+
return installed.version === version;
|
|
136
|
+
} catch {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function linkOrCopyFirstExecutable(sourcePath, targetPath) {
|
|
142
|
+
mkdirSync(dirname(targetPath), { recursive: true });
|
|
143
|
+
const tempPath = `${targetPath}.${process.pid}.${Date.now()}.tmp${process.platform === "win32" ? WINDOWS_EXECUTABLE_SUFFIX : ""}`;
|
|
144
|
+
rmSync(tempPath, { force: true });
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
try {
|
|
148
|
+
linkSync(sourcePath, tempPath);
|
|
149
|
+
} catch {
|
|
150
|
+
copyFileSync(sourcePath, tempPath);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
chmodSync(tempPath, 0o755);
|
|
154
|
+
rmSync(targetPath, { force: true });
|
|
155
|
+
renameSync(tempPath, targetPath);
|
|
156
|
+
} finally {
|
|
157
|
+
rmSync(tempPath, { force: true });
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function hardLinkCommandTarget(sourcePath, targetPath) {
|
|
162
|
+
if (sourcePath === targetPath) return;
|
|
163
|
+
|
|
164
|
+
const tempPath = `${targetPath}.${process.pid}.${Date.now()}.tmp${process.platform === "win32" ? WINDOWS_EXECUTABLE_SUFFIX : ""}`;
|
|
165
|
+
rmSync(tempPath, { force: true });
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
linkSync(sourcePath, tempPath);
|
|
169
|
+
chmodSync(tempPath, 0o755);
|
|
170
|
+
rmSync(targetPath, { force: true });
|
|
171
|
+
renameSync(tempPath, targetPath);
|
|
172
|
+
} finally {
|
|
173
|
+
rmSync(tempPath, { force: true });
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function repairCommandTargets() {
|
|
178
|
+
const platformPackage = currentPlatformPackage();
|
|
179
|
+
|
|
180
|
+
if (windowsTargetsAreCurrent(platformPackage.version)) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const firstTargetPath = commandTargetPath(COMMANDS[0]);
|
|
185
|
+
linkOrCopyFirstExecutable(platformPackage.binaryPath, firstTargetPath);
|
|
186
|
+
|
|
187
|
+
for (const command of COMMANDS.slice(1)) {
|
|
188
|
+
hardLinkCommandTarget(firstTargetPath, commandTargetPath(command));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (process.platform === "win32") {
|
|
192
|
+
writeFileSync(installedJsonPath(), `${JSON.stringify({ version: platformPackage.version }, null, 2)}\n`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function runExecutable(executablePath) {
|
|
197
|
+
const result = spawnSync(executablePath, process.argv.slice(2), { stdio: "inherit" });
|
|
198
|
+
|
|
199
|
+
if (result.error) {
|
|
200
|
+
fail(result.error.message);
|
|
201
|
+
}
|
|
202
|
+
if (result.status !== 0) {
|
|
203
|
+
process.exit(result.status ?? 1);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function removeInstallScript() {
|
|
208
|
+
try {
|
|
209
|
+
rmSync(rootInstallScriptPath, { force: true });
|
|
210
|
+
// TODO: should we also remove the postinstall script line from package.json?
|
|
211
|
+
} catch {
|
|
212
|
+
// Cleanup is best-effort; failing to remove install.js should not fail package installation.
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/* ------------------------------------------------------------------ daemon registration ---------- */
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Registering a package's daemons with the operating system so they start at login.
|
|
220
|
+
*
|
|
221
|
+
* At login, not at boot: a daemon here acts for one user, with access to that user's keychain, SSH agent
|
|
222
|
+
* and home directory, and a system-wide service would have none of those. On Windows that is a scheduled
|
|
223
|
+
* task rather than a service for the same reason — a service runs in session 0, isolated from the desktop
|
|
224
|
+
* the user is actually logged into.
|
|
225
|
+
*
|
|
226
|
+
* This runs in the installer, which is plain JavaScript, because it has to run on a machine that has only
|
|
227
|
+
* just received the package. The daemon binary cannot register itself: nothing has started it yet.
|
|
228
|
+
*/
|
|
229
|
+
|
|
230
|
+
function jiveHome() {
|
|
231
|
+
return join(homedir(), DAEMON_HOME_DIR);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function registryDir() {
|
|
235
|
+
return join(jiveHome(), DAEMON_REGISTRY_SUBDIR);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function logDir() {
|
|
239
|
+
return join(jiveHome(), DAEMON_LOG_SUBDIR);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function recordPath(id) {
|
|
243
|
+
return join(registryDir(), `${id}.json`);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* The tree this copy of the package was installed into, which is where its daemons should work.
|
|
248
|
+
*
|
|
249
|
+
* Derived from the path rather than from INIT_CWD, which npm sets and Bun does not, and which answers the
|
|
250
|
+
* wrong question anyway: where the install was *typed*, not what it installed into. Walking out of the
|
|
251
|
+
* enclosing node_modules answers it for a project install and a global one alike, and a package with no
|
|
252
|
+
* node_modules above it — one built and installed in place — is its own root.
|
|
253
|
+
*/
|
|
254
|
+
function installRoot() {
|
|
255
|
+
let dir = packageDir;
|
|
256
|
+
while (true) {
|
|
257
|
+
if (basename(dir) === "node_modules") return dirname(dir);
|
|
258
|
+
const parent = dirname(dir);
|
|
259
|
+
if (parent === dir) return packageDir;
|
|
260
|
+
dir = parent;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* The id a registration is replaced at: the package's name, the daemon's name, and a digest of the
|
|
266
|
+
* directory this copy of the package was installed into.
|
|
267
|
+
*
|
|
268
|
+
* **Where the package is installed is what decides how many of its daemons exist.** Reinstalling or
|
|
269
|
+
* upgrading the same copy keeps the id, so the registration is replaced; a second copy installed somewhere
|
|
270
|
+
* else gets its own id, so each project that installs its own copy gets its own daemon and a globally
|
|
271
|
+
* installed copy gets exactly one.
|
|
272
|
+
*
|
|
273
|
+
* An earlier version had each daemon declare `local` or `global` in the manifest instead. That was a
|
|
274
|
+
* second source of truth that contradicted the real one: a `global` daemon installed into two projects
|
|
275
|
+
* registered once, pointing at whichever installed last, so uninstalling the second took the first
|
|
276
|
+
* project's daemon away — the very failure `local` existed to prevent.
|
|
277
|
+
*/
|
|
278
|
+
/** One id segment: dots separate segments, so a dot inside a name cannot be left as one. */
|
|
279
|
+
function idSegment(value) {
|
|
280
|
+
return value.replace(/[^A-Za-z0-9-]+/g, "-");
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Scope and name get a segment each, and an unscoped package gets "_" where its scope would be.
|
|
285
|
+
*
|
|
286
|
+
* Joining them with a dash instead would not be reversible: `@ozyman42/ozy-cli` and a package actually
|
|
287
|
+
* named `ozyman42-ozy-cli` would produce the same text. "_" is safe as the marker because npm forbids a
|
|
288
|
+
* scope or name from starting with it, so no real scope can collide with it.
|
|
289
|
+
*/
|
|
290
|
+
function packageSegments() {
|
|
291
|
+
if (!PACKAGE_NAME.startsWith("@")) return ["_", idSegment(PACKAGE_NAME)];
|
|
292
|
+
const separator = PACKAGE_NAME.indexOf("/");
|
|
293
|
+
return [
|
|
294
|
+
idSegment(PACKAGE_NAME.slice(1, separator)),
|
|
295
|
+
idSegment(PACKAGE_NAME.slice(separator + 1)),
|
|
296
|
+
];
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function daemonId(name) {
|
|
300
|
+
const [scope, packageName] = packageSegments();
|
|
301
|
+
const installDigest = createHash("sha256").update(packageDir).digest("hex").slice(0, 10);
|
|
302
|
+
return `${DAEMON_ID_PREFIX}.${scope}.${packageName}.${idSegment(name)}.${installDigest}`;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function run(argv, allowFailure) {
|
|
306
|
+
const result = spawnSync(argv[0], argv.slice(1), { stdio: ["ignore", "ignore", "pipe"] });
|
|
307
|
+
if (result.error || result.status !== 0) {
|
|
308
|
+
if (!allowFailure) {
|
|
309
|
+
const detail = result.error ? result.error.message : String(result.stderr || "").trim();
|
|
310
|
+
console.error(` ${argv.join(" ")} failed: ${detail}`);
|
|
311
|
+
}
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function xmlEscape(value) {
|
|
318
|
+
return String(value)
|
|
319
|
+
.split("&").join("&")
|
|
320
|
+
.split("<").join("<")
|
|
321
|
+
.split(">").join(">");
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* launchd. KeepAlive on SuccessfulExit=false restarts a daemon that crashes but respects one that exits
|
|
326
|
+
* zero, which is how a daemon that has removed its own registration stays stopped.
|
|
327
|
+
*/
|
|
328
|
+
function registerLaunchd(id, name, executable, workingDirectory, logPath) {
|
|
329
|
+
// The id verbatim, rather than a reverse-DNS label: one string names this daemon on every platform.
|
|
330
|
+
const label = id;
|
|
331
|
+
const agentsDir = join(homedir(), "Library", "LaunchAgents");
|
|
332
|
+
const plistPath = join(agentsDir, `${label}.plist`);
|
|
333
|
+
const domain = `gui/${process.getuid()}`;
|
|
334
|
+
const unregister = ["launchctl", "bootout", `${domain}/${label}`];
|
|
335
|
+
|
|
336
|
+
mkdirSync(agentsDir, { recursive: true });
|
|
337
|
+
writeFileSync(plistPath, [
|
|
338
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
339
|
+
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">',
|
|
340
|
+
'<plist version="1.0">',
|
|
341
|
+
"<dict>",
|
|
342
|
+
` <key>Label</key><string>${xmlEscape(label)}</string>`,
|
|
343
|
+
` <key>ProgramArguments</key><array><string>${xmlEscape(executable)}</string></array>`,
|
|
344
|
+
` <key>WorkingDirectory</key><string>${xmlEscape(workingDirectory)}</string>`,
|
|
345
|
+
" <key>RunAtLoad</key><true/>",
|
|
346
|
+
" <key>KeepAlive</key><dict><key>SuccessfulExit</key><false/></dict>",
|
|
347
|
+
` <key>StandardOutPath</key><string>${xmlEscape(logPath)}</string>`,
|
|
348
|
+
` <key>StandardErrorPath</key><string>${xmlEscape(logPath)}</string>`,
|
|
349
|
+
" <key>ProcessType</key><string>Background</string>",
|
|
350
|
+
"</dict>",
|
|
351
|
+
"</plist>",
|
|
352
|
+
"",
|
|
353
|
+
].join("\n"));
|
|
354
|
+
|
|
355
|
+
// Booting out first: bootstrap refuses a label that is already loaded, and a reinstall is the common case.
|
|
356
|
+
// bootout returns before launchd has finished stopping a daemon that is running, and bootstrap is refused
|
|
357
|
+
// until it has ("Bootstrap failed: 5"), so a reinstall over a live daemon left it stopped until the next
|
|
358
|
+
// login. Retry for a few seconds; only the last failure is reported.
|
|
359
|
+
run(unregister, true);
|
|
360
|
+
const attempts = 20;
|
|
361
|
+
let started = false;
|
|
362
|
+
for (let attempt = 1; attempt <= attempts && !started; attempt++) {
|
|
363
|
+
if (attempt > 1) Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 250);
|
|
364
|
+
started = run(["launchctl", "bootstrap", domain, plistPath], attempt < attempts);
|
|
365
|
+
}
|
|
366
|
+
return { kind: "launchd", unit: label, files: [plistPath], unregister, started };
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** systemd user units. `--user` rather than system units, for the same reason launchd gets a LaunchAgent. */
|
|
370
|
+
function registerSystemd(id, name, executable, workingDirectory, logPath) {
|
|
371
|
+
const unit = `${id}.service`;
|
|
372
|
+
const unitDir = join(process.env.XDG_CONFIG_HOME || join(homedir(), ".config"), "systemd", "user");
|
|
373
|
+
const unitPath = join(unitDir, unit);
|
|
374
|
+
const unregister = ["systemctl", "--user", "disable", "--now", unit];
|
|
375
|
+
|
|
376
|
+
mkdirSync(unitDir, { recursive: true });
|
|
377
|
+
writeFileSync(unitPath, [
|
|
378
|
+
"[Unit]",
|
|
379
|
+
`Description=Jive daemon ${name} for ${PACKAGE_NAME}`,
|
|
380
|
+
"",
|
|
381
|
+
"[Service]",
|
|
382
|
+
"Type=simple",
|
|
383
|
+
`ExecStart=${executable}`,
|
|
384
|
+
`WorkingDirectory=${workingDirectory}`,
|
|
385
|
+
"Restart=on-failure",
|
|
386
|
+
"RestartSec=5",
|
|
387
|
+
"",
|
|
388
|
+
"[Install]",
|
|
389
|
+
"WantedBy=default.target",
|
|
390
|
+
"",
|
|
391
|
+
].join("\n"));
|
|
392
|
+
|
|
393
|
+
run(["systemctl", "--user", "daemon-reload"], true);
|
|
394
|
+
const started = run(["systemctl", "--user", "enable", "--now", unit], false);
|
|
395
|
+
return { kind: "systemd", unit, files: [unitPath], unregister, started };
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Windows Task Scheduler, through a .cmd shim.
|
|
400
|
+
*
|
|
401
|
+
* The shim exists because schtasks can set neither a working directory nor environment variables, and it
|
|
402
|
+
* also supplies the restart-on-crash behaviour the other two platforms get from their service manager: an
|
|
403
|
+
* ONLOGON task starts its program once and does not watch it. The loop mirrors launchd's KeepAlive, so a
|
|
404
|
+
* daemon that exits zero after removing itself is not restarted.
|
|
405
|
+
*/
|
|
406
|
+
function registerScheduledTask(id, name, executable, workingDirectory, logPath) {
|
|
407
|
+
const taskName = id;
|
|
408
|
+
const shimPath = join(registryDir(), `${id}.cmd`);
|
|
409
|
+
const unregister = ["schtasks", "/Delete", "/F", "/TN", taskName];
|
|
410
|
+
|
|
411
|
+
writeFileSync(shimPath, [
|
|
412
|
+
"@echo off",
|
|
413
|
+
`cd /d "${workingDirectory}"`,
|
|
414
|
+
":jive_loop",
|
|
415
|
+
`"${executable}" >> "${logPath}" 2>&1`,
|
|
416
|
+
"if %ERRORLEVEL% EQU 0 goto :eof",
|
|
417
|
+
"timeout /t 5 /nobreak > nul",
|
|
418
|
+
"goto jive_loop",
|
|
419
|
+
"",
|
|
420
|
+
].join("\r\n"));
|
|
421
|
+
|
|
422
|
+
const started = run([
|
|
423
|
+
"schtasks", "/Create", "/F",
|
|
424
|
+
"/SC", "ONLOGON",
|
|
425
|
+
"/TN", taskName,
|
|
426
|
+
"/TR", `"${shimPath}"`,
|
|
427
|
+
"/RL", "LIMITED",
|
|
428
|
+
], false);
|
|
429
|
+
return { kind: "scheduled-task", unit: taskName, files: [shimPath], unregister, started };
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const DAEMON_REGISTRARS = {
|
|
433
|
+
darwin: registerLaunchd,
|
|
434
|
+
linux: registerSystemd,
|
|
435
|
+
win32: registerScheduledTask,
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
function readRecord(path) {
|
|
439
|
+
try {
|
|
440
|
+
return JSON.parse(readFileSync(path, "utf-8"));
|
|
441
|
+
} catch {
|
|
442
|
+
return undefined;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function removeRegistration(record, reason) {
|
|
447
|
+
console.log(`Removing daemon ${record.id} (${reason})`);
|
|
448
|
+
if (Array.isArray(record.unregister) && record.unregister.length > 0) {
|
|
449
|
+
run(record.unregister, true);
|
|
450
|
+
}
|
|
451
|
+
for (const file of record.files || []) {
|
|
452
|
+
rmSync(file, { force: true });
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Drops this package's registrations that should no longer exist.
|
|
458
|
+
*
|
|
459
|
+
* Three cases, all of which would otherwise leave the operating system starting a missing or unwanted
|
|
460
|
+
* executable at every login: a daemon this version of the package no longer has, a copy of the package
|
|
461
|
+
* whose directory is gone, and a registration of this very copy under an id we no longer use — which is
|
|
462
|
+
* what makes a change to the id scheme replace its registrations instead of doubling them.
|
|
463
|
+
*
|
|
464
|
+
* Only this package's own registrations are considered, by name, so another package's daemon is never
|
|
465
|
+
* touched; and only this copy's, except where the copy itself has been deleted.
|
|
466
|
+
*/
|
|
467
|
+
function pruneRemovedDaemons(currentIds) {
|
|
468
|
+
let entries;
|
|
469
|
+
try {
|
|
470
|
+
entries = readdirSync(registryDir());
|
|
471
|
+
} catch {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
for (const entry of entries) {
|
|
476
|
+
if (!entry.endsWith(".json")) continue;
|
|
477
|
+
const path = join(registryDir(), entry);
|
|
478
|
+
const record = readRecord(path);
|
|
479
|
+
if (!record || record.packageName !== PACKAGE_NAME) continue;
|
|
480
|
+
|
|
481
|
+
const reason =
|
|
482
|
+
!DAEMONS.includes(record.name) ? `no longer part of ${PACKAGE_NAME}`
|
|
483
|
+
: !existsSync(record.packageDir) ? `${record.packageDir} is gone`
|
|
484
|
+
: record.packageDir === packageDir && !currentIds.includes(record.id) ? "superseded by a new id"
|
|
485
|
+
: undefined;
|
|
486
|
+
if (reason === undefined) continue;
|
|
487
|
+
|
|
488
|
+
removeRegistration(record, reason);
|
|
489
|
+
rmSync(path, { force: true });
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function registerDaemons() {
|
|
494
|
+
// Two reasons not to register. The caller asked for repair alone — the local deploy does, because its
|
|
495
|
+
// install exists so the launcher can resolve a platform package, not because anyone chose to install
|
|
496
|
+
// that copy. Or this is a build agent, which installs a package to run one command and is then
|
|
497
|
+
// discarded: registering something to start at a login that will never happen is pure noise, and in a
|
|
498
|
+
// container it fails outright.
|
|
499
|
+
const skip = IS_INSTALL_SCRIPT && process.argv.includes(REPAIR_ONLY_FLAG) ? `${REPAIR_ONLY_FLAG} was passed`
|
|
500
|
+
: process.env.CI ? "CI is set"
|
|
501
|
+
: undefined;
|
|
502
|
+
const names = skip === undefined ? DAEMONS : [];
|
|
503
|
+
|
|
504
|
+
// Pruning first, and with no current ids when skipping: declining to run daemons here has to also take
|
|
505
|
+
// away any this copy registered earlier, or one such install would leave them starting at every login
|
|
506
|
+
// forever. Before the early return below, too — a version that drops its last daemon still has to take
|
|
507
|
+
// the previous version's registration away, and the record carries everything undoing it needs.
|
|
508
|
+
pruneRemovedDaemons(names.map(daemonId));
|
|
509
|
+
if (skip !== undefined) {
|
|
510
|
+
if (DAEMONS.length > 0) console.log(`${skip} — not registering ${DAEMONS.join(", ")}`);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
if (names.length === 0) return;
|
|
514
|
+
|
|
515
|
+
const registrar = DAEMON_REGISTRARS[process.platform];
|
|
516
|
+
if (!registrar) {
|
|
517
|
+
console.log(`No daemon support for ${process.platform} — ${names.join(", ")} will not start automatically`);
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
mkdirSync(registryDir(), { recursive: true });
|
|
522
|
+
mkdirSync(logDir(), { recursive: true });
|
|
523
|
+
|
|
524
|
+
for (const name of names) {
|
|
525
|
+
const id = daemonId(name);
|
|
526
|
+
const path = recordPath(id);
|
|
527
|
+
const previous = readRecord(path);
|
|
528
|
+
const workingDirectory = installRoot();
|
|
529
|
+
const executable = commandTargetPath(name);
|
|
530
|
+
const logPath = join(logDir(), `${id}.log`);
|
|
531
|
+
|
|
532
|
+
console.log(
|
|
533
|
+
`${previous ? "Replacing existing" : "Creating"} daemon ${id}`
|
|
534
|
+
+ ` (${name} from ${PACKAGE_NAME}) in ${workingDirectory}`,
|
|
535
|
+
);
|
|
536
|
+
|
|
537
|
+
// The record goes down before the unit is loaded, because loading it starts the daemon, and the first
|
|
538
|
+
// thing the daemon does is read this file to find out how it was registered.
|
|
539
|
+
const pending = {
|
|
540
|
+
id,
|
|
541
|
+
name,
|
|
542
|
+
packageName: PACKAGE_NAME,
|
|
543
|
+
packageDir,
|
|
544
|
+
workingDirectory,
|
|
545
|
+
executable,
|
|
546
|
+
registeredAt: new Date().toISOString(),
|
|
547
|
+
files: [path],
|
|
548
|
+
unregister: previous && previous.unregister ? previous.unregister : [],
|
|
549
|
+
};
|
|
550
|
+
writeFileSync(path, `${JSON.stringify(pending, null, 2)}\n`);
|
|
551
|
+
|
|
552
|
+
const outcome = registrar(id, name, executable, workingDirectory, logPath);
|
|
553
|
+
// kind and unit are recorded for the sake of reading a daemon's status later: they are what a listing
|
|
554
|
+
// command has to ask the platform about, and only the installer knows which registrar ran.
|
|
555
|
+
writeFileSync(path, `${JSON.stringify({
|
|
556
|
+
...pending,
|
|
557
|
+
kind: outcome.kind,
|
|
558
|
+
unit: outcome.unit,
|
|
559
|
+
logPath,
|
|
560
|
+
files: [...outcome.files, path],
|
|
561
|
+
unregister: outcome.unregister,
|
|
562
|
+
}, null, 2)}\n`);
|
|
563
|
+
|
|
564
|
+
if (!outcome.started) {
|
|
565
|
+
console.error(` ${id} is registered but could not be started; it will start at your next login`);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
repairCommandTargets();
|
|
571
|
+
registerDaemons();
|
|
572
|
+
removeInstallScript();
|
|
573
|
+
if (!IS_INSTALL_SCRIPT) {
|
|
574
|
+
const commandName = basename(launcherPath);
|
|
575
|
+
runExecutable(commandTargetPath(commandName));
|
|
576
|
+
}
|