UNPKG

nesh

Version:

An enhanced, extensible shell for Node.js

101 lines (90 loc) 3.14 kB
// Generated by CoffeeScript 1.11.1 (function() { var __doc__, colors, displayUsage, fs, optimist, vm; __doc__ = "Shorthand for requiring and assigning a module\n\n.require colors -> colors = require('colors')\n.require ./User -> User = require('./User')\n.require ./model/User -> User = require('./model/User')\n.require use-global-fibrous -> useGlobalFibrous = require('use-global-fibrous')\n.require lodash-node/modern/objects -> objects = require('lodash-node/modern/objects')\n\nIf the `require` is successful, the name of the variable that the module\nhas been assigned to will be pre-filled into the REPL as the next\nline of input. This behavior can be disabled with the command-line\nargument `--no-require-echo`.\n"; colors = require('colors'); optimist = require('optimist'); vm = require('vm'); fs = require('fs'); displayUsage = function(repl) { repl.outputStream.write(colors.cyan("Usage: .require <unquoted-module-name-or-path> [assign-to]\n")); return repl.displayPrompt(); }; exports.postStart = function(context) { var action, exec, repl; repl = context.repl; exec = function(s) { if (repl.useGlobal) { return vm.runInThisContext(s); } else { return vm.runInContext(s, repl.context); } }; action = function(m) { var c, capNext, e, e2, expanded, i, len, ok, tokens, vName; if (m.trim().length === 0) { displayUsage(repl); return; } if (/^[\.\/]+$/.test(m)) { m = fs.realpathSync(process.cwd() + "/" + m); } tokens = m.split(/\s+/); if (tokens.length > 2) { displayUsage(repl); return; } else if (tokens.length === 2) { vName = tokens[1]; m = tokens[0]; } else { vName = ""; capNext = false; for (i = 0, len = m.length; i < len; i++) { c = m[i]; if (c === '/') { vName = ""; } else if (c === '-') { capNext = true; } else if (c === '.') { } else { if (capNext) { vName += c.toUpperCase(); } else { vName += c; } capNext = false; } } } expanded = vName + " = require(" + (JSON.stringify(m)) + ")"; repl.outputStream.write(colors.green(expanded + "\n")); ok = true; try { exec(expanded); } catch (error) { e = error; ok = false; try { if (m.slice(0, 2) !== "./") { action("./" + m); } else { throw e; } } catch (error) { e2 = error; ok = false; repl.outputStream.write(colors.red(".require: " + e + "\n")); } } repl.displayPrompt(); if (ok && optimist.argv['require-echo'] !== false) { return repl.rli.write(vName); } }; return repl.defineCommand('require', { help: "Require a module and assign it to a variable with the same name", action: action }); }; }).call(this); //# sourceMappingURL=require.js.map