ramda-cli
Version:
A command-line tool for processing JSON with Ramda and LiveScript
148 lines (147 loc) • 5.13 kB
JavaScript
// Generated by LiveScript 1.6.0
(function(){
var Path, fs, polka, serveStatic, opn, R, without, finished, querystring, StreamConcat, bodyParser, tempfile, compression, stringArgv, getPort, SSE, argv, compileFun, argvToString, processInputStream, debug, TIMEOUT, timer, startTimer, clearTimer, start, out$ = typeof exports != 'undefined' && exports || this;
Path = require('path');
fs = require('fs');
polka = require('polka');
serveStatic = require('serve-static');
opn = require('opn');
R = require('ramda'), without = R.without;
finished = require('stream').finished;
querystring = require('querystring');
StreamConcat = require('stream-concat');
bodyParser = require('body-parser');
tempfile = require('tempfile');
compression = require('compression');
stringArgv = require('string-argv');
getPort = require('get-port');
SSE = require('sse');
argv = require('./argv');
compileFun = require('./compile-fun');
argvToString = require('./argv-to-string');
processInputStream = require('./stream').processInputStream;
debug = require('debug')('ramda-cli:server');
TIMEOUT = 1000;
timer = null;
startTimer = function(cb){
if (!timer) {
debug("starting timer");
return timer = setTimeout(cb, TIMEOUT);
}
};
clearTimer = function(){
debug("clearing timer");
clearTimeout(timer);
return timer = null;
};
out$.start = start = async function(logError, stdin, stderr, processArgv, onComplete){
var stdinFinished, tmpFilePath, stdinTmpFile, input, sseClient, sseSendJson, tempFileStream, onClose, port, app;
stdinFinished = false;
tmpFilePath = tempfile();
stdinTmpFile = fs.createWriteStream(tmpFilePath, {
flags: 'w'
});
finished(stdin, function(err){
return stdinFinished = true;
});
input = null;
sseClient = null;
sseSendJson = function(data){
if (sseClient) {
return sseClient.send(JSON.stringify(data));
}
};
stdin.pipe(stdinTmpFile);
tempFileStream = function(){
return fs.createReadStream(tmpFilePath, {
flags: 'r'
});
};
onClose = function(){
return onComplete(stdinFinished
? tempFileStream()
: new StreamConcat([tempFileStream(), stdin]), input);
};
port = (await getPort({
host: '127.0.0.1'
}));
app = polka().use(compression({
filter: function(req, res){
if (req.path === '/eval') {
return false;
} else {
return compression.filter(req, res);
}
}
})).use(serveStatic(Path.join(__dirname, '..', 'web-dist'), {
'index': ['index.html']
})).post('/eval', bodyParser.text(), async function(req, res){
var opts, onError, ref$, getUninstalled, npmInstall, pkgSpecs, toBeInstalled, imports, fun, err, newStdin, readStream, combined;
res.setHeader('Content-Type', 'text/plain');
input = req.body;
opts = argv.parse(stringArgv(input, 'node', 'dummy.js'));
onError = function(err){
res.writeHead(400);
return res.end(err.stack);
};
try {
if (opts['import'].length) {
ref$ = require('./npm-install'), getUninstalled = ref$.getUninstalled, npmInstall = ref$.npmInstall;
pkgSpecs = opts['import'].map(function(it){
return it.packageSpec;
});
toBeInstalled = (await getUninstalled(pkgSpecs));
if (toBeInstalled.length) {
sseSendJson({
event: 'NPM_INSTALL_START'
});
}
imports = (await npmInstall(pkgSpecs, opts['import'], stderr));
if (toBeInstalled.length) {
sseSendJson({
event: 'NPM_INSTALL_FINISH'
});
}
}
fun = (await compileFun(opts, imports, stderr));
} catch (e$) {
err = e$;
return onError(err);
}
newStdin = opts.slurp || stdinFinished
? tempFileStream()
: (readStream = tempFileStream(), combined = new StreamConcat([readStream, stdin]), req.on('close', function(){
return combined.destroy();
}), combined);
return processInputStream(onError, opts, fun, newStdin, res);
}).post('/unload', function(req, res){
startTimer(onClose);
debug('got unload beacon, timer started');
res.writeHead(200);
return res.end('OK');
}).get('/ping', function(req, res){
debug('got ping');
if (timer) {
clearTimer();
}
res.writeHead(200);
return res.end('OK');
}).listen(port, '127.0.0.1', function(err){
var sse, argv, qs;
sse = new SSE(app.server);
sse.on('connection', function(client){
return sseClient = client;
});
debug("listening at port " + app.server.address().port);
argv = without(["--interactive", "-I"])(
processArgv.slice(2));
qs = querystring.stringify({
input: argvToString(argv)
});
return opn("http://localhost:" + app.server.address().port + "?" + qs, {
wait: false
});
});
return app.server;
};
}).call(this);