Skip to content

Commit

Permalink
determine shell for .cmd, .bat, .ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
gcampbell-msft committed Sep 5, 2024
1 parent f35be38 commit f245098
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/proc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export interface ExecutionResult {

export interface ExecutionOptions {
environment?: Environment;
shell?: boolean;
shell?: boolean | string;
silent?: boolean;
cwd?: string;
encoding?: BufferEncoding;
Expand All @@ -113,6 +113,18 @@ export function buildCmdStr(command: string, args?: string[]): string {
return cmdarr.map(a => /[ \n\r\f;\t]/.test(a) ? `"${a}"` : a).join(' ');
}

export function determineShell(command: string): string | boolean {
if (command.endsWith('.cmd') || command.endsWith('.bat')) {
return 'cmd';
}

if (command.endsWith('.ps1')) {
return 'powershell';
}

return false;
}

/**
* Execute a command and return the result
* @param command The binary to execute
Expand Down Expand Up @@ -146,9 +158,10 @@ export function execute(command: string, args?: string[], outputConsumer?: Outpu
log.debug(localize('execution.environment', ' with environment: {0}', JSON.stringify(final_env)));
}
}

const spawn_opts: proc.SpawnOptions = {
env: final_env,
shell: !!options.shell
shell: options.shell ?? determineShell(command)
};
if (options?.cwd !== undefined) {
util.createDirIfNotExistsSync(options.cwd);
Expand Down

0 comments on commit f245098

Please sign in to comment.