Tod*_*odd 3 windows node.js git-bash
我在 Windows 10 中使用 GitBash,并希望在 child_process.exec 调用中执行 git 命令。我认为,由于我通过“Git For Windows”安装了 git,因此我只需将 shell 指定为 GitBash 可执行文件即可。我已经尝试了我能想到的 GitBash 可执行文件路径的所有变体,但总是失败。节点正在寻找的路径是什么?
无效路径示例
c:/program files/git/usr/bin/bash
c:/program\ files/git/usr/bin/bash
/c/program\ files/git/usr/bin/bash
c:\\program files\\git\\usr\\bin\\bash
const { expect } = require('chai');
const { exec } = require('child_process');
describe.only('exec', function(){
it('should work', function(done){
let shellPath = "c:\\program files\\git\\usr\\bin\\bash";
expect(exec(`cat <<< "abc"`, { shell: shellPath }, (err, stdout) => {
expect(err).to.be.null;
expect(stdout.trim()).to.be.equal("abc");
done();
}));
});
});
Run Code Online (Sandbox Code Playgroud)
第一个断言失败并显示:
expected [Error: Command failed: cat <<< "abc" << was unexpected at this time.] to be null
Run Code Online (Sandbox Code Playgroud)
这种方法存在一些问题。
正如参考所述,exec 自动使用 Windows 特定的 shell 参数,这些参数不适用于 Bash。
另一个问题是PATH可能没有设置为 GitBash 二进制文件路径。
这应该可行:
delete process.platform;
process.platform = 'linux';
exec(`cat <<< "abc"`, {
env: { PATH: 'C:\\Program Files\\git\\usr\\bin' },
shell: 'C:\\Program Files\\git\\usr\\bin\\bash.exe'
}, (err, stdout) => {
...
});
process.platform = 'win32';
Run Code Online (Sandbox Code Playgroud)
该解决方案的可行性可能取决于bash.exe实施。
git在 Node 中运行不需要使用自定义 shell ;这是由 Git 可执行文件处理的。
| 归档时间: |
|
| 查看次数: |
4491 次 |
| 最近记录: |