pi-extmgr 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 +22 -0
- package/package.json +1 -1
- package/src/packages/extensions.ts +19 -11
- package/src/utils/npm-exec.ts +180 -3
- package/src/utils/ui-helpers.ts +10 -4
package/README.md
CHANGED
|
@@ -20,6 +20,28 @@ If Pi is already running, use `/reload`.
|
|
|
20
20
|
|
|
21
21
|
Requires Node.js `>=22.20.0`.
|
|
22
22
|
|
|
23
|
+
### npm prefix permissions
|
|
24
|
+
|
|
25
|
+
Pi global package installs use `npm install -g`. If `npm_config_prefix` points at an
|
|
26
|
+
unwritable prefix such as `/usr/local`, install or reload can fail with `EACCES`.
|
|
27
|
+
Use a writable npm prefix, configure Pi's `npmCommand` setting for your Node
|
|
28
|
+
version manager, or install project-local with `pi install npm:pi-extmgr -l`.
|
|
29
|
+
|
|
30
|
+
`npmCommand` examples for `~/.pi/agent/settings.json`:
|
|
31
|
+
|
|
32
|
+
```jsonc
|
|
33
|
+
{
|
|
34
|
+
// mise: run npm through Node 22
|
|
35
|
+
// "npmCommand": ["mise", "exec", "node@22", "--", "npm"]
|
|
36
|
+
|
|
37
|
+
// bun: use Bun as Pi's package-manager command
|
|
38
|
+
// "npmCommand": ["bun"]
|
|
39
|
+
|
|
40
|
+
// nvm: start Pi from an nvm-enabled shell and use npm on PATH
|
|
41
|
+
// "npmCommand": ["npm"]
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
23
45
|
## Features
|
|
24
46
|
|
|
25
47
|
- **Unified manager UI**
|
package/package.json
CHANGED
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
normalizeRelativePath,
|
|
20
20
|
resolveRelativePathSelection,
|
|
21
21
|
} from "../utils/relative-path-selection.js";
|
|
22
|
-
import {
|
|
22
|
+
import { resolveConfiguredNpmRootCommand } from "../utils/npm-exec.js";
|
|
23
23
|
|
|
24
24
|
interface PackageSettingsObject {
|
|
25
25
|
source: string;
|
|
@@ -39,7 +39,7 @@ export interface PackageManifest {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
const execFileAsync = promisify(execFile);
|
|
42
|
-
let globalNpmRootCache: string | null | undefined;
|
|
42
|
+
let globalNpmRootCache: { key: string; root: string | null } | undefined;
|
|
43
43
|
|
|
44
44
|
function normalizeSource(source: string): string {
|
|
45
45
|
return source
|
|
@@ -58,24 +58,32 @@ function normalizePackageRootCandidate(candidate: string): string {
|
|
|
58
58
|
return resolved;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
async function getGlobalNpmRoot(): Promise<string | undefined> {
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
async function getGlobalNpmRoot(cwd: string): Promise<string | undefined> {
|
|
62
|
+
let npmCommand: ReturnType<typeof resolveConfiguredNpmRootCommand>;
|
|
63
|
+
try {
|
|
64
|
+
npmCommand = resolveConfiguredNpmRootCommand(cwd);
|
|
65
|
+
} catch {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const cacheKey = [npmCommand.command, ...npmCommand.args].join("\0");
|
|
70
|
+
|
|
71
|
+
if (globalNpmRootCache?.key === cacheKey) {
|
|
72
|
+
return globalNpmRootCache.root ?? undefined;
|
|
64
73
|
}
|
|
65
74
|
|
|
66
75
|
try {
|
|
67
|
-
const npmCommand = resolveNpmCommand(["root", "-g"]);
|
|
68
76
|
const { stdout } = await execFileAsync(npmCommand.command, npmCommand.args, {
|
|
69
77
|
timeout: 2_000,
|
|
70
78
|
windowsHide: true,
|
|
71
79
|
});
|
|
72
|
-
const root =
|
|
73
|
-
globalNpmRootCache = root || null;
|
|
80
|
+
const root = npmCommand.getRoot(stdout);
|
|
81
|
+
globalNpmRootCache = { key: cacheKey, root: root || null };
|
|
74
82
|
} catch {
|
|
75
|
-
globalNpmRootCache = null;
|
|
83
|
+
globalNpmRootCache = { key: cacheKey, root: null };
|
|
76
84
|
}
|
|
77
85
|
|
|
78
|
-
return globalNpmRootCache ?? undefined;
|
|
86
|
+
return globalNpmRootCache.root ?? undefined;
|
|
79
87
|
}
|
|
80
88
|
|
|
81
89
|
async function resolveNpmPackageRoot(
|
|
@@ -96,7 +104,7 @@ async function resolveNpmPackageRoot(
|
|
|
96
104
|
const packageDir = process.env.PI_PACKAGE_DIR || join(homedir(), ".pi", "agent");
|
|
97
105
|
const globalCandidates = [join(packageDir, "npm", "node_modules", packageName)];
|
|
98
106
|
|
|
99
|
-
const npmGlobalRoot = await getGlobalNpmRoot();
|
|
107
|
+
const npmGlobalRoot = await getGlobalNpmRoot(cwd);
|
|
100
108
|
if (npmGlobalRoot) {
|
|
101
109
|
globalCandidates.unshift(join(npmGlobalRoot, packageName));
|
|
102
110
|
}
|
package/src/utils/npm-exec.ts
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
1
3
|
import path from "node:path";
|
|
2
4
|
import { execPath, platform } from "node:process";
|
|
3
|
-
import { type ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
5
|
+
import { type ExtensionAPI, getAgentDir, SettingsManager } from "@mariozechner/pi-coding-agent";
|
|
4
6
|
|
|
5
7
|
interface NpmCommandResolutionOptions {
|
|
6
8
|
platform?: NodeJS.Platform;
|
|
7
9
|
nodeExecPath?: string;
|
|
10
|
+
npmCommand?: readonly string[] | undefined;
|
|
11
|
+
cwd?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface ResolvedNpmCommand {
|
|
15
|
+
command: string;
|
|
16
|
+
args: string[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface ResolvedNpmRootCommand extends ResolvedNpmCommand {
|
|
20
|
+
getRoot(stdout: string): string;
|
|
8
21
|
}
|
|
9
22
|
|
|
10
23
|
interface NpmExecOptions {
|
|
@@ -12,15 +25,148 @@ interface NpmExecOptions {
|
|
|
12
25
|
signal?: AbortSignal;
|
|
13
26
|
}
|
|
14
27
|
|
|
28
|
+
const settingsManagersByPath = new Map<string, SettingsManager>();
|
|
29
|
+
let warnedAboutBunGlobalDirHeuristic = false;
|
|
30
|
+
|
|
15
31
|
function getNpmCliPath(nodeExecPath: string, runtimePlatform: NodeJS.Platform): string {
|
|
16
32
|
const pathImpl = runtimePlatform === "win32" ? path.win32 : path;
|
|
17
33
|
return pathImpl.join(pathImpl.dirname(nodeExecPath), "node_modules", "npm", "bin", "npm-cli.js");
|
|
18
34
|
}
|
|
19
35
|
|
|
36
|
+
function getConfiguredNpmBase(
|
|
37
|
+
npmCommand?: readonly string[] | undefined
|
|
38
|
+
): ResolvedNpmCommand | undefined {
|
|
39
|
+
if (!npmCommand || npmCommand.length === 0) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const [command, ...args] = npmCommand;
|
|
44
|
+
if (!command?.trim()) {
|
|
45
|
+
throw new Error("Invalid npmCommand: first array entry must be a non-empty command");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return { command, args: [...args] };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getSettingsNpmCommand(cwd: string): string[] | undefined {
|
|
52
|
+
const agentDir = getAgentDir();
|
|
53
|
+
const cacheKey = `${agentDir}\0${cwd}`;
|
|
54
|
+
const cached = settingsManagersByPath.get(cacheKey);
|
|
55
|
+
if (cached) {
|
|
56
|
+
return cached.getNpmCommand();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const settingsManager = SettingsManager.create(cwd, agentDir);
|
|
60
|
+
settingsManagersByPath.set(cacheKey, settingsManager);
|
|
61
|
+
return settingsManager.getNpmCommand();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function getCommandName(command: string): string {
|
|
65
|
+
return (command.split(/[\\/]/).pop() ?? "").replace(/\.(cmd|exe)$/i, "");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function expandHome(input: string): string {
|
|
69
|
+
if (input === "~") return homedir();
|
|
70
|
+
if (input.startsWith("~/")) return path.join(homedir(), input.slice(2));
|
|
71
|
+
return input;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function resolveBunConfigPath(value: string, baseDir: string): string {
|
|
75
|
+
const expanded = expandHome(value.trim());
|
|
76
|
+
return path.isAbsolute(expanded) ? expanded : path.resolve(baseDir, expanded);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function stripTomlComment(line: string): string {
|
|
80
|
+
return line.replace(/\s+#.*$/, "").trim();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function readBunGlobalDirFromBunfig(configPath: string): string | undefined {
|
|
84
|
+
if (!existsSync(configPath)) return undefined;
|
|
85
|
+
|
|
86
|
+
let content: string;
|
|
87
|
+
try {
|
|
88
|
+
content = readFileSync(configPath, "utf8");
|
|
89
|
+
} catch {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let inInstallSection = false;
|
|
94
|
+
for (const rawLine of content.split(/\r?\n/)) {
|
|
95
|
+
const line = stripTomlComment(rawLine);
|
|
96
|
+
if (!line) continue;
|
|
97
|
+
|
|
98
|
+
const sectionMatch = line.match(/^\[([^\]]+)]$/);
|
|
99
|
+
if (sectionMatch) {
|
|
100
|
+
inInstallSection = sectionMatch[1]?.trim() === "install";
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const keyMatch = line.match(/^(install\.)?globalDir\s*=\s*(["'])(.*?)\2/);
|
|
105
|
+
if (!keyMatch) continue;
|
|
106
|
+
if (!inInstallSection && !keyMatch[1]) continue;
|
|
107
|
+
|
|
108
|
+
const value = keyMatch[3]?.trim();
|
|
109
|
+
return value ? resolveBunConfigPath(value, path.dirname(configPath)) : undefined;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function getBunGlobalDir(cwd?: string): string | undefined {
|
|
116
|
+
const envGlobalDir = process.env.BUN_INSTALL_GLOBAL_DIR?.trim();
|
|
117
|
+
if (envGlobalDir) {
|
|
118
|
+
return resolveBunConfigPath(envGlobalDir, process.cwd());
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const candidates = [
|
|
122
|
+
path.join(homedir(), ".bunfig.toml"),
|
|
123
|
+
...(process.env.XDG_CONFIG_HOME
|
|
124
|
+
? [path.join(process.env.XDG_CONFIG_HOME, ".bunfig.toml")]
|
|
125
|
+
: []),
|
|
126
|
+
...(cwd ? [path.join(cwd, "bunfig.toml")] : []),
|
|
127
|
+
];
|
|
128
|
+
|
|
129
|
+
let globalDir: string | undefined;
|
|
130
|
+
for (const configPath of candidates) {
|
|
131
|
+
globalDir = readBunGlobalDirFromBunfig(configPath) ?? globalDir;
|
|
132
|
+
}
|
|
133
|
+
return globalDir;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function warnAboutBunGlobalDirHeuristic(): void {
|
|
137
|
+
if (warnedAboutBunGlobalDirHeuristic) return;
|
|
138
|
+
warnedAboutBunGlobalDirHeuristic = true;
|
|
139
|
+
console.warn(
|
|
140
|
+
"[extmgr] Could not read Bun globalDir from BUN_INSTALL_GLOBAL_DIR or bunfig.toml; " +
|
|
141
|
+
"guessing from `bun pm bin -g`. If Bun's globalDir is customized, set BUN_INSTALL_GLOBAL_DIR."
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function getBunNodeModulesRoot(globalBinDir: string, cwd?: string): string {
|
|
146
|
+
const globalDir = getBunGlobalDir(cwd);
|
|
147
|
+
if (globalDir) {
|
|
148
|
+
return path.join(globalDir, "node_modules");
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Best-effort fallback for Bun's default layout: globalBinDir is usually
|
|
152
|
+
// ~/.bun/bin and globalDir is usually ~/.bun/install/global. This may be
|
|
153
|
+
// wrong when [install].globalDir is customized in bunfig.toml.
|
|
154
|
+
warnAboutBunGlobalDirHeuristic();
|
|
155
|
+
return path.join(path.dirname(globalBinDir), "install", "global", "node_modules");
|
|
156
|
+
}
|
|
157
|
+
|
|
20
158
|
export function resolveNpmCommand(
|
|
21
159
|
npmArgs: string[],
|
|
22
160
|
options?: NpmCommandResolutionOptions
|
|
23
|
-
):
|
|
161
|
+
): ResolvedNpmCommand {
|
|
162
|
+
const configured = getConfiguredNpmBase(options?.npmCommand);
|
|
163
|
+
if (configured) {
|
|
164
|
+
return {
|
|
165
|
+
command: configured.command,
|
|
166
|
+
args: [...configured.args, ...npmArgs],
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
24
170
|
const runtimePlatform = options?.platform ?? platform;
|
|
25
171
|
|
|
26
172
|
if (runtimePlatform === "win32") {
|
|
@@ -34,13 +180,44 @@ export function resolveNpmCommand(
|
|
|
34
180
|
return { command: "npm", args: npmArgs };
|
|
35
181
|
}
|
|
36
182
|
|
|
183
|
+
export function resolveConfiguredNpmCommand(npmArgs: string[], cwd: string): ResolvedNpmCommand {
|
|
184
|
+
return resolveNpmCommand(npmArgs, { npmCommand: getSettingsNpmCommand(cwd) });
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function resolveNpmRootCommand(
|
|
188
|
+
options?: NpmCommandResolutionOptions
|
|
189
|
+
): ResolvedNpmRootCommand {
|
|
190
|
+
const configured = getConfiguredNpmBase(options?.npmCommand);
|
|
191
|
+
|
|
192
|
+
if (configured && getCommandName(configured.command) === "bun") {
|
|
193
|
+
return {
|
|
194
|
+
command: configured.command,
|
|
195
|
+
args: [...configured.args, "pm", "bin", "-g"],
|
|
196
|
+
getRoot: (stdout) => {
|
|
197
|
+
const binDir = stdout.trim();
|
|
198
|
+
return binDir ? getBunNodeModulesRoot(binDir, options?.cwd) : "";
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const resolved = resolveNpmCommand(["root", "-g"], options);
|
|
204
|
+
return {
|
|
205
|
+
...resolved,
|
|
206
|
+
getRoot: (stdout) => stdout.trim(),
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function resolveConfiguredNpmRootCommand(cwd: string): ResolvedNpmRootCommand {
|
|
211
|
+
return resolveNpmRootCommand({ npmCommand: getSettingsNpmCommand(cwd), cwd });
|
|
212
|
+
}
|
|
213
|
+
|
|
37
214
|
export async function execNpm(
|
|
38
215
|
pi: ExtensionAPI,
|
|
39
216
|
npmArgs: string[],
|
|
40
217
|
ctx: { cwd: string },
|
|
41
218
|
options: NpmExecOptions
|
|
42
219
|
): Promise<{ code: number; stdout: string; stderr: string; killed: boolean }> {
|
|
43
|
-
const resolved =
|
|
220
|
+
const resolved = resolveConfiguredNpmCommand(npmArgs, ctx.cwd);
|
|
44
221
|
return pi.exec(resolved.command, resolved.args, {
|
|
45
222
|
timeout: options.timeout,
|
|
46
223
|
cwd: ctx.cwd,
|
package/src/utils/ui-helpers.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { type ExtensionCommandContext } from "@mariozechner/pi-coding-agent";
|
|
5
5
|
import { UI } from "../constants.js";
|
|
6
|
-
import { notify } from "./notify.js";
|
|
6
|
+
import { error as notifyError, notify } from "./notify.js";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Confirm and trigger reload
|
|
@@ -20,12 +20,18 @@ export async function confirmReload(
|
|
|
20
20
|
|
|
21
21
|
const confirmed = await ctx.ui.confirm("Reload Required", `${reason}\nReload pi now?`);
|
|
22
22
|
|
|
23
|
-
if (confirmed) {
|
|
23
|
+
if (!confirmed) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
try {
|
|
24
28
|
await ctx.reload();
|
|
25
29
|
return true;
|
|
30
|
+
} catch (error) {
|
|
31
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
32
|
+
notifyError(ctx, `Reload failed: ${message}`);
|
|
33
|
+
return false;
|
|
26
34
|
}
|
|
27
|
-
|
|
28
|
-
return false;
|
|
29
35
|
}
|
|
30
36
|
|
|
31
37
|
/**
|