-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
133 lines (99 loc) · 3.25 KB
/
main.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var FILE = require("modules/file"),
ARGS = require("modules/args"),
UTIL = require("modules/util"),
SYSTEM = require("modules/system");
exports.main = function(env)
{
var optParser = new ARGS.Parser(),
cliOptions;
optParser.arg("ProgramName");
optParser.help("Run a given demo program using the Demo Program Runner");
optParser.option("--port").set().help("If specified serve program via HTTP port");
optParser.option("-h", "--help").bool().help("Display usage information");
cliOptions = optParser.parse(["./"].concat(env.args));
if (cliOptions.help === true)
{
optParser.printHelp(cliOptions);
return;
}
if (cliOptions.args.length === 0)
{
optParser.print('\0red(\0bold(' + "Error: No ProgramName specified!" + '\0)\0)\n');
optParser.printHelp(cliOptions);
return;
}
var testProgramsPath = FILE.dirname(module.id),
programPath = cliOptions.args[0].replace(/\/$/,""),
programAbsolutePath = FILE.realpath(module.id.replace(/\/[^\/]*$/, "/" + programPath));
module.print("\0cyan(|---\n");
module.print("| You are running the '" + programPath + "' demo program via the Demo Program Runner included in the `test-programs-js` project.\n");
module.print("|---\0)\n");
runProgram(function()
{
if (cliOptions.port)
{
serveProgram();
}
});
function runProgram(callback)
{
// Run program inline
var command = "commonjs --platform " + require.platform + " " + programAbsolutePath;
module.load({
"location": programAbsolutePath
}, function(id)
{
module.print("\0cyan(|---\n");
module.print("| Running program (equivalent command):\0) \0magenta(" + command + "\0)\n");
module.print("\0cyan(|--- Program stdout & stderr follows --->\0)\n");
var sEnv = UTIL.deepCopy(env);
sEnv.args.shift();
sEnv.onDone = function()
{
callback();
}
if (require(id).main(sEnv) !== sEnv.onDone)
{
callback();
}
});
}
function serveProgram()
{
// Serve program via HTTP port
var command = "commonjs --platform node --script export " + testProgramsPath + " " + programPath + " " + programPath + "/exported";
module.print("\0cyan(|---\n");
module.print("| Exporting program:\0) \0magenta(" + command + "\0)\n");
module.print("\0cyan(|--->\0)\n");
exportProgram();
function exportProgram()
{
SYSTEM.exec(command, function(stdout, stderr)
{
module.print(stdout);
module.print("\0red(" + stderr + "\0)");
runServer();
});
}
function runServer()
{
module.load({
"location": require.pkg(module.mappings["server"]).id()
}, function(id)
{
var sEnv = UTIL.deepCopy(env);
sEnv.programPathResolver = function(path)
{
return testProgramsPath + "/server";
}
sEnv.args.unshift("--reloading");
command = "commonjs --platform node --script serve " + sEnv.programPathResolver() + " --reloading --port " + cliOptions.port + " " + sEnv.programPathResolver() + " " + programAbsolutePath;
module.print("\0cyan(|---\n");
module.print("| Running server (equivalent command):\0) \0magenta(" + command + "\0)\n");
module.print("\0cyan(|--- Server stdout & stderr follows --->\0)\n");
sEnv.args.push(programAbsolutePath);
require(id).main(sEnv);
});
}
}
}