From c8f3ce32c73e092940e29dc72f0abba3b6529936 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 6 Aug 2021 16:17:02 -0700 Subject: [PATCH] [Fix] `bin/tape`: delay requires until needed This ensures that `--require`s happen before everything tape needs, as much as possible --- bin/tape | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/bin/tape b/bin/tape index 436884d9..ac6cfc1a 100755 --- a/bin/tape +++ b/bin/tape @@ -2,15 +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 hasImport = require('has-dynamic-import'); - -var tape = require('../'); var opts = parseOpts(process.argv.slice(2), { alias: { r: 'require', i: 'ignore' }, @@ -24,24 +16,33 @@ if (typeof opts.require === 'string') { opts.require = [opts.require]; } +var resolveModule; opts.require.forEach(function (module) { var options = { basedir: cwd, extensions: Object.keys(require.extensions) }; 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, options)); } }); +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'); + 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. @@ -58,12 +59,16 @@ var files = opts._.reduce(function (result, arg) { return resolvePath(cwd, file); }); +var hasImport = require('has-dynamic-import'); + 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); }); +var tape = require('../'); + function importFiles(hasSupport) { if (!hasSupport) { return files.forEach(function (x) { require(x); });