shell.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // ShellJS
  3. // Unix shell commands on top of Node's API
  4. //
  5. // Copyright (c) 2012 Artur Adib
  6. // http://github.com/shelljs/shelljs
  7. //
  8. var common = require('./src/common');
  9. //@
  10. //@ All commands run synchronously, unless otherwise stated.
  11. //@ All commands accept standard bash globbing characters (`*`, `?`, etc.),
  12. //@ compatible with the [node `glob` module](https://github.com/isaacs/node-glob).
  13. //@
  14. //@ For less-commonly used commands and features, please check out our [wiki
  15. //@ page](https://github.com/shelljs/shelljs/wiki).
  16. //@
  17. // Include the docs for all the default commands
  18. //@commands
  19. // Load all default commands
  20. require('./commands').forEach(function (command) {
  21. require('./src/' + command);
  22. });
  23. //@
  24. //@ ### exit(code)
  25. //@
  26. //@ Exits the current process with the given exit `code`.
  27. exports.exit = process.exit;
  28. //@include ./src/error
  29. exports.error = require('./src/error');
  30. //@include ./src/common
  31. exports.ShellString = common.ShellString;
  32. //@
  33. //@ ### env['VAR_NAME']
  34. //@
  35. //@ Object containing environment variables (both getter and setter). Shortcut
  36. //@ to `process.env`.
  37. exports.env = process.env;
  38. //@
  39. //@ ### Pipes
  40. //@
  41. //@ Examples:
  42. //@
  43. //@ ```javascript
  44. //@ grep('foo', 'file1.txt', 'file2.txt').sed(/o/g, 'a').to('output.txt');
  45. //@ echo('files with o\'s in the name:\n' + ls().grep('o'));
  46. //@ cat('test.js').exec('node'); // pipe to exec() call
  47. //@ ```
  48. //@
  49. //@ Commands can send their output to another command in a pipe-like fashion.
  50. //@ `sed`, `grep`, `cat`, `exec`, `to`, and `toEnd` can appear on the right-hand
  51. //@ side of a pipe. Pipes can be chained.
  52. //@
  53. //@ ## Configuration
  54. //@
  55. exports.config = common.config;
  56. //@
  57. //@ ### config.silent
  58. //@
  59. //@ Example:
  60. //@
  61. //@ ```javascript
  62. //@ var sh = require('shelljs');
  63. //@ var silentState = sh.config.silent; // save old silent state
  64. //@ sh.config.silent = true;
  65. //@ /* ... */
  66. //@ sh.config.silent = silentState; // restore old silent state
  67. //@ ```
  68. //@
  69. //@ Suppresses all command output if `true`, except for `echo()` calls.
  70. //@ Default is `false`.
  71. //@
  72. //@ ### config.fatal
  73. //@
  74. //@ Example:
  75. //@
  76. //@ ```javascript
  77. //@ require('shelljs/global');
  78. //@ config.fatal = true; // or set('-e');
  79. //@ cp('this_file_does_not_exist', '/dev/null'); // throws Error here
  80. //@ /* more commands... */
  81. //@ ```
  82. //@
  83. //@ If `true`, the script will throw a Javascript error when any shell.js
  84. //@ command encounters an error. Default is `false`. This is analogous to
  85. //@ Bash's `set -e`.
  86. //@
  87. //@ ### config.verbose
  88. //@
  89. //@ Example:
  90. //@
  91. //@ ```javascript
  92. //@ config.verbose = true; // or set('-v');
  93. //@ cd('dir/');
  94. //@ rm('-rf', 'foo.txt', 'bar.txt');
  95. //@ exec('echo hello');
  96. //@ ```
  97. //@
  98. //@ Will print each command as follows:
  99. //@
  100. //@ ```
  101. //@ cd dir/
  102. //@ rm -rf foo.txt bar.txt
  103. //@ exec echo hello
  104. //@ ```
  105. //@
  106. //@ ### config.globOptions
  107. //@
  108. //@ Example:
  109. //@
  110. //@ ```javascript
  111. //@ config.globOptions = {nodir: true};
  112. //@ ```
  113. //@
  114. //@ Use this value for calls to `glob.sync()` instead of the default options.
  115. //@
  116. //@ ### config.reset()
  117. //@
  118. //@ Example:
  119. //@
  120. //@ ```javascript
  121. //@ var shell = require('shelljs');
  122. //@ // Make changes to shell.config, and do stuff...
  123. //@ /* ... */
  124. //@ shell.config.reset(); // reset to original state
  125. //@ // Do more stuff, but with original settings
  126. //@ /* ... */
  127. //@ ```
  128. //@
  129. //@ Reset `shell.config` to the defaults:
  130. //@
  131. //@ ```javascript
  132. //@ {
  133. //@ fatal: false,
  134. //@ globOptions: {},
  135. //@ maxdepth: 255,
  136. //@ noglob: false,
  137. //@ silent: false,
  138. //@ verbose: false,
  139. //@ }
  140. //@ ```