/
tape
executable file
·87 lines (69 loc) · 2.53 KB
/
tape
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
#!/usr/bin/env node
'use strict';
var resolveModule = require('resolve').sync;
var resolvePath = require('path').resolve;
var readFileSync = require('fs').readFileSync;
var parseOpts = require('minimist');
var glob = require('glob');
var ignore = require('dotignore');
var hasImport = require('has-dynamic-import');
var tape = require('../');
var opts = parseOpts(process.argv.slice(2), {
alias: { r: 'require', i: 'ignore' },
string: ['require', 'ignore'],
default: { r: [], i: null }
});
var cwd = process.cwd();
if (typeof opts.require === 'string') {
opts.require = [opts.require];
}
opts.require.forEach(function (module) {
var options = { basedir: cwd, extensions: Object.keys(require.extensions) };
if (module) {
/* This check ensures we ignore `-r ""`, trailing `-r`, or
* other silly things the user might (inadvertently) be doing.
*/
require(resolveModule(module, options));
}
});
if (typeof opts.ignore === 'string') {
try {
var ignoreStr = readFileSync(resolvePath(cwd, opts.ignore || '.gitignore'), 'utf-8');
} catch (e) {
console.error(e.message);
process.exit(2);
}
var matcher = ignore.createMatcher(ignoreStr);
}
var files = opts._.reduce(function (result, arg) {
// If glob does not match, `files` will be an empty array.
// Note: `glob.sync` may throw an error and crash the node process.
var globFiles = glob.sync(arg);
if (!Array.isArray(globFiles)) {
throw new TypeError('unknown error: glob.sync("' + arg + '") did not return an array or throw. Please report this.');
}
return result.concat(globFiles);
}, []).filter(function (file) {
return !matcher || !matcher.shouldIgnore(file);
}).map(function (file) {
return resolvePath(cwd, file);
});
hasImport().then(function (hasSupport) {
// the nextTick callback gets called outside the promise chain, avoiding
// promises and unhandled rejections when only loading commonjs files
process.nextTick(importFiles, hasSupport);
});
function importFiles(hasSupport) {
if (!hasSupport) {
return files.forEach(function (x) { require(x); });
}
var importOrRequire = require('./import-or-require');
tape.wait();
var filesPromise = files.reduce(function (promise, file) {
return promise ? promise.then(function () {
return importOrRequire(file);
}) : importOrRequire(file);
}, null);
return filesPromise ? filesPromise.then(function () { tape.run(); }) : tape.run();
}
// vim: ft=javascript