Skip to content

Commit

Permalink
refactor: glob by default for commands (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfischer authored Jul 27, 2016
1 parent 06ae7d9 commit d411176
Show file tree
Hide file tree
Showing 29 changed files with 50 additions and 52 deletions.
2 changes: 0 additions & 2 deletions src/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ var common = require('./common');
var fs = require('fs');

common.register('cat', _cat, {
globStart: 1,
canReceivePipe: true,
wrapOutput: true,
});

//@
Expand Down
5 changes: 1 addition & 4 deletions src/cd.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
var fs = require('fs');
var common = require('./common');

common.register('cd', _cd, {
globStart: 1,
wrapOutput: true,
});
common.register('cd', _cd, {});

//@
//@ ### cd([dir])
Expand Down
2 changes: 0 additions & 2 deletions src/chmod.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ var PERMS = (function (base) {
});

common.register('chmod', _chmod, {
globStart: 1,
wrapOutput: true,
});

//@
Expand Down
17 changes: 13 additions & 4 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ function ShellString(stdout, stderr, code) {
}
that.stderr = stderr;
that.code = code;

// A list of all commands that can appear on the right-hand side of a pipe
// (populated by calls to common.wrap())
pipeMethods.forEach(function (cmd) {
Expand Down Expand Up @@ -313,7 +312,7 @@ function wrap(cmd, fn, options) {

// Perform glob-expansion on all arguments after globStart, but preserve
// the arguments before it (like regexes for sed and grep)
if (!config.noglob && typeof options.globStart === 'number') {
if (!config.noglob && options.allowGlobbing === true) {
args = args.slice(0, options.globStart).concat(expand(args.slice(options.globStart)));
}

Expand Down Expand Up @@ -359,11 +358,21 @@ function _readFromPipe(that) {
}
exports.readFromPipe = _readFromPipe;

var DEFAULT_WRAP_OPTIONS = {
allowGlobbing: true,
canReceivePipe: false,
cmdOptions: false,
globStart: 1,
pipeOnly: false,
unix: true,
wrapOutput: true,
};

// Register a new ShellJS command
function _register(name, implementation, wrapOptions) {
wrapOptions = wrapOptions || {};
if (wrapOptions.pipeOnly && wrapOptions.canReceivePipe === false)
throw new Error('pipeOnly (true) conflicts with canReceivePipe (false)');
// If an option isn't specified, use the default
wrapOptions = objectAssign({}, DEFAULT_WRAP_OPTIONS, wrapOptions);
if (wrapOptions.pipeOnly) {
wrapOptions.canReceivePipe = true;
shellMethods[name] = wrap(name, implementation, wrapOptions);
Expand Down
6 changes: 4 additions & 2 deletions src/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ var path = require('path');
var common = require('./common');
var os = require('os');

common.register('cp', _cp, {globStart: 1, cmdOptions: {
common.register('cp', _cp, {
cmdOptions: {
'f': '!no_force',
'n': 'no_force',
'R': 'recursive',
'r': 'recursive',
'L': 'followsymlink',
'P': 'noFollowsymlink',
}
},
wrapOutput: false,
});

// Buffered file copy, synchronous
Expand Down
12 changes: 9 additions & 3 deletions src/dirs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ var common = require('./common');
var _cd = require('./cd');
var path = require('path');

common.register('dirs', _dirs, {globStart: 1});
common.register('pushd', _pushd, {globStart: 1});
common.register('popd', _popd, {globStart: 1});
common.register('dirs', _dirs, {
wrapOutput: false,
});
common.register('pushd', _pushd, {
wrapOutput: false,
});
common.register('popd', _popd, {
wrapOutput: false,
});

// Pushd/popd/dirs internals
var _dirStack = [];
Expand Down
4 changes: 3 additions & 1 deletion src/echo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var common = require('./common');

common.register('echo', _echo, {wrapOutput: true});
common.register('echo', _echo, {
allowGlobbing: false,
});

//@
//@ ### echo(string [, string ...])
Expand Down
1 change: 1 addition & 0 deletions src/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var DEFAULT_MAXBUFFER_SIZE = 20*1024*1024;
common.register('exec', _exec, {
unix: false,
canReceivePipe: true,
wrapOutput: false,
});

// Hack to run child_process.exec() synchronously (sync avoids callback hell)
Expand Down
5 changes: 1 addition & 4 deletions src/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ var path = require('path');
var common = require('./common');
var _ls = require('./ls');

common.register('find', _find, {
globStart: 1,
wrapOutput: true,
});
common.register('find', _find, {});

//@
//@ ### find(path [, path ...])
Expand Down
1 change: 0 additions & 1 deletion src/grep.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ common.register('grep', _grep, {
'v': 'inverse',
'l': 'nameOnly',
},
wrapOutput: true,
});

//@
Expand Down
2 changes: 0 additions & 2 deletions src/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ var common = require('./common');
var fs = require('fs');

common.register('head', _head, {
globStart: 1,
canReceivePipe: true,
cmdOptions: {
'n': 'numLines',
},
wrapOutput: true,
});

// This reads n or more lines, or the entire file, whichever is less.
Expand Down
2 changes: 0 additions & 2 deletions src/ln.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ var path = require('path');
var common = require('./common');

common.register('ln', _ln, {
globStart: 1,
cmdOptions: {
's': 'symlink',
'f': 'force',
},
wrapOutput: true,
});

//@
Expand Down
2 changes: 0 additions & 2 deletions src/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ var glob = require('glob');
var globPatternRecursive = path.sep + '**' + path.sep + '*';

common.register('ls', _ls, {
globStart: 1,
cmdOptions: {
'R': 'recursive',
'A': 'all',
'a': 'all_deprecated',
'd': 'directory',
'l': 'long',
},
wrapOutput: true,
});

//@
Expand Down
2 changes: 0 additions & 2 deletions src/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ var fs = require('fs');
var path = require('path');

common.register('mkdir', _mkdir, {
globStart: 1,
cmdOptions: {
'p': 'fullpath',
},
wrapOutput: true,
});

// Recursively creates 'dir'
Expand Down
2 changes: 0 additions & 2 deletions src/mv.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ var cp = require('./cp');
var rm = require('./rm');

common.register('mv', _mv, {
globStart: 1,
cmdOptions: {
'f': '!no_force',
'n': 'no_force',
},
wrapOutput: true,
});

//@
Expand Down
4 changes: 3 additions & 1 deletion src/pwd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var path = require('path');
var common = require('./common');

common.register('pwd', _pwd, {wrapOutput: true});
common.register('pwd', _pwd, {
allowGlobbing: false,
});

//@
//@ ### pwd()
Expand Down
2 changes: 0 additions & 2 deletions src/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ var common = require('./common');
var fs = require('fs');

common.register('rm', _rm, {
globStart: 1,
cmdOptions: {
'f': 'force',
'r': 'recursive',
'R': 'recursive',
},
wrapOutput: true,
});

// Recursively removes 'dir'
Expand Down
1 change: 0 additions & 1 deletion src/sed.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ common.register('sed', _sed, {
cmdOptions: {
'i': 'inplace',
},
wrapOutput: true,
});

//@
Expand Down
5 changes: 4 additions & 1 deletion src/set.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
var common = require('./common');

common.register('set', _set);
common.register('set', _set, {
allowGlobbing: false,
wrapOutput: false,
});

//@
//@ ### set(options)
Expand Down
2 changes: 0 additions & 2 deletions src/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ var common = require('./common');
var fs = require('fs');

common.register('sort', _sort, {
globStart: 1,
canReceivePipe: true,
cmdOptions: {
'r': 'reverse',
'n': 'numerical',
},
wrapOutput: true,
});

// parse out the number prefix of a line
Expand Down
2 changes: 0 additions & 2 deletions src/tail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ var common = require('./common');
var fs = require('fs');

common.register('tail', _tail, {
globStart: 1,
canReceivePipe: true,
cmdOptions: {
'n': 'numLines',
},
wrapOutput: true,
});

//@
Expand Down
5 changes: 4 additions & 1 deletion src/tempdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ var common = require('./common');
var os = require('os');
var fs = require('fs');

common.register('tempdir', _tempDir);
common.register('tempdir', _tempDir, {
allowGlobbing: false,
wrapOutput: false,
});

// Returns false if 'dir' is not a writeable directory, 'dir' otherwise
function writeableDir(dir) {
Expand Down
1 change: 1 addition & 0 deletions src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ common.register('test', _test, {
'p': 'pipe',
'S': 'socket',
},
wrapOutput: false,
});


Expand Down
2 changes: 1 addition & 1 deletion src/to.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ var fs = require('fs');
var path = require('path');

common.register('to', _to, {
globStart: 1,
pipeOnly: true,
wrapOutput: false,
});

//@
Expand Down
2 changes: 1 addition & 1 deletion src/toEnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ var fs = require('fs');
var path = require('path');

common.register('toEnd', _toEnd, {
globStart: 1,
pipeOnly: true,
wrapOutput: false,
});

//@
Expand Down
2 changes: 0 additions & 2 deletions src/touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ var common = require('./common');
var fs = require('fs');

common.register('touch', _touch, {
globStart: 1,
cmdOptions: {
'a': 'atime_only',
'c': 'no_create',
'd': 'date',
'm': 'mtime_only',
'r': 'reference',
},
wrapOutput: true,
});

//@
Expand Down
2 changes: 0 additions & 2 deletions src/uniq.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ function lpad(c, str){
}

common.register('uniq', _uniq, {
globStart: 1,
canReceivePipe: true,
cmdOptions: {
'i': 'ignoreCase',
'c': 'count',
'd': 'duplicates',
},
wrapOutput: true,
});

//@
Expand Down
4 changes: 3 additions & 1 deletion src/which.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ var common = require('./common');
var fs = require('fs');
var path = require('path');

common.register('which', _which, {wrapOutput: true});
common.register('which', _which, {
allowGlobbing: false,
});

// XP's system default value for PATHEXT system variable, just in case it's not
// set on Windows.
Expand Down
3 changes: 1 addition & 2 deletions test/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ assert.ok(!shell.foo);

// Register the plugin
plugin.register('foo', fooImplementation, {
globStart: 1,
cmdOptions: {
'f': 'flag',
},
Expand Down Expand Up @@ -69,7 +68,7 @@ shell.foo('-f', 'filename');
assert.equal(data, 12);
assert.equal(fname, 'filename');

// The command supports globbing
// The command supports globbing by default
shell.foo('-f', 're*u?ces');
assert.equal(data, 12);
assert.equal(fname, 'resources');
Expand Down

0 comments on commit d411176

Please sign in to comment.