I am finding a way to run shell command in javascript in a synchronous-like way.
The best I currently found is spawnSync
but it returns unexpected empty string while the command is correct in shell.
const foo = spawnSync('git', [
'log',
'--diff-filter=A',
'--format="%cI"',
'--',
`path/to/a/file/in/repo`,
])
.stdout.toString() // -> ''
the command runs well in shell however
$ git log --diff-filter=A --format="%cI" -- "path/to/a/file/in/repo"
2023-12-07T22:11:50+08:00
You are calling wrong method, stout
is a Readable Stream. You will need to add a listener to data
event which emits output data from the child process.
const foo = spawnSync('git', [
'log',
'--diff-filter=A',
'--format="%cI"',
'--',
'path/to/a/file/in/repo'
])
foo.stdout.on("data", (data) => {
console.log(data);
});
foo.on("close", (code) => {
console.log(`child process close all stdio with code ${code}`);
});
foo.on("exit", (code) => {
console.log(`child process exited with code ${code}`);
});