Skip to content

Commit 3951a87

Browse files
nfischerariporad
authored andcommitted
refactor: commands now register themselves (shelljs#475)
1 parent 3e37ae4 commit 3951a87

26 files changed

Lines changed: 90 additions & 63 deletions

shell.js

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
var common = require('./src/common');
1010

11-
1211
//@
1312
//@ All commands run synchronously, unless otherwise stated.
1413
//@ All commands accept standard bash globbing characters (`*`, `?`, etc.),
@@ -24,99 +23,76 @@ var common = require('./src/common');
2423
// ```
2524
// //@include ./src/fileName
2625
// var functionName = require('./src/fileName');
27-
// exports.nameOfCommand = common.wrap(nameOfCommand, functionName, {idx: firstIndexToExpand});
26+
// exports.nameOfCommand = common.wrap(nameOfCommand, functionName, {globStart: firstIndexToExpand});
2827
// ```
2928
//
3029
// The //@include includes the docs for that command
3130
//
32-
// firstIndexToExpand should usually be 1 (so, put {idx: 1})
31+
// firstIndexToExpand should usually be 1 (so, put {globStart: 1})
3332
// Increase this value if the command takes arguments that shouldn't be expanded
3433
// with wildcards, such as with the regexes for sed & grep
3534

3635
//@include ./src/cd
37-
var _cd = require('./src/cd');
38-
exports.cd = common.wrap('cd', _cd, {idx: 1});
36+
require('./src/cd');
3937

4038
//@include ./src/pwd
41-
var _pwd = require('./src/pwd');
42-
exports.pwd = common.wrap('pwd', _pwd);
39+
require('./src/pwd');
4340

4441
//@include ./src/ls
45-
var _ls = require('./src/ls');
46-
exports.ls = common.wrap('ls', _ls, {idx: 1});
42+
require('./src/ls');
4743

4844
//@include ./src/find
49-
var _find = require('./src/find');
50-
exports.find = common.wrap('find', _find, {idx: 1});
45+
require('./src/find');
5146

5247
//@include ./src/cp
53-
var _cp = require('./src/cp');
54-
exports.cp = common.wrap('cp', _cp, {idx: 1});
48+
require('./src/cp');
5549

5650
//@include ./src/rm
57-
var _rm = require('./src/rm');
58-
exports.rm = common.wrap('rm', _rm, {idx: 1});
51+
require('./src/rm');
5952

6053
//@include ./src/mv
61-
var _mv = require('./src/mv');
62-
exports.mv = common.wrap('mv', _mv, {idx: 1});
54+
require('./src/mv');
6355

6456
//@include ./src/mkdir
65-
var _mkdir = require('./src/mkdir');
66-
exports.mkdir = common.wrap('mkdir', _mkdir, {idx: 1});
57+
require('./src/mkdir');
6758

6859
//@include ./src/test
69-
var _test = require('./src/test');
70-
exports.test = common.wrap('test', _test);
60+
require('./src/test');
7161

7262
//@include ./src/cat
73-
var _cat = require('./src/cat');
74-
exports.cat = common.wrap('cat', _cat, {idx: 1, canReceivePipe: true});
63+
require('./src/cat');
7564

7665
//@include ./src/head
77-
var _head = require('./src/head');
78-
exports.head = common.wrap('head', _head, {idx: 1, canReceivePipe: true});
66+
require('./src/head');
7967

8068
//@include ./src/tail
81-
var _tail = require('./src/tail');
82-
exports.tail = common.wrap('tail', _tail, {idx: 1, canReceivePipe: true});
69+
require('./src/tail');
8370

8471
// The below commands have been moved to common.ShellString(), and are only here
8572
// for generating the docs
8673
//@include ./src/to
8774
//@include ./src/toEnd
8875

8976
//@include ./src/sed
90-
var _sed = require('./src/sed');
91-
exports.sed = common.wrap('sed', _sed, {idx: 3, canReceivePipe: true}); // don't glob-expand regexes
77+
require('./src/sed');
9278

9379
//@include ./src/sort
94-
var _sort = require('./src/sort');
95-
exports.sort = common.wrap('sort', _sort, {idx: 1, canReceivePipe: true});
80+
require('./src/sort');
9681

9782
//@include ./src/grep
98-
var _grep = require('./src/grep');
99-
exports.grep = common.wrap('grep', _grep, {idx: 2, canReceivePipe: true}); // don't glob-expand the regex
83+
require('./src/grep');
10084

10185
//@include ./src/which
102-
var _which = require('./src/which');
103-
exports.which = common.wrap('which', _which);
86+
require('./src/which');
10487

10588
//@include ./src/echo
106-
var _echo = require('./src/echo');
107-
exports.echo = common.wrap('echo', _echo);
89+
require('./src/echo');
10890

10991
//@include ./src/dirs
110-
var _dirs = require('./src/dirs').dirs;
111-
exports.dirs = common.wrap('dirs', _dirs, {idx: 1});
112-
var _pushd = require('./src/dirs').pushd;
113-
exports.pushd = common.wrap('pushd', _pushd, {idx: 1});
114-
var _popd = require('./src/dirs').popd;
115-
exports.popd = common.wrap('popd', _popd, {idx: 1});
92+
require('./src/dirs');
11693

11794
//@include ./src/ln
118-
var _ln = require('./src/ln');
119-
exports.ln = common.wrap('ln', _ln, {idx: 1});
95+
require('./src/ln');
12096

12197
//@
12298
//@ ### exit(code)
@@ -129,33 +105,28 @@ exports.exit = process.exit;
129105
exports.env = process.env;
130106

131107
//@include ./src/exec
132-
var _exec = require('./src/exec');
133-
exports.exec = common.wrap('exec', _exec, {notUnix:true, canReceivePipe: true});
108+
require('./src/exec');
134109

135110
//@include ./src/chmod
136-
var _chmod = require('./src/chmod');
137-
exports.chmod = common.wrap('chmod', _chmod, {idx: 1});
111+
require('./src/chmod');
138112

139113
//@include ./src/touch
140-
var _touch = require('./src/touch');
141-
exports.touch = common.wrap('touch', _touch, {idx: 1});
114+
require('./src/touch');
142115

143116
//@include ./src/set
144-
var _set = require('./src/set');
145-
exports.set = common.wrap('set', _set);
117+
require('./src/set');
146118

147119

148120
//@
149121
//@ ## Non-Unix commands
150122
//@
151123

152124
//@include ./src/tempdir
153-
var _tempDir = require('./src/tempdir');
154-
exports.tempdir = common.wrap('tempdir', _tempDir);
125+
require('./src/tempdir');
155126

156127
//@include ./src/error
157-
var _error = require('./src/error');
158-
exports.error = _error;
128+
129+
exports.error = require('./src/error');
159130

160131
//@include ./src/common
161132
exports.ShellString = common.ShellString;

src/cat.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var common = require('./common');
22
var fs = require('fs');
33

4+
common.register('cat', _cat, {globStart: 1, canReceivePipe: true});
5+
46
//@
57
//@ ### cat(file [, file ...])
68
//@ ### cat(file_array)

src/cd.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var fs = require('fs');
22
var common = require('./common');
33

4+
common.register('cd', _cd, {globStart: 1});
5+
46
//@
57
//@ ### cd([dir])
68
//@ Changes to directory `dir` for the duration of the script. Changes to home

src/chmod.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var PERMS = (function (base) {
3030
READ : 4
3131
});
3232

33+
common.register('chmod', _chmod, {globStart: 1});
34+
3335
//@
3436
//@ ### chmod(octal_mode || octal_string, file)
3537
//@ ### chmod(symbolic_mode, file)

src/common.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ function ShellString(stdout, stderr, code) {
102102
}
103103
that.stderr = stderr;
104104
that.code = code;
105-
that.to = function() {wrap('to', _to, {idx: 1}).apply(that.stdout, arguments); return that;};
106-
that.toEnd = function() {wrap('toEnd', _toEnd, {idx: 1}).apply(that.stdout, arguments); return that;};
105+
that.to = function() {wrap('to', _to, {globStart: 1}).apply(that.stdout, arguments); return that;};
106+
that.toEnd = function() {wrap('toEnd', _toEnd, {globStart: 1}).apply(that.stdout, arguments); return that;};
107107
// A list of all commands that can appear on the right-hand side of a pipe
108108
// (populated by calls to common.wrap())
109109
pipeMethods.forEach(function (cmd) {
@@ -313,10 +313,10 @@ function wrap(cmd, fn, options) {
313313
return arg;
314314
});
315315

316-
// Perform glob-expansion on all arguments after idx, but preserve the
317-
// arguments before it (like regexes for sed and grep)
318-
if (!config.noglob && typeof options.idx === 'number')
319-
args = args.slice(0, options.idx).concat(expand(args.slice(options.idx)));
316+
// Perform glob-expansion on all arguments after globStart, but preserve
317+
// the arguments before it (like regexes for sed and grep)
318+
if (!config.noglob && typeof options.globStart === 'number')
319+
args = args.slice(0, options.globStart).concat(expand(args.slice(options.globStart)));
320320
try {
321321
retValue = fn.apply(this, args);
322322
} catch (e) {
@@ -348,3 +348,9 @@ function _readFromPipe(that) {
348348
return that instanceof String ? that.toString() : '';
349349
}
350350
exports.readFromPipe = _readFromPipe;
351+
352+
// Register a new ShellJS command
353+
function _register(name, implementation, wrapOptions) {
354+
shell[name] = wrap(name, implementation, wrapOptions);
355+
}
356+
exports.register = _register;

src/cp.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var path = require('path');
33
var common = require('./common');
44
var os = require('os');
55

6+
common.register('cp', _cp, {globStart: 1});
7+
68
// Buffered file copy, synchronous
79
// (Using readFileSync() + writeFileSync() could easily cause a memory overflow
810
// with large files)

src/dirs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ var common = require('./common');
22
var _cd = require('./cd');
33
var path = require('path');
44

5+
common.register('dirs', _dirs, {globStart: 1});
6+
common.register('pushd', _pushd, {globStart: 1});
7+
common.register('popd', _popd, {globStart: 1});
8+
59
// Pushd/popd/dirs internals
610
var _dirStack = [];
711

src/echo.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var common = require('./common');
22

3+
common.register('echo', _echo);
4+
35
//@
46
//@ ### echo(string [, string ...])
57
//@

src/exec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ var child = require('child_process');
77

88
var DEFAULT_MAXBUFFER_SIZE = 20*1024*1024;
99

10+
common.register('exec', _exec, {notUnix:true, canReceivePipe: true});
11+
1012
// Hack to run child_process.exec() synchronously (sync avoids callback hell)
1113
// Uses a custom wait loop that checks for a flag file, created when the child process is done.
1214
// (Can't do a wait loop that checks for internal Node variables/messages as

src/find.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var path = require('path');
33
var common = require('./common');
44
var _ls = require('./ls');
55

6+
common.register('find', _find, {globStart: 1});
7+
68
//@
79
//@ ### find(path [, path ...])
810
//@ ### find(path_array)

0 commit comments

Comments
 (0)