runsync

2020-01-16

runsync

Polyfill of spawnSync and execSync for Node-0.10.x (Unix only yet)

runsync

Polyfill of spawnSync and execSync for Node-0.10.x (Unix only yet)

Build StatusCoverage Status

NPM

Instllation

Requires [node-gyp] (https://github.com/TooTallNate/node-gyp)

$ npm install runsync

Usage

var runsync = require("runsync");
var result = runsync.spawn("echo", ["Hello", "World!"], { encoding: "utf8" });
console.log(result.stdout); // => Hello world!\n

runsync.exec("sleep 1");

result = runsync.popen("echo Error message 1>&2", { encoding: "utf8" });
console.log(result.stderr); // => Error message\n

API

runsync.spawn(executable, [args], [options])

var res = runsync.spawn("node", ["-e", "console.log('Hello, World!')"], { encoding: "utf8" });
console.log(res.stdout) // => 'Hello, World!\n'

runsync.exec(command, [options])

var output = runsync.exec("sleep 3 && echo Hello!", { timeout: 1000 });
// => throw Exception because of timeout

runsync.execFile(command, [options])

var html = runsync.execFile("curl", ["--silent", "-X", "GET", "http://example.com"]);
console.log(html.toString()); // => '<!doctype html>\n<html>\n<head>\n ...'

runsync.popen(command, [options])

  • This is similar to runsync.exec, but it returns spawn objectlikes runsync.spawn.
  • This method will not throw Exceptions even if command fails.
var result = runsync.popen("echo `cat` && echo strerr 1>&2", { input: "stdin", encoding: "utf8" });
console.log(result.stdout) // => "stdin\n"
console.log(result.stderr) // => "stderr\n"

runsync.shell(command, [options])

  • This is similar to runsync.exec, but always set 'inherit'to options.stdio.
  • Returns Nothing(undefined).
  • This method will throw Exceptions if command fails.
try {
  runsync.shell("mocha --reporter nyan");
  // 31  -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_,------,
  // 1   -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_|   /\_/\ 
  // 0   -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-^|__( x .x) 
  //     -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-  ""  "" 
  //  31 passing (468ms)
  //  1 failing

} catch(err) {
  console.log(err.message);
  // => 'Command failed: `mocha -u tdd --reporter nyan`'
}

以上是 runsync 的使用教程帮助文档。


上一篇:node.svn
下一篇:@jkhong/cli-js
相关教程
关注微信

扫码加入 JavaScript 社区

相关文章
暂无相关文章

首次访问,需要验证
微信扫码,关注即可
(仅需验证一次)

欢迎加入 JavaScript 社区

号内回复关键字:

回到顶部