@ai-primitives-hub/cli 0.2.0 → 0.2.2-next.0
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 +26 -1
- package/dist/commands/bundle-build.d.ts +1 -0
- package/dist/commands/bundle-build.d.ts.map +1 -1
- package/dist/commands/bundle-build.js +23 -4
- package/dist/commands/bundle-build.js.map +1 -1
- package/dist/commands/collection-affected.d.ts.map +1 -1
- package/dist/commands/collection-affected.js +1 -34
- package/dist/commands/collection-affected.js.map +1 -1
- package/dist/commands/collection-validate.d.ts +15 -2
- package/dist/commands/collection-validate.d.ts.map +1 -1
- package/dist/commands/collection-validate.js +42 -4
- package/dist/commands/collection-validate.js.map +1 -1
- package/dist/commands/doctor.d.ts +4 -0
- package/dist/commands/doctor.d.ts.map +1 -1
- package/dist/commands/doctor.js +37 -0
- package/dist/commands/doctor.js.map +1 -1
- package/dist/commands/explain.d.ts.map +1 -1
- package/dist/commands/explain.js +54 -4
- package/dist/commands/explain.js.map +1 -1
- package/dist/commands/hub-replicate.d.ts +1 -0
- package/dist/commands/hub-replicate.d.ts.map +1 -1
- package/dist/commands/hub-replicate.js +16 -5
- package/dist/commands/hub-replicate.js.map +1 -1
- package/dist/commands/hub-scoped-governance.d.ts +21 -0
- package/dist/commands/hub-scoped-governance.d.ts.map +1 -0
- package/dist/commands/hub-scoped-governance.js +74 -0
- package/dist/commands/hub-scoped-governance.js.map +1 -0
- package/dist/commands/hub.d.ts +6 -0
- package/dist/commands/hub.d.ts.map +1 -1
- package/dist/commands/hub.js +68 -0
- package/dist/commands/hub.js.map +1 -1
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +3 -10
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/install.d.ts +18 -10
- package/dist/commands/install.d.ts.map +1 -1
- package/dist/commands/install.js +472 -253
- package/dist/commands/install.js.map +1 -1
- package/dist/commands/plugin-create.d.ts.map +1 -1
- package/dist/commands/plugin-create.js +72 -43
- package/dist/commands/plugin-create.js.map +1 -1
- package/dist/commands/profile.d.ts +42 -4
- package/dist/commands/profile.d.ts.map +1 -1
- package/dist/commands/profile.js +739 -144
- package/dist/commands/profile.js.map +1 -1
- package/dist/commands/registry.d.ts.map +1 -1
- package/dist/commands/registry.js +5 -0
- package/dist/commands/registry.js.map +1 -1
- package/dist/commands/security-rate.d.ts +26 -0
- package/dist/commands/security-rate.d.ts.map +1 -0
- package/dist/commands/security-rate.js +281 -0
- package/dist/commands/security-rate.js.map +1 -0
- package/dist/commands/security-scan.d.ts +40 -0
- package/dist/commands/security-scan.d.ts.map +1 -0
- package/dist/commands/security-scan.js +395 -0
- package/dist/commands/security-scan.js.map +1 -0
- package/dist/commands/status.d.ts +18 -11
- package/dist/commands/status.d.ts.map +1 -1
- package/dist/commands/status.js +42 -33
- package/dist/commands/status.js.map +1 -1
- package/dist/commands/uninstall.d.ts +2 -9
- package/dist/commands/uninstall.d.ts.map +1 -1
- package/dist/commands/uninstall.js +413 -147
- package/dist/commands/uninstall.js.map +1 -1
- package/dist/commands/update.d.ts +1 -2
- package/dist/commands/update.d.ts.map +1 -1
- package/dist/commands/update.js +379 -56
- package/dist/commands/update.js.map +1 -1
- package/dist/commands/validation-governance.d.ts +27 -0
- package/dist/commands/validation-governance.d.ts.map +1 -0
- package/dist/commands/validation-governance.js +92 -0
- package/dist/commands/validation-governance.js.map +1 -0
- package/dist/doctor/diagnostics.d.ts +72 -5
- package/dist/doctor/diagnostics.d.ts.map +1 -1
- package/dist/doctor/diagnostics.js +95 -40
- package/dist/doctor/diagnostics.js.map +1 -1
- package/dist/doctor/primitive-driver.d.ts +24 -0
- package/dist/doctor/primitive-driver.d.ts.map +1 -0
- package/dist/doctor/primitive-driver.js +162 -0
- package/dist/doctor/primitive-driver.js.map +1 -0
- package/dist/doctor/primitives-text.d.ts +27 -0
- package/dist/doctor/primitives-text.d.ts.map +1 -0
- package/dist/doctor/primitives-text.js +131 -0
- package/dist/doctor/primitives-text.js.map +1 -0
- package/dist/doctor/primitives.d.ts +24 -0
- package/dist/doctor/primitives.d.ts.map +1 -0
- package/dist/doctor/primitives.js +101 -0
- package/dist/doctor/primitives.js.map +1 -0
- package/dist/framework/error.d.ts +19 -18
- package/dist/framework/error.d.ts.map +1 -1
- package/dist/framework/error.js +78 -22
- package/dist/framework/error.js.map +1 -1
- package/dist/framework/index.d.ts +3 -1
- package/dist/framework/index.d.ts.map +1 -1
- package/dist/framework/index.js +42 -1
- package/dist/framework/index.js.map +1 -1
- package/dist/framework/install-record.d.ts +555 -0
- package/dist/framework/install-record.d.ts.map +1 -0
- package/dist/framework/install-record.js +968 -0
- package/dist/framework/install-record.js.map +1 -0
- package/dist/framework/target.d.ts +9 -12
- package/dist/framework/target.d.ts.map +1 -1
- package/dist/framework/target.js +9 -12
- package/dist/framework/target.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +3 -1
- package/dist/main.js.map +1 -1
- package/dist/version.d.ts +7 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +15 -0
- package/dist/version.js.map +1 -0
- package/package.json +6 -4
|
@@ -0,0 +1,968 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.renderRecordLine = exports.recordOutputFields = exports.prepareOldFileOnly = exports.oldFileOnlyBundles = exports.finishRecordRun = exports.recordPlannedInstall = exports.checkPlannedInstallMcp = exports.tokensForRecordSource = exports.recordHubOf = exports.configuredRecordSources = exports.configuredSources = exports.prepareUnknownUninstall = exports.recordedTools = exports.migrateDecision = exports.prepareRecordDecision = exports.prepareRecordSelection = exports.recordUnknownUninstall = exports.recordRepositoryUninstall = exports.otherKeysOfIdentity = exports.prepareUninstallForTarget = exports.checkRecordSourceHub = exports.prepareRecordForTarget = exports.prepareUserUninstall = exports.prepareRepositoryUninstall = exports.checkRepositoryInstallMcp = exports.recordRepositoryInstall = exports.prepareUserRecord = exports.prepareRepositoryRecord = exports.usesInstallRecord = exports.createLayoutLoader = exports.userRecordLocation = exports.createRecordServices = exports.toInstallRecordError = exports.toCommandRecordError = exports.loadLayoutLayers = exports.writeBundleMcpServers = exports.checkBundleMcpServers = exports.bundleMcpOf = exports.renderMigration = exports.writerEnv = exports.CLI_RECORD_GENERATED_BY = void 0;
|
|
37
|
+
/**
|
|
38
|
+
* Install record wiring for the CLI: builds the record store and service,
|
|
39
|
+
* selects the record a command acts on and gates record sources. User level
|
|
40
|
+
* records into the shared user record and only reads the old user lockfile.
|
|
41
|
+
* Writing helpers migrate their level's old files before the first record
|
|
42
|
+
* change; reads, workspace scope and an old-format `--lockfile` never migrate.
|
|
43
|
+
* @module framework/install-record
|
|
44
|
+
*/
|
|
45
|
+
const path = __importStar(require("node:path"));
|
|
46
|
+
const app_1 = require("@ai-primitives-hub/app");
|
|
47
|
+
const core_1 = require("@ai-primitives-hub/core");
|
|
48
|
+
const infra_1 = require("@ai-primitives-hub/infra");
|
|
49
|
+
const version_1 = require("../version");
|
|
50
|
+
const error_1 = require("./error");
|
|
51
|
+
const hub_manager_1 = require("./hub-manager");
|
|
52
|
+
/** Writer stamp for records written by the CLI. */
|
|
53
|
+
exports.CLI_RECORD_GENERATED_BY = `ai-primitives-hub-cli@${version_1.CLI_VERSION}`;
|
|
54
|
+
/**
|
|
55
|
+
* The env a target writer expands `~` against: at user scope `HOME` is the
|
|
56
|
+
* user level's home, so it matches the pre-write folder check.
|
|
57
|
+
* @param env - CLI environment.
|
|
58
|
+
* @param scope - Effective scope of the target.
|
|
59
|
+
*/
|
|
60
|
+
const writerEnv = (env, scope) => (scope === 'user' ? { ...env, HOME: (0, app_1.resolveUserHome)(env) } : env);
|
|
61
|
+
exports.writerEnv = writerEnv;
|
|
62
|
+
/**
|
|
63
|
+
* The text lines of a migration report, printed before the `Record:` line.
|
|
64
|
+
* @param report - The command's migration report, if one ran.
|
|
65
|
+
*/
|
|
66
|
+
const renderMigration = (report) => {
|
|
67
|
+
if (report === undefined) {
|
|
68
|
+
return '';
|
|
69
|
+
}
|
|
70
|
+
const droppedFiles = report.droppedFiles.length;
|
|
71
|
+
const droppedEntries = report.droppedEntries.length;
|
|
72
|
+
const lines = [
|
|
73
|
+
...report.renamed.map((renamed) => ` renamed ${renamed.from} → ${renamed.to}`),
|
|
74
|
+
...report.mergedFrom.map((merged) => ` merged ${merged}`),
|
|
75
|
+
...report.needsReinstall.map((entry) => ` needs reinstall: ${entry.bundleId}@${entry.version} (tool unknown)`),
|
|
76
|
+
...report.versionConflicts.map((conflict) => ` version conflict: ${conflict.bundleId} kept ${conflict.kept}, dropped ${conflict.dropped}`),
|
|
77
|
+
...(droppedFiles > 0 ? [` dropped ${String(droppedFiles)} file path${droppedFiles === 1 ? '' : 's'}`] : []),
|
|
78
|
+
...(droppedEntries > 0 ? [` dropped ${String(droppedEntries)} already-recorded entr${droppedEntries === 1 ? 'y' : 'ies'}`] : []),
|
|
79
|
+
...report.warnings.map((warning) => ` warning: ${warning}`)
|
|
80
|
+
];
|
|
81
|
+
return lines.length === 0 ? '' : `Migrated ${report.level} record:\n${lines.join('\n')}\n`;
|
|
82
|
+
};
|
|
83
|
+
exports.renderMigration = renderMigration;
|
|
84
|
+
/**
|
|
85
|
+
* The MCP servers a deployment manifest declares, and what names them.
|
|
86
|
+
* @param manifest - Validated deployment manifest.
|
|
87
|
+
* @param manifest.id
|
|
88
|
+
* @param env - Environment.
|
|
89
|
+
* @param sourceRepository - Repository of the source the bundle came from.
|
|
90
|
+
*/
|
|
91
|
+
const bundleMcpOf = (manifest, env, sourceRepository) => {
|
|
92
|
+
const fields = manifest;
|
|
93
|
+
const servers = fields.mcpServers !== null && typeof fields.mcpServers === 'object' && !Array.isArray(fields.mcpServers)
|
|
94
|
+
? fields.mcpServers
|
|
95
|
+
: undefined;
|
|
96
|
+
const repository = typeof fields.repository === 'string'
|
|
97
|
+
? fields.repository
|
|
98
|
+
: (fields.repository !== null && typeof fields.repository === 'object' && typeof fields.repository.url === 'string'
|
|
99
|
+
? fields.repository.url
|
|
100
|
+
: undefined);
|
|
101
|
+
return {
|
|
102
|
+
servers,
|
|
103
|
+
manifestId: manifest.id,
|
|
104
|
+
...(repository === undefined ? {} : { manifestRepository: repository }),
|
|
105
|
+
...(sourceRepository === undefined ? {} : { sourceRepository }),
|
|
106
|
+
env
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
exports.bundleMcpOf = bundleMcpOf;
|
|
110
|
+
// VS Code's user folder, which holds its user-level `mcp.json`.
|
|
111
|
+
const vscodeUserDirOf = (env, tool, home) => {
|
|
112
|
+
const product = tool === 'vscode-insiders' ? 'Code - Insiders' : 'Code';
|
|
113
|
+
if (process.platform === 'darwin') {
|
|
114
|
+
return path.join(home, 'Library', 'Application Support', product, 'User');
|
|
115
|
+
}
|
|
116
|
+
if (process.platform === 'win32') {
|
|
117
|
+
return path.join(env.APPDATA ?? path.join(home, 'AppData', 'Roaming'), product, 'User');
|
|
118
|
+
}
|
|
119
|
+
return path.join(env.XDG_CONFIG_HOME ?? path.join(home, '.config'), product, 'User');
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* The tool's MCP file at the record's level and the servers to write there.
|
|
123
|
+
* @param location - Record file the install is recorded in.
|
|
124
|
+
* @param install - Tool, workspace root, bundle and its servers.
|
|
125
|
+
* @param layers - Layout layers, built-in first, as the payload writer resolves them.
|
|
126
|
+
* @returns The file and servers, or a warning when the tool has no MCP file there.
|
|
127
|
+
*/
|
|
128
|
+
const planBundleMcp = (location, install, layers) => {
|
|
129
|
+
const names = Object.keys(install.mcp.servers ?? {});
|
|
130
|
+
const layout = core_1.TARGET_TYPES.includes(install.tool)
|
|
131
|
+
? (0, app_1.resolveMcpLayoutConfig)(install.tool, location.level, layers)
|
|
132
|
+
: undefined;
|
|
133
|
+
if (names.length === 0 || layout === undefined) {
|
|
134
|
+
return { warnings: names.length === 0 ? [] : [`${install.tool} has no MCP file at ${location.level} level; MCP servers not installed: ${names.join(', ')}.`] };
|
|
135
|
+
}
|
|
136
|
+
const home = location.level === 'user' ? location.root : (install.mcp.env.HOME ?? location.root);
|
|
137
|
+
const file = {
|
|
138
|
+
path: path.normalize((0, core_1.resolveMcpConfigPath)(layout, {
|
|
139
|
+
HOME: home,
|
|
140
|
+
USERPROFILE: home,
|
|
141
|
+
workspaceRoot: install.workspaceRoot,
|
|
142
|
+
vscodeUserDir: vscodeUserDirOf(install.mcp.env, install.tool, home)
|
|
143
|
+
})),
|
|
144
|
+
serversKey: layout.serversKey
|
|
145
|
+
};
|
|
146
|
+
const servers = (0, app_1.prepareMcpServers)({
|
|
147
|
+
servers: install.mcp.servers,
|
|
148
|
+
bundleId: install.bundleId,
|
|
149
|
+
version: install.version,
|
|
150
|
+
manifestId: install.mcp.manifestId,
|
|
151
|
+
...(install.mcp.manifestRepository === undefined ? {} : { manifestRepository: install.mcp.manifestRepository }),
|
|
152
|
+
...(install.mcp.sourceRepository === undefined ? {} : { sourceRepository: install.mcp.sourceRepository }),
|
|
153
|
+
bundlePath: location.level === 'user' ? home : install.workspaceRoot,
|
|
154
|
+
...(layout.envPlaceholderStyle === undefined ? {} : { envPlaceholderStyle: layout.envPlaceholderStyle })
|
|
155
|
+
});
|
|
156
|
+
return { file, servers };
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Check that a bundle's MCP servers can be written, writing nothing; run before the bundle's files are written.
|
|
160
|
+
* @param service - Record service.
|
|
161
|
+
* @param location - Record file the install is recorded in.
|
|
162
|
+
* @param install - Tool, workspace root, bundle and its servers.
|
|
163
|
+
* @param layers - Layout layers, built-in first.
|
|
164
|
+
* @throws {RegistryError} `RECORD.MCP_CONFLICT`, `RECORD.MCP_CONFIG_UNREADABLE` or `RECORD.PATH_OUTSIDE_ROOT`.
|
|
165
|
+
*/
|
|
166
|
+
const checkBundleMcpServers = async (service, location, install, layers) => {
|
|
167
|
+
const plan = planBundleMcp(location, install, layers);
|
|
168
|
+
if ('file' in plan) {
|
|
169
|
+
await service.checkMcpServers({ location, file: plan.file, servers: plan.servers });
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
exports.checkBundleMcpServers = checkBundleMcpServers;
|
|
173
|
+
/**
|
|
174
|
+
* Write a bundle's MCP servers into the tool's MCP file at the record's level.
|
|
175
|
+
* A tool with no MCP file there gets none, with a warning.
|
|
176
|
+
* @param service - Record service.
|
|
177
|
+
* @param location - Record file the install is recorded in.
|
|
178
|
+
* @param install - Tool, workspace root, bundle and its servers.
|
|
179
|
+
* @param layers - Layout layers, built-in first.
|
|
180
|
+
* @returns The servers to record, and warnings.
|
|
181
|
+
* @throws {RegistryError} `RECORD.MCP_CONFLICT`, `RECORD.MCP_CONFIG_UNREADABLE` or `RECORD.PATH_OUTSIDE_ROOT`.
|
|
182
|
+
*/
|
|
183
|
+
const writeBundleMcpServers = async (service, location, install, layers) => {
|
|
184
|
+
const plan = planBundleMcp(location, install, layers);
|
|
185
|
+
if (!('file' in plan)) {
|
|
186
|
+
return { mcpServers: [], warnings: plan.warnings };
|
|
187
|
+
}
|
|
188
|
+
return { mcpServers: await service.writeMcpServers({ location, file: plan.file, servers: plan.servers }), warnings: [] };
|
|
189
|
+
};
|
|
190
|
+
exports.writeBundleMcpServers = writeBundleMcpServers;
|
|
191
|
+
/**
|
|
192
|
+
* Layout layers for a command: built-in, user and project, as the payload writer uses them.
|
|
193
|
+
* @param ctx - CLI context.
|
|
194
|
+
*/
|
|
195
|
+
const loadLayoutLayers = (ctx) => (0, exports.createLayoutLoader)(ctx).load();
|
|
196
|
+
exports.loadLayoutLayers = loadLayoutLayers;
|
|
197
|
+
/**
|
|
198
|
+
* Re-raise a record error with the command name and its file in the message; code, hint and context are kept.
|
|
199
|
+
* @param command - Message prefix.
|
|
200
|
+
* @param error - A `RECORD.*` error.
|
|
201
|
+
* @param written - Files already written, for an install that failed after the write.
|
|
202
|
+
*/
|
|
203
|
+
const toCommandRecordError = (command, error, written) => {
|
|
204
|
+
let message = `${command}: ${(0, core_1.describeRecordError)(error)}`;
|
|
205
|
+
if (written !== undefined && written.length > 0) {
|
|
206
|
+
message += ` These files were written but are not recorded; run the install again to record them: ${written.join(', ')}`;
|
|
207
|
+
}
|
|
208
|
+
return new core_1.RegistryError({
|
|
209
|
+
code: error.code,
|
|
210
|
+
message,
|
|
211
|
+
hint: error.hint,
|
|
212
|
+
context: {
|
|
213
|
+
...error.context,
|
|
214
|
+
...(written !== undefined && written.length > 0 ? { written: [...written] } : {})
|
|
215
|
+
},
|
|
216
|
+
cause: error
|
|
217
|
+
});
|
|
218
|
+
};
|
|
219
|
+
exports.toCommandRecordError = toCommandRecordError;
|
|
220
|
+
/**
|
|
221
|
+
* Re-raise a record error for `install`, with its file in the message.
|
|
222
|
+
* @param error - A `RECORD.*` error.
|
|
223
|
+
* @param written - Files already written, for a failure after the write.
|
|
224
|
+
*/
|
|
225
|
+
const toInstallRecordError = (error, written) => (0, exports.toCommandRecordError)('install', error, written);
|
|
226
|
+
exports.toInstallRecordError = toInstallRecordError;
|
|
227
|
+
/**
|
|
228
|
+
* Record store and service for one CLI command.
|
|
229
|
+
* @param fs - Filesystem port.
|
|
230
|
+
*/
|
|
231
|
+
const createRecordServices = (fs = new infra_1.NodeRecordFs()) => (0, app_1.createInstallRecordServices)({ generatedBy: exports.CLI_RECORD_GENERATED_BY, fs });
|
|
232
|
+
exports.createRecordServices = createRecordServices;
|
|
233
|
+
/**
|
|
234
|
+
* The shared user record location, rooted at home.
|
|
235
|
+
* @param store - Record store.
|
|
236
|
+
* @param env - Environment for the home and XDG folders.
|
|
237
|
+
*/
|
|
238
|
+
const userRecordLocation = (store, env) => store.resolveUser({ home: (0, app_1.resolveUserHome)(env), path: new infra_1.XdgAppStorage(env).getPaths().userRecord });
|
|
239
|
+
exports.userRecordLocation = userRecordLocation;
|
|
240
|
+
/**
|
|
241
|
+
* The layout config loader the CLI writers and record checks use.
|
|
242
|
+
* @param ctx - CLI context.
|
|
243
|
+
*/
|
|
244
|
+
const createLayoutLoader = (ctx) => new infra_1.FileSystemLayoutConfigLoader({ cwd: ctx.cwd(), fs: ctx.fs, userConfigDir: (0, infra_1.resolveUserConfigDir)(ctx.env) });
|
|
245
|
+
exports.createLayoutLoader = createLayoutLoader;
|
|
246
|
+
/**
|
|
247
|
+
* Whether a target's installs are kept in an install record (repository or user scope).
|
|
248
|
+
* @param target - Effective target.
|
|
249
|
+
*/
|
|
250
|
+
const usesInstallRecord = (target) => target.scope === 'repository' || target.scope === 'user';
|
|
251
|
+
exports.usesInstallRecord = usesInstallRecord;
|
|
252
|
+
/**
|
|
253
|
+
* The `info/exclude` section the record store uses at a git root, also in a worktree.
|
|
254
|
+
* @param ctx - CLI context.
|
|
255
|
+
* @param fs - Record filesystem port.
|
|
256
|
+
* @param gitRoot - Git root.
|
|
257
|
+
* @returns The exclusion, or `undefined` without a trusted git directory.
|
|
258
|
+
*/
|
|
259
|
+
const recordExclusion = async (ctx, fs, gitRoot) => {
|
|
260
|
+
const excludeFile = await (0, app_1.resolveExcludeFile)(fs, gitRoot).catch(() => ({ warning: 'unreadable' }));
|
|
261
|
+
return 'file' in excludeFile
|
|
262
|
+
? new infra_1.GitInfoExclude({ fs: ctx.fs, workspaceRoot: gitRoot, excludePath: excludeFile.file })
|
|
263
|
+
: undefined;
|
|
264
|
+
};
|
|
265
|
+
/**
|
|
266
|
+
* Build the record services and locate the repository record files. Without
|
|
267
|
+
* `given`, both files at the git root are loaded and checked first.
|
|
268
|
+
* @param ctx - CLI context.
|
|
269
|
+
* @param target - Repository-scope target.
|
|
270
|
+
* @param fs - Filesystem port.
|
|
271
|
+
* @param given - Record files already selected; both files at the git root otherwise.
|
|
272
|
+
*/
|
|
273
|
+
const openRepositoryRecords = async (ctx, target, fs, given) => {
|
|
274
|
+
const services = (0, exports.createRecordServices)(fs);
|
|
275
|
+
const workspaceRoot = target.rootPath ?? ctx.cwd();
|
|
276
|
+
const { locations, location } = given === undefined || given.length === 0
|
|
277
|
+
? await services.store.loadRepository({ start: workspaceRoot, commitMode: target.commitMode })
|
|
278
|
+
: { locations: given, location: given[0] };
|
|
279
|
+
return {
|
|
280
|
+
...services,
|
|
281
|
+
workspaceRoot,
|
|
282
|
+
locations,
|
|
283
|
+
location,
|
|
284
|
+
exclusion: (await recordExclusion(ctx, fs, location.root)) ?? new infra_1.GitInfoExclude({ fs: ctx.fs, workspaceRoot })
|
|
285
|
+
};
|
|
286
|
+
};
|
|
287
|
+
/**
|
|
288
|
+
* Load and check both repository record files, then migrate the old
|
|
289
|
+
* `prompt-registry.*` files at the git root, before anything is written.
|
|
290
|
+
* @param ctx - CLI context.
|
|
291
|
+
* @param target - Repository-scope target.
|
|
292
|
+
* @param fs - Filesystem port.
|
|
293
|
+
* @throws {RegistryError} A `RECORD.*` error naming the file; nothing was written.
|
|
294
|
+
*/
|
|
295
|
+
const prepareRepositoryRecord = async (ctx, target, fs = new infra_1.NodeRecordFs()) => {
|
|
296
|
+
try {
|
|
297
|
+
const records = await openRepositoryRecords(ctx, target, fs);
|
|
298
|
+
const migration = await (0, app_1.migrateLevel)({ store: records.store, fs, targets: records.locations, env: ctx.env });
|
|
299
|
+
for (const location of records.locations) {
|
|
300
|
+
await records.store.load(location);
|
|
301
|
+
}
|
|
302
|
+
return {
|
|
303
|
+
location: records.location,
|
|
304
|
+
service: records.service,
|
|
305
|
+
workspaceRoot: records.workspaceRoot,
|
|
306
|
+
exclusion: records.exclusion,
|
|
307
|
+
layers: await (0, exports.loadLayoutLayers)(ctx),
|
|
308
|
+
...(migration === undefined ? {} : { migration })
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
throw (0, core_1.isRecordError)(error) ? (0, exports.toInstallRecordError)(error) : error;
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
exports.prepareRepositoryRecord = prepareRepositoryRecord;
|
|
316
|
+
// The layout a user-scope target writes with, and its base folder with `HOME` set to home.
|
|
317
|
+
const resolveUserTargetLayout = async (ctx, target, home) => {
|
|
318
|
+
const layout = await (0, app_1.resolveLayoutAsync)(target, (0, exports.createLayoutLoader)(ctx));
|
|
319
|
+
return { layout, baseDir: path.resolve((0, core_1.expandPath)(layout.baseDir, { ...ctx.env, HOME: home })) };
|
|
320
|
+
};
|
|
321
|
+
/**
|
|
322
|
+
* Load and check the shared user record and refuse a target folder outside
|
|
323
|
+
* home before anything is written, then import the old user lockfile once.
|
|
324
|
+
* @param ctx - CLI context.
|
|
325
|
+
* @param target - User-scope target.
|
|
326
|
+
* @param fs - Filesystem port.
|
|
327
|
+
* @throws {RegistryError} A `RECORD.*` error naming the file or folder; nothing was written.
|
|
328
|
+
*/
|
|
329
|
+
const prepareUserRecord = async (ctx, target, fs = new infra_1.NodeRecordFs()) => {
|
|
330
|
+
const { store, service } = (0, exports.createRecordServices)(fs);
|
|
331
|
+
try {
|
|
332
|
+
const location = (0, exports.userRecordLocation)(store, ctx.env);
|
|
333
|
+
const home = location.root;
|
|
334
|
+
await store.load(location);
|
|
335
|
+
const { layout, baseDir } = await resolveUserTargetLayout(ctx, target, home);
|
|
336
|
+
const routes = Object.values(layout.kindRoutes);
|
|
337
|
+
const folders = new Set(routes.length === 0 ? [baseDir] : routes.map((route) => path.join(baseDir, route)));
|
|
338
|
+
for (const folder of folders) {
|
|
339
|
+
await (0, app_1.assertUserFolderInsideHome)(fs, home, folder);
|
|
340
|
+
}
|
|
341
|
+
const migration = await (0, app_1.migrateLevel)({ store, fs, targets: [location], env: ctx.env });
|
|
342
|
+
await store.load(location);
|
|
343
|
+
return { location, service, workspaceRoot: home, layers: await (0, exports.loadLayoutLayers)(ctx), ...(migration === undefined ? {} : { migration }) };
|
|
344
|
+
}
|
|
345
|
+
catch (error) {
|
|
346
|
+
throw (0, core_1.isRecordError)(error) ? (0, exports.toInstallRecordError)(error) : error;
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
exports.prepareUserRecord = prepareUserRecord;
|
|
350
|
+
/**
|
|
351
|
+
* Record a repository install, then remove the files it superseded. On failure
|
|
352
|
+
* the written files stay and the error lists them.
|
|
353
|
+
* @param prepared - From {@link prepareRepositoryRecord}.
|
|
354
|
+
* @param install - What was installed.
|
|
355
|
+
* @throws {RegistryError} A `RECORD.*` error naming the file and the written files.
|
|
356
|
+
*/
|
|
357
|
+
const recordRepositoryInstall = async (prepared, install) => {
|
|
358
|
+
let change;
|
|
359
|
+
const mcpWarnings = [];
|
|
360
|
+
try {
|
|
361
|
+
const mcp = install.mcp === undefined
|
|
362
|
+
? undefined
|
|
363
|
+
: await (0, exports.writeBundleMcpServers)(prepared.service, prepared.location, {
|
|
364
|
+
tool: install.tool, workspaceRoot: prepared.workspaceRoot, bundleId: install.bundleId, version: install.version, mcp: install.mcp
|
|
365
|
+
}, prepared.layers ?? [infra_1.defaultLayouts]);
|
|
366
|
+
mcpWarnings.push(...(mcp?.warnings ?? []));
|
|
367
|
+
change = await prepared.service.recordInstall({
|
|
368
|
+
location: prepared.location,
|
|
369
|
+
bundleId: install.bundleId,
|
|
370
|
+
version: install.version,
|
|
371
|
+
tool: install.tool,
|
|
372
|
+
source: install.source,
|
|
373
|
+
...(install.hub === undefined ? {} : { hub: install.hub }),
|
|
374
|
+
sourceType: install.source.type,
|
|
375
|
+
writtenPaths: install.writtenPaths,
|
|
376
|
+
workspaceRoot: prepared.workspaceRoot,
|
|
377
|
+
...(install.bundle === undefined ? {} : { name: install.bundle.name, bundleFiles: install.bundle.files }),
|
|
378
|
+
...(mcp === undefined ? {} : { mcpServers: mcp.mcpServers })
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
catch (error) {
|
|
382
|
+
throw (0, core_1.isRecordError)(error) ? (0, exports.toInstallRecordError)(error, install.writtenPaths) : error;
|
|
383
|
+
}
|
|
384
|
+
const removal = await prepared.service.deleteSuperseded(change);
|
|
385
|
+
return {
|
|
386
|
+
lockfile: prepared.location.path,
|
|
387
|
+
deleted: removal.deleted,
|
|
388
|
+
warnings: [...mcpWarnings, ...change.warnings, ...removal.warnings]
|
|
389
|
+
};
|
|
390
|
+
};
|
|
391
|
+
exports.recordRepositoryInstall = recordRepositoryInstall;
|
|
392
|
+
/**
|
|
393
|
+
* Refuse an install whose MCP servers cannot be written, before the bundle's files are written.
|
|
394
|
+
* @param prepared - From {@link prepareRecordForTarget}; `undefined` for a target that keeps the old lockfile.
|
|
395
|
+
* @param install - Bundle, version, tool and its servers.
|
|
396
|
+
* @param install.bundleId - Bundle id.
|
|
397
|
+
* @param install.version - Version to install.
|
|
398
|
+
* @param install.tool - The target's type.
|
|
399
|
+
* @param install.mcp - The bundle's servers.
|
|
400
|
+
* @throws {RegistryError} `RECORD.MCP_CONFLICT`, `RECORD.MCP_CONFIG_UNREADABLE` or `RECORD.PATH_OUTSIDE_ROOT`, as `install` reports it.
|
|
401
|
+
*/
|
|
402
|
+
const checkRepositoryInstallMcp = async (prepared, install) => {
|
|
403
|
+
if (prepared === undefined) {
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
try {
|
|
407
|
+
await (0, exports.checkBundleMcpServers)(prepared.service, prepared.location, { ...install, workspaceRoot: prepared.workspaceRoot }, prepared.layers ?? [infra_1.defaultLayouts]);
|
|
408
|
+
}
|
|
409
|
+
catch (error) {
|
|
410
|
+
throw (0, core_1.isRecordError)(error) ? (0, exports.toInstallRecordError)(error) : error;
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
exports.checkRepositoryInstallMcp = checkRepositoryInstallMcp;
|
|
414
|
+
/**
|
|
415
|
+
* Add the lone `unknown` bundles and the migration report to an uninstall prepare.
|
|
416
|
+
* @param parts - What the prepare supplies.
|
|
417
|
+
*/
|
|
418
|
+
const toPreparedUninstall = async (parts) => {
|
|
419
|
+
const { migration, ...rest } = parts;
|
|
420
|
+
return {
|
|
421
|
+
...rest,
|
|
422
|
+
loneUnknown: new Set((await parts.service.findLoneUnknown(parts.locations)).keys()),
|
|
423
|
+
...(migration === undefined ? {} : { migration })
|
|
424
|
+
};
|
|
425
|
+
};
|
|
426
|
+
/**
|
|
427
|
+
* Load, check and migrate the repository record files before anything is removed.
|
|
428
|
+
* Bundles not in `entries` fall back to the old store.
|
|
429
|
+
* @param ctx - CLI context.
|
|
430
|
+
* @param target - Repository-scope target.
|
|
431
|
+
* @param options - Record files already selected (`uninstall --lockfile`) and the preview flag.
|
|
432
|
+
* @param options.locations - The selected record files.
|
|
433
|
+
* @param options.dryRun - `true` for `--dry-run`, which never migrates.
|
|
434
|
+
* @param fs - Filesystem port.
|
|
435
|
+
* @throws {RegistryError} A `RECORD.*` error naming the file; nothing was removed.
|
|
436
|
+
*/
|
|
437
|
+
const prepareRepositoryUninstall = async (ctx, target, options = {}, fs = new infra_1.NodeRecordFs()) => {
|
|
438
|
+
try {
|
|
439
|
+
const { store, service, locations, location, exclusion } = await openRepositoryRecords(ctx, target, fs, options.locations);
|
|
440
|
+
const given = options.locations !== undefined && options.locations.length > 0;
|
|
441
|
+
const migration = options.dryRun === true || given ? undefined : await (0, app_1.migrateLevel)({ store, fs, targets: locations, env: ctx.env });
|
|
442
|
+
return await toPreparedUninstall({
|
|
443
|
+
location,
|
|
444
|
+
locations,
|
|
445
|
+
service,
|
|
446
|
+
tool: target.type,
|
|
447
|
+
exclusion,
|
|
448
|
+
entries: await service.entriesForTool(locations, target.type),
|
|
449
|
+
migration
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
catch (error) {
|
|
453
|
+
throw (0, core_1.isRecordError)(error) ? (0, exports.toCommandRecordError)('uninstall', error) : error;
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
exports.prepareRepositoryUninstall = prepareRepositoryUninstall;
|
|
457
|
+
// Per bundle, the home-relative paths the old user lockfile lists, resolved as the old uninstall does.
|
|
458
|
+
const oldUserLockfilePaths = async (ctx, target, home) => {
|
|
459
|
+
const out = new Map();
|
|
460
|
+
const lock = await (0, app_1.readLockfile)((0, app_1.resolveUserConfigPaths)(ctx.env).userLockfile, ctx.fs);
|
|
461
|
+
if (lock === null || Object.keys(lock.bundles).length === 0) {
|
|
462
|
+
return out;
|
|
463
|
+
}
|
|
464
|
+
const { layout, baseDir } = await resolveUserTargetLayout(ctx, target, home);
|
|
465
|
+
for (const [bundleId, entry] of Object.entries(lock.bundles)) {
|
|
466
|
+
const paths = new Set();
|
|
467
|
+
for (const file of entry.files) {
|
|
468
|
+
let absolute;
|
|
469
|
+
if (file.installedPath === undefined) {
|
|
470
|
+
const route = (0, core_1.matchKindRoute)(file.path, layout.kindRoutes);
|
|
471
|
+
absolute = route === null ? undefined : path.join(baseDir, route.outPrefix, route.tail);
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
absolute = path.join(baseDir, file.installedPath);
|
|
475
|
+
}
|
|
476
|
+
if (absolute !== undefined) {
|
|
477
|
+
paths.add(path.relative(home, path.resolve(absolute)).split(path.sep).join('/'));
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
out.set(bundleId, paths);
|
|
481
|
+
}
|
|
482
|
+
return out;
|
|
483
|
+
};
|
|
484
|
+
/**
|
|
485
|
+
* Load and check the shared user record and import the old user lockfile
|
|
486
|
+
* once before anything is removed. Bundles not recorded there fall back to
|
|
487
|
+
* the old user lockfile.
|
|
488
|
+
* @param ctx - CLI context.
|
|
489
|
+
* @param target - User-scope target.
|
|
490
|
+
* @param options - Preview flag.
|
|
491
|
+
* @param options.dryRun - `true` for `--dry-run`, which never migrates.
|
|
492
|
+
* @param fs - Filesystem port.
|
|
493
|
+
* @throws {RegistryError} A `RECORD.*` error naming the file; nothing was removed.
|
|
494
|
+
*/
|
|
495
|
+
const prepareUserUninstall = async (ctx, target, options = {}, fs = new infra_1.NodeRecordFs()) => {
|
|
496
|
+
const { store, service } = (0, exports.createRecordServices)(fs);
|
|
497
|
+
try {
|
|
498
|
+
const location = (0, exports.userRecordLocation)(store, ctx.env);
|
|
499
|
+
await store.load(location);
|
|
500
|
+
const migration = options.dryRun === true ? undefined : await (0, app_1.migrateLevel)({ store, fs, targets: [location], env: ctx.env });
|
|
501
|
+
const { record } = await store.load(location);
|
|
502
|
+
const entries = await service.entriesForTool([location], target.type);
|
|
503
|
+
// Once the old user lockfile was imported, the user level no longer reads it.
|
|
504
|
+
const imported = record[core_1.IMPORTED_USER_LOCKFILE_FIELD] !== undefined;
|
|
505
|
+
const keepPaths = entries.size === 0 || imported ? new Map() : await oldUserLockfilePaths(ctx, target, location.root);
|
|
506
|
+
return await toPreparedUninstall({
|
|
507
|
+
location,
|
|
508
|
+
locations: [location],
|
|
509
|
+
service,
|
|
510
|
+
tool: target.type,
|
|
511
|
+
entries,
|
|
512
|
+
keepPaths,
|
|
513
|
+
ignoresOldStore: imported,
|
|
514
|
+
migration
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
catch (error) {
|
|
518
|
+
throw (0, core_1.isRecordError)(error) ? (0, exports.toCommandRecordError)('uninstall', error) : error;
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
exports.prepareUserUninstall = prepareUserUninstall;
|
|
522
|
+
/**
|
|
523
|
+
* Load and check the install record a target installs into, before anything is written.
|
|
524
|
+
* @param ctx - CLI context.
|
|
525
|
+
* @param target - Effective target.
|
|
526
|
+
* @returns The prepared record; `undefined` for a target that keeps the old lockfile.
|
|
527
|
+
* @throws {RegistryError} A `RECORD.*` error naming the file or folder; nothing was written.
|
|
528
|
+
*/
|
|
529
|
+
const prepareRecordForTarget = async (ctx, target) => {
|
|
530
|
+
if (!(0, exports.usesInstallRecord)(target)) {
|
|
531
|
+
return undefined;
|
|
532
|
+
}
|
|
533
|
+
return target.scope === 'repository' ? (0, exports.prepareRepositoryRecord)(ctx, target) : (0, exports.prepareUserRecord)(ctx, target);
|
|
534
|
+
};
|
|
535
|
+
exports.prepareRecordForTarget = prepareRecordForTarget;
|
|
536
|
+
/**
|
|
537
|
+
* Refuse, before any file is written, a source the record holds from another hub.
|
|
538
|
+
* @param prepared - From {@link prepareRecordForTarget}; nothing is checked without it.
|
|
539
|
+
* @param source - Source to record.
|
|
540
|
+
* @param hub - The hub named by `source.hubId`.
|
|
541
|
+
* @throws {RegistryError} `RECORD.HUB_CONFLICT`, or any `RECORD.*` load error.
|
|
542
|
+
*/
|
|
543
|
+
const checkRecordSourceHub = async (prepared, source, hub) => {
|
|
544
|
+
if (prepared !== undefined) {
|
|
545
|
+
await prepared.service.checkSourceHub(prepared.location, source, hub);
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
exports.checkRecordSourceHub = checkRecordSourceHub;
|
|
549
|
+
/**
|
|
550
|
+
* Load and check the install record a target uninstalls from, before anything is removed.
|
|
551
|
+
* @param ctx - CLI context.
|
|
552
|
+
* @param target - Effective target.
|
|
553
|
+
* @param options - Selected files and the preview flag.
|
|
554
|
+
* @param options.locations - Repository level only: record files selected by `--lockfile`.
|
|
555
|
+
* @param options.dryRun - `true` for `--dry-run`, which never migrates.
|
|
556
|
+
* @returns The prepared record; `undefined` for a target that keeps the old lockfile.
|
|
557
|
+
* @throws {RegistryError} A `RECORD.*` error naming the file; nothing was removed.
|
|
558
|
+
*/
|
|
559
|
+
const prepareUninstallForTarget = async (ctx, target, options = {}) => {
|
|
560
|
+
if (!(0, exports.usesInstallRecord)(target)) {
|
|
561
|
+
return undefined;
|
|
562
|
+
}
|
|
563
|
+
return target.scope === 'repository'
|
|
564
|
+
? (0, exports.prepareRepositoryUninstall)(ctx, target, options)
|
|
565
|
+
: (0, exports.prepareUserUninstall)(ctx, target, { dryRun: options.dryRun });
|
|
566
|
+
};
|
|
567
|
+
exports.prepareUninstallForTarget = prepareUninstallForTarget;
|
|
568
|
+
/**
|
|
569
|
+
* The tool's other keys of the bundle's identity, such as an older versioned key a merge left next to it.
|
|
570
|
+
* @param prepared - The level's record files, loaded and checked.
|
|
571
|
+
* @param bundleId - Recorded key being uninstalled.
|
|
572
|
+
* @throws {RegistryError} A `RECORD.*` error naming the file.
|
|
573
|
+
*/
|
|
574
|
+
const otherKeysOfIdentity = async (prepared, bundleId) => {
|
|
575
|
+
const rows = (await prepared.service.listEntries(prepared.locations)).filter((row) => row.tool === prepared.tool);
|
|
576
|
+
const own = rows.find((row) => row.bundleId === bundleId);
|
|
577
|
+
if (own === undefined) {
|
|
578
|
+
return [];
|
|
579
|
+
}
|
|
580
|
+
const identity = (0, app_1.recordIdentityOf)(bundleId, own.version);
|
|
581
|
+
return [...new Set(rows
|
|
582
|
+
.filter((row) => row.bundleId !== bundleId && (0, app_1.recordIdentityOf)(row.bundleId, row.version) === identity)
|
|
583
|
+
.map((row) => row.bundleId))];
|
|
584
|
+
};
|
|
585
|
+
exports.otherKeysOfIdentity = otherKeysOfIdentity;
|
|
586
|
+
/**
|
|
587
|
+
* Remove one bundle's entry for the target's type, then delete its files except
|
|
588
|
+
* those another entry lists or that changed since the install (each kept with a warning).
|
|
589
|
+
* @param prepared - From {@link prepareRepositoryUninstall}.
|
|
590
|
+
* @param bundleId - Bundle to uninstall.
|
|
591
|
+
* @throws {RegistryError} `RECORD.ENTRY_NOT_FOUND` or another `RECORD.*` error naming the file.
|
|
592
|
+
*/
|
|
593
|
+
const recordRepositoryUninstall = async (prepared, bundleId) => {
|
|
594
|
+
let result;
|
|
595
|
+
try {
|
|
596
|
+
result = await prepared.service.uninstallEntry({
|
|
597
|
+
location: prepared.location,
|
|
598
|
+
bundleId,
|
|
599
|
+
tool: prepared.tool,
|
|
600
|
+
// A file any old user lockfile entry lists stays on disk for it, whichever bundle that entry is.
|
|
601
|
+
keep: prepared.keepPaths === undefined ? undefined : new Set([...prepared.keepPaths.values()].flatMap((paths) => [...paths]))
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
catch (error) {
|
|
605
|
+
throw (0, core_1.isRecordError)(error) ? (0, exports.toCommandRecordError)('uninstall', error) : error;
|
|
606
|
+
}
|
|
607
|
+
const { change, deleted } = result;
|
|
608
|
+
const warnings = [...result.warnings, ...result.modified.map((file) => `Did not remove ${file}: it was changed after install.`)];
|
|
609
|
+
if (change.location.commitMode === 'local-only' && deleted.length > 0 && prepared.exclusion !== undefined) {
|
|
610
|
+
// Drop the removed files' `info/exclude` entries, as the old uninstall did.
|
|
611
|
+
try {
|
|
612
|
+
await prepared.exclusion.remove(deleted.map((relative) => path.join(change.location.root, ...relative.split('/'))));
|
|
613
|
+
}
|
|
614
|
+
catch (error) {
|
|
615
|
+
warnings.push(`Could not update .git/info/exclude: ${error instanceof Error ? error.message : String(error)}`);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
return {
|
|
619
|
+
lockfile: change.location.path,
|
|
620
|
+
removed: deleted,
|
|
621
|
+
kept: result.kept,
|
|
622
|
+
mcpServersRemoved: result.mcpServersRemoved.map((server) => server.name),
|
|
623
|
+
warnings
|
|
624
|
+
};
|
|
625
|
+
};
|
|
626
|
+
exports.recordRepositoryUninstall = recordRepositoryUninstall;
|
|
627
|
+
/**
|
|
628
|
+
* Remove a bundle's lone `unknown` entry and keep every file it listed.
|
|
629
|
+
* @param prepared - Result of {@link prepareRepositoryUninstall} or {@link prepareUserUninstall}.
|
|
630
|
+
* @param bundleId - Bundle whose only entry is `unknown`.
|
|
631
|
+
* @throws {RegistryError} A `RECORD.*` error naming the file.
|
|
632
|
+
*/
|
|
633
|
+
const recordUnknownUninstall = async (prepared, bundleId) => {
|
|
634
|
+
let result;
|
|
635
|
+
try {
|
|
636
|
+
result = await prepared.service.removeUnknownEntry(prepared.location, bundleId);
|
|
637
|
+
}
|
|
638
|
+
catch (error) {
|
|
639
|
+
throw (0, core_1.isRecordError)(error) ? (0, exports.toCommandRecordError)('uninstall', error) : error;
|
|
640
|
+
}
|
|
641
|
+
return {
|
|
642
|
+
lockfile: result.change.location.path,
|
|
643
|
+
removed: result.deleted,
|
|
644
|
+
kept: result.kept,
|
|
645
|
+
mcpServersRemoved: [],
|
|
646
|
+
warnings: result.warnings
|
|
647
|
+
};
|
|
648
|
+
};
|
|
649
|
+
exports.recordUnknownUninstall = recordUnknownUninstall;
|
|
650
|
+
/**
|
|
651
|
+
* Select the record a command acts on and load every selected file before
|
|
652
|
+
* anything is written. With `input.write`, a level with a new record file is
|
|
653
|
+
* migrated first and selected again.
|
|
654
|
+
* @param ctx - CLI context.
|
|
655
|
+
* @param input - Command name, `--lockfile`, `--scope` and `write`.
|
|
656
|
+
* @param target - Resolved target, when the command has one.
|
|
657
|
+
* @param fs - Filesystem port.
|
|
658
|
+
* @throws {RegistryError} A `RECORD.*` error naming the command and the file; nothing was written.
|
|
659
|
+
*/
|
|
660
|
+
const prepareRecordSelection = async (ctx, input, target, fs = new infra_1.NodeRecordFs()) => {
|
|
661
|
+
const { store, service } = (0, exports.createRecordServices)(fs);
|
|
662
|
+
const userLockfile = (0, app_1.resolveUserConfigPaths)(ctx.env).userLockfile;
|
|
663
|
+
const user = (0, exports.userRecordLocation)(store, ctx.env);
|
|
664
|
+
try {
|
|
665
|
+
const select = () => (0, app_1.selectRecord)({
|
|
666
|
+
store,
|
|
667
|
+
fs,
|
|
668
|
+
cwd: ctx.cwd(),
|
|
669
|
+
given: {
|
|
670
|
+
...(input.lockfile === undefined || input.lockfile === '' ? {} : { lockfile: input.lockfile }),
|
|
671
|
+
...(input.scope === undefined ? {} : { scope: input.scope }),
|
|
672
|
+
...(target === undefined ? {} : { target: { scope: target.scope, rootPath: target.rootPath } })
|
|
673
|
+
},
|
|
674
|
+
findOldLockfile: (start) => (0, app_1.findLockfile)(start, ctx.fs, userLockfile),
|
|
675
|
+
userLockfile,
|
|
676
|
+
userRecord: user.path,
|
|
677
|
+
home: user.root
|
|
678
|
+
});
|
|
679
|
+
let selection = await select();
|
|
680
|
+
let migration;
|
|
681
|
+
if (input.write === true && selection.locations.length > 0) {
|
|
682
|
+
const targets = selection.level === 'user' ? selection.locations : await store.resolveRepository({ start: selection.root });
|
|
683
|
+
// A `--lockfile` record outside the git root is never migrated.
|
|
684
|
+
if (targets[0].root === selection.root) {
|
|
685
|
+
migration = await (0, app_1.migrateLevel)({ store, fs, targets, env: ctx.env });
|
|
686
|
+
const migrated = await select();
|
|
687
|
+
// An old file with no bundles leaves no record to infer the level from: keep the level and root selected.
|
|
688
|
+
if (migrated.level === selection.level) {
|
|
689
|
+
selection = migrated;
|
|
690
|
+
}
|
|
691
|
+
else {
|
|
692
|
+
const { oldLockfile: _migrated, ...kept } = selection;
|
|
693
|
+
selection = kept;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
const exclusion = selection.level === 'repository' && selection.locations.length > 0
|
|
698
|
+
? await recordExclusion(ctx, fs, selection.root)
|
|
699
|
+
: undefined;
|
|
700
|
+
return {
|
|
701
|
+
selection,
|
|
702
|
+
store,
|
|
703
|
+
service,
|
|
704
|
+
fs,
|
|
705
|
+
...(exclusion === undefined ? {} : { exclusion }),
|
|
706
|
+
...(migration === undefined ? {} : { migration })
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
catch (error) {
|
|
710
|
+
throw (0, core_1.isRecordError)(error) ? (0, exports.toCommandRecordError)(input.command, error) : error;
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
exports.prepareRecordSelection = prepareRecordSelection;
|
|
714
|
+
/**
|
|
715
|
+
* Select the record and list its entries; `options.write` migrates the level first.
|
|
716
|
+
* @param ctx - CLI context.
|
|
717
|
+
* @param input - Command name and the typed `--lockfile` / `--scope`.
|
|
718
|
+
* @param options - Selection options.
|
|
719
|
+
* @param options.write - `true` to migrate before selecting.
|
|
720
|
+
* @param options.target - Effective target the level is selected for, when there is one.
|
|
721
|
+
* @throws {RegistryError} A `RECORD.*` error naming the command and the file.
|
|
722
|
+
*/
|
|
723
|
+
const prepareRecordDecision = async (ctx, input, options = {}) => {
|
|
724
|
+
const { target } = options;
|
|
725
|
+
const prepared = await (0, exports.prepareRecordSelection)(ctx, { ...input, write: options.write === true }, target);
|
|
726
|
+
const { selection, service } = prepared;
|
|
727
|
+
try {
|
|
728
|
+
const rows = selection.locations.length === 0 ? [] : await service.listEntries(selection.locations);
|
|
729
|
+
return { input, ...(target === undefined ? {} : { target }), prepared, rows };
|
|
730
|
+
}
|
|
731
|
+
catch (error) {
|
|
732
|
+
throw (0, core_1.isRecordError)(error) ? (0, exports.toCommandRecordError)(input.command, error) : error;
|
|
733
|
+
}
|
|
734
|
+
};
|
|
735
|
+
exports.prepareRecordDecision = prepareRecordDecision;
|
|
736
|
+
/**
|
|
737
|
+
* The decision a real run acts on: selected again with `write`, so the level is migrated; a dry run keeps `decision`.
|
|
738
|
+
* @param ctx - CLI context.
|
|
739
|
+
* @param decision - The read-only decision.
|
|
740
|
+
* @param options - Preview options.
|
|
741
|
+
* @param options.dryRun - `true` for `--dry-run`.
|
|
742
|
+
* @throws {RegistryError} A `RECORD.*` error naming the command and the file.
|
|
743
|
+
*/
|
|
744
|
+
const migrateDecision = async (ctx, decision, options = {}) => options.dryRun === true ? decision : (0, exports.prepareRecordDecision)(ctx, decision.input, { write: true, target: decision.target });
|
|
745
|
+
exports.migrateDecision = migrateDecision;
|
|
746
|
+
/**
|
|
747
|
+
* The distinct tools a bundle has entries for, in row order.
|
|
748
|
+
* @param rows - Rows of a {@link RecordDecision}.
|
|
749
|
+
* @param bundleId - Bundle id.
|
|
750
|
+
*/
|
|
751
|
+
const recordedTools = (rows, bundleId) => [...new Set(rows.filter((row) => row.bundleId === bundleId).map((row) => row.tool))];
|
|
752
|
+
exports.recordedTools = recordedTools;
|
|
753
|
+
/**
|
|
754
|
+
* Prepare `uninstall <bundle-id>` of a lone `unknown` entry with no target; the level is migrated first unless `--dry-run`.
|
|
755
|
+
* @param ctx - CLI context.
|
|
756
|
+
* @param decision - The read-only decision that found the entry.
|
|
757
|
+
* @param bundleId - Bundle to uninstall.
|
|
758
|
+
* @param options - Preview options.
|
|
759
|
+
* @param options.dryRun - `true` for `--dry-run`, which never migrates.
|
|
760
|
+
* @throws {RegistryError} A `RECORD.*` error naming the file; nothing was removed.
|
|
761
|
+
*/
|
|
762
|
+
const prepareUnknownUninstall = async (ctx, decision, bundleId, options = {}) => {
|
|
763
|
+
const migrated = await (0, exports.migrateDecision)(ctx, decision, options);
|
|
764
|
+
const tools = (0, exports.recordedTools)(migrated.rows, bundleId);
|
|
765
|
+
if (tools.length !== 1 || tools[0] !== core_1.UNKNOWN_TOOL_NAME) {
|
|
766
|
+
return { changed: migrated };
|
|
767
|
+
}
|
|
768
|
+
const { selection, service, migration } = migrated.prepared;
|
|
769
|
+
try {
|
|
770
|
+
const location = await service.holdingLocation(selection.locations, bundleId, core_1.UNKNOWN_TOOL_NAME);
|
|
771
|
+
return {
|
|
772
|
+
uninstall: await toPreparedUninstall({
|
|
773
|
+
location: location ?? selection.locations[0],
|
|
774
|
+
locations: selection.locations,
|
|
775
|
+
service,
|
|
776
|
+
tool: core_1.UNKNOWN_TOOL_NAME,
|
|
777
|
+
entries: new Map(),
|
|
778
|
+
migration
|
|
779
|
+
})
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
catch (error) {
|
|
783
|
+
throw (0, core_1.isRecordError)(error) ? (0, exports.toCommandRecordError)('uninstall', error) : error;
|
|
784
|
+
}
|
|
785
|
+
};
|
|
786
|
+
exports.prepareUnknownUninstall = prepareUnknownUninstall;
|
|
787
|
+
/**
|
|
788
|
+
* Configured sources by id for the source trust gate, in `orderConfiguredSources`
|
|
789
|
+
* order. A hub that cannot be read contributes no source.
|
|
790
|
+
* @param ctx - CLI context.
|
|
791
|
+
*/
|
|
792
|
+
const configuredSources = async (ctx) => (await (0, exports.configuredRecordSources)(ctx)).configured;
|
|
793
|
+
exports.configuredSources = configuredSources;
|
|
794
|
+
/**
|
|
795
|
+
* Configured sources by id, every configured source and the shareable hubs.
|
|
796
|
+
* A hub that cannot be read contributes no source.
|
|
797
|
+
* @param ctx - CLI context.
|
|
798
|
+
*/
|
|
799
|
+
const configuredRecordSources = async (ctx) => {
|
|
800
|
+
const manager = (0, hub_manager_1.createHubManager)({ ctx });
|
|
801
|
+
// No readable active hub pointer: no source counts as the active hub's.
|
|
802
|
+
const activeHubId = await manager.getActiveHubId().catch(() => null);
|
|
803
|
+
const candidates = await manager.listSourcesAcrossAllHubs();
|
|
804
|
+
const ordered = (0, app_1.orderConfiguredSources)(candidates, activeHubId);
|
|
805
|
+
return {
|
|
806
|
+
configured: new Map(ordered.map((source) => [source.id, source])),
|
|
807
|
+
candidates,
|
|
808
|
+
hubs: (0, app_1.recordHubsOf)(await manager.listHubs())
|
|
809
|
+
};
|
|
810
|
+
};
|
|
811
|
+
exports.configuredRecordSources = configuredRecordSources;
|
|
812
|
+
/**
|
|
813
|
+
* The shareable hub a configured source came from, from the CLI hub store.
|
|
814
|
+
* @param ctx - CLI context.
|
|
815
|
+
* @param source - Configured source.
|
|
816
|
+
*/
|
|
817
|
+
const recordHubOf = async (ctx, source) => {
|
|
818
|
+
const hubId = source?.hubId;
|
|
819
|
+
if (hubId === undefined) {
|
|
820
|
+
return undefined;
|
|
821
|
+
}
|
|
822
|
+
const hub = await (0, hub_manager_1.createHubManager)({ ctx }).getHub(hubId);
|
|
823
|
+
return hub === null ? undefined : (0, app_1.toRecordHub)(hubId, hub.config.metadata.name, hub.reference);
|
|
824
|
+
};
|
|
825
|
+
exports.recordHubOf = recordHubOf;
|
|
826
|
+
/** Token provider that never returns a token. */
|
|
827
|
+
const NO_TOKENS = new infra_1.StaticTokenProvider('');
|
|
828
|
+
/**
|
|
829
|
+
* The token provider for a planned entry: none for a source taken from the record.
|
|
830
|
+
* @param tokens - The command's token provider.
|
|
831
|
+
* @param fromRecordOnly - `true` when no configured source has the entry's id.
|
|
832
|
+
*/
|
|
833
|
+
const tokensForRecordSource = (tokens, fromRecordOnly) => fromRecordOnly ? NO_TOKENS : tokens;
|
|
834
|
+
exports.tokensForRecordSource = tokensForRecordSource;
|
|
835
|
+
/**
|
|
836
|
+
* Refuse a planned entry whose MCP servers cannot be written, before its writer runs.
|
|
837
|
+
* @param service - Record service.
|
|
838
|
+
* @param item - The planned entry.
|
|
839
|
+
* @param target - Effective target that will write it.
|
|
840
|
+
* @param ctx - CLI context.
|
|
841
|
+
* @param version - Version to write.
|
|
842
|
+
* @param mcp - The bundle's servers.
|
|
843
|
+
* @throws {RegistryError} `RECORD.MCP_CONFLICT`, `RECORD.MCP_CONFIG_UNREADABLE` or `RECORD.PATH_OUTSIDE_ROOT`.
|
|
844
|
+
*/
|
|
845
|
+
const checkPlannedInstallMcp = async (service, item, target, ctx, version, mcp) => {
|
|
846
|
+
const workspaceRoot = item.location.level === 'user' ? item.location.root : (target.rootPath ?? ctx.cwd());
|
|
847
|
+
await (0, exports.checkBundleMcpServers)(service, item.location, { tool: item.tool, workspaceRoot, bundleId: item.bundleId, version, mcp }, await (0, exports.loadLayoutLayers)(ctx));
|
|
848
|
+
};
|
|
849
|
+
exports.checkPlannedInstallMcp = checkPlannedInstallMcp;
|
|
850
|
+
/**
|
|
851
|
+
* Re-record a planned entry after its writer ran; {@link finishRecordRun} deletes superseded files.
|
|
852
|
+
* @param service - Record service.
|
|
853
|
+
* @param item - The planned entry.
|
|
854
|
+
* @param target - Effective target that wrote it.
|
|
855
|
+
* @param ctx - CLI context.
|
|
856
|
+
* @param version - Version written.
|
|
857
|
+
* @param written - The writer's `written` list.
|
|
858
|
+
* @param syncSiblings - Which sibling entries to sync.
|
|
859
|
+
* @param bundle - The installed bundle's manifest id and files.
|
|
860
|
+
* @param mcp
|
|
861
|
+
* @throws {RegistryError} A `RECORD.*` error naming the file.
|
|
862
|
+
*/
|
|
863
|
+
const recordPlannedInstall = async (service, item, target, ctx, version, written, syncSiblings, bundle, mcp) => {
|
|
864
|
+
// User level: home is the workspace root, as the user-scope install records it.
|
|
865
|
+
const workspaceRoot = item.location.level === 'user' ? item.location.root : (target.rootPath ?? ctx.cwd());
|
|
866
|
+
const mcpWrite = mcp === undefined
|
|
867
|
+
? undefined
|
|
868
|
+
: await (0, exports.writeBundleMcpServers)(service, item.location, { tool: item.tool, workspaceRoot, bundleId: item.bundleId, version, mcp }, await (0, exports.loadLayoutLayers)(ctx));
|
|
869
|
+
const change = await service.recordInstall({
|
|
870
|
+
location: item.location,
|
|
871
|
+
bundleId: item.bundleId,
|
|
872
|
+
version,
|
|
873
|
+
tool: item.tool,
|
|
874
|
+
source: { sourceId: item.sourceId, ...item.source, ...(item.hubId === undefined ? {} : { hubId: item.hubId }) },
|
|
875
|
+
...(item.hub === undefined ? {} : { hub: item.hub }),
|
|
876
|
+
sourceType: item.sourceType,
|
|
877
|
+
writtenPaths: written,
|
|
878
|
+
workspaceRoot,
|
|
879
|
+
syncSiblings,
|
|
880
|
+
...(bundle === undefined ? {} : { name: bundle.name, bundleFiles: bundle.files }),
|
|
881
|
+
...(mcpWrite === undefined ? {} : { mcpServers: mcpWrite.mcpServers })
|
|
882
|
+
});
|
|
883
|
+
return { ...change, mcpWarnings: mcpWrite?.warnings ?? [] };
|
|
884
|
+
};
|
|
885
|
+
exports.recordPlannedInstall = recordPlannedInstall;
|
|
886
|
+
/**
|
|
887
|
+
* End a replay or update run: remove superseded files after every save, keeping still-listed ones.
|
|
888
|
+
* @param service - Record service.
|
|
889
|
+
* @param changes - Every record change of the run.
|
|
890
|
+
* @returns The warnings of the saves and of the removal.
|
|
891
|
+
*/
|
|
892
|
+
const finishRecordRun = async (service, changes) => {
|
|
893
|
+
const removal = await service.deleteSupersededAfterAll(changes);
|
|
894
|
+
return [...changes.flatMap((change) => [...(change.mcpWarnings ?? []), ...change.warnings]), ...removal.warnings];
|
|
895
|
+
};
|
|
896
|
+
exports.finishRecordRun = finishRecordRun;
|
|
897
|
+
/**
|
|
898
|
+
* The bundles only the old file has; the old code handles them, and the record wins for the others.
|
|
899
|
+
* @param ctx - CLI context.
|
|
900
|
+
* @param prepared - The command's record selection.
|
|
901
|
+
* @returns `undefined` when there is no readable old file.
|
|
902
|
+
*/
|
|
903
|
+
const oldFileOnlyBundles = async (ctx, prepared) => {
|
|
904
|
+
const lockPath = prepared.selection.oldLockfile;
|
|
905
|
+
const lock = lockPath === undefined ? null : await (0, app_1.readLockfile)(lockPath, ctx.fs);
|
|
906
|
+
if (lockPath === undefined || lock === null) {
|
|
907
|
+
return undefined;
|
|
908
|
+
}
|
|
909
|
+
const rows = await prepared.service.listEntries(prepared.selection.locations);
|
|
910
|
+
const recordedIds = new Set(rows.map((row) => row.bundleId));
|
|
911
|
+
const recordedIdentities = new Set(rows.map((row) => (0, app_1.recordIdentityOf)(row.bundleId, row.version)));
|
|
912
|
+
const onlyOld = (bundleId) => !recordedIds.has(bundleId) && !recordedIdentities.has((0, app_1.recordIdentityOf)(bundleId, lock.bundles[bundleId].version));
|
|
913
|
+
return { lockPath, lock, bundleIds: Object.keys(lock.bundles).filter((bundleId) => onlyOld(bundleId)) };
|
|
914
|
+
};
|
|
915
|
+
exports.oldFileOnlyBundles = oldFileOnlyBundles;
|
|
916
|
+
/**
|
|
917
|
+
* The old-file-only bundles and their target: the given one, else the only
|
|
918
|
+
* configured one, with the overrides applied; else the missing-target reason.
|
|
919
|
+
* @param ctx - CLI context.
|
|
920
|
+
* @param prepared - The command's record selection.
|
|
921
|
+
* @param resolve - Command name, the given target and the overrides.
|
|
922
|
+
* @returns `undefined` when no bundle is only in the old file.
|
|
923
|
+
*/
|
|
924
|
+
const prepareOldFileOnly = async (ctx, prepared, resolve) => {
|
|
925
|
+
const old = await (0, exports.oldFileOnlyBundles)(ctx, prepared);
|
|
926
|
+
if (old === undefined || old.bundleIds.length === 0) {
|
|
927
|
+
return undefined;
|
|
928
|
+
}
|
|
929
|
+
if (resolve.target !== undefined) {
|
|
930
|
+
return { ...old, target: resolve.target };
|
|
931
|
+
}
|
|
932
|
+
try {
|
|
933
|
+
return { ...old, target: await (0, error_1.resolveCommandTarget)(ctx, resolve.command, undefined, resolve.overrides) };
|
|
934
|
+
}
|
|
935
|
+
catch (error) {
|
|
936
|
+
return { ...old, reason: error instanceof Error ? error.message : String(error) };
|
|
937
|
+
}
|
|
938
|
+
};
|
|
939
|
+
exports.prepareOldFileOnly = prepareOldFileOnly;
|
|
940
|
+
/**
|
|
941
|
+
* The `level` and `records` JSON fields: the record files that exist, or the old file.
|
|
942
|
+
* @param selection - The command's record selection.
|
|
943
|
+
* @param fs - Filesystem port.
|
|
944
|
+
* @param oldFileRead - The old file, when the command also read it.
|
|
945
|
+
*/
|
|
946
|
+
const recordOutputFields = async (selection, fs, oldFileRead) => {
|
|
947
|
+
if (selection.locations.length === 0) {
|
|
948
|
+
return { level: selection.level, records: selection.oldLockfile === undefined ? [] : [selection.oldLockfile] };
|
|
949
|
+
}
|
|
950
|
+
const existing = [];
|
|
951
|
+
for (const location of selection.locations) {
|
|
952
|
+
if (await fs.exists(location.path)) {
|
|
953
|
+
existing.push(location.path);
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
if (oldFileRead !== undefined) {
|
|
957
|
+
existing.push(oldFileRead);
|
|
958
|
+
}
|
|
959
|
+
return { level: selection.level, records: existing.length > 0 ? existing : [selection.locations[0].path] };
|
|
960
|
+
};
|
|
961
|
+
exports.recordOutputFields = recordOutputFields;
|
|
962
|
+
/**
|
|
963
|
+
* The `Record: <level> <path>` text line; several files are comma-separated.
|
|
964
|
+
* @param fields - Output fields.
|
|
965
|
+
*/
|
|
966
|
+
const renderRecordLine = (fields) => `Record: ${fields.level} ${fields.records.join(', ')}\n`;
|
|
967
|
+
exports.renderRecordLine = renderRecordLine;
|
|
968
|
+
//# sourceMappingURL=install-record.js.map
|