Skip to content

Instantly share code, notes, and snippets.

@sanjarcode
Last active January 1, 2024 08:19
Show Gist options
  • Save sanjarcode/fc912ddae3d563fdb7da1e1e06344860 to your computer and use it in GitHub Desktop.
Save sanjarcode/fc912ddae3d563fdb7da1e1e06344860 to your computer and use it in GitHub Desktop.
Node.js stuff
const util = require("util");
const promisifiedExec = util.promisify(require("child_process").exec);
async function runCommand(command) {
let retVal;
try {
const { stdout, stderr } = await promisifiedExec(command);
retVal = [stderr || null, stdout];
} catch (e) {
retVal = [{ code: e.code, message: e.message }, e.stdout];
}
return retVal;
}
// Examples
runCommand.ExampleSuccess = async () => {
const [error, output] = await runCommand("ls");
console.log('ExampleSuccess: "ls", result:');
console.log({ error, output }, "\n");
return [error, output];
};
runCommand.ExampleFailure = async () => {
const [error, output] = await runCommand("lsx");
console.log('ExampleFailure: "lsx", result:');
console.log({ error, output }, "\n");
return [error, output];
};
if (require.main === module) {
runCommand.ExampleSuccess();
runCommand.ExampleFailure();
}
module.exports = runCommand;
import util from "util";
import { exec } from "child_process";
const promisifiedExec = util.promisify(exec);
async function runCommand(command) {
let retVal;
try {
const { stdout, stderr } = await promisifiedExec(command);
retVal = [stderr || null, stdout];
} catch (e) {
retVal = [{ code: e.code, message: e.message }, e.stdout];
}
return retVal;
}
// Examples
runCommand.ExampleSuccess = async () => {
const [error, output] = await runCommand("ls");
console.log('ExampleSuccess: "ls", result:');
console.log({ error, output }, "\n");
return [error, output];
};
runCommand.ExampleFailure = async () => {
const [error, output] = await runCommand("lsx");
console.log('ExampleFailure: "lsx", result:');
console.log({ error, output }, "\n");
return [error, output];
};
if (import.meta.url === `file://${process.argv[1]}`) {
runCommand.ExampleSuccess();
runCommand.ExampleFailure();
}
export default runCommand;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment