Skip to content

Commit

Permalink
[Fix] bin/tape: delay requires until needed
Browse files Browse the repository at this point in the history
This ensures that `--require`s happen before everything tape needs, as much as possible
  • Loading branch information
ljharb committed Aug 6, 2021
1 parent 51ae645 commit b803fd8
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions bin/tape
Expand Up @@ -2,12 +2,7 @@

'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 opts = parseOpts(process.argv.slice(2), {
alias: { r: 'require', i: 'ignore' },
Expand All @@ -21,23 +16,32 @@ if (typeof opts.require === 'string') {
opts.require = [opts.require];
}

var resolveModule;
opts.require.forEach(function (module) {
if (module) {
if (!resolveModule) { resolveModule = require('resolve').sync; }
// This check ensures we ignore `-r ""`, trailing `-r`, or other silly things the user might (inadvertently) be doing.
require(resolveModule(module, { basedir: cwd }));
}
});

var resolvePath = require('path').resolve;

var matcher;
if (typeof opts.ignore === 'string') {
var readFileSync = require('fs').readFileSync;
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 ignore = require('dotignore');
matcher = ignore.createMatcher(ignoreStr);
}

var glob = require('glob');

opts._.forEach(function (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 files = glob.sync(arg);
Expand Down

0 comments on commit b803fd8

Please sign in to comment.