microspawn
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Node Version License

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 fancy

  • script(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 named data. 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);
});

Package Sidebar

Install

npm i microspawn

Weekly Downloads

0

Version

1.0.1

License

GPL-3.0

Unpacked Size

52.7 kB

Total Files

12

Last publish

Collaborators

  • crafter999