Files
vscode-front-matter/src/utils/evaluateCommand.ts
2024-10-28 15:07:35 +01:00

31 lines
783 B
TypeScript

import { exec } from 'child_process';
import { getShellPath } from '../utils';
import { Logger } from '../helpers';
/**
* Evaluate the command dynamically using `which` command
* @param command
* @returns
*/
export const evaluateCommand = (command: string): Promise<string> => {
const shell = getShellPath();
let shellPath: string | undefined = undefined;
if (typeof shell !== 'string' && !!shell) {
shellPath = shell.path;
} else {
shellPath = shell || undefined;
}
return new Promise((resolve, reject) => {
exec(`which ${command}`, { shell: shellPath }, (error, stdout) => {
if (error) {
Logger.error(`Error evaluating command: ${command}`);
reject(error);
return;
}
resolve(stdout.trim());
});
});
};