microspawn
v1.0.1
Published
Spawn some nasty processes on node.js
Downloads
15
Maintainers
Readme
Install
npm install microspawn
Usage
run(program: string, args: string[] | string, options?: SpawnOptions, stderr?: boolean): Promise
Returns a promise which contains the output.log(program: string, args: string[] | string, options?: SpawnOptions): void
Log the output, nothing fancyscript(scriptContents: string): Promise
Run a Linux script using/bin/sh
WARNING: argument should be the script contents, not the file location of a script.stream(program: string, args: string[], options?: SpawnOptions): Readable
Return an event nameddata
. Can be useful for listening for some data inside a shell.
Examples
Example #1 : Running a simple command with args
try {
let args = "-screenshot test.jpg ipinfo.io";
await microspawn.run("C:\\Program Files\\Mozilla Firefox\\firefox.exe", args)
} catch (e) {
console.error(e);
process.exit(1);
}
Example #2: Running a command inside a shell
await microspawn.log("powershell.exe", "dir",{shell: true, detached: true});
Example #3: Run inline script (shell arguments don't work)
(async()=> {
let arg = "hello";
let script = `test="${arg}"
if [ "$test" = "hello" ]; then
echo "world"
fi`;
let result = await ms.script(script).catch(e => {
console.error(e);
process.exit(1);
});
console.log(result);
})();
Example #4: Run command and start listening for data
let nodejsPath = process.platform === "linux" ? "/usr/bin/node" : "C:\\node.exe";
const nodeInception = "\"var i=0;setInterval(()=>{process.stdout.write(i.toString());i++},1000);\"";
const nodeInception2 = "\"for(let x=0;x<=999999;x++){console.log(x);}\"";
const nodeInception3 = "\"setTimeout(()=>{console.log('hello'+'world')},1000)\"";
let streamFromStd = microspawn.stream(nodejsPath,
`-e ${nodeInception2}`, {shell: true});
streamFromStd.on("data", (data) => {
// do something with the data
console.log(parseInt(data)*100);
});