@bobfrankston/npmglobalize 1.0.155 → 1.0.156
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/lib.d.ts +9 -0
- package/lib.js +38 -4
- package/package.json +1 -1
package/lib.d.ts
CHANGED
|
@@ -260,6 +260,15 @@ export declare function runCommand(cmd: string, args: string[], options?: {
|
|
|
260
260
|
output: string;
|
|
261
261
|
stderr: string;
|
|
262
262
|
};
|
|
263
|
+
/** Install a package globally in WSL, auto-fixing the root-owned /usr/local/lib/node_modules
|
|
264
|
+
* EACCES case by switching npm to a user prefix (~/.npm-global) and retrying once.
|
|
265
|
+
* Output is captured (so we can scan for the error) and then mirrored to the terminal. */
|
|
266
|
+
export declare function installInWsl(wslArgs: string[], opts?: {
|
|
267
|
+
cwd?: string;
|
|
268
|
+
}): {
|
|
269
|
+
success: boolean;
|
|
270
|
+
fixed: boolean;
|
|
271
|
+
};
|
|
263
272
|
/** Run a command and throw on failure */
|
|
264
273
|
export declare function runCommandOrThrow(cmd: string, args: string[], options?: {
|
|
265
274
|
silent?: boolean;
|
package/lib.js
CHANGED
|
@@ -1420,6 +1420,40 @@ export function runCommand(cmd, args, options = {}) {
|
|
|
1420
1420
|
return { success: false, output: '', stderr: error.message };
|
|
1421
1421
|
}
|
|
1422
1422
|
}
|
|
1423
|
+
/** Install a package globally in WSL, auto-fixing the root-owned /usr/local/lib/node_modules
|
|
1424
|
+
* EACCES case by switching npm to a user prefix (~/.npm-global) and retrying once.
|
|
1425
|
+
* Output is captured (so we can scan for the error) and then mirrored to the terminal. */
|
|
1426
|
+
export function installInWsl(wslArgs, opts = {}) {
|
|
1427
|
+
console.log(colors.cyan(`> wsl ${wslArgs.join(' ')}`));
|
|
1428
|
+
let result = runCommand('wsl', wslArgs, { cwd: opts.cwd, silent: true });
|
|
1429
|
+
if (result.output)
|
|
1430
|
+
process.stdout.write(result.output);
|
|
1431
|
+
if (result.stderr)
|
|
1432
|
+
process.stderr.write(result.stderr);
|
|
1433
|
+
if (result.success)
|
|
1434
|
+
return { success: true, fixed: false };
|
|
1435
|
+
const combined = (result.output || '') + '\n' + (result.stderr || '');
|
|
1436
|
+
const permIssue = /EACCES/.test(combined) && /\/usr\/(?:local\/)?lib\/node_modules/.test(combined);
|
|
1437
|
+
if (!permIssue)
|
|
1438
|
+
return { success: false, fixed: false };
|
|
1439
|
+
console.log(colors.yellow('Detected root-owned npm prefix in WSL — switching to ~/.npm-global and retrying...'));
|
|
1440
|
+
const fix = `set -e; npm config set prefix "$HOME/.npm-global"; mkdir -p "$HOME/.npm-global/bin"; if ! grep -q '\\.npm-global/bin' "$HOME/.bashrc" 2>/dev/null; then printf '\\n# npm user-prefix bin (added by npmglobalize)\\nexport PATH="$HOME/.npm-global/bin:$PATH"\\n' >> "$HOME/.bashrc"; fi`;
|
|
1441
|
+
const fixResult = runCommand('wsl', ['bash', '-lc', fix], { silent: true });
|
|
1442
|
+
if (!fixResult.success) {
|
|
1443
|
+
console.error(colors.red('Failed to apply WSL npm prefix fix:'));
|
|
1444
|
+
if (fixResult.stderr)
|
|
1445
|
+
process.stderr.write(fixResult.stderr);
|
|
1446
|
+
return { success: false, fixed: false };
|
|
1447
|
+
}
|
|
1448
|
+
console.log(colors.green('✓ WSL npm prefix set to ~/.npm-global; PATH appended to ~/.bashrc'));
|
|
1449
|
+
console.log(colors.cyan(`> wsl ${wslArgs.join(' ')}`));
|
|
1450
|
+
result = runCommand('wsl', wslArgs, { cwd: opts.cwd, silent: true });
|
|
1451
|
+
if (result.output)
|
|
1452
|
+
process.stdout.write(result.output);
|
|
1453
|
+
if (result.stderr)
|
|
1454
|
+
process.stderr.write(result.stderr);
|
|
1455
|
+
return { success: result.success, fixed: result.success };
|
|
1456
|
+
}
|
|
1423
1457
|
/** Diagnose common build/version failure patterns and print actionable hints.
|
|
1424
1458
|
* Returns true if a diagnosis was printed. */
|
|
1425
1459
|
function diagnoseBuildFailure(errorText, cwd) {
|
|
@@ -4672,10 +4706,10 @@ export async function globalize(cwd, options = {}, configOptions = {}) {
|
|
|
4672
4706
|
waitForNpmVersion(pkgName, pkgVersion);
|
|
4673
4707
|
console.log(`Installing in WSL${useLocalWsl ? ' (local)' : ' from registry'}: ${pkgName}@${pkgVersion}...`);
|
|
4674
4708
|
if (!dryRun) {
|
|
4675
|
-
const wslResult =
|
|
4709
|
+
const wslResult = installInWsl(wslArgs, { cwd });
|
|
4676
4710
|
if (wslResult.success) {
|
|
4677
4711
|
wslInstallOk = true;
|
|
4678
|
-
console.log(colors.green(`✓ Installed in WSL: ${pkgName}@${pkgVersion}`));
|
|
4712
|
+
console.log(colors.green(`✓ Installed in WSL: ${pkgName}@${pkgVersion}${wslResult.fixed ? ' (after prefix fix)' : ''}`));
|
|
4679
4713
|
}
|
|
4680
4714
|
else {
|
|
4681
4715
|
console.error(colors.yellow('✗ WSL install failed (is npm installed in WSL?)'));
|
|
@@ -5076,9 +5110,9 @@ export async function globalizeWorkspace(rootDir, options = {}, configOptions =
|
|
|
5076
5110
|
if (wsl) {
|
|
5077
5111
|
console.log(`Installing ${pkgName} in WSL (local)...`);
|
|
5078
5112
|
if (!dryRun) {
|
|
5079
|
-
const wslResult =
|
|
5113
|
+
const wslResult = installInWsl(['npm', 'install', '-g', '.'], { cwd: rootDir });
|
|
5080
5114
|
if (wslResult.success) {
|
|
5081
|
-
console.log(colors.green(`✓ Installed in WSL: ${pkgName}@${pkgVersion}`));
|
|
5115
|
+
console.log(colors.green(`✓ Installed in WSL: ${pkgName}@${pkgVersion}${wslResult.fixed ? ' (after prefix fix)' : ''}`));
|
|
5082
5116
|
}
|
|
5083
5117
|
else {
|
|
5084
5118
|
console.error(colors.yellow('✗ WSL install failed (is npm installed in WSL?)'));
|