shell.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. //@ Exits the current process with the given exit code.
  26. exports.exit = process.exit;
  27. //@include ./src/error
  28. exports.error = require('./src/error');
  29. //@include ./src/common
  30. exports.ShellString = common.ShellString;
  31. //@
  32. //@ ### env['VAR_NAME']
  33. //@ Object containing environment variables (both getter and setter). Shortcut
  34. //@ to process.env.
  35. exports.env = process.env;
  36. //@
  37. //@ ### Pipes
  38. //@
  39. //@ Examples:
  40. //@
  41. //@ ```javascript
  42. //@ grep('foo', 'file1.txt', 'file2.txt').sed(/o/g, 'a').to('output.txt');
  43. //@ echo('files with o\'s in the name:\n' + ls().grep('o'));
  44. //@ cat('test.js').exec('node'); // pipe to exec() call
  45. //@ ```
  46. //@
  47. //@ Commands can send their output to another command in a pipe-like fashion.
  48. //@ `sed`, `grep`, `cat`, `exec`, `to`, and `toEnd` can appear on the right-hand
  49. //@ side of a pipe. Pipes can be chained.
  50. //@
  51. //@ ## Configuration
  52. //@
  53. exports.config = common.config;
  54. //@
  55. //@ ### config.silent
  56. //@
  57. //@ Example:
  58. //@
  59. //@ ```javascript
  60. //@ var sh = require('shelljs');
  61. //@ var silentState = sh.config.silent; // save old silent state
  62. //@ sh.config.silent = true;
  63. //@ /* ... */
  64. //@ sh.config.silent = silentState; // restore old silent state
  65. //@ ```
  66. //@
  67. //@ Suppresses all command output if `true`, except for `echo()` calls.
  68. //@ Default is `false`.
  69. //@
  70. //@ ### config.fatal
  71. //@
  72. //@ Example:
  73. //@
  74. //@ ```javascript
  75. //@ require('shelljs/global');
  76. //@ config.fatal = true; // or set('-e');
  77. //@ cp('this_file_does_not_exist', '/dev/null'); // throws Error here
  78. //@ /* more commands... */
  79. //@ ```
  80. //@
  81. //@ If `true` the script will throw a Javascript error when any shell.js
  82. //@ command encounters an error. Default is `false`. This is analogous to
  83. //@ Bash's `set -e`
  84. //@
  85. //@ ### config.verbose
  86. //@
  87. //@ Example:
  88. //@
  89. //@ ```javascript
  90. //@ config.verbose = true; // or set('-v');
  91. //@ cd('dir/');
  92. //@ rm('-rf', 'foo.txt', 'bar.txt');
  93. //@ exec('echo hello');
  94. //@ ```
  95. //@
  96. //@ Will print each command as follows:
  97. //@
  98. //@ ```
  99. //@ cd dir/
  100. //@ rm -rf foo.txt bar.txt
  101. //@ exec echo hello
  102. //@ ```
  103. //@
  104. //@ ### config.globOptions
  105. //@
  106. //@ Example:
  107. //@
  108. //@ ```javascript
  109. //@ config.globOptions = {nodir: true};
  110. //@ ```
  111. //@
  112. //@ Use this value for calls to `glob.sync()` instead of the default options.
  113. //@
  114. //@ ### config.reset()
  115. //@
  116. //@ Example:
  117. //@
  118. //@ ```javascript
  119. //@ var shell = require('shelljs');
  120. //@ // Make changes to shell.config, and do stuff...
  121. //@ /* ... */
  122. //@ shell.config.reset(); // reset to original state
  123. //@ // Do more stuff, but with original settings
  124. //@ /* ... */
  125. //@ ```
  126. //@
  127. //@ Reset shell.config to the defaults:
  128. //@
  129. //@ ```javascript
  130. //@ {
  131. //@ fatal: false,
  132. //@ globOptions: {},
  133. //@ maxdepth: 255,
  134. //@ noglob: false,
  135. //@ silent: false,
  136. //@ verbose: false,
  137. //@ }
  138. //@ ```