Unexpected result spawn of stdout - node.js

I try to spawn git process but i got unexpected result. stdout pipe empty. while the stderr pipe only clonning directory info. it should output cloning directory then progress of clonning.
const { spawn } = require('child_process');
const subprocess = spawn('git', ['clone', 'https://github.com/expressjs/express.git'], {
cwd: 'C:/Software Development/git/bin'
});
subprocess.stdout.on('data', data => {
console.log('Body info: \n')
console.log(data.toString())
});
subprocess.stderr.on('data', data => {
console.log('Error info: \n')
console.log(data.toString())
})

Related

How to make spawned process know it's not writing to a terminal (nodejs)

I'm writing a little wrapper to the lzma command in Linux but the program refuses to write to stdout because it believes to be in an interactive terminal:
lzma: Compressed data cannot be written to a terminal
This is my code in js
// Compress a given string using the lzma program, and return the compressed contents.
const { spawn } = require('child_process')
const compress = str => new Promise((resolve, reject) => {
const lzma = spawn('lzma', ["-z", "-c", "-9"],
{ stdio: [process.stdin, process.stdout, process.stderr] })
lzma.on('error', err => {
// console.log(`Failed to start lzma: ${err}`)
reject(err)
})
lzma.on('exit', (code, signal) => {
if (code) {
// console.log(`lzma process exited with code ${code}`)
reject(code)
}
else if (signal) {
// console.log(`lzma process exited with signal ${signal}`)
reject(signal)
}
else {
// console.log(`lzma process exited successfully`)
resolve(compressed)
}
})
lzma.stdin.write(str)
lzma.stdin.end()
let compressed = ''
lzma.stdout.on('data', data => { compressed += data })
})
module.exports = compress
How do I override this behavior?
Try changing line 9 to:
{ stdio: ["pipe", "pipe", "inherit"] })
lzma.stdin and lzma.stdout won't do anything unless they're connected to a pipe. "inherit" means that if lzma writes to stderr, you'll see it on the calling process's stderr.
See the node docs for more info.

Why command runs in terminal but not in extension calls?

If in a terminal I run git flow release publish '1.0.0' it is executed successfully.
If I use child_process.spawn in a test.js and then run it with node ./test.js file it also works fine.
But when I use child_process.spawn inside VS Code extension it produces error
fatal: could not read Username for 'https://github.com': No such device or address
If I edit .git/config and add my name https//$myname#github.com...... then error is
fatal: could not read Password for 'https://github.com': No such device or address.
Here is my code same in test file and VS Code
const spawn = require("child_process").spawn;
let cmd = 'git';
let args = ['flow', 'release', 'publish', '0.2.8'];
// let args = ['flow', 'release', 'delete', '-f', '-r', '0.2.8'];
const child = spawn(cmd, args);
let stdout = [];
let stderr = [];
child.stdout?.on('data', (data) => {
console.log(`[gitflow] Stdout ${data}`);
stdout.push(data.toString());
});
child.stderr?.on('data', (data) => {
console.log(`[gitflow] Stderr ${data}`);
stderr.push(data.toString());
});
child.on('error', err => {
console.log(`[gitflow] Error "${cmd} ${args?.join(" ")}" returned`, err);
});
child.on('spawm', err => {
console.log(`[gitflow] Spawn "${cmd} ${args?.join(" ")}" returned`, err);
});
child.on('exit', code => {
console.log(`[gitflow] Exit "${cmd} ${args?.join(" ")} exited`, code);
});
child.on('close', retc => {
console.log(`[gitflow] Command "${cmd}"`, retc, stderr, stdout);
});

Node.js child_process STDOUT not showing progressbar

I've a problem with displaying the progressbar of a process executed by child_process spawn in a discord bot.
CLI Output:
2021-09-16 16:11:03 Welcome to Minecraft Overviewer version 0.17.39 (2402e41)!
2021-09-16 16:11:03 Generating textures...
2021-09-16 16:12:52 Preprocessing...
2021-09-16 16:15:14 Rendering 228498 total tiles.
99% [===================================================================================================================================================================================================== ] 228494 17.65t/s eta 00h 00m 00s
2021-09-16 19:50:57 Rendering complete!
When using the child_process spawn I get every output except for the progressbar.
Node script:
const child_process = require('child_process');
const command = "python";
const arguments = ["./Minecraft-Overviewer/overviewer.py", "--config=./world.py"]
var child = child_process.spawn(command, arguments, {
encoding: 'utf8',
shell: true
});
child.on('error', (error) => {
message.channel.send('Error!\r\n' + error);
});
child.stdout.setEncoding('utf8');
child.stdout.on('data', (data) => {
data = data.toString();
message.channel.send(`STDOUT: ${data}`);
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
message.channel.send(`DATA: ${data}`);
});
child.on('close', (code) => {
//Here you can get the exit code of the script
switch (code) {
case 0:
message.channel.send('Process exited.');
break;
}
});
Any ideas on how I can catch the progressbar as well?
Many thanks in advance!

Nodejs listing directories with child_process.spawn ends before receiving all data

const spawn = require('child_process').spawn;
function main() {
ls = spawn('dir', {shell: true});
ls.stdout.on('data', data => console.log('DATA RECEIVED'));
ls.on('exit', code => console.log('EXITED'));
}
main();
This runs on Windows, and the result is
DATA RECEIVED
EXITED
DATA RECEIVED
Why is EXITED printed out before the last line? Is there any way safe to check whether all the data is received or not?

Node.js Child_process Spawn doesn't return realtime data on console in windows 7

My python scripts returns some text on console while execution is going on, but when executed via child_process.spawn in node.js, stdout.on doesn't give me real-time output on console, instead stdout.on is executed when spawn process is about to end.
var spawn = require("child_process").spawn
var command = ['esx_trace.py', '-w', '28796829', '-v', '8496', '-i', '10.1.1.38', '-u', 'root', '-p', 'whatever', '-t', '5'];
var child = spawn('python', command);
child.stdout.on("data", function (data) {
console.log("spawnSTDOUT:", data.toString())
})
child.stderr.on("data", function (data) {
console.log("spawnSTDERR:", data.toString())
})
child.on("exit", function (code) {
console.log("spawnEXIT:", code.toString())
})

Resources