-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·92 lines (86 loc) · 2.74 KB
/
app.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
//////////////////////////////////////////////////////////////////////////////////
//
// app.js
//
// Bootstrap code for running a javascript app in windows. Run as:
//
// cscript.js app.js <appname> <app arguments> ...
//
/////////////////////////////////////////////////////////////////////////////////
"use strict";
/////////////////////////////////////////////////////////////////////////////////
// Bootstrap code, basic module loading functionality
/////////////////////////////////////////////////////////////////////////////////
//
// The module loaded is run inside a function, with one argument, global which
// points to the global context. So global.FN is the same as FN (as long as a
// version of FN does not exist in local scope).
//
// The module should return its interface at the end of the script. The basic
// pattern for a module is:-
//
// var interface = { ... };
// return interface;
//
// Or:-
//
// return function() {
// }
//
// The appname argument causes <appname>.js to be loaded. The interface returned
// must define main = function(args) {}, which is called once the module is
// loaded.
function require(FN) {
var cache = require.__cache = require.__cache || {};
if (FN.substr(FN.length-3) !== '.js') FN += ".js";
if (cache[FN]) return cache[FN];
var FSO = WScript.CreateObject("Scripting.FileSystemObject");
var T = null;
try {
var TS = FSO.OpenTextFile(FN,1);
if (TS.AtEndOfStream) return "";
T = TS.ReadAll();
TS.Close();
TS = null;
} catch(e) {
WScript.echo("LOAD ERROR! " + e.number + ", " + e.description + ", FN=" + FN);
WScript.quit();
return;
}
FSO = null;
T = "(function(global){\n" + '"use strict";' + "\n" + T + "})(this);\n\n////@ sourceURL=" + FN;
try {
cache[FN] = eval(T);
} catch(e) {
WScript.echo("PARSE ERROR! " + e.number + ", " + e.description + ", FN=" + FN);
WScript.quit();
}
if ("VERSIONINFO" in cache[FN]) WScript.echo(cache[FN].VERSIONINFO);
return cache[FN];
}
/////////////////////////////////////////////////////////////////////////////////
// Load script, and call app.main()
/////////////////////////////////////////////////////////////////////////////////
var arguments = WScript.arguments;
if (arguments.length > 0) {
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(WScript.arguments(i));
}
var name = args.shift();
var app = require(name);
if (app) {
if (app.main) {
var exitstatus = app.main.call(app, args);
if (typeof exitstatus != undefined) {
WScript.quit(exitstatus);
}
} else {
WScript.echo("Error, missing main entry point in " + name + ".js");
WScript.quit(1);
}
} else {
WScript.echo("Error, cannot find " + name + ".js");
WScript.quit(1);
}
}