on master help sign in
Refactor
63af75dAmjad Masad
languages/nodejs/main.go+15−22
@@ -7,6 +7,7 @@ import (
7 "os"
7 "os"
8 "os/exec"
8 "os/exec"
9 "syscall"
9 "syscall"
10+
10 "github.com/replit/prybar/utils"
11 "github.com/replit/prybar/utils"
11)
12)
12
13
@@ -39,35 +40,27 @@ func Execute(config *utils.Config) {
39 panic(err)
40 panic(err)
40 }
41 }
41
42
42− env := os.Environ()
43− args := []string{"node"}
43+ args := []string{"node", findHelper("repl")}
44−
45− if !config.Quiet {
46− args = append(args, "-r", findHelper("version"))
47
44
45+ if config.Quiet {
46+ os.Setenv("PRYBAR_QUIET", "1")
48 }
47 }
49
48
50− if config.Code != "" {
49+ os.Setenv("PRYBAR_CODE", config.Code)
51− args = append(args, "-e", config.Code)
52− }
53−
54− if config.Exp != "" {
50+ os.Setenv("PRYBAR_EXP", config.Exp)
55− args = append(args, "-p", config.Exp)
51+ os.Setenv("PRYBAR_PS1", config.Ps1)
56− }
57
52
58 if config.Interactive {
53 if config.Interactive {
59− args = append(args, findHelper("repl"))
54+ os.Setenv("PRYBAR_INTERACTIVE", "1")
60− }
61−
62− if config.Ps1 != "" {
63− env = append(env, "NODE_PROMPT="+config.Ps1)
64 }
55 }
65
56
57+ // We only support one file, despite the fact that this
58+ // variable is a list.
66− if config.Args != nil && len(config.Args) > 0 {
59+ if len(config.Args) >= 1 {
67− args = append(args, config.Args...)
60+ os.Setenv("PRYBAR_FILE", config.Args[0])
68− } else if config.Exp == "" && config.Code == "" {
61+ } else {
69− args = append(args, "-e", "")
62+ os.Setenv("PRYBAR_FILE", "")
70 }
63 }
71
64
72− syscall.Exec(path, args, env)
65+ syscall.Exec(path, args, os.Environ())
73}
66}
languages/nodejs/repl.js+50−31
@@ -1,16 +1,24 @@
1−const repl = require('repl');
1+const repl = require("repl");
2−const path = require('path');
2+const path = require("path");
3−const fs = require('fs');
3+const fs = require("fs");
4−const vm = require('vm');
4+const vm = require("vm");
5−const rl = require(path.join(process.cwd(), 'prybar_assets', 'nodejs', 'readline-sync.js'));
5+const rl = require(path.join(
6+ process.cwd(),
7+ "prybar_assets",
8+ "nodejs",
9+ "readline-sync.js"
10+));
6−const tty = require('tty');
11+const tty = require("tty");
7−const Module = require('module');
12+const Module = require("module");
8
13
9let r;
14let r;
15+if (!process.env.PRYBAR_QUIET) {
16+ console.log("Node " + process.version + " on " + process.platform);
17+}
10
18
11// Red errors.
19// Red errors.
12function logError(msg) {
20function logError(msg) {
13− process.stdout.write('\u001b[0m\u001b[31m' + msg + '\u001b[0m');
21+ process.stdout.write("\u001b[0m\u001b[31m" + msg + "\u001b[0m");
14}
22}
15
23
16// The nodejs repl operates in raw mode and does some funky stuff to
24// The nodejs repl operates in raw mode and does some funky stuff to
@@ -42,17 +50,17 @@ function handleError(e) {
42 r.lastError = e;
50 r.lastError = e;
43 }
51 }
44
52
45− if (e && typeof e === 'object' && e.stack && e.name) {
53+ if (e && typeof e === "object" && e.stack && e.name) {
46− if (e.name === 'SyntaxError') {
54+ if (e.name === "SyntaxError") {
47 e.stack = e.stack
55 e.stack = e.stack
48− .replace(/^repl:\d+\r?\n/, '')
56+ .replace(/^repl:\d+\r?\n/, "")
49− .replace(/^\s+at\s.*\n?/gm, '');
57+ .replace(/^\s+at\s.*\n?/gm, "");
50 }
58 }
51
59
52 logError(e.stack);
60 logError(e.stack);
53 } else {
61 } else {
54 // For some reason needs a newline to flush.
62 // For some reason needs a newline to flush.
55− logError('Thrown: ' + r.writer(e) + '\n');
63+ logError("Thrown: " + r.writer(e) + "\n");
56 }
64 }
57
65
58 if (r) {
66 if (r) {
@@ -64,18 +72,18 @@ function handleError(e) {
64
72
65function start(context) {
73function start(context) {
66 r = repl.start({
74 r = repl.start({
67− prompt: process.env.NODE_PROMPT,
75+ prompt: process.env.PRYBAR_PS1,
68 });
76 });
69 if (context) r.context = context;
77 if (context) r.context = context;
70
78
71 // remove the internal error and ours for red etc.
79 // remove the internal error and ours for red etc.
72− r._domain.removeListener('error', r._domain.listeners('error')[0]);
80+ r._domain.removeListener("error", r._domain.listeners("error")[0]);
73− r._domain.on('error', handleError);
81+ r._domain.on("error", handleError);
74− process.on('uncaughtException', handleError);
82+ process.on("uncaughtException", handleError);
75}
83}
76
84
77global.alert = console.log;
85global.alert = console.log;
78−global.prompt = p => {
86+global.prompt = (p) => {
79 pauseRepl();
87 pauseRepl();
80 clearLine();
88 clearLine();
81
89
@@ -91,7 +99,7 @@ global.prompt = p => {
91 return ret;
99 return ret;
92};
100};
93
101
94−global.confirm = q => {
102+global.confirm = (q) => {
95 pauseRepl();
103 pauseRepl();
96 clearLine();
104 clearLine();
97
105
@@ -103,13 +111,23 @@ global.confirm = q => {
103 if (r) setImmediate(() => r.displayPrompt());
111 if (r) setImmediate(() => r.displayPrompt());
104 return ret;
112 return ret;
105};
113};
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) {
106−console.log(process.argv)
121+ console.log(vm.runInThisContext(process.env.PRYBAR_EXP));
107−if (process.argv[2]) {
122+ if (process.env.PRYBAR_INTERACTIVE) {
123+ start();
124+ }
125+} else if (process.env.PRYBAR_FILE) {
108− const mainPath = path.resolve(process.argv[2]);
126+ const mainPath = path.resolve(process.env.PRYBAR_FILE);
109− const main = fs.readFileSync(mainPath, 'utf-8');
127+ const main = fs.readFileSync(mainPath, "utf-8");
110 const module = new Module(mainPath, null);
128 const module = new Module(mainPath, null);
111
129
112− module.id = '.';
130+ module.id = ".";
113 module.filename = mainPath;
131 module.filename = mainPath;
114 module.paths = Module._nodeModulePaths(path.dirname(mainPath));
132 module.paths = Module._nodeModulePaths(path.dirname(mainPath));
115
133
@@ -136,7 +154,7 @@ if (process.argv[2]) {
136 }
154 }
137
155
138 console.log(
156 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"
140 );
158 );
141 const context = vm.createContext(sandbox);
159 const context = vm.createContext(sandbox);
142
160
@@ -162,14 +180,15 @@ if (process.argv[2]) {
162
180
163 module.loaded = true;
181 module.loaded = true;
164
182
165− if (typeof res !== 'undefined') {
183+ if (typeof res !== "undefined") {
166 console.log(res);
184 console.log(res);
167 }
185 }
168 }
186 }
169
187
188+ if (process.env.PRYBAR_INTERACTIVE) {
170− process.on('SIGINT', () => start(context));
189+ process.once("SIGINT", () => start(context));
171−
172− process.on('beforeExit', () => start(context));
190+ process.once("beforeExit", () => start(context));
173−} else {
191+ }
192+} else if (process.env.PRYBAR_INTERACTIVE) {
174 start();
193 start();
175−}
194+}
languages/nodejs/version.jsdeleted−1
@@ -1 +0,0 @@
1−console.log('Node ' + process.version + ' on ' + process.platform);