Last active
January 1, 2024 08:19
-
-
Save sanjarcode/fc912ddae3d563fdb7da1e1e06344860 to your computer and use it in GitHub Desktop.
Node.js stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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