Skip to content

Commit 4a26c65

Browse files
committed
update
1 parent 9a4d53f commit 4a26c65

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

bin/completions/completion-producers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const packageJsonScriptCompletion = async (
1313
);
1414
};
1515

16-
// provides completions for package dependencies from package.json.. `pnpm add <dependency>`
16+
// provides completions for package dependencies from package.json.. for commands like remove `pnpm remove <dependency>`
1717
export const packageJsonDependencyCompletion = async (
1818
complete: Complete
1919
): Promise<void> => {

bin/handlers/pnpm-handler.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
import { execFile, execFileSync } from 'child_process';
2-
import { promisify } from 'util';
1+
import { promisify } from 'node:util';
2+
import child_process from 'node:child_process';
3+
4+
const exec = promisify(child_process.exec);
5+
const { execSync } = child_process;
36
import type { PackageManagerCompletion } from '../package-manager-completion.js';
7+
import { Command } from '../../src/t.js';
8+
9+
interface LazyCommand extends Command {
10+
_lazyCommand?: string;
11+
_optionsLoaded?: boolean;
12+
optionsRaw?: Map<string, any>;
13+
}
14+
415
import {
516
packageJsonScriptCompletion,
617
packageJsonDependencyCompletion,
@@ -15,7 +26,8 @@ import {
1526
type ParsedOption,
1627
} from '../utils/text-utils.js';
1728

18-
const execFileP = promisify(execFile);
29+
// regex to detect options section in help text
30+
const OPTIONS_SECTION_RE = /^\s*Options:/i;
1931

2032
// we parse the pnpm help text to extract commands and their descriptions!
2133
export function parsePnpmHelp(helpText: string): Record<string, string> {
@@ -49,7 +61,7 @@ export function parsePnpmHelp(helpText: string): Record<string, string> {
4961
};
5062

5163
for (const line of helpLines) {
52-
if (/^\s*Options:/i.test(line)) break; // we stop at options
64+
if (OPTIONS_SECTION_RE.test(line)) break; // we stop at options
5365

5466
// we match the command row
5567
const rowMatch = line.match(COMMAND_ROW_RE);
@@ -81,7 +93,7 @@ export async function getPnpmCommandsFromMainHelp(): Promise<
8193
Record<string, string>
8294
> {
8395
try {
84-
const { stdout } = await execFileP('pnpm', ['--help'], {
96+
const { stdout } = await exec('pnpm --help', {
8597
encoding: 'utf8',
8698
timeout: 500,
8799
maxBuffer: 4 * 1024 * 1024,
@@ -157,9 +169,12 @@ export function parsePnpmOptions(
157169
}
158170

159171
// we load the dynamic options synchronously when requested ( separated from the command loading )
160-
export function loadDynamicOptionsSync(cmd: any, command: string): void {
172+
export function loadDynamicOptionsSync(
173+
cmd: LazyCommand,
174+
command: string
175+
): void {
161176
try {
162-
const stdout = execFileSync('pnpm', [command, '--help'], {
177+
const stdout = execSync(`pnpm ${command} --help`, {
163178
encoding: 'utf8',
164179
timeout: 500,
165180
});
@@ -174,7 +189,8 @@ export function loadDynamicOptionsSync(cmd: any, command: string): void {
174189
}
175190

176191
// we setup the lazy option loading for a command
177-
function setupLazyOptionLoading(cmd: any, command: string): void {
192+
193+
function setupLazyOptionLoading(cmd: LazyCommand, command: string): void {
178194
cmd._lazyCommand = command;
179195
cmd._optionsLoaded = false;
180196

0 commit comments

Comments
 (0)