@icoretech/warden-mcp 0.1.19 → 0.1.21
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/bin/patch-bitwarden-cli-lib.js +117 -0
- package/bin/patch-bitwarden-cli.js +3 -27
- package/dist/bw/bwSession.js +13 -6
- package/package.json +1 -1
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { dirname, isAbsolute, relative, resolve } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
|
|
7
|
+
const require = createRequire(import.meta.url);
|
|
8
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
const defaultPackageDir = resolve(__dirname, '..');
|
|
10
|
+
|
|
11
|
+
function isModuleNotFoundFor(specifier, error) {
|
|
12
|
+
if (!(error instanceof Error)) return false;
|
|
13
|
+
if ('code' in error && error.code !== 'MODULE_NOT_FOUND') return false;
|
|
14
|
+
return error.message.includes(specifier);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function findInstallRoot(packageJsonPath) {
|
|
18
|
+
let currentDir = dirname(packageJsonPath);
|
|
19
|
+
|
|
20
|
+
while (true) {
|
|
21
|
+
const parentDir = dirname(currentDir);
|
|
22
|
+
if (parentDir === currentDir) break;
|
|
23
|
+
if (
|
|
24
|
+
currentDir.endsWith('/node_modules') ||
|
|
25
|
+
currentDir.endsWith('\\node_modules')
|
|
26
|
+
) {
|
|
27
|
+
return parentDir;
|
|
28
|
+
}
|
|
29
|
+
currentDir = parentDir;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
throw new Error(
|
|
33
|
+
`[warden-mcp] could not determine install root from ${packageJsonPath}`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function resolvePatchPackagePlan({
|
|
38
|
+
packageDir = defaultPackageDir,
|
|
39
|
+
resolveDependency = (specifier) => require.resolve(specifier),
|
|
40
|
+
exists = existsSync,
|
|
41
|
+
} = {}) {
|
|
42
|
+
const patchesDir = resolve(packageDir, 'patches');
|
|
43
|
+
if (!exists(patchesDir)) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
`[warden-mcp] patches directory not found at ${patchesDir}`,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let cliPackageJsonPath;
|
|
50
|
+
try {
|
|
51
|
+
cliPackageJsonPath = resolveDependency('@bitwarden/cli/package.json');
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (isModuleNotFoundFor('@bitwarden/cli/package.json', error)) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const installRoot = findInstallRoot(cliPackageJsonPath);
|
|
60
|
+
const patchDir = relative(installRoot, patchesDir) || '.';
|
|
61
|
+
if (isAbsolute(patchDir) || patchDir.startsWith('..')) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`[warden-mcp] patch directory ${patchesDir} is outside install root ${installRoot}`,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
cwd: installRoot,
|
|
69
|
+
args: [
|
|
70
|
+
resolveDependency('patch-package/dist/index.js'),
|
|
71
|
+
'--patch-dir',
|
|
72
|
+
patchDir,
|
|
73
|
+
'--error-on-fail',
|
|
74
|
+
],
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function applyBundledBwPatch({
|
|
79
|
+
packageDir = defaultPackageDir,
|
|
80
|
+
resolveDependency,
|
|
81
|
+
exists,
|
|
82
|
+
spawn = spawnSync,
|
|
83
|
+
nodeExecPath = process.execPath,
|
|
84
|
+
logError = (message) => console.error(message),
|
|
85
|
+
} = {}) {
|
|
86
|
+
let plan;
|
|
87
|
+
try {
|
|
88
|
+
plan = resolvePatchPackagePlan({ packageDir, resolveDependency, exists });
|
|
89
|
+
} catch (error) {
|
|
90
|
+
const message =
|
|
91
|
+
error instanceof Error
|
|
92
|
+
? error.message
|
|
93
|
+
: '[warden-mcp] failed to prepare patch-package';
|
|
94
|
+
logError(message);
|
|
95
|
+
return 1;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!plan) return 0;
|
|
99
|
+
|
|
100
|
+
const result = spawn(nodeExecPath, plan.args, {
|
|
101
|
+
cwd: plan.cwd,
|
|
102
|
+
stdio: 'inherit',
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
if (result.error) {
|
|
106
|
+
logError(
|
|
107
|
+
`[warden-mcp] failed to execute patch-package: ${result.error.message}`,
|
|
108
|
+
);
|
|
109
|
+
return 1;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return result.status ?? 1;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function isDirectRun(importMetaUrl, argv = process.argv) {
|
|
116
|
+
return argv[1] === fileURLToPath(importMetaUrl);
|
|
117
|
+
}
|
|
@@ -1,31 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import { existsSync } from 'node:fs';
|
|
5
|
-
import { createRequire } from 'node:module';
|
|
6
|
-
import { dirname, resolve } from 'node:path';
|
|
7
|
-
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import { applyBundledBwPatch, isDirectRun } from './patch-bitwarden-cli-lib.js';
|
|
8
4
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const rootDir = resolve(__dirname, '..');
|
|
12
|
-
const cliPackageDir = resolve(__dirname, '../node_modules/@bitwarden/cli');
|
|
13
|
-
|
|
14
|
-
if (!existsSync(cliPackageDir)) {
|
|
15
|
-
process.exit(0);
|
|
5
|
+
if (isDirectRun(import.meta.url)) {
|
|
6
|
+
process.exit(applyBundledBwPatch());
|
|
16
7
|
}
|
|
17
|
-
|
|
18
|
-
const patchPackageEntrypoint = require.resolve('patch-package/dist/index.js');
|
|
19
|
-
const result = spawnSync(process.execPath, [patchPackageEntrypoint], {
|
|
20
|
-
cwd: rootDir,
|
|
21
|
-
stdio: 'inherit',
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
if (result.error) {
|
|
25
|
-
console.error(
|
|
26
|
-
`[warden-mcp] failed to execute patch-package: ${result.error.message}`,
|
|
27
|
-
);
|
|
28
|
-
process.exit(1);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
process.exit(result.status ?? 1);
|
package/dist/bw/bwSession.js
CHANGED
|
@@ -156,12 +156,19 @@ export class BwSessionManager {
|
|
|
156
156
|
this.configuredHost = null;
|
|
157
157
|
await runBw(['logout'], { env: this.baseEnv(), timeoutMs: 30_000 }).catch(() => { });
|
|
158
158
|
const home = this.homeDir;
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
159
|
+
const cliStateDirs = [
|
|
160
|
+
join(home, '.config', 'Bitwarden CLI'),
|
|
161
|
+
join(home, 'Library', 'Application Support', 'Bitwarden CLI'),
|
|
162
|
+
join(home, 'AppData', 'Roaming', 'Bitwarden CLI'),
|
|
163
|
+
];
|
|
164
|
+
for (const dir of cliStateDirs) {
|
|
165
|
+
await rm(join(dir, 'data.json'), {
|
|
166
|
+
force: true,
|
|
167
|
+
}).catch(() => { });
|
|
168
|
+
await rm(join(dir, 'config.json'), {
|
|
169
|
+
force: true,
|
|
170
|
+
}).catch(() => { });
|
|
171
|
+
}
|
|
165
172
|
}
|
|
166
173
|
async ensureUnlockedInternal() {
|
|
167
174
|
// Ensure server config points to BW_HOST.
|