-
Notifications
You must be signed in to change notification settings - Fork 2
/
globstar.js
executable file
·171 lines (147 loc) · 4.2 KB
/
globstar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env node
'use strict';
var path = require('path');
var EOL = require('os').EOL;
var format = require('util').format;
var spawn = require('child_process').spawn;
var npmlog = require('npmlog');
var glob = require('glob').sync;
var onetime = require('onetime');
var objectAssign = require('object-assign');
var packageJson = require('./package.json');
var yargs = require('yargs')
.usage([
'Run programs with globstar support.',
'',
'Usage: $0 [OPTION]... -- COMMAND [ARG]... ',
'Note the -- between the $0 OPTIONS and the COMMAND and its arguments',
].join(EOL))
.demand(1, 'COMMAND required')
// nodir
.boolean('nodir')
.describe('nodir', 'glob patterns do not match directories, only files')
// ignore
.array('i')
.alias('i', 'ignore')
.describe('i', 'add glob pattern to exclude from matches')
// node
.boolean('n')
.alias('n', 'node')
.describe('n', 'same as `--ignore "node_modules/**"`')
// verbose
.count('v')
.alias('v', 'verbose')
.describe('v', 'explain what is being done')
// version
.version(packageJson.version, 'version', 'display version information')
// help
.help('help')
// epilog
.epilog([
'Report $0 bugs to <' + packageJson.bugs.url + '>',
'$0 home page: <' + packageJson.homepage + '>',
].join(EOL));
function parseArgv(argv) {
var err = null;
var args = yargs.fail(function yargsFail(message) {
err = new Error(message);
err.name = 'YargsError';
}).parse(argv);
// set defaults
args = objectAssign({
_: [],
$0: path.relative(process.cwd(), __filename),
verbose: 0,
}, args);
// translate args
args.verbose = 2 - Math.max(2 - args.verbose, 0); // 0 <= verbose <= 2
npmlog.level = ['info', 'verbose', 'silly'][args.verbose];
if (args.node) {
args.ignore = args.ignore || [];
args.ignore.push('node_modules/**');
}
// setup result
var result = err || {};
result.logLevel = ['info', 'verbose', 'silly'][args.verbose];
result.cmd = args._[0];
result.args = args._.slice(1);
result.$0 = args.$0;
result.globOpts = args;
Object.keys(result.globOpts).filter(function nonGlobFlag(opt) {
switch (opt) {
case '$0':
case 'verbose':
case 'node':
return true;
default:
return opt.length === 1;
}
}).forEach(function deleteFlag(flag) {
delete result.globOpts[flag];
});
return result;
}
function globArgs(args, globOpts) {
globOpts.nonull = true; // override --no-null etc.
npmlog.log('silly', 'globstar', {opts: globOpts});
return args.map(function globArg(arg) {
return glob(arg, globOpts);
}).reduce(function flatten(previous, current) {
return previous.concat(current);
}, []);
}
function spawnCommand(cmd, args, cb) {
cb = onetime(cb);
var opts = {
stdio: 'inherit',
};
/* istanbul ignore next: platform specific */
if (process.platform === 'win32') {
args = ['/c', '"' + cmd + '"'].concat(args);
cmd = 'cmd';
opts.windowsVerbatimArguments = true;
}
npmlog.log('verbose', 'globstar', 'Running: `' + cmd + '` ' + format(args));
var child = spawn(cmd, args, opts);
child.on('error', cb);
child.on('close', function onClose(status) {
if (status) {
var err = new Error('Exit status ' + status);
err.name = 'SpawnError';
err.status = status;
cb(err);
} else {
cb();
}
});
}
function main(argv, exit) {
function errorHandler(err) {
if (err) {
if (err.name !== 'SpawnError') {
if (npmlog.level === 'silly') {
npmlog.log('error', 'globstar', err);
} else {
npmlog.log('error', 'globstar', err.message);
}
npmlog.log('info', 'globstar', 'Try \'' + argv.$0 + ' --help\' for ' +
'more information');
npmlog.log('verbose', 'globstar@' + packageJson.version, __filename);
}
exit(err.status || 1);
return;
}
exit(0);
}
argv = parseArgv(argv);
if (argv instanceof Error) {
errorHandler(argv);
return;
}
spawnCommand(argv.cmd, globArgs(argv.args, argv.globOpts), errorHandler);
}
module.exports = main;
/* istanbul ignore if: coverd by test.js (fs.spawn) */
if (require.main === module) {
main(process.argv.slice(2), process.exit.bind(process));
}