@@ -1,16 +1,24 @@
1−const repl = require('repl');
2−const path = require('path');
3−const fs = require('fs');
4−const vm = require('vm');
5−const rl = require(path.join(process.cwd(), 'prybar_assets', 'nodejs', 'readline-sync.js'));
6−const tty = require('tty');
7−const Module = require('module');
1+const repl = require("repl");
2+const path = require("path");
3+const fs = require("fs");
4+const vm = require("vm");
5+const rl = require(path.join(
6+ process.cwd(),
7+ "prybar_assets",
8+ "nodejs",
9+ "readline-sync.js"
10+));
11+const tty = require("tty");
12+const Module = require("module");
813
914let r;
15+if (!process.env.PRYBAR_QUIET) {
16+ console.log("Node " + process.version + " on " + process.platform);
17+}
1018
1119// Red errors.
1220function logError(msg) {
13− process.stdout.write('\u001b[0m\u001b[31m' + msg + '\u001b[0m');
21+ process.stdout.write("\u001b[0m\u001b[31m" + msg + "\u001b[0m");
1422}
1523
1624// The nodejs repl operates in raw mode and does some funky stuff to
@@ -42,17 +50,17 @@ function handleError(e) {
4250 r.lastError = e;
4351 }
4452
45− if (e && typeof e === 'object' && e.stack && e.name) {
46− if (e.name === 'SyntaxError') {
53+ if (e && typeof e === "object" && e.stack && e.name) {
54+ if (e.name === "SyntaxError") {
4755 e.stack = e.stack
48− .replace(/^repl:\d+\r?\n/, '')
49− .replace(/^\s+at\s.*\n?/gm, '');
56+ .replace(/^repl:\d+\r?\n/, "")
57+ .replace(/^\s+at\s.*\n?/gm, "");
5058 }
5159
5260 logError(e.stack);
5361 } else {
5462 // For some reason needs a newline to flush.
55− logError('Thrown: ' + r.writer(e) + '\n');
63+ logError("Thrown: " + r.writer(e) + "\n");
5664 }
5765
5866 if (r) {
@@ -64,18 +72,18 @@ function handleError(e) {
6472
6573function start(context) {
6674 r = repl.start({
67− prompt: process.env.NODE_PROMPT,
75+ prompt: process.env.PRYBAR_PS1,
6876 });
6977 if (context) r.context = context;
7078
7179 // remove the internal error and ours for red etc.
72− r._domain.removeListener('error', r._domain.listeners('error')[0]);
73− r._domain.on('error', handleError);
74− process.on('uncaughtException', handleError);
80+ r._domain.removeListener("error", r._domain.listeners("error")[0]);
81+ r._domain.on("error", handleError);
82+ process.on("uncaughtException", handleError);
7583}
7684
7785global.alert = console.log;
78−global.prompt = p => {
86+global.prompt = (p) => {
7987 pauseRepl();
8088 clearLine();
8189
@@ -91,7 +99,7 @@ global.prompt = p => {
9199 return ret;
92100};
93101
94−global.confirm = q => {
102+global.confirm = (q) => {
95103 pauseRepl();
96104 clearLine();
97105
@@ -103,13 +111,23 @@ global.confirm = q => {
103111 if (r) setImmediate(() => r.displayPrompt());
104112 return ret;
105113};
106−console.log(process.argv)
107−if (process.argv[2]) {
108− const mainPath = path.resolve(process.argv[2]);
109− const main = fs.readFileSync(mainPath, 'utf-8');
114+
115+if (process.env.PRYBAR_CODE) {
116+ vm.runInThisContext(process.env.PRYBAR_CODE);
117+ if (process.env.PRYBAR_INTERACTIVE) {
118+ start();
119+ }
120+} else if (process.env.PRYBAR_EXP) {
121+ console.log(vm.runInThisContext(process.env.PRYBAR_EXP));
122+ if (process.env.PRYBAR_INTERACTIVE) {
123+ start();
124+ }
125+} else if (process.env.PRYBAR_FILE) {
126+ const mainPath = path.resolve(process.env.PRYBAR_FILE);
127+ const main = fs.readFileSync(mainPath, "utf-8");
110128 const module = new Module(mainPath, null);
111129
112− module.id = '.';
130+ module.id = ".";
113131 module.filename = mainPath;
114132 module.paths = Module._nodeModulePaths(path.dirname(mainPath));
115133
@@ -136,7 +154,7 @@ if (process.argv[2]) {
136154 }
137155
138156 console.log(
139− '\u001b[0m\u001b[90mHint: hit control+c anytime to enter REPL.\u001b[0m',
157+ "\u001b[0m\u001b[90mHint: hit control+c anytime to enter REPL.\u001b[0m"
140158 );
141159 const context = vm.createContext(sandbox);
142160
@@ -162,14 +180,15 @@ if (process.argv[2]) {
162180
163181 module.loaded = true;
164182
165− if (typeof res !== 'undefined') {
183+ if (typeof res !== "undefined") {
166184 console.log(res);
167185 }
168186 }
169187
170− process.on('SIGINT', () => start(context));
171−
172− process.on('beforeExit', () => start(context));
173−} else {
188+ if (process.env.PRYBAR_INTERACTIVE) {
189+ process.once("SIGINT", () => start(context));
190+ process.once("beforeExit", () => start(context));
191+ }
192+} else if (process.env.PRYBAR_INTERACTIVE) {
174193 start();
175−}
194+}