UNPKG

ramda-cli

Version:

A command-line tool for processing JSON with Ramda and LiveScript

232 lines (231 loc) 7.02 kB
// Generated by LiveScript 1.6.0 (function(){ var R, isNil, append, flip, type, forEach, reduce, ref$, PassThrough, pipeline, through, JSONStream, split2, inspect, isThenable, takeLines, removeExtraNewlines, isBrowser, debug, reduceStream, concatStream, unconcatStream, mapStream, jsonStringifyStream, debugStream, appendBuffer, blankObjStream, csvOpts, tableOutputStream, inspectStream, rawOutputStringify, rawOutputStream, optsToOutputStream, optsToInputParserStream, makeStdinParser, makeInputStream, makeMapStream, processInputStream, out$ = typeof exports != 'undefined' && exports || this; R = require('ramda'), isNil = R.isNil, append = R.append, flip = R.flip, type = R.type, forEach = R.forEach, reduce = R.reduce; ref$ = require('stream'), PassThrough = ref$.PassThrough, pipeline = ref$.pipeline; through = require('through2'); JSONStream = require('JSONStream'); split2 = require('split2'); inspect = require('util').inspect; ref$ = require('./utils'), isThenable = ref$.isThenable, takeLines = ref$.takeLines, removeExtraNewlines = ref$.removeExtraNewlines, isBrowser = ref$.isBrowser; debug = require('debug')('ramda-cli:stream'); if (isBrowser()) { debug.enabled = true; } reduceStream = function(fn, acc){ return through.obj(function(chunk, arg$, next){ acc = fn(acc, chunk); return next(); }, function(next){ this.push(acc); return next(); }); }; out$.concatStream = concatStream = function(){ return reduceStream(flip(append), []); }; unconcatStream = function(){ return through.obj(function(chunk, arg$, next){ switch (type(chunk)) { case 'Array': forEach(bind$(this, 'push'), chunk); break; default: this.push(chunk); } return next(); }); }; mapStream = function(fun){ return through.obj(function(chunk, arg$, next){ var push, val, e, this$ = this; push = function(x){ if (x !== null) { this$.push(x); } return next(); }; try { val = fun(chunk); } catch (e$) { e = e$; return next(e); } if (isThenable(val)) { return val.then(push); } else { return push(val); } }); }; jsonStringifyStream = function(compact){ var indent; indent = !compact ? 2 : void 8; return through.obj(function(data, arg$, next){ var json; json = (function(){ switch (type(data)) { case 'Function': return data.toString(); default: return JSON.stringify(data, null, indent); } }()); this.push(json + '\n'); return next(); }); }; debugStream = function(debug, opts, str){ return through.obj(function(chunk, arg$, next){ if (debug.enabled && opts.veryVerbose) { debug(inspect(chunk), str); } this.push(chunk); return next(); }); }; appendBuffer = function(buf1, buf2){ return Buffer.concat([buf1, buf2]); }; blankObjStream = function(){ var x$; x$ = PassThrough({ objectMode: true }); x$.end({}); return x$; }; csvOpts = function(type, delimiter, headers, unstrict){ var opts; unstrict == null && (unstrict = true); opts = { headers: headers, delimiter: delimiter, includeEndRowDelimiter: true, discardUnmappedColumns: true, strictColumnHandling: false }; switch (type) { case 'csv': return opts; case 'tsv': return opts.delimiter = '\t', opts; } }; tableOutputStream = function(compact){ var formatTable, opts; formatTable = require('./format-table'); opts = { compact: compact }; return through.obj(function(chunk, arg$, next){ this.push(formatTable(chunk, opts) + "\n"); return next(); }); }; inspectStream = function(depth){ return through.obj(function(chunk, arg$, next){ this.push(inspect(chunk, { colors: true, depth: depth }) + '\n'); return next(); }); }; rawOutputStringify = function(it){ if (type(it) === 'Object') { return JSON.stringify(it); } else { return it.toString(); } }; rawOutputStream = function(compact){ return through.obj(function(chunk, arg$, next){ var end, this$ = this; end = !compact ? "\n" : ''; switch (type(chunk)) { case 'Array': forEach(function(it){ return this$.push(rawOutputStringify(it) + "" + end); }, chunk); break; default: this.push(removeExtraNewlines(rawOutputStringify(chunk) + "" + end)); } return next(); }); }; optsToOutputStream = function(opts){ switch (opts.outputType) { case 'pretty': return inspectStream(opts.prettyDepth); case 'raw': return rawOutputStream(opts.compact); case 'csv': case 'tsv': return require('fast-csv').createWriteStream(csvOpts(opts.outputType, opts.csvDelimiter, opts.headers)); case 'table': return tableOutputStream(opts.compact); default: return jsonStringifyStream(opts.compact); } }; optsToInputParserStream = function(opts){ switch (opts.inputType) { case 'raw': return split2(); case 'csv': case 'tsv': return require('fast-csv')(csvOpts(opts.inputType, opts.csvDelimiter, opts.headers)); default: return JSONStream.parse(opts.jsonPath); } }; makeStdinParser = function(onError, opts, stdin){ var inputParser, s; inputParser = optsToInputParserStream(opts); if (opts.inputType === 'json') { stdin.on('end', function(){ return inputParser.write('\n'); }); } s = stdin.pipe(debugStream(debug, opts, 'stdin')).pipe(inputParser).on('error', function(it){ return onError(it); }).pipe(debugStream(debug, opts, "after input-parser")); if (opts.slurp) { s = s.pipe(concatStream()); } return s; }; makeInputStream = function(onError, opts, stdin){ if (opts.stdin) { return makeStdinParser(onError, opts, stdin); } else { return blankObjStream(); } }; makeMapStream = function(opts, fun){ if (opts.transduce) { return require('transduce-stream')(fun, { objectMode: true }); } else { return mapStream(fun); } }; out$.processInputStream = processInputStream = function(onError, opts, fun, inputStream, outputStream){ var s; s = makeInputStream(onError, opts, inputStream).pipe(makeMapStream(opts, fun)).on('error', onError).pipe(debugStream(debug, opts, "after map-stream")); if (opts.unslurp) { s = s.pipe(unconcatStream()); } s = s.pipe(optsToOutputStream(opts)).pipe(debugStream(debug, opts, 'stdout')); if (outputStream) { s.pipe(outputStream); } return s; }; function bind$(obj, key, target){ return function(){ return (target || obj)[key].apply(obj, arguments) }; } }).call(this);