UNPKG

tranz

Version:

The framework for transform anything

114 lines (96 loc) 2.69 kB
/** * @file bin * @author Cuttle Cong * @date 2018/9/28 */ import minimost from './minimost' import tranz from './' import * as concat from 'concat-stream' import * as fs from 'fs' const pkg = require('../package') /* istanbul ignore next: should be run in lib/bin.js */ const arg = minimost(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'] }) as any function run(input, processors, opts, from?) { input = String(input) return tranz(input, processors, opts) .then(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(error => { console.error(error) process.exit(1) }) } // console.log(arg) ;(function() { if (arg.flags.help) { console.log(` ${pkg.name} ${pkg.version}: ${pkg.description} Usage ${pkg.name} [file] [options] Options -h, --help More help -v Version -p, --processor Set processor (e.g. \`-p ./trans -p upper\`) -i, --input Set input (e.g. \`-i $PWD\`) --no-userc Disable runtime configuration use \`--userc\` enable it --parallel Run processor parallelly use \`--no-parallel\` disable it -w, --write Overwrite file by transformed string --to Write file to where --name Use which rc config Examples ${pkg.name} -i $PWD -p ./upper cat $PWD | ${pkg.name} -p ./upper See https://github.com/imcuttle/tranz for more information. `) return } if (arg.flags.version) { return console.log(pkg.version) } const processors = arg.flags.processors ? Array.isArray(arg.flags.processors) ? arg.flags.processors : [arg.flags.processors] : void 0 const opts = { userc: arg.flags.userc, parallel: arg.flags.parallel, name: arg.flags.name } if (arg.input && arg.input.length) { arg.input.forEach(function(filename) { const 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) }) ) } })()