Created
May 16, 2023 12:01
-
-
Save jaalorsa517/58f8cc0a6f1448d21b4ef2143440527c to your computer and use it in GitHub Desktop.
Este script es para poder ejecutar comandos consola desde JS
This file contains hidden or 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
/** | |
* Script para ejecutar comando en consola a través de nodeJS | |
* | |
* Para windows, el path se respeta como nativo de windows. Ejemplo: C:\foldTest | |
* | |
* La función spawn, en windows funciona spawn("cmd.exe",myCommand). | |
* Por ejemplo, spawn("cmd.exe","dir") | |
* | |
*/ | |
const PATH = "/home/user"; | |
const commands = [ | |
{ | |
command: "pwd", | |
args:[], | |
isActive: true | |
}, | |
]; | |
const { spawn } = require("child_process"); | |
function ejecutarComandosEnSerie(commands) { | |
const [command] = commands; | |
process.chdir(`${PATH}`); | |
let proceso | |
if (command.isActive) { | |
proceso = spawn(command.command, command.args, {}); | |
} | |
else { | |
proceso = spawn("echo 'next command...'") | |
} | |
proceso.stdout.on("data", (data) => { | |
console.log(data.toString()); | |
}); | |
proceso.stderr.on("data", (data) => { | |
console.error(`stderr: ${data}`); | |
}); | |
proceso.on("close", () => { | |
if (commands.length > 1) { | |
ejecutarComandosEnSerie(commands.slice(1)); | |
} | |
}); | |
} | |
ejecutarComandosEnSerie(commands); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment