javascript - 如何在 TS 中运行命令?
问题描述
我试图从 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
当我这样做时,我得到了一个错误:
生成 C:\WINDOWS\system32\cmd。
当我删除 cwd 然后它工作但我需要使用“CWD”选项
解决方案
您所做的是尝试将路径设置为文字字符串:${path}
。
要使用字符串插值,您必须使用反引号而不是单引号,如下所示:
`${path}` // resolves to the path
但由于您没有对任何东西使用插值,您可以直接将路径放入函数中:
exec(command, {cwd: path}, callback);
您遇到的错误是因为路径${path}
无效,cmd 没有启动。
推荐阅读
- python-3.8 - 按数字计算数字在列表编号中重复的次数
- facebook - 是否允许在开发模式下使用从 Instagram 基本显示 API 应用程序检索到的数据用于生产目的?
- javascript - innerHTML 删除图像标签的右斜线
- machine-learning - 机器学习:如何指导模型识别不存在的数据集?
- r - 如何使用 R 提取唯一 ID 以及整个数据框?
- python - NoReverseMatch at / 'QuizApp' 不是在 'basic_app' 中注册的命名空间
- php - 在 WooCommerce 页面上显示创建和修改的产品日期
- ios - Xcode 12.4 未在运行 iOS14.4 的 iPhone 上安装应用程序
- unicode - 是否有一些标准编码可以将二进制数据编码为每个数据字节一个 UTF-8 可表示的 unicode 字符序列?
- javascript - Javascript 在不破坏应用程序的情况下创建异步函数