barndog 0.2.1 → 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 +19 -0
- package/dist/cli.js +27 -1
- package/dist/commands/asset-list.js +5 -1
- package/dist/commands/harnesses.js +32 -2
- package/dist/commands/mcp-env.js +130 -41
- package/dist/commands/sets.js +13 -2
- package/dist/commands/uninstall.js +62 -0
- package/dist/config/paths.js +2 -0
- package/dist/entry/update-check.js +15 -0
- package/dist/features/adoption/adopt.js +31 -30
- package/dist/features/auth/session.js +22 -0
- package/dist/features/collaboration/workspace-store.js +2 -1
- package/dist/features/doctor/index.js +25 -8
- package/dist/features/environment/events.js +8 -1
- package/dist/features/environment/kept-values.js +68 -0
- package/dist/features/environment/moves.js +17 -1
- package/dist/features/environment/stage.js +20 -20
- package/dist/features/environment/sync.js +2 -2
- package/dist/features/inventory/index.js +45 -2
- package/dist/features/mcps/import.js +36 -15
- package/dist/features/mcps/index.js +12 -2
- package/dist/features/mcps/values.js +108 -20
- package/dist/features/migration/migrate.js +15 -0
- package/dist/features/migration/move.js +34 -9
- package/dist/features/native/adapters.js +1 -2
- package/dist/features/native/hook-shell.js +11 -0
- package/dist/features/secrets/index.js +94 -29
- package/dist/features/uninstall/index.js +126 -0
- package/dist/features/uninstall/leave.js +152 -0
- package/dist/features/uninstall/survey.js +103 -0
- package/dist/features/update-check/index.js +108 -0
- package/dist/features/workspace/format.js +6 -1
- package/dist/harnesses/claude.js +1 -1
- package/dist/harnesses/codex.js +46 -22
- package/dist/harnesses/cursor.js +1 -1
- package/dist/harnesses/opencode.js +1 -1
- package/dist/harnesses/read-renamed-variables.js +141 -0
- package/dist/harnesses/renamed-variables.js +99 -0
- package/dist/platform.js +12 -1
- package/dist/program.js +21 -9
- package/dist/ui/add-mcp.js +22 -16
- package/dist/ui/adopt.js +11 -6
- package/dist/ui/connection.js +2 -0
- package/dist/ui/harness-picker.js +7 -2
- package/dist/ui/mcp.js +89 -23
- package/dist/ui/move.js +2 -1
- package/dist/ui/output.js +33 -8
- package/dist/ui/secret-input.js +9 -0
- package/dist/ui/uninstall.js +174 -0
- package/dist/ui/update.js +10 -0
- package/dist/version.js +2 -2
- package/package.json +1 -1
- package/dist/entry/mcp-launch.js +0 -71
|
@@ -14,7 +14,8 @@ import { environmentCatalog } from "../server/catalog.js";
|
|
|
14
14
|
import { parsed } from "../server/client.js";
|
|
15
15
|
import { synchronize } from "../environment/sync.js";
|
|
16
16
|
import { locked } from "../environment/transaction.js";
|
|
17
|
-
import { assetPath, stableId } from "../workspace/format.js";
|
|
17
|
+
import { assetPath, repoAssetId, stableId, } from "../workspace/format.js";
|
|
18
|
+
import { updateKeptValues, withMovedServers, } from "../environment/kept-values.js";
|
|
18
19
|
import { checkRepositoryUrl, cloneOrigin, git } from "../workspace/git.js";
|
|
19
20
|
import { connectGitWorkspace, createGitWorkspace, inspectRepository, } from "../workspace/setup.js";
|
|
20
21
|
import { LoginRequiredError, normalizeServer, apiClient, } from "../auth/transport.js";
|
|
@@ -152,7 +153,8 @@ function candidateFiles(item, path) {
|
|
|
152
153
|
path: `${path}/${entry.path}`,
|
|
153
154
|
}));
|
|
154
155
|
}
|
|
155
|
-
async function moveToGit(workspace,
|
|
156
|
+
async function moveToGit(workspace, move, runtime) {
|
|
157
|
+
const { items, to, sharedIds = new Map() } = move;
|
|
156
158
|
const { home } = runtime;
|
|
157
159
|
const shared = await gitRepoSource(workspace, runtime).model();
|
|
158
160
|
const tip = defined(await acceptedCommit(workspace.workspaceId, runtime), "This Git workspace hasn't been fetched yet");
|
|
@@ -227,9 +229,15 @@ async function moveToGit(workspace, items, to, runtime) {
|
|
|
227
229
|
name: item.name,
|
|
228
230
|
status: "moved",
|
|
229
231
|
})));
|
|
232
|
+
for (const { item } of moving) {
|
|
233
|
+
if (item.kind === "mcp") {
|
|
234
|
+
sharedIds.set(item.id, repoAssetId(workspace.workspaceId, "mcp", scope, item.name));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
230
237
|
return result;
|
|
231
238
|
}
|
|
232
|
-
async function moveToCloud(conn,
|
|
239
|
+
async function moveToCloud(conn, move, runtime) {
|
|
240
|
+
const { items, to, sharedIds = new Map() } = move;
|
|
233
241
|
const scope = await selectOrigin(to ?? "me", conn, runtime);
|
|
234
242
|
if (!scope.canPublish) {
|
|
235
243
|
throw new CliError(`You can't publish to ${originLabel(scope)}.`, "barndog local move --to me");
|
|
@@ -284,7 +292,7 @@ async function moveToCloud(conn, items, to, runtime) {
|
|
|
284
292
|
definition: item.definition,
|
|
285
293
|
};
|
|
286
294
|
if (asset.kind === "mcp") {
|
|
287
|
-
await publishMcp(asset, scope, conn, runtime);
|
|
295
|
+
sharedIds.set(item.id, (await publishMcp(asset, scope, conn, runtime)).id);
|
|
288
296
|
}
|
|
289
297
|
else {
|
|
290
298
|
await publishPackage({ ...asset, kind: asset.kind }, scope, conn, runtime);
|
|
@@ -308,6 +316,18 @@ async function moveToCloud(conn, items, to, runtime) {
|
|
|
308
316
|
direct: true,
|
|
309
317
|
};
|
|
310
318
|
}
|
|
319
|
+
/**
|
|
320
|
+
* A shared copy that sits beside its local server starts with the local one's
|
|
321
|
+
* kept values; after that each keeps its own. A full move carries them over
|
|
322
|
+
* when sync finishes it (`environment/moves.ts`).
|
|
323
|
+
*/
|
|
324
|
+
async function copyKeptValues(sharedIds, runtime) {
|
|
325
|
+
const copies = [...sharedIds].map(([from, to]) => ({ from, to, copy: true }));
|
|
326
|
+
if (!copies.length) {
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
await updateKeptValues((secrets) => withMovedServers(secrets, copies), runtime);
|
|
330
|
+
}
|
|
311
331
|
/** A one-off destination (`--repo`, `--server`) leaves this machine as it is, so it only copies. */
|
|
312
332
|
const keepsLocal = (options) => options.keepLocal || Boolean(options.repo ?? options.server);
|
|
313
333
|
/**
|
|
@@ -365,17 +385,19 @@ export function moveLocalAssets(options, runtime) {
|
|
|
365
385
|
throw new CliError("--as renames one asset, so name exactly one.", "barndog local move <name> --as <new-name>");
|
|
366
386
|
}
|
|
367
387
|
const { items, invalid } = await candidates(overlay, options.names, options.as, options.kind);
|
|
388
|
+
// Only this machine's own workspace: a one-off destination isn't installed here.
|
|
389
|
+
const sharedIds = new Map();
|
|
368
390
|
const moveItems = async () => {
|
|
369
391
|
if (options.repo) {
|
|
370
|
-
return moveToGit(await repoDestination(options.repo, runtime), items, options.to, runtime);
|
|
392
|
+
return moveToGit(await repoDestination(options.repo, runtime), { items, to: options.to }, runtime);
|
|
371
393
|
}
|
|
372
394
|
if (options.server) {
|
|
373
|
-
return moveToCloud(await serverDestination(options.server, runtime), items, options.to, runtime);
|
|
395
|
+
return moveToCloud(await serverDestination(options.server, runtime), { items, to: options.to }, runtime);
|
|
374
396
|
}
|
|
375
397
|
if (settings.gitWorkspace) {
|
|
376
|
-
return moveToGit(settings.gitWorkspace, items, options.to, runtime);
|
|
398
|
+
return moveToGit(settings.gitWorkspace, { items, to: options.to, sharedIds }, runtime);
|
|
377
399
|
}
|
|
378
|
-
return moveToCloud(await connection(runtime), items, options.to, runtime);
|
|
400
|
+
return moveToCloud(await connection(runtime), { items, to: options.to, sharedIds }, runtime);
|
|
379
401
|
};
|
|
380
402
|
const result = await moveItems();
|
|
381
403
|
result.rows.unshift(...invalid);
|
|
@@ -384,6 +406,9 @@ export function moveLocalAssets(options, runtime) {
|
|
|
384
406
|
if (!result.keepLocal) {
|
|
385
407
|
result.removed = await finishMove(overlay.label, result, moved, items);
|
|
386
408
|
}
|
|
409
|
+
else {
|
|
410
|
+
await copyKeptValues(sharedIds, runtime);
|
|
411
|
+
}
|
|
387
412
|
if (moved.length) {
|
|
388
413
|
await synchronize(undefined, runtime, undefined, { prompt: false });
|
|
389
414
|
}
|
|
@@ -412,7 +437,7 @@ export async function listLocalAssets(runtime) {
|
|
|
412
437
|
state: "moving",
|
|
413
438
|
movingTo: moveDestination(entry),
|
|
414
439
|
}
|
|
415
|
-
: { state: "
|
|
440
|
+
: { state: "available" };
|
|
416
441
|
};
|
|
417
442
|
return {
|
|
418
443
|
folder: settings.localWorkspace.path,
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { fileURLToPath } from "node:url";
|
|
2
1
|
import { coerce, satisfies, validRange } from "semver";
|
|
3
2
|
import { programOutput, findExecutable, runtimeExecutable, } from "../../platform.js";
|
|
4
3
|
import { adapters, nativeFile } from "../../harnesses/registry.js";
|
|
@@ -72,7 +71,7 @@ export function mcpEntry(harness, server, kept, runtime) {
|
|
|
72
71
|
harness,
|
|
73
72
|
file: nativeFile(harness, "mcp", home),
|
|
74
73
|
key: server.name,
|
|
75
|
-
value: adapters[harness].mcp.server(server.definition,
|
|
74
|
+
value: adapters[harness].mcp.server(server.definition, used, runtime.platform),
|
|
76
75
|
...(Object.keys(used).length
|
|
77
76
|
? {
|
|
78
77
|
secrets: Object.entries(used).map(([variable, value]) => ({
|
|
@@ -43,6 +43,17 @@ function hookLine(root, command, harness, platform) {
|
|
|
43
43
|
};
|
|
44
44
|
}
|
|
45
45
|
export const hookCommand = (root, command, harness, platform) => lineFor(harness, hookLine(root, command, harness, platform), platform);
|
|
46
|
+
/**
|
|
47
|
+
* A command `hookCommand()` wrote for the folder `from`, rewritten to set the
|
|
48
|
+
* root variables to `to` instead; the Hook author's own command is kept as
|
|
49
|
+
* is. Undefined when the command doesn't start the way BarnDog writes it.
|
|
50
|
+
*/
|
|
51
|
+
export function movedHookCommand(command, folders, harness, platform) {
|
|
52
|
+
const prefix = hookCommand(folders.from, "", harness, platform);
|
|
53
|
+
return command.startsWith(prefix)
|
|
54
|
+
? hookCommand(folders.to, command.slice(prefix.length), harness, platform)
|
|
55
|
+
: undefined;
|
|
56
|
+
}
|
|
46
57
|
/** How `hooks test` runs a Hook: in the shell its tool would use. */
|
|
47
58
|
export async function hookInvocation(root, command, harness, runtime) {
|
|
48
59
|
const line = hookLine(root, command, harness, runtime.platform);
|
|
@@ -2,6 +2,7 @@ import { chmod, lstat, mkdir, readFile } from "node:fs/promises";
|
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { barndogPaths } from "../../config/paths.js";
|
|
4
4
|
import { CliError } from "../../errors.js";
|
|
5
|
+
import { secretRefs } from "../../harnesses/formats.js";
|
|
5
6
|
import { privatePermissions, protectPrivate, removeCommand, } from "../../platform.js";
|
|
6
7
|
import { POSIX_MODE } from "../../shared/constants.js";
|
|
7
8
|
import { ifExists, writeAtomic } from "../../shared/files.js";
|
|
@@ -9,16 +10,35 @@ import { withPidLock } from "../../shared/lock.js";
|
|
|
9
10
|
import { VARIABLE_NAME } from "../../shared/patterns.js";
|
|
10
11
|
import { posixQuote } from "../../shared/shell.js";
|
|
11
12
|
const variable = z.string().regex(new RegExp(`^${VARIABLE_NAME.source}$`));
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
/** A kept value, or another environment variable the server reads in its place. */
|
|
14
|
+
const keptValueSchema = z.union([
|
|
15
|
+
z.strictObject({ value: z.string() }),
|
|
16
|
+
z.strictObject({ from: variable }),
|
|
17
|
+
]);
|
|
18
|
+
const secretsSchema = z.discriminatedUnion("version", [
|
|
19
|
+
z.strictObject({
|
|
20
|
+
version: z.literal(1),
|
|
21
|
+
values: z.record(variable, z.string()),
|
|
22
|
+
}),
|
|
23
|
+
z.strictObject({
|
|
24
|
+
version: z.literal(2),
|
|
25
|
+
/** MCP asset ID → variable → its value. */
|
|
26
|
+
servers: z.record(z.string(), z.record(variable, keptValueSchema)),
|
|
27
|
+
/** Values from a version 1 file that no installed server was using. */
|
|
28
|
+
unassigned: z.record(variable, z.string()),
|
|
29
|
+
}),
|
|
30
|
+
]);
|
|
31
|
+
/** The variable a kept value says to read instead; undefined for a value kept as written. */
|
|
32
|
+
export const readsFrom = (kept) => kept && "from" in kept ? kept.from : undefined;
|
|
33
|
+
/** The value kept as written; undefined for one that reads another variable. */
|
|
34
|
+
export const literalOf = (kept) => kept && "value" in kept ? kept.value : undefined;
|
|
35
|
+
const empty = { version: 2, servers: {}, unassigned: {} };
|
|
36
|
+
/** The file as stored; an empty current one when there's no file yet. */
|
|
37
|
+
export async function readStoredSecrets(runtime) {
|
|
18
38
|
const path = barndogPaths(runtime.home).secrets;
|
|
19
39
|
const info = await ifExists(lstat(path));
|
|
20
40
|
if (!info) {
|
|
21
|
-
return
|
|
41
|
+
return empty;
|
|
22
42
|
}
|
|
23
43
|
if (!info.isFile()) {
|
|
24
44
|
throw new CliError(`${path} isn't a regular file, so BarnDog can't read the MCP values it keeps.`, "Delete it, then set the values again with barndog mcp env set.", { command: removeCommand(path, runtime.platform) });
|
|
@@ -31,7 +51,7 @@ export async function readSecrets(runtime) {
|
|
|
31
51
|
if (!parsed.success) {
|
|
32
52
|
throw new CliError(`${path} is damaged, so BarnDog can't read the MCP values it keeps.`, "Delete it, then set the values again with barndog mcp env set.", { command: removeCommand(path, runtime.platform) });
|
|
33
53
|
}
|
|
34
|
-
return parsed.data
|
|
54
|
+
return parsed.data;
|
|
35
55
|
}
|
|
36
56
|
function parseJson(text) {
|
|
37
57
|
try {
|
|
@@ -41,37 +61,82 @@ function parseJson(text) {
|
|
|
41
61
|
return undefined;
|
|
42
62
|
}
|
|
43
63
|
}
|
|
44
|
-
/**
|
|
45
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Replaces the file with what `change` makes of it, under a lock, and returns
|
|
66
|
+
* the file's path and what it now holds. `change` receives the file as stored,
|
|
67
|
+
* so a version 1 file has to be converted there.
|
|
68
|
+
*/
|
|
69
|
+
export async function changeSecrets(change, runtime) {
|
|
46
70
|
const paths = barndogPaths(runtime.home);
|
|
47
|
-
for (const name of Object.keys(changes)) {
|
|
48
|
-
if (!variable.safeParse(name).success) {
|
|
49
|
-
throw new CliError(`${name} isn't an environment variable name.`, "Use letters, digits and underscores, not starting with a digit.");
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
71
|
await mkdir(paths.root, {
|
|
53
72
|
recursive: true,
|
|
54
73
|
mode: POSIX_MODE.privateDirectory,
|
|
55
74
|
});
|
|
56
75
|
await chmod(paths.root, POSIX_MODE.privateDirectory);
|
|
57
76
|
await protectPrivate(paths.root, runtime);
|
|
58
|
-
await withPidLock(paths.secretsLock, {
|
|
77
|
+
const secrets = await withPidLock(paths.secretsLock, {
|
|
59
78
|
starting: "Another change to the kept MCP values is starting. Wait for it to finish, then try again.",
|
|
60
79
|
running: "Another change to the kept MCP values is running. Wait for it to finish, then try again.",
|
|
61
80
|
}, async () => {
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
};
|
|
65
|
-
for (const [name, value] of Object.entries(changes)) {
|
|
66
|
-
if (value === null) {
|
|
67
|
-
delete values[name];
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
values[name] = value;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
await writeAtomic(paths.secrets, JSON.stringify({ version: 1, values }, null, 2) + "\n");
|
|
81
|
+
const next = withoutEmptyServers(change(await readStoredSecrets(runtime)));
|
|
82
|
+
await writeAtomic(paths.secrets, JSON.stringify({ version: 2, ...next }, null, 2) + "\n");
|
|
74
83
|
await protectPrivate(paths.secrets, runtime);
|
|
84
|
+
return next;
|
|
75
85
|
});
|
|
76
|
-
return paths.secrets;
|
|
86
|
+
return { file: paths.secrets, secrets };
|
|
87
|
+
}
|
|
88
|
+
function withoutEmptyServers(secrets) {
|
|
89
|
+
return {
|
|
90
|
+
servers: Object.fromEntries(Object.entries(secrets.servers).filter(([, values]) => Object.keys(values).length > 0)),
|
|
91
|
+
unassigned: secrets.unassigned,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/** Throws unless `name` can be an environment variable's name. */
|
|
95
|
+
export function assertVariableName(name) {
|
|
96
|
+
if (!variable.safeParse(name).success) {
|
|
97
|
+
throw new CliError(`${name} isn't an environment variable name.`, "Use letters, digits and underscores, not starting with a digit.");
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* A server's definition with each variable kept as `from` read from that other
|
|
102
|
+
* variable instead, and the values written into its entries as they are.
|
|
103
|
+
*/
|
|
104
|
+
export function applyKeptValues(definition, values = {}) {
|
|
105
|
+
const rewrite = (refs) => Object.fromEntries(Object.entries(refs).map(([key, ref]) => {
|
|
106
|
+
const from = readsFrom(values[ref.secretRef]);
|
|
107
|
+
return [key, from ? { ...ref, secretRef: from } : ref];
|
|
108
|
+
}));
|
|
109
|
+
const kept = Object.fromEntries(Object.entries(values).flatMap(([name, value]) => {
|
|
110
|
+
const literal = literalOf(value);
|
|
111
|
+
return literal === undefined ? [] : [[name, literal]];
|
|
112
|
+
}));
|
|
113
|
+
return {
|
|
114
|
+
definition: definition.transport === "stdio"
|
|
115
|
+
? { ...definition, environment: rewrite(definition.environment) }
|
|
116
|
+
: { ...definition, headers: rewrite(definition.headers) },
|
|
117
|
+
kept,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The variables a definition names that have no value on this machine:
|
|
122
|
+
* BarnDog keeps none for this server and `env` doesn't set the variable read.
|
|
123
|
+
* Each once, in the order the definition names them.
|
|
124
|
+
*/
|
|
125
|
+
export function missingValues(definition, values, env) {
|
|
126
|
+
const missing = new Map();
|
|
127
|
+
for (const { secretRef } of Object.values(secretRefs(definition))) {
|
|
128
|
+
const kept = values[secretRef];
|
|
129
|
+
const from = readsFrom(kept);
|
|
130
|
+
const read = from ?? secretRef;
|
|
131
|
+
const found = literalOf(kept) ??
|
|
132
|
+
literalOf(from ? values[from] : undefined) ??
|
|
133
|
+
env[read];
|
|
134
|
+
if (!found) {
|
|
135
|
+
missing.set(secretRef, {
|
|
136
|
+
variable: secretRef,
|
|
137
|
+
...(from ? { from } : {}),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return [...missing.values()];
|
|
77
142
|
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `barndog uninstall`: takes BarnDog off this machine. BarnDog's own
|
|
3
|
+
* registrations (the session-start sync, the knowledge index, OpenCode's
|
|
4
|
+
* plugin module) always go. The developer's Skills, MCP servers and Hooks
|
|
5
|
+
* either stay in each coding tool as plain files of its own, or go too, with
|
|
6
|
+
* adopted assets put back where they came from. Then every session is signed
|
|
7
|
+
* out and `~/.barndog/` is deleted, except the local workspace when kept.
|
|
8
|
+
*/
|
|
9
|
+
import { readdir, rm } from "node:fs/promises";
|
|
10
|
+
import { basename, join, relative, sep } from "node:path";
|
|
11
|
+
import { barndogPaths } from "../../config/paths.js";
|
|
12
|
+
import { readSettings } from "../../config/settings.js";
|
|
13
|
+
import { readState } from "../../config/state.js";
|
|
14
|
+
import { fileText, ifExists, isInside } from "../../shared/files.js";
|
|
15
|
+
import { readAdoptions } from "../adoption/record.js";
|
|
16
|
+
import { undoAdoption } from "../adoption/undo.js";
|
|
17
|
+
import { signOutEverywhere } from "../auth/session.js";
|
|
18
|
+
import { locked, transact } from "../environment/transaction.js";
|
|
19
|
+
import { entryNames, linkWrites, nativeWrites, stateText, } from "../environment/writes.js";
|
|
20
|
+
import { leaveInPlace } from "./leave.js";
|
|
21
|
+
import { mcpVariables } from "./survey.js";
|
|
22
|
+
/** Undoes each adoption holding a chosen asset: whole when unfinished or fully chosen. */
|
|
23
|
+
async function restoreAdopted(options, runtime) {
|
|
24
|
+
if (options.keepSetup || !options.restore.length) {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
const restored = [];
|
|
28
|
+
for (const record of await readAdoptions(runtime.home)) {
|
|
29
|
+
const open = record.items.filter((item) => !item.undone);
|
|
30
|
+
const chosen = open.filter((item) => options.restore.includes(item.key));
|
|
31
|
+
if (record.status === "undone" || !chosen.length) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const whole = record.status === "pending" || chosen.length === open.length;
|
|
35
|
+
const result = await undoAdoption({
|
|
36
|
+
id: record.id,
|
|
37
|
+
only: whole ? undefined : chosen.map((item) => item.key),
|
|
38
|
+
force: options.force,
|
|
39
|
+
}, runtime);
|
|
40
|
+
restored.push(...result.restored);
|
|
41
|
+
}
|
|
42
|
+
return restored;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Takes BarnDog's entries out of the coding tools' settings (all of them, or
|
|
46
|
+
* only its own registrations when the setup stays, with Hooks pointed at
|
|
47
|
+
* their new folders), removes Skill links, and empties `state.json`, as one
|
|
48
|
+
* journaled change.
|
|
49
|
+
*/
|
|
50
|
+
async function removeEntries(keepSetup, runtime) {
|
|
51
|
+
const { home } = runtime;
|
|
52
|
+
const stateFile = barndogPaths(home).state;
|
|
53
|
+
const state = await readState(home);
|
|
54
|
+
const kept = keepSetup
|
|
55
|
+
? await leaveInPlace(state, runtime)
|
|
56
|
+
: { left: [], hooks: [] };
|
|
57
|
+
// MCP entries that stay aren't BarnDog's to edit any more, so they are left out.
|
|
58
|
+
const previous = keepSetup
|
|
59
|
+
? state.native.filter((entry) => entry.kind !== "mcp")
|
|
60
|
+
: state.native;
|
|
61
|
+
const nameOf = entryNames(state.installed.map((entry) => [entry.id, basename(entry.path)]));
|
|
62
|
+
const settings = await nativeWrites(previous, kept.hooks, {
|
|
63
|
+
trees: [],
|
|
64
|
+
updated: new Set(),
|
|
65
|
+
nameOf,
|
|
66
|
+
platform: runtime.platform,
|
|
67
|
+
});
|
|
68
|
+
// A Skill left in place is a folder now, so only links still to remove are written.
|
|
69
|
+
const links = keepSetup
|
|
70
|
+
? { writes: [], events: [] }
|
|
71
|
+
: await linkWrites(state.links, [], new Set());
|
|
72
|
+
const writes = [
|
|
73
|
+
...links.writes,
|
|
74
|
+
...settings.writes,
|
|
75
|
+
{
|
|
76
|
+
kind: "file",
|
|
77
|
+
path: stateFile,
|
|
78
|
+
before: await fileText(stateFile),
|
|
79
|
+
after: stateText({ links: [], native: [], trees: [] }),
|
|
80
|
+
},
|
|
81
|
+
];
|
|
82
|
+
await transact(runtime, writes);
|
|
83
|
+
const removed = [...links.events, ...settings.events].filter((event) => event.type === "removed" ||
|
|
84
|
+
(event.type === "native" && event.action === "removed"));
|
|
85
|
+
return {
|
|
86
|
+
left: kept.left,
|
|
87
|
+
removed,
|
|
88
|
+
variables: keepSetup ? mcpVariables(state) : [],
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** Deletes `root`, or everything in it except the top folder holding `keep`. */
|
|
92
|
+
async function deleteRoot(root, keep) {
|
|
93
|
+
if (!keep) {
|
|
94
|
+
await rm(root, { recursive: true, force: true });
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const [top] = relative(root, keep).split(sep);
|
|
98
|
+
for (const name of (await ifExists(readdir(root))) ?? []) {
|
|
99
|
+
if (name !== top) {
|
|
100
|
+
await rm(join(root, name), { recursive: true, force: true });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export async function uninstall(options, runtime) {
|
|
105
|
+
const root = barndogPaths(runtime.home).root;
|
|
106
|
+
// Read before anything changes; a damaged config.yaml only means no workspace to keep.
|
|
107
|
+
const settings = await readSettings(runtime.home).catch(() => null);
|
|
108
|
+
const workspace = settings?.localWorkspace?.path;
|
|
109
|
+
const restored = await restoreAdopted(options, runtime);
|
|
110
|
+
const { left, removed, variables } = await locked(runtime, () => removeEntries(options.keepSetup, runtime));
|
|
111
|
+
const signedOut = await signOutEverywhere(runtime);
|
|
112
|
+
const keptWorkspace = options.keepWorkspace && workspace && isInside(root, workspace)
|
|
113
|
+
? workspace
|
|
114
|
+
: undefined;
|
|
115
|
+
await deleteRoot(root, keptWorkspace);
|
|
116
|
+
return {
|
|
117
|
+
keptSetup: options.keepSetup,
|
|
118
|
+
left,
|
|
119
|
+
removed,
|
|
120
|
+
variables,
|
|
121
|
+
restored,
|
|
122
|
+
signedOut,
|
|
123
|
+
root,
|
|
124
|
+
keptWorkspace,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Leaves BarnDog's Skills and Hooks in each coding tool as plain files of the
|
|
3
|
+
* tool's own, so they keep working once `~/.barndog/` is gone: each Skill link
|
|
4
|
+
* becomes a copy of the folder it pointed at, and each Hook folder is copied
|
|
5
|
+
* beside the tool's hook settings, where its registrations are pointed next.
|
|
6
|
+
* MCP entries need nothing: they never point into BarnDog's files.
|
|
7
|
+
*
|
|
8
|
+
* Every step checks what is already done, so an interrupted uninstall can run
|
|
9
|
+
* again.
|
|
10
|
+
*/
|
|
11
|
+
import { cp, lstat, rename, rm, unlink } from "node:fs/promises";
|
|
12
|
+
import { basename, dirname, join } from "node:path";
|
|
13
|
+
import { CliError } from "../../errors.js";
|
|
14
|
+
import { isHookHarness, nativeFile } from "../../harnesses/registry.js";
|
|
15
|
+
import { ifExists } from "../../shared/files.js";
|
|
16
|
+
import { isRecord } from "../../shared/values.js";
|
|
17
|
+
import { movedHookCommand } from "../native/hook-shell.js";
|
|
18
|
+
import { linkTarget } from "../environment/transaction.js";
|
|
19
|
+
import { treeFingerprint } from "../workspace/tree.js";
|
|
20
|
+
/** Where a Hook's folder goes: `hooks/<name>` beside the tool's hook settings. */
|
|
21
|
+
const hookFolder = (harness, name, home) => join(dirname(nativeFile(harness, "hook", home)), "hooks", name);
|
|
22
|
+
function placements(state, home) {
|
|
23
|
+
const skills = state.links.map((link) => ({
|
|
24
|
+
kind: "skill",
|
|
25
|
+
name: basename(link.target),
|
|
26
|
+
harness: link.harness,
|
|
27
|
+
path: link.target,
|
|
28
|
+
source: link.source,
|
|
29
|
+
}));
|
|
30
|
+
const hooks = [];
|
|
31
|
+
for (const entry of state.native) {
|
|
32
|
+
const folder = state.installed.find((installed) => installed.kind === "hook" && installed.id === entry.id);
|
|
33
|
+
if (entry.kind !== "hook" ||
|
|
34
|
+
!folder ||
|
|
35
|
+
hooks.some((hook) => hook.source === folder.path && hook.harness === entry.harness)) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const name = basename(folder.path);
|
|
39
|
+
hooks.push({
|
|
40
|
+
kind: "hook",
|
|
41
|
+
name,
|
|
42
|
+
harness: entry.harness,
|
|
43
|
+
path: hookFolder(entry.harness, name, home),
|
|
44
|
+
source: folder.path,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return [...skills, ...hooks];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Whether the folder is already in place: true when `path` holds the same
|
|
51
|
+
* files as `source`. Throws when something else is there.
|
|
52
|
+
*/
|
|
53
|
+
async function placed(placement, runtime) {
|
|
54
|
+
const { path, source } = placement;
|
|
55
|
+
const info = await ifExists(lstat(path));
|
|
56
|
+
if (!info) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (info.isSymbolicLink() && placement.kind === "skill") {
|
|
60
|
+
if ((await linkTarget(path)) !== source) {
|
|
61
|
+
throw new CliError(`${path} was changed outside BarnDog, so uninstall stopped before changing anything.`, "Restore or remove that link, then run barndog uninstall again");
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
const [here, there] = await Promise.all([
|
|
66
|
+
treeFingerprint(path, runtime),
|
|
67
|
+
treeFingerprint(source, runtime),
|
|
68
|
+
]);
|
|
69
|
+
if (here === null || here !== there) {
|
|
70
|
+
throw new CliError(`${path} already exists, so BarnDog can't leave its ${placement.name} ${placement.kind === "skill" ? "Skill" : "Hook"} there.`, "Move it aside, then run barndog uninstall again");
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
/** Copies beside the destination first, so a Skill is never missing for long. */
|
|
75
|
+
async function place(placement) {
|
|
76
|
+
const { path, source } = placement;
|
|
77
|
+
const staging = `${path}.barndog-uninstall`;
|
|
78
|
+
await rm(staging, { recursive: true, force: true });
|
|
79
|
+
await cp(source, staging, { recursive: true });
|
|
80
|
+
if (await ifExists(lstat(path))) {
|
|
81
|
+
await unlink(path);
|
|
82
|
+
}
|
|
83
|
+
await rename(staging, path);
|
|
84
|
+
}
|
|
85
|
+
/** The Hook registration with its root variables pointing at `to`. */
|
|
86
|
+
function movedHook(entry, folders, platform) {
|
|
87
|
+
const { harness } = entry;
|
|
88
|
+
if (!isHookHarness(harness)) {
|
|
89
|
+
throw new Error(`${harness} has no hook settings to move a Hook in`);
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
...entry,
|
|
93
|
+
value: mapCommands(entry.value, (command) => {
|
|
94
|
+
const moved = movedHookCommand(command, folders, harness, platform);
|
|
95
|
+
if (moved === undefined) {
|
|
96
|
+
throw new Error(`A ${entry.key} Hook registration doesn't start the way BarnDog writes it`);
|
|
97
|
+
}
|
|
98
|
+
return moved;
|
|
99
|
+
}),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/** Rewrites the handler's `command`, or each of its nested `hooks[].command`. */
|
|
103
|
+
function mapCommands(value, change) {
|
|
104
|
+
const next = { ...value };
|
|
105
|
+
if (typeof next.command === "string") {
|
|
106
|
+
next.command = change(next.command);
|
|
107
|
+
}
|
|
108
|
+
if (Array.isArray(next.hooks)) {
|
|
109
|
+
next.hooks = next.hooks.map((hook) => isRecord(hook) && typeof hook.command === "string"
|
|
110
|
+
? { ...hook, command: change(hook.command) }
|
|
111
|
+
: hook);
|
|
112
|
+
}
|
|
113
|
+
return next;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Puts every Skill and Hook folder in place and returns the Hook registrations
|
|
117
|
+
* pointed at their new folders, for the caller to write with the rest.
|
|
118
|
+
*/
|
|
119
|
+
export async function leaveInPlace(state, runtime) {
|
|
120
|
+
const planned = placements(state, runtime.home);
|
|
121
|
+
// Check every destination before changing any.
|
|
122
|
+
const done = new Set();
|
|
123
|
+
for (const placement of planned) {
|
|
124
|
+
if (await placed(placement, runtime)) {
|
|
125
|
+
done.add(placement);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
for (const placement of planned) {
|
|
129
|
+
if (!done.has(placement)) {
|
|
130
|
+
await place(placement);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const hooks = state.native.flatMap((entry) => {
|
|
134
|
+
const folder = planned.find((placement) => placement.kind === "hook" &&
|
|
135
|
+
placement.harness === entry.harness &&
|
|
136
|
+
state.installed.some((installed) => installed.id === entry.id && installed.path === placement.source));
|
|
137
|
+
return entry.kind === "hook" && folder
|
|
138
|
+
? [
|
|
139
|
+
movedHook(entry, { from: folder.source, to: folder.path }, runtime.platform),
|
|
140
|
+
]
|
|
141
|
+
: [];
|
|
142
|
+
});
|
|
143
|
+
return {
|
|
144
|
+
left: planned.map(({ kind, name, harness, path }) => ({
|
|
145
|
+
kind,
|
|
146
|
+
name,
|
|
147
|
+
harness,
|
|
148
|
+
path,
|
|
149
|
+
})),
|
|
150
|
+
hooks,
|
|
151
|
+
};
|
|
152
|
+
}
|