UNPKG

tranz

Version:

The framework for transform anything

81 lines 3.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * @file bin * @author Cuttle Cong * @date 2018/9/28 */ var minimost_1 = require("./minimost"); var _1 = require("./"); var concat = require("concat-stream"); var fs = require("fs"); var pkg = require('../package'); /* istanbul ignore next: should be run in lib/bin.js */ var arg = minimost_1.default(process.argv.slice(2), { alias: { h: 'help', v: 'version', p: 'processors', i: 'input', w: 'write', processor: 'processors' }, default: { userc: true }, boolean: ['help', 'userc', 'parallel', 'write'] }); function run(input, processors, opts, from) { input = String(input); return _1.default(input, processors, opts) .then(function (output) { output = String(output); if (arg.flags.write && from) { return fs.writeFileSync(from, output); } if (arg.flags.to) { return fs.writeFileSync(arg.flags.to, output); } process.stdout.write(output); }) .catch(function (error) { console.error(error); process.exit(1); }); } // console.log(arg) ; (function () { if (arg.flags.help) { console.log(" " + pkg.name + " " + pkg.version + ": " + pkg.description + "\n \n Usage\n \n " + pkg.name + " [file] [options]\n \n Options\n \n -h, --help More help\n -v Version\n -p, --processor Set processor (e.g. `-p ./trans -p upper`)\n -i, --input Set input (e.g. `-i $PWD`)\n --no-userc Disable runtime configuration\n use `--userc` enable it\n --parallel Run processor parallelly\n use `--no-parallel` disable it\n -w, --write Overwrite file by transformed string\n --to Write file to where\n --name Use which rc config\n \n Examples\n \n " + pkg.name + " -i $PWD -p ./upper\n cat $PWD | " + pkg.name + " -p ./upper\n \n See https://github.com/imcuttle/tranz for more information.\n "); return; } if (arg.flags.version) { return console.log(pkg.version); } var processors = arg.flags.processors ? Array.isArray(arg.flags.processors) ? arg.flags.processors : [arg.flags.processors] : void 0; var opts = { userc: arg.flags.userc, parallel: arg.flags.parallel, name: arg.flags.name }; if (arg.input && arg.input.length) { arg.input.forEach(function (filename) { var input = fs.readFileSync(filename).toString(); run(input, processors, opts, filename); }); } else if (typeof arg.flags.input !== 'undefined') { run(arg.flags.input, processors, opts); } else { process.stdin.pipe(concat(function (string) { run(String(string), processors, opts); })); } })(); //# sourceMappingURL=bin.js.map