javascript - 如何在TS中运行命令?

标签 javascript node.js typescript

我尝试从 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/

相关文章:

javascript - 使用 Javascript 检测取消的表单帖子

javascript - 识别经过身份验证的用户的类型

node.js - 安装phonegap时出现错误

json - Node Elasticsearch - 批量索引不起作用 - 不支持 Content-Type header [application/x-ldjson]

typescript :属性在类型 'never' 上不存在,但功能有效吗?

javascript - Angular:Bootstrap 4 深色和浅色模式切换

php - Youtube channel 嵌入尺寸

javascript - (数字(值) !== NaN) 始终为真

node.js - 使用 typescript 和 Node js 从 Elasticsearch 中删除记录

javascript - 为什么dojo.number.format在不同的浏览器中返回不同的结果?