Skip to content

Commit db0683b

Browse files
committed
ps1
1 parent 66faa7f commit db0683b

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

bin/package-manager-completion.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,43 @@ function runCompletionCommand(
2626
): string {
2727
const args = [...leadingArgs, 'complete', '--', ...completionArgs];
2828

29-
// On Windows, prefer invoking via powershell.exe so .ps1 shims work.
30-
if (process.platform === 'win32' && path.extname(command) === '') {
31-
const ps1Path = `${command}.ps1`;
29+
const result = spawnSync(command, args, completionSpawnOptions);
30+
31+
// Windows: npm may only produce a .ps1 shim; spawnSync won't resolve .ps1 via PATHEXT.
32+
// Fallback: invoke through PowerShell so .ps1 shims (e.g. nuxt.ps1) are discoverable.
33+
if (
34+
result.error &&
35+
(result.error as { code?: string }).code === 'ENOENT' &&
36+
process.platform === 'win32' &&
37+
path.extname(command) === ''
38+
) {
39+
const psArgs = args.map(powerShellQuote);
40+
const psCommand = `& ${command} ${psArgs.join(' ')}`.trimEnd();
41+
3242
const psResult = spawnSync(
3343
'powershell.exe',
3444
[
3545
'-NoLogo',
3646
'-NoProfile',
3747
'-ExecutionPolicy',
3848
'Bypass',
39-
'-File',
40-
ps1Path,
41-
...args,
49+
'-Command',
50+
psCommand,
4251
],
4352
completionSpawnOptions
4453
);
4554

46-
// If that fails, fall back to invoking the command directly.
47-
if (
48-
!psResult.error &&
49-
(typeof psResult.status !== 'number' || psResult.status === 0)
50-
) {
51-
return (psResult.stdout ?? '').trim();
55+
if (psResult.error) {
56+
throw psResult.error;
57+
}
58+
if (typeof psResult.status === 'number' && psResult.status !== 0) {
59+
throw new Error(
60+
`Completion command "${command}" (PowerShell fallback) exited with code ${psResult.status}`
61+
);
5262
}
63+
return (psResult.stdout ?? '').trim();
5364
}
5465

55-
const result = spawnSync(command, args, completionSpawnOptions);
56-
5766
if (result.error) {
5867
throw result.error;
5968
}
@@ -67,6 +76,11 @@ function runCompletionCommand(
6776
return (result.stdout ?? '').trim();
6877
}
6978

79+
function powerShellQuote(value: string): string {
80+
// Use single quotes and escape embedded single quotes by doubling them.
81+
return `'${value.replace(/'/g, "''")}'`;
82+
}
83+
7084
async function checkCliHasCompletions(
7185
cliName: string,
7286
packageManager: string

0 commit comments

Comments
 (0)