UNPKG

1.62 kBPlain TextView Raw
1#!/usr/bin/env node
2
3var resolveModule = require('resolve').sync;
4var resolvePath = require('path').resolve;
5var readFileSync = require('fs').readFileSync;
6var parseOpts = require('minimist');
7var glob = require('glob');
8var ignore = require('dotignore');
9
10var opts = parseOpts(process.argv.slice(2), {
11 alias: { r: 'require', i: 'ignore' },
12 string: ['require', 'ignore'],
13 default: { r: [], i: null }
14});
15
16var cwd = process.cwd();
17
18if (typeof opts.require === 'string') {
19 opts.require = [opts.require];
20}
21
22opts.require.forEach(function (module) {
23 if (module) {
24 /* This check ensures we ignore `-r ""`, trailing `-r`, or
25 * other silly things the user might (inadvertently) be doing.
26 */
27 require(resolveModule(module, { basedir: cwd }));
28 }
29});
30
31if (typeof opts.ignore === 'string') {
32 try {
33 var ignoreStr = readFileSync(resolvePath(cwd, opts.ignore || '.gitignore'), 'utf-8');
34 } catch (e) {
35 console.error(e.message);
36 process.exit(2);
37 }
38 var matcher = ignore.createMatcher(ignoreStr);
39}
40
41opts._.forEach(function (arg) {
42 // If glob does not match, `files` will be an empty array.
43 // Note: `glob.sync` may throw an error and crash the node process.
44 var files = glob.sync(arg);
45
46 if (!Array.isArray(files)) {
47 throw new TypeError('unknown error: glob.sync did not return an array or throw. Please report this.');
48 }
49
50 files.filter(function (file) { return !matcher || !matcher.shouldIgnore(file); }).forEach(function (file) {
51 require(resolvePath(cwd, file));
52 });
53});
54
55// vim: ft=javascript