diff --git a/bin.js b/bin.js new file mode 100755 index 0000000..7c8e937 --- /dev/null +++ b/bin.js @@ -0,0 +1,69 @@ +#!/usr/bin/env node + +const resolvePath = require('path').resolve +const parseOpts = require('minimist') +const glob = require('glob') +const browserify = require('browserify') +const fromArgs = require('browserify/bin/args') +const run = require('tape-run') +const pump = require('pump') + +let args = process.argv.slice(2) + +const opts = parseOpts(args, { + '--': true, + alias: { + wait: 'w', + port: 'p', + static: 's', + browser: 'b', + render: 'r', + 'keep-open': ['k', 'keepOpen'], + node: ['n', 'node-integration', 'nodeIntegration'] + } +}) + +const cwd = process.cwd() + +const fileSet = new Set() + +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, { + ignore: ['node_modules/**', '.git/**'] + }) + + if (!Array.isArray(files)) { + throw new TypeError('unknown error: glob.sync did not return an array or throw. Please report this.') + } + + files.forEach(function (file) { + fileSet.add(resolvePath(cwd, file)) + }) +}) + +const browserifyArgs = opts['--'] + +delete opts['--'] +delete opts._ + +let bundler +if (browserifyArgs && Array.isArray(browserifyArgs)) { + // CLI args for browserify + bundler = fromArgs(browserifyArgs, { + entries: Array.from(fileSet) + }) +} else { + // just assume JS only options + bundler = browserify(Array.from(fileSet)) +} + +const tapeRun = run(opts) +tapeRun.on('results', (results) => { + process.exit(Number(!results.ok)) +}) + +pump(bundler.bundle(), tapeRun, process.stdout, (err) => { + if (err) console.error(err) +}) diff --git a/package.json b/package.json index 5c157ed..92196c5 100644 --- a/package.json +++ b/package.json @@ -6,19 +6,24 @@ "bugs": { "url": "https://github.com/hyperdivision/vhs-tape/issues" }, + "bin": "bin.js", "dependencies": { "events.once": "^2.0.2", "fast-on-load": "^1.1.0", - "tape": "^4.6.2" + "glob": "^7.1.4", + "minimist": "^1.2.0", + "pump": "^3.0.0", + "resolve": "^1.12.0", + "tape": "^4.6.2", + "tape-run": "^6.0.1", + "browserify": "^16.5.0" }, "devDependencies": { - "browserify": "^16.0.0", "budo": "^11.6.2", "dependency-check": "^3.0.0", "hui": "^1.2.5", "npm-run-all": "^4.0.0", - "standard": "^12.0.1", - "tape-run": "^6.0.0" + "standard": "^12.0.1" }, "homepage": "https://github.com/hyperdivision/vhs-tape#readme", "keywords": [