UNPKG

ramda-cli

Version:

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

159 lines (158 loc) 5.05 kB
// Generated by LiveScript 1.6.0 (function(){ var vm, Path, fs, getUserConfig, config, R, apply, map, join, isEmpty, split, tap, pipe, identity, reverse, fromPairs, path, reduce, assocPath, adjust, toPairs, inspect, isBrowser, debug, fixMatch, relativeToCwd, wrapIn, wrapInParens, wrapInPipe, lines, words, unlines, unwords, renameKeysBy, constructPipe, pickDotPaths, makeSandbox, compileLivescript, evaluate, selectCompiler, compileWithOpts, compileAndEval, compileFun; vm = require('vm'); Path = require('path'); fs = require('fs'); getUserConfig = require('./get-user-config'); config = require('./config'); R = require('ramda'), apply = R.apply, map = R.map, join = R.join, isEmpty = R.isEmpty, split = R.split, tap = R.tap, pipe = R.pipe, identity = R.identity, reverse = R.reverse, fromPairs = R.fromPairs, path = R.path, reduce = R.reduce, assocPath = R.assocPath, adjust = R.adjust, toPairs = R.toPairs; inspect = require('util').inspect; isBrowser = require('./utils').isBrowser; debug = require('debug')('ramda-cli:compile-fun'); if (isBrowser()) { debug.enabled = true; } fixMatch = function(it){ return (it + "").replace(/\bmatch\b/g, function(m, i, str){ if (str[i - 1] !== '.') { return 'R.match'; } else { return m; } }); }; relativeToCwd = function(p){ return Path.join(process.cwd(), p); }; wrapIn = curry$(function(a, b, str){ return a + "" + str + b; }); wrapInParens = wrapIn('(', ')'); wrapInPipe = wrapIn('pipe(', ')'); lines = split('\n'); words = split(' '); unlines = join('\n'); unwords = join(' '); renameKeysBy = curry$(function(fn, obj){ return fromPairs( map(adjust(fn, 0))( toPairs(obj))); }); constructPipe = pipe(map(wrapInParens), join(','), wrapInPipe); pickDotPaths = curry$(function(paths, obj){ return reduce(function(res, p){ var val; val = path(p, obj); if (val != null) { return assocPath(p, val, res); } else { return res; } }, {}, map(split('.'), paths)); }); makeSandbox = function(opts, imports){ var importsObj, helpers, userConfig, ref$; importsObj = fromPairs( function(it){ return it.map(function(it){ return [it.alias, it.exports]; }); }( imports)); helpers = { flat: function(){ return apply(require('flat'), arguments); }, readFile: function(filePath){ return fs.readFileSync(relativeToCwd(filePath), 'utf8'); }, id: R.identity, lines: lines, words: words, unlines: unlines, unwords: unwords, then: curry$(function(fn, promise){ return promise.then(fn); }), pickDotPaths: pickDotPaths, renameKeysBy: renameKeysBy }; helpers._then = helpers.then; userConfig = getUserConfig(); return ref$ = { R: R, require: require, console: console, process: process }, import$(ref$, R), import$(ref$, userConfig), import$(ref$, helpers), import$(ref$, importsObj); }; compileLivescript = function(code){ var livescript; livescript = require('livescript'); return livescript.compile(code, { bare: true, header: false }); }; evaluate = function(opts, sandbox, code){ return vm.runInNewContext(code, sandbox); }; selectCompiler = function(opts){ switch (false) { case !opts.js: return identity; default: return compileLivescript; } }; compileWithOpts = function(code, opts){ return selectCompiler(opts)( code); }; compileAndEval = function(code, opts, imports){ var sandbox; sandbox = makeSandbox(opts, imports); return evaluate(opts, sandbox, tap(function(it){ return debug(it + "", 'compiled code'); })( compileWithOpts(code, opts))); }; compileFun = async function(opts, imports, stderr){ var fns, pipedInlineFunctions; imports == null && (imports = []); if (isEmpty(opts._)) { opts._ = ['identity']; } fns = (opts.transduce ? reverse : identity)(opts._); pipedInlineFunctions = constructPipe((await (async function(){ switch (false) { case !opts.js: return fns; default: return map(fixMatch, fns); } }()))); debug(inspect(pipedInlineFunctions), 'input code'); return compileAndEval(pipedInlineFunctions, opts, imports); }; module.exports = compileFun; function curry$(f, bound){ var context, _curry = function(args) { return f.length > 1 ? function(){ var params = args ? args.concat() : []; context = bound ? context || this : this; return params.push.apply(params, arguments) < f.length && arguments.length ? _curry.call(context, params) : f.apply(context, params); } : f; }; return _curry(); } function import$(obj, src){ var own = {}.hasOwnProperty; for (var key in src) if (own.call(src, key)) obj[key] = src[key]; return obj; } }).call(this);