mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-25 16:07:37 +00:00
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:
parent
760c191081
commit
ca045ea4c5
33
README.md
33
README.md
@ -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/
|
||||
```
|
||||
|
||||
23
shell.js
23
shell.js
@ -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/
|
||||
//@ ```
|
||||
|
||||
@ -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
49
src/set.js
Normal 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;
|
||||
@ -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
52
test/set.js
Normal 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);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user