brainx-skill 1.0.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/LICENSE +201 -0
- package/README.md +94 -0
- package/adapters/claude.js +7 -0
- package/adapters/codex.js +8 -0
- package/adapters/cursor.js +7 -0
- package/bin/brainx.js +13 -0
- package/lib/adapter-transaction.js +295 -0
- package/lib/bundle.js +136 -0
- package/lib/cli.js +67 -0
- package/lib/constants.js +11 -0
- package/lib/hash.js +41 -0
- package/lib/installer.js +239 -0
- package/lib/paths.js +61 -0
- package/lib/prompts.js +166 -0
- package/lib/receipt.js +143 -0
- package/manifest.json +6 -0
- package/package.json +40 -0
- package/skills/brainx-install/SKILL.md +222 -0
- package/skills/brainx-install/references/compatibility-and-release-matching.md +42 -0
package/lib/bundle.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('node:fs/promises');
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
const { PACKAGE_NAME, SKILL_NAME_PATTERN } = require('./constants');
|
|
6
|
+
const { digestTree } = require('./hash');
|
|
7
|
+
|
|
8
|
+
class BundleValidationError extends Error {
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = 'BundleValidationError';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function readJson(filePath, fsApi) {
|
|
16
|
+
let contents;
|
|
17
|
+
try {
|
|
18
|
+
contents = await fsApi.readFile(filePath, 'utf8');
|
|
19
|
+
} catch (error) {
|
|
20
|
+
throw new BundleValidationError(`Cannot read ${filePath}: ${error.message}`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
return JSON.parse(contents);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
throw new BundleValidationError(`Invalid JSON in ${filePath}: ${error.message}`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function validatePortableTree(root, fsApi, pathApi) {
|
|
31
|
+
const entries = await fsApi.readdir(root, { withFileTypes: true });
|
|
32
|
+
for (const entry of entries) {
|
|
33
|
+
const entryPath = pathApi.join(root, entry.name);
|
|
34
|
+
if (entry.isDirectory()) {
|
|
35
|
+
await validatePortableTree(entryPath, fsApi, pathApi);
|
|
36
|
+
} else if (!entry.isFile()) {
|
|
37
|
+
throw new BundleValidationError(
|
|
38
|
+
`Skill bundles may contain only regular files and directories: ${entryPath}`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function validateBundle(packageRoot, options = {}) {
|
|
45
|
+
const fsApi = options.fsApi || fs;
|
|
46
|
+
const pathApi = options.pathApi || path;
|
|
47
|
+
const packageJsonPath = pathApi.join(packageRoot, 'package.json');
|
|
48
|
+
const manifestPath = pathApi.join(packageRoot, 'manifest.json');
|
|
49
|
+
const skillsRoot = pathApi.join(packageRoot, 'skills');
|
|
50
|
+
const packageJson = await readJson(packageJsonPath, fsApi);
|
|
51
|
+
const manifest = await readJson(manifestPath, fsApi);
|
|
52
|
+
|
|
53
|
+
if (packageJson.name !== PACKAGE_NAME) {
|
|
54
|
+
throw new BundleValidationError(
|
|
55
|
+
`Expected package name ${PACKAGE_NAME}, found ${packageJson.name || 'missing'}`,
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
if (typeof packageJson.version !== 'string' || packageJson.version.length === 0) {
|
|
59
|
+
throw new BundleValidationError('package.json must contain a package version');
|
|
60
|
+
}
|
|
61
|
+
if (manifest.schemaVersion !== 1 || !Array.isArray(manifest.skills)) {
|
|
62
|
+
throw new BundleValidationError('manifest.json must use schemaVersion 1 and contain a skills array');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const skillNames = [];
|
|
66
|
+
const seen = new Set();
|
|
67
|
+
for (const name of manifest.skills) {
|
|
68
|
+
if (typeof name !== 'string' || !SKILL_NAME_PATTERN.test(name)) {
|
|
69
|
+
throw new BundleValidationError(`Invalid or unsafe skill name in manifest: ${name}`);
|
|
70
|
+
}
|
|
71
|
+
if (seen.has(name)) {
|
|
72
|
+
throw new BundleValidationError(`Duplicate skill in manifest: ${name}`);
|
|
73
|
+
}
|
|
74
|
+
seen.add(name);
|
|
75
|
+
skillNames.push(name);
|
|
76
|
+
}
|
|
77
|
+
if (skillNames.length === 0) {
|
|
78
|
+
throw new BundleValidationError('manifest.json must declare at least one skill');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let topLevelEntries;
|
|
82
|
+
try {
|
|
83
|
+
topLevelEntries = await fsApi.readdir(skillsRoot, { withFileTypes: true });
|
|
84
|
+
} catch (error) {
|
|
85
|
+
throw new BundleValidationError(`Cannot read skills directory: ${error.message}`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const actualNames = [];
|
|
89
|
+
for (const entry of topLevelEntries) {
|
|
90
|
+
if (!entry.isDirectory()) {
|
|
91
|
+
throw new BundleValidationError(
|
|
92
|
+
`The skills directory may contain only declared skill directories: ${entry.name}`,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
actualNames.push(entry.name);
|
|
96
|
+
}
|
|
97
|
+
const missing = skillNames.filter((name) => !actualNames.includes(name));
|
|
98
|
+
if (missing.length) {
|
|
99
|
+
throw new BundleValidationError(
|
|
100
|
+
`Manifest declares missing skill directories: ${missing.join(', ')}`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const skills = [];
|
|
105
|
+
for (const name of skillNames) {
|
|
106
|
+
const sourcePath = pathApi.join(skillsRoot, name);
|
|
107
|
+
const skillFile = pathApi.join(sourcePath, 'SKILL.md');
|
|
108
|
+
let stat;
|
|
109
|
+
try {
|
|
110
|
+
stat = await fsApi.lstat(skillFile);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
throw new BundleValidationError(`Missing SKILL.md for ${name}: ${error.message}`);
|
|
113
|
+
}
|
|
114
|
+
if (!stat.isFile() || stat.size === 0) {
|
|
115
|
+
throw new BundleValidationError(`SKILL.md for ${name} must be a nonempty regular file`);
|
|
116
|
+
}
|
|
117
|
+
await validatePortableTree(sourcePath, fsApi, pathApi);
|
|
118
|
+
skills.push({
|
|
119
|
+
name,
|
|
120
|
+
sourcePath,
|
|
121
|
+
digest: await digestTree(sourcePath, { fsApi, pathApi }),
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
packageName: packageJson.name,
|
|
127
|
+
version: packageJson.version,
|
|
128
|
+
packageRoot,
|
|
129
|
+
skills,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
module.exports = {
|
|
134
|
+
BundleValidationError,
|
|
135
|
+
validateBundle,
|
|
136
|
+
};
|
package/lib/cli.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('node:fs/promises');
|
|
4
|
+
const os = require('node:os');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const { DEFAULT_ADAPTERS, runInstaller } = require('./installer');
|
|
7
|
+
const { NonInteractiveInstallError, promptForInstall } = require('./prompts');
|
|
8
|
+
|
|
9
|
+
const USAGE = `Usage:
|
|
10
|
+
npx brainx-skill install
|
|
11
|
+
npx brainx-skill update`;
|
|
12
|
+
|
|
13
|
+
function parseCommand(args) {
|
|
14
|
+
if (args.length !== 1 || (args[0] !== 'install' && args[0] !== 'update')) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return args[0];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function runCli(args, options = {}) {
|
|
21
|
+
const stderr = options.stderr || process.stderr;
|
|
22
|
+
const command = parseCommand(args);
|
|
23
|
+
if (!command) {
|
|
24
|
+
stderr.write(`${USAGE}\n`);
|
|
25
|
+
return 1;
|
|
26
|
+
}
|
|
27
|
+
const installer = options.installer || runInstaller;
|
|
28
|
+
if (command === 'update') {
|
|
29
|
+
return installer(command, options);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const selectInstall = options.promptForInstall || promptForInstall;
|
|
33
|
+
const homeDir = options.homeDir || os.homedir();
|
|
34
|
+
const cwd = options.cwd || process.cwd();
|
|
35
|
+
let selection;
|
|
36
|
+
try {
|
|
37
|
+
selection = await selectInstall({
|
|
38
|
+
adapters: options.adapters || DEFAULT_ADAPTERS,
|
|
39
|
+
fsApi: options.fsApi || fs,
|
|
40
|
+
pathApi: options.pathApi || path,
|
|
41
|
+
homeDir,
|
|
42
|
+
cwd,
|
|
43
|
+
stdin: options.stdin || process.stdin,
|
|
44
|
+
stdout: options.stdout || process.stdout,
|
|
45
|
+
});
|
|
46
|
+
} catch (error) {
|
|
47
|
+
if (error instanceof NonInteractiveInstallError) {
|
|
48
|
+
stderr.write('Interactive installation requires a terminal.\n');
|
|
49
|
+
stderr.write('Run this command in an interactive shell.\n');
|
|
50
|
+
return 1;
|
|
51
|
+
}
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return installer(command, {
|
|
56
|
+
...options,
|
|
57
|
+
...selection,
|
|
58
|
+
homeDir,
|
|
59
|
+
cwd,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = {
|
|
64
|
+
USAGE,
|
|
65
|
+
parseCommand,
|
|
66
|
+
runCli,
|
|
67
|
+
};
|
package/lib/constants.js
ADDED
package/lib/hash.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('node:crypto');
|
|
4
|
+
const fs = require('node:fs/promises');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
|
|
7
|
+
async function digestTree(root, options = {}) {
|
|
8
|
+
const fsApi = options.fsApi || fs;
|
|
9
|
+
const pathApi = options.pathApi || path;
|
|
10
|
+
const hash = crypto.createHash('sha256');
|
|
11
|
+
|
|
12
|
+
async function walk(directory, relativeDirectory) {
|
|
13
|
+
const entries = await fsApi.readdir(directory, { withFileTypes: true });
|
|
14
|
+
entries.sort((left, right) => left.name.localeCompare(right.name));
|
|
15
|
+
|
|
16
|
+
for (const entry of entries) {
|
|
17
|
+
const absolutePath = pathApi.join(directory, entry.name);
|
|
18
|
+
const relativePath = relativeDirectory
|
|
19
|
+
? `${relativeDirectory}/${entry.name}`
|
|
20
|
+
: entry.name;
|
|
21
|
+
|
|
22
|
+
if (entry.isDirectory()) {
|
|
23
|
+
hash.update(`D\0${relativePath}\0`);
|
|
24
|
+
await walk(absolutePath, relativePath);
|
|
25
|
+
} else if (entry.isFile()) {
|
|
26
|
+
const contents = await fsApi.readFile(absolutePath);
|
|
27
|
+
hash.update(`F\0${relativePath}\0${contents.length}\0`);
|
|
28
|
+
hash.update(contents);
|
|
29
|
+
} else {
|
|
30
|
+
throw new Error(`Unsupported filesystem entry: ${absolutePath}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
await walk(root, '');
|
|
36
|
+
return hash.digest('hex');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
module.exports = {
|
|
40
|
+
digestTree,
|
|
41
|
+
};
|
package/lib/installer.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const os = require('node:os');
|
|
4
|
+
const fs = require('node:fs/promises');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const codex = require('../adapters/codex');
|
|
7
|
+
const claude = require('../adapters/claude');
|
|
8
|
+
const cursor = require('../adapters/cursor');
|
|
9
|
+
const { prepareAdapter, AdapterOperationError } = require('./adapter-transaction');
|
|
10
|
+
const { validateBundle } = require('./bundle');
|
|
11
|
+
const { PACKAGE_NAME, RECEIPT_SCHEMA_VERSION } = require('./constants');
|
|
12
|
+
const { resolveDestinationRoot, resolveLocations } = require('./paths');
|
|
13
|
+
const { readReceipt, writeReceiptAtomic } = require('./receipt');
|
|
14
|
+
|
|
15
|
+
const DEFAULT_ADAPTERS = [claude, codex, cursor];
|
|
16
|
+
|
|
17
|
+
function writeLine(stream, text = '') {
|
|
18
|
+
stream.write(`${text}\n`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function adapterLabel(adapter) {
|
|
22
|
+
return adapter.promptLabel || adapter.label;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function formatAdapterFailure(error, stderr) {
|
|
26
|
+
writeLine(stderr, `x ${adapterLabel(error.adapter)} installation failed`);
|
|
27
|
+
writeLine(stderr, ` Path: ${error.affectedPath}`);
|
|
28
|
+
writeLine(stderr, ` Action not performed: ${error.action}`);
|
|
29
|
+
writeLine(stderr, ` Resolve: ${error.resolution}`);
|
|
30
|
+
if (error.cause && error.cause.message) {
|
|
31
|
+
writeLine(stderr, ` Detail: ${error.cause.message}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function asAdapterError(adapter, destination, error) {
|
|
36
|
+
if (error instanceof AdapterOperationError) {
|
|
37
|
+
return error;
|
|
38
|
+
}
|
|
39
|
+
return new AdapterOperationError(
|
|
40
|
+
adapter,
|
|
41
|
+
destination,
|
|
42
|
+
'BrainX skills were not installed at this destination.',
|
|
43
|
+
'Check the path and its permissions, then run the command again.',
|
|
44
|
+
error,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function runInstaller(command, options = {}) {
|
|
49
|
+
const fsApi = options.fsApi || fs;
|
|
50
|
+
const pathApi = options.pathApi || path;
|
|
51
|
+
const availableAdapters = options.adapters || DEFAULT_ADAPTERS;
|
|
52
|
+
const stdout = options.stdout || process.stdout;
|
|
53
|
+
const stderr = options.stderr || process.stderr;
|
|
54
|
+
const packageRoot = options.packageRoot || path.resolve(__dirname, '..');
|
|
55
|
+
const homeDir = options.homeDir || os.homedir();
|
|
56
|
+
const cwd = options.cwd || process.cwd();
|
|
57
|
+
const now = options.now || (() => new Date().toISOString());
|
|
58
|
+
const stateLocations = resolveLocations(homeDir, [], pathApi);
|
|
59
|
+
const readReceiptFn = options.readReceipt || readReceipt;
|
|
60
|
+
const writeReceiptFn = options.writeReceipt || writeReceiptAtomic;
|
|
61
|
+
|
|
62
|
+
let receipt;
|
|
63
|
+
try {
|
|
64
|
+
receipt = await readReceiptFn(
|
|
65
|
+
stateLocations.receiptPath,
|
|
66
|
+
availableAdapters,
|
|
67
|
+
null,
|
|
68
|
+
{ fsApi, pathApi },
|
|
69
|
+
);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
writeLine(stderr, `x Cannot inspect BrainX-managed installation state: ${error.message}`);
|
|
72
|
+
writeLine(stderr, ` Receipt: ${stateLocations.receiptPath}`);
|
|
73
|
+
writeLine(stderr, ' No skill directories were changed.');
|
|
74
|
+
return 1;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const hasManagedInstallation = Boolean(
|
|
78
|
+
receipt && availableAdapters.some((adapter) => receipt.adapters[adapter.id]),
|
|
79
|
+
);
|
|
80
|
+
if (command === 'update' && !hasManagedInstallation) {
|
|
81
|
+
writeLine(stdout, 'No BrainX-managed skills are currently installed.');
|
|
82
|
+
writeLine(stdout, 'Run: npx brainx-skill install');
|
|
83
|
+
return 0;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let adapters;
|
|
87
|
+
let destinations;
|
|
88
|
+
if (command === 'update') {
|
|
89
|
+
adapters = availableAdapters.filter((adapter) => receipt.adapters[adapter.id]);
|
|
90
|
+
destinations = Object.fromEntries(adapters.map((adapter) => [
|
|
91
|
+
adapter.id,
|
|
92
|
+
receipt.adapters[adapter.id].destination,
|
|
93
|
+
]));
|
|
94
|
+
} else {
|
|
95
|
+
const selectedIds = options.selectedHarnessIds || availableAdapters.map((adapter) => adapter.id);
|
|
96
|
+
const selected = new Set(selectedIds);
|
|
97
|
+
if (selected.size === 0 || selectedIds.some((id) => (
|
|
98
|
+
!availableAdapters.some((adapter) => adapter.id === id)
|
|
99
|
+
))) {
|
|
100
|
+
writeLine(stderr, 'x Invalid harness selection.');
|
|
101
|
+
return 1;
|
|
102
|
+
}
|
|
103
|
+
adapters = availableAdapters.filter((adapter) => selected.has(adapter.id));
|
|
104
|
+
const installLocations = resolveLocations(homeDir, adapters, pathApi, {
|
|
105
|
+
scope: options.scope || 'global',
|
|
106
|
+
cwd,
|
|
107
|
+
});
|
|
108
|
+
destinations = installLocations.destinations;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let bundle;
|
|
112
|
+
try {
|
|
113
|
+
bundle = await validateBundle(packageRoot, { fsApi, pathApi });
|
|
114
|
+
} catch (error) {
|
|
115
|
+
writeLine(stderr, `x BrainX skill bundle validation failed: ${error.message}`);
|
|
116
|
+
writeLine(stderr, ' No skill directories were changed.');
|
|
117
|
+
return 1;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const timestamp = now();
|
|
121
|
+
const results = [];
|
|
122
|
+
const committedTransactions = [];
|
|
123
|
+
const failures = [];
|
|
124
|
+
|
|
125
|
+
for (const adapter of adapters) {
|
|
126
|
+
const destination = destinations[adapter.id];
|
|
127
|
+
const previousRecord = receipt ? receipt.adapters[adapter.id] : undefined;
|
|
128
|
+
try {
|
|
129
|
+
const transaction = await prepareAdapter(
|
|
130
|
+
adapter,
|
|
131
|
+
destination,
|
|
132
|
+
bundle,
|
|
133
|
+
previousRecord,
|
|
134
|
+
{
|
|
135
|
+
fsApi,
|
|
136
|
+
pathApi,
|
|
137
|
+
uuid: options.uuid,
|
|
138
|
+
timestamp,
|
|
139
|
+
copySkill: options.copySkill,
|
|
140
|
+
homeDir: resolveDestinationRoot(destination, adapter, pathApi),
|
|
141
|
+
},
|
|
142
|
+
);
|
|
143
|
+
if (transaction.status === 'current') {
|
|
144
|
+
results.push(transaction);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
await transaction.commit();
|
|
148
|
+
committedTransactions.push(transaction);
|
|
149
|
+
results.push({ ...transaction, status: 'installed' });
|
|
150
|
+
} catch (error) {
|
|
151
|
+
failures.push(asAdapterError(adapter, destination, error));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (committedTransactions.length > 0) {
|
|
156
|
+
const nextAdapters = receipt ? { ...receipt.adapters } : {};
|
|
157
|
+
for (const transaction of committedTransactions) {
|
|
158
|
+
nextAdapters[transaction.adapter.id] = transaction.record;
|
|
159
|
+
}
|
|
160
|
+
const nextReceipt = {
|
|
161
|
+
schemaVersion: RECEIPT_SCHEMA_VERSION,
|
|
162
|
+
packageName: PACKAGE_NAME,
|
|
163
|
+
lastOperation: {
|
|
164
|
+
command,
|
|
165
|
+
at: timestamp,
|
|
166
|
+
},
|
|
167
|
+
adapters: nextAdapters,
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
try {
|
|
171
|
+
await writeReceiptFn(stateLocations.receiptPath, nextReceipt, {
|
|
172
|
+
fsApi,
|
|
173
|
+
pathApi,
|
|
174
|
+
uuid: options.uuid,
|
|
175
|
+
});
|
|
176
|
+
} catch (error) {
|
|
177
|
+
const rollbackFailures = [];
|
|
178
|
+
for (const transaction of [...committedTransactions].reverse()) {
|
|
179
|
+
try {
|
|
180
|
+
await transaction.rollback();
|
|
181
|
+
} catch (rollbackError) {
|
|
182
|
+
rollbackFailures.push(rollbackError.message);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
writeLine(stderr, `x BrainX receipt could not be written: ${error.message}`);
|
|
186
|
+
writeLine(stderr, ` Receipt: ${stateLocations.receiptPath}`);
|
|
187
|
+
writeLine(stderr, ' Installed changes were rolled back.');
|
|
188
|
+
if (rollbackFailures.length) {
|
|
189
|
+
writeLine(stderr, ` Rollback detail: ${rollbackFailures.join('; ')}`);
|
|
190
|
+
}
|
|
191
|
+
for (const failure of failures) {
|
|
192
|
+
formatAdapterFailure(failure, stderr);
|
|
193
|
+
}
|
|
194
|
+
return 1;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
for (const transaction of committedTransactions) {
|
|
198
|
+
try {
|
|
199
|
+
await transaction.finalize();
|
|
200
|
+
} catch (error) {
|
|
201
|
+
failures.push(new AdapterOperationError(
|
|
202
|
+
transaction.adapter,
|
|
203
|
+
transaction.destination,
|
|
204
|
+
'The installation succeeded, but its temporary backup could not be removed.',
|
|
205
|
+
'Check permissions and remove the reported .brainx-backup directory after verifying the installation.',
|
|
206
|
+
error,
|
|
207
|
+
));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const allCurrent = failures.length === 0
|
|
213
|
+
&& results.length === adapters.length
|
|
214
|
+
&& results.every((result) => result.status === 'current');
|
|
215
|
+
if (allCurrent) {
|
|
216
|
+
writeLine(stdout, `\u2713 BrainX skills ${bundle.version} are already installed`);
|
|
217
|
+
} else {
|
|
218
|
+
for (const adapter of adapters) {
|
|
219
|
+
const result = results.find((candidate) => candidate.adapter.id === adapter.id);
|
|
220
|
+
if (!result) {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
const state = result.status === 'current' ? 'are already installed' : 'installed';
|
|
224
|
+
writeLine(stdout, `\u2713 BrainX skills ${bundle.version} ${state} for ${adapterLabel(adapter)}`);
|
|
225
|
+
writeLine(stdout, ` ${result.destination}`);
|
|
226
|
+
writeLine(stdout);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
for (const failure of failures) {
|
|
231
|
+
formatAdapterFailure(failure, stderr);
|
|
232
|
+
}
|
|
233
|
+
return failures.length > 0 ? 1 : 0;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
module.exports = {
|
|
237
|
+
DEFAULT_ADAPTERS,
|
|
238
|
+
runInstaller,
|
|
239
|
+
};
|
package/lib/paths.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('node:path');
|
|
4
|
+
|
|
5
|
+
function resolveLocations(homeDir, adapters, pathApi = path, options = {}) {
|
|
6
|
+
if (typeof homeDir !== 'string' || !pathApi.isAbsolute(homeDir)) {
|
|
7
|
+
throw new Error(`Home directory must be an absolute path: ${homeDir}`);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const scope = options.scope || 'global';
|
|
11
|
+
if (scope !== 'global' && scope !== 'project') {
|
|
12
|
+
throw new Error(`Unknown installation scope: ${scope}`);
|
|
13
|
+
}
|
|
14
|
+
const baseDir = scope === 'global' ? homeDir : options.cwd;
|
|
15
|
+
if (typeof baseDir !== 'string' || !pathApi.isAbsolute(baseDir)) {
|
|
16
|
+
throw new Error(`Installation base directory must be an absolute path: ${baseDir}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const destinations = {};
|
|
20
|
+
for (const adapter of adapters) {
|
|
21
|
+
destinations[adapter.id] = pathApi.resolve(baseDir, ...adapter.homePath);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
homeDir: pathApi.resolve(homeDir),
|
|
26
|
+
baseDir: pathApi.resolve(baseDir),
|
|
27
|
+
stateDir: pathApi.resolve(homeDir, '.brainx'),
|
|
28
|
+
receiptPath: pathApi.resolve(homeDir, '.brainx', 'receipt.json'),
|
|
29
|
+
destinations,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function resolveDestinationRoot(destination, adapter, pathApi = path) {
|
|
34
|
+
let current = pathApi.resolve(destination);
|
|
35
|
+
for (const part of [...adapter.homePath].reverse()) {
|
|
36
|
+
const name = pathApi.basename(current);
|
|
37
|
+
const matches = pathApi === path.win32
|
|
38
|
+
? name.toLowerCase() === part.toLowerCase()
|
|
39
|
+
: name === part;
|
|
40
|
+
if (!matches) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
current = pathApi.dirname(current);
|
|
44
|
+
}
|
|
45
|
+
return current;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function samePath(left, right, pathApi = path) {
|
|
49
|
+
const normalizedLeft = pathApi.normalize(pathApi.resolve(left));
|
|
50
|
+
const normalizedRight = pathApi.normalize(pathApi.resolve(right));
|
|
51
|
+
if (pathApi === path.win32) {
|
|
52
|
+
return normalizedLeft.toLowerCase() === normalizedRight.toLowerCase();
|
|
53
|
+
}
|
|
54
|
+
return normalizedLeft === normalizedRight;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
module.exports = {
|
|
58
|
+
resolveDestinationRoot,
|
|
59
|
+
resolveLocations,
|
|
60
|
+
samePath,
|
|
61
|
+
};
|