web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.6 KiB

  1. # exec-sh
  2. [![NPM](https://nodei.co/npm/exec-sh.png)](https://nodei.co/npm/exec-sh/)
  3. [![NPM Downloads](https://img.shields.io/npm/dm/exec-sh.svg)](https://www.npmjs.com/package/exec-sh)
  4. [![Build Status](https://travis-ci.org/tsertkov/exec-sh.svg?branch=master)](https://travis-ci.org/tsertkov/exec-sh)
  5. [![Coverage Status](https://img.shields.io/coveralls/tsertkov/exec-sh.svg)](https://coveralls.io/r/tsertkov/exec-sh?branch=master)
  6. [![David Status](https://david-dm.org/tsertkov/exec-sh.png)](https://david-dm.org/tsertkov/exec-sh)
  7. > Execute shell command forwarding all stdio streams.
  8. ## Features
  9. exec-sh is a wrapper for [`child_process.spawn`](http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) with some improvements:
  10. - Cross platform command execution:
  11. - Windows: `cmd /C COMMAND`
  12. - others: `sh -c COMMAND`
  13. - Fowrards all stdio streams to current terminal (by default):
  14. - `execSh("bash")`
  15. - `execsh("echo -n Say: && read i && echo Said:$i")`
  16. - stdout and stderr are passed to callback when available
  17. - `execSh("pwd", console.log)`
  18. ## Showcase
  19. ```javascript
  20. // JavaScript
  21. execSh("echo hello exec-sh && bash", { cwd: "/home" }, function(err){
  22. if (err) {
  23. console.log("Exit code: ", err.code);
  24. }
  25. });
  26. ```
  27. ```sh
  28. # Terminal output: interactive bash session
  29. hello exec-sh
  30. bash-3.2$ pwd
  31. /home
  32. bash-3.2$ exit 99
  33. exit
  34. Exit code: 99
  35. ```
  36. ## Usage
  37. ```javascript
  38. var execSh = require("../");
  39. // run interactive bash shell
  40. execSh("echo lorem && bash", { cwd: "/home" }, function(err){
  41. if (err) {
  42. console.log("Exit code: ", err.code);
  43. return;
  44. }
  45. // collect streams output
  46. var child = execSh(["bash -c id", "echo lorem >&2"], true,
  47. function(err, stdout, stderr){
  48. console.log("error: ", err);
  49. console.log("stdout: ", stdout);
  50. console.log("stderr: ", stderr);
  51. });
  52. });
  53. ```
  54. ## Promise Interface
  55. ```javascript
  56. var execShPromise = require("exec-sh").promise;
  57. // run interactive bash shell
  58. const run = async () => {
  59. let out;
  60. try {
  61. out = await execShPromise('pwd', true);
  62. } catch (e) {
  63. console.log('Error: ', e);
  64. console.log('Stderr: ', e.stderr);
  65. console.log('Stdout: ', e.stdout);
  66. return e;
  67. }
  68. console.log('out: ', out.stdout, out.stderr);
  69. }
  70. run();
  71. ```
  72. ## Public API
  73. ### `execSh(command, [options], [callback])`
  74. Execute shell command forwarding all stdio.
  75. **Parameters:**
  76. - `command {String|Array}` - The command to run, or array of commands
  77. - `[options] {Object|TRUE}` - Options object passed directly to [`child_process.spawn`](http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options), when `TRUE` then `{ stdio: null }` used
  78. - `[callback] {Function}` - `callback(err, stdout, stderr)`
  79. - `err {Error|NULL}` - Error object. Has `code` property containing last command exit code when available
  80. - `stdout {String|NULL}` - aggregated stdout or `NULL` if not available
  81. - `stderr {String|NULL}` - aggregated stderr or `NULL` if not available
  82. **Return Values:**
  83. Returns [ChildProcess](http://nodejs.org/api/child_process.html#child_process_class_childprocess) object.
  84. ## Private API
  85. Complete API Documentation including private and public methods is generated from source code by JSDoc tool and is [available here](https://s3.eu-central-1.amazonaws.com/tsertkov-artifacts/exec-sh/master/jsdoc/index.html).
  86. ## Code Coverage
  87. Code coverage report for all files is [available here](https://s3.eu-central-1.amazonaws.com/tsertkov-artifacts/exec-sh/master/coverage/lcov-report/index.html).
  88. ## Scripts
  89. - `npm test` - run tests
  90. - `npm run jsdoc` - build jsdoc
  91. - `npm run dev` - run tests continuously
  92. ## License
  93. The MIT License (MIT)