问题:如何在
我正在尝试构建一个小模块,其中需要从
以下为我的代码:
node.js
中使用passphrase
运行git push
?我正在尝试构建一个小模块,其中需要从
node.js
运行git push
到远程repo
,但是当我用node.js exec
而不是终端时,会出现错误。以下为我的代码:
./command.ts
import * as util from 'util';
const exec = util.promisify(require('child_process').exec);
export default function command(command: string): Promise<string> {
return exec(command, {cwd: process.cwd()}).then((resp) => {
const data = resp.stdout.toString().replace(/[\n\r]/g, '');
return Promise.resolve(data);
});
}
./index.ts
import command from './command';
async function init() {
try {
await command('git add .');
await command('git commit -m "my commit" ');
conat result = await command('git push');
} catch (e) {
console.log(e);
}
}
init();
当我运行ts-node ./index.ts
时,出现以下错误。
Error: Command failed: git push
git@hostname.org: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
但是,当我在终端运行 git push
时,会提示输入密码并且可以工作。
有什么办法解决这个问题吗?是否有一种方法可以使用 node.js
运行带有密码的 git push
命令?
请注意,我希望在不使用任何外部库的情况下解决此问题。
先行致谢。
git push https://username:password@myrepository.biz/file.git --all
。 - Jiby Jose