我尝试从 TS 运行命令
function executeCommand(command : string, callback: (error: ExecException, stdout : string, stderr : string) => void): void {
const path= path.join(vscode.workspace.rootPath,"products");
exec(command, {cwd: '${path}'}, callback);
}
const command = ({
'darwin': ``,
'linux': `cf env`,
'win32': `cf env`
} as any)[platform];
executeCommand(command, (error, stdout, stderr) => {
if (error) {
console.warn(error);
}
...
}
我想在不同的路径上运行命令cf env
,所以我尝试更改cwd
当我这样做时,我收到一个错误:
spawn C:\WINDOWS\system32\cmd.
当我删除 cwd 后它就起作用了,但我需要使用“CWD”选项
最佳答案
您所做的就是尝试将路径设置为文字字符串:${path}
。
要使用字符串插值,您必须使用反引号而不是单引号,如下所示:
`${path}` // resolves to the path
但是由于您没有使用插值进行任何操作,因此您可以将路径直接放入函数中:
exec(command, {cwd: path}, callback);
您遇到的错误是因为路径 ${path}
无效,cmd 无法启动。
关于javascript - 如何在TS中运行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57572814/