forked from onilabs/stratifiedjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sjs
executable file
·112 lines (98 loc) · 2.68 KB
/
sjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env node
// vim: syntax=sjs:
var path = require('path');
var fs = require('fs');
var sjs_home = path.dirname(fs.realpathSync(__filename));
var sjs_node = require(sjs_home + '/stratified-node');
function usage() {
return (
"Usage: sjs [options] [script.sjs [arguments]]\n\n" +
"Without a script.sjs argument you get dropped into a stratified REPL\n\n" +
"Options:\n" +
" -h, --help display this help message\n" +
" -d, --dev load development mode (src/build/devmode.sjs)\n" +
" -e, --eval STR evaluate STR\n" +
"\nDocumentation is at http://onilabs.com/stratifiedjs");
}
// -------------------- possible runner functions: --------------------
function runSJScript(url) {
return function() {
sjs_node.run(url);
};
}
function runRepl(beforeHook) {
return function() {
var runRepl = function() {
sjs_node.run('sjs:nodejs/repl');
};
// Drop into REPL:
if(beforeHook) {
beforeHook(runRepl);
} else {
runRepl();
}
};
};
function loadDevExtensions(next) {
sjs_node.require("./src/build/devmode", {callback:
function(err, devmode) {
if(err) throw err;
process.stdout.write("devmode loaded (try `dev.eval` and `dev.build`)\n");
sjs_node.getGlobal().dev = devmode;
return next();
}
});
};
function runEval(str) {
return function() {
// XXX eval'd code will be operating on global scope; place sjs's 'require' function there:
sjs_node.getGlobal().require = sjs_node.require;
return sjs_node.eval(str);
};
};
// -------------------- process options & run --------------------
function processArgs() {
// default run function - execute REPL with no hooks
var run = runRepl(null);
for (var i=2; i<process.argv.length; ++i) {
var flag = process.argv[i];
switch (flag) {
case "-h":
case "--help":
throw new Error(usage());
case "-d":
case "--dev":
run = runRepl(loadDevExtensions);
break;
case '-e':
case '--eval':
++i;
var str = process.argv[i];
run = runEval(str);
break;
default:
// we assume 'flag' is a script. remove everything up to now from
// argv (except `node`) and exec it:
process.ARGV = process.argv = [process.argv[0]].concat(process.argv.slice(i));
if (flag == '--') {
return run;
}
if (flag[0] == '-') {
throw new Error("Unknown flag '"+flag+"'\n" + usage());
}
run = runSJScript(flag);
return run;
}
}
return run;
}
function main() {
try {
var run = processArgs();
} catch(e) {
process.stderr.write(e.message + "\n");
process.exit(1);
}
return run();
};
sjs_node.init(main);