I have written a script that will execute several Angular CLI commands (i.e. ng build
or ng test
) asynchronously. The purpose of this is so we can execute these functions on different angular projects at the same time.
This works, but the part that doesn't is the output from these commands does not get logged to the console.
Here's essentially what I'm doing (this code is executed by NodeJS):
const exec = require('child_process').exec;
async function execute(cmd) {
return new Promise(function(resolve, reject) {
exec(cmd, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve({ stdout, stderr });
}
});
});
}
function executeCommandAsync(cmd) {
const projects = ['proj1', 'proj2']; // arbitrary list, we get it in a different way
projects.forEach(project => {
console.log(`Executing command '${cmd}' on project ${project}...`);
execute(`${cmd} ${project}`)
.then(() =>
console.log(
`Successfully executed command '${cmd}' on project '${project}'!`
)
)
.catch(err => {
console.log(
`Error occurred executing '${cmd}' command on project '${project}'`
);
console.log(`${err}`);
});
});
}
So in the above example, I can pass in a command to execute on all projects, i.e. 'ng build'. The result in the console is something like this:
Executing command 'ng build' on project 'proj1'...
Executing command 'ng build' on project 'proj2'...
Successfully executed command 'ng build' on project 'proj1'!
Successfully executed command 'ng build' on project 'proj2'!
What's missing here is the output from the ng build
commands. Is there a way that I can log the output from these commands to the console as they are running, or can you only log output after the promises have been resolved? I haven't gotten anything past logging the promise itself out to the console.
My only idea is that the commands are being executed in a separate context from the current console, so the output isn't logged, but I'm not sure how to get around that.