tape 1.7 KB

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