Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

README.md 3.2 KiB

3 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. ## Public API
  55. ### `execSh(command, [options], [callback])`
  56. Execute shell command forwarding all stdio.
  57. **Parameters:**
  58. - `command {String|Array}` - The command to run, or array of commands
  59. - `[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
  60. - `[callback] {Function}` - `callback(err, stdout, stderr)`
  61. - `err {Error|NULL}` - Error object. Has `code` property containing last command exit code when available
  62. - `stdout {String|NULL}` - aggregated stdout or `NULL` if not available
  63. - `stderr {String|NULL}` - aggregated stderr or `NULL` if not available
  64. **Return Values:**
  65. Returns [ChildProcess](http://nodejs.org/api/child_process.html#child_process_class_childprocess) object.
  66. ## Private API
  67. 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).
  68. ## Code Coverage
  69. 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).
  70. ## Scripts
  71. - `npm test` - run tests
  72. - `npm run jsdoc` - build jsdoc
  73. - `npm run dev` - run tests continuously
  74. ## License
  75. The MIT License (MIT)