feat(set): add new set() command

Add new set() command as a wrapper for `config` variables. This takes the `-e`,
`-v`, `+e`, and `+v` flags.
This commit is contained in:
Nate Fischer 2016-01-31 18:56:01 -08:00
parent 760c191081
commit ca045ea4c5
6 changed files with 163 additions and 5 deletions

View File

@ -563,6 +563,21 @@ A FILE argument that does not exist is created empty, unless -c is supplied.
This is a partial implementation of *[touch(1)](http://linux.die.net/man/1/touch)*.
### set(options)
Available options:
+ `+/-e`: exit upon error (`config.fatal`)
+ `+/-v`: verbose: show all commands (`config.verbose`)
Examples:
```javascript
set('-e'); // exit upon first error
set('+e'); // this undoes a "set('-e')"
```
Sets global configuration variables
## Non-Unix commands
@ -606,10 +621,26 @@ Example:
```javascript
require('shelljs/global');
config.fatal = true;
config.fatal = true; // or set('-e');
cp('this_file_does_not_exist', '/dev/null'); // dies here
/* more commands... */
```
If `true` the script will die on errors. Default is `false`. This is
analogous to Bash's `set -e`
### config.verbose
Example:
```javascript
config.verbose = true; // or set('-v');
cd('dir/');
ls('subdir/');
```
Will print each command as follows:
```
cd dir/
ls subdir/
```

View File

@ -111,6 +111,10 @@ exports.chmod = common.wrap('chmod', _chmod);
var _touch = require('./src/touch');
exports.touch = common.wrap('touch', _touch);
//@include ./src/set
var _set = require('./src/set');
exports.set = common.wrap('set', _set);
//@
//@ ## Non-Unix commands
@ -154,10 +158,27 @@ exports.config = common.config;
//@
//@ ```javascript
//@ require('shelljs/global');
//@ config.fatal = true;
//@ config.fatal = true; // or set('-e');
//@ cp('this_file_does_not_exist', '/dev/null'); // dies here
//@ /* more commands... */
//@ ```
//@
//@ If `true` the script will die on errors. Default is `false`. This is
//@ analogous to Bash's `set -e`
//@
//@ ### config.verbose
//@ Example:
//@
//@ ```javascript
//@ config.verbose = true; // or set('-v');
//@ cd('dir/');
//@ ls('subdir/');
//@ ```
//@
//@ Will print each command as follows:
//@
//@ ```
//@ cd dir/
//@ ls subdir/
//@ ```

View File

@ -5,7 +5,8 @@ var _ls = require('./ls');
// Module globals
var config = {
silent: false,
fatal: false
fatal: false,
verbose: false,
};
exports.config = config;
@ -195,6 +196,12 @@ function wrap(cmd, fn, options) {
try {
var args = [].slice.call(arguments, 0);
if (config.verbose) {
args.unshift(cmd);
console.log.apply(console, args);
args.shift();
}
if (options && options.notUnix) {
retValue = fn.apply(this, args);
} else {

49
src/set.js Normal file
View File

@ -0,0 +1,49 @@
var common = require('./common');
//@
//@ ### set(options)
//@ Available options:
//@
//@ + `+/-e`: exit upon error (`config.fatal`)
//@ + `+/-v`: verbose: show all commands (`config.verbose`)
//@
//@ Examples:
//@
//@ ```javascript
//@ set('-e'); // exit upon first error
//@ set('+e'); // this undoes a "set('-e')"
//@ ```
//@
//@ Sets global configuration variables
function _set(options) {
if (!options) {
var args = [].slice.call(arguments, 0);
if (args.length < 2)
common.error('must provide an argument');
options = args[1];
}
var negate = (options[0] === '+');
if (negate) {
options = '-' + options.slice(1); // parseOptions needs a '-' prefix
}
options = common.parseOptions(options, {
'e': 'fatal',
'v': 'verbose'
});
var key;
if (negate) {
for (key in options)
options[key] = !options[key];
}
for (key in options) {
// Only change the global config if `negate` is false and the option is true
// or if `negate` is true and the option is false (aka negate !== option)
if (negate !== options[key]) {
common.config[key] = options[key];
}
}
return;
}
module.exports = _set;

View File

@ -22,8 +22,6 @@ var fs = require('fs');
//@ Update the access and modification times of each FILE to the current time.
//@ A FILE argument that does not exist is created empty, unless -c is supplied.
//@ This is a partial implementation of *[touch(1)](http://linux.die.net/man/1/touch)*.
//@
//
function _touch(opts, files) {
opts = common.parseOptions(opts, {
'a': 'atime_only',

52
test/set.js Normal file
View File

@ -0,0 +1,52 @@
var shell = require('..');
var assert = require('assert');
var oldConfigSilent = shell.config.silent;
shell.config.silent = true;
shell.rm('-rf', 'tmp');
shell.mkdir('tmp');
//
// Valids
//
// initial values
assert.strictEqual(oldConfigSilent, false);
assert.strictEqual(shell.config.verbose, false);
assert.strictEqual(shell.config.fatal, false);
// default behavior
var result = shell.exec('node -e \"require(\'../global\'); ls(\'file_doesnt_exist\'); echo(1234);\"');
assert.equal(result.code, 0);
assert.equal(result.stdout, '1234\n');
assert.equal(result.stderr, 'ls: no such file or directory: file_doesnt_exist\n');
// set -e
var result = shell.exec('node -e \"require(\'../global\'); set(\'-e\'); ls(\'file_doesnt_exist\'); echo(1234);\"');
assert.equal(result.code, 1);
assert.equal(result.stdout, '');
assert.equal(result.stderr, 'ls: no such file or directory: file_doesnt_exist\n');
// set -v
var result = shell.exec('node -e \"require(\'../global\'); set(\'-v\'); ls(\'file_doesnt_exist\'); echo(1234);\"');
assert.equal(result.code, 0);
assert.equal(result.stdout, 'ls file_doesnt_exist\n1234\n');
assert.equal(result.stderr, 'ls: no such file or directory: file_doesnt_exist\n');
// set -ev
var result = shell.exec('node -e \"require(\'../global\'); set(\'-ev\'); ls(\'file_doesnt_exist\'); echo(1234);\"');
assert.equal(result.code, 1);
assert.equal(result.stdout, 'ls file_doesnt_exist\n');
assert.equal(result.stderr, 'ls: no such file or directory: file_doesnt_exist\n');
// set -e, set +e
var result = shell.exec('node -e \"require(\'../global\'); set(\'-e\'); set(\'+e\'); ls(\'file_doesnt_exist\'); echo(1234);\"');
assert.equal(result.code, 0);
assert.equal(result.stdout, '1234\n');
assert.equal(result.stderr, 'ls: no such file or directory: file_doesnt_exist\n');
shell.exit(123);