Work on child processes with node.js

This commit is contained in:
Scott Idem
2022-10-27 12:56:19 -04:00
parent cdae86dd86
commit c37c87f6d4

View File

@@ -868,19 +868,112 @@ exports.run_osascript = async function ({cmd=null, interactive=false, language=n
// Run raw command
// Updated 2022-05-07
exports.run_cmd = async function ({cmd=null}) {
exports.run_cmd = async function ({cmd=null, return_stdout=null, return_stdin=null, sync=null}) {
console.log('*** Electron framework export: run_cmd() ***');
console.log(`Command String: ${cmd}`);
child_process.exec(cmd, (err, stdout, stdin) => {
if (err) throw err;
console.log(stdout);
console.log(stdin);
});
let result;
console.log('Finished');
return true;
if (!sync) {
result = child_process.exec(cmd, (err, stdout, stdin) => {
// if (err) throw err;
if (err) {
console.log('Error:', err);
return false;
};
console.log('stdout:', stdout);
// console.log('stdin:', stdin);
if (return_stdout) {
console.log('Finished and returning stdout');
return stdout;
} else {
console.log('Finished and returning true');
return true;
}
});
} else {
result = child_process.execSync(cmd, (err, stdout, stdin) => {
// if (err) throw err;
if (err) {
console.log('Error:', err);
return false;
};
console.log('stdout:', stdout);
// console.log('stdin:', stdin);
if (return_stdout) {
console.log('Finished and returning stdout');
return stdout;
} else {
console.log('Finished and returning true');
return true;
}
});
}
console.log('Result:', result);
return result;
}
// Run raw command sync
// Updated 2022-05-07
exports.run_cmd_sync = function ({cmd=null, return_stdout=null, return_stdin=null}) {
console.log('*** Electron framework export: run_cmd() ***');
console.log(`Command String: ${cmd}`);
let stdout;
try {
stdout = child_process.execSync(cmd, {encoding: 'utf8'});
console.log('Std Out:', stdout);
} catch (err) {
console.error('Error:', err);
return false;
}
if (return_stdout) {
console.log('Finished and returning stdout');
return stdout;
} else {
console.log('Finished and returning true');
return true;
}
// let result;
// let stdout;
// let stderr;
// try {
// let { stdout, stderr } = child_process.execSync(cmd, (err, stdout, stdin) => {
// // if (err) throw err;
// if (err) {
// console.log('Error:', err);
// return false;
// };
// console.log('stdout:', stdout);
// // console.log('stdin:', stdin);
// if (return_stdout) {
// console.log('Finished and returning stdout');
// return stdout;
// } else {
// console.log('Finished and returning true');
// return true;
// }
// });
// } catch (err) {
// console.error(err);
// return false;
// }
// console.log('Result:', result);
// return result;
}