welsonjs/app.js

217 lines
6.1 KiB
JavaScript
Raw Normal View History

2020-06-28 14:22:57 +00:00
//////////////////////////////////////////////////////////////////////////////////
//
// app.js
//
// Bootstrap code for running a javascript app in windows. Run as:
//
// cscript.js app.js <appname> <app arguments> ...
//
/////////////////////////////////////////////////////////////////////////////////
2020-07-03 09:15:23 +00:00
//"use strict";
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
// 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 module = { ... };
// return module;
//
// 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.
2020-07-07 23:16:52 +00:00
2020-07-23 02:41:33 +00:00
var exit = function(status) {
if (typeof(WScript) !== "undefined") {
WScript.quit(status);
}
2020-07-23 02:44:02 +00:00
console.warn("Exit caused by: " + status);
2020-07-23 02:41:33 +00:00
};
2020-07-03 09:15:23 +00:00
2020-06-28 14:22:57 +00:00
var console = {
2020-07-23 02:41:33 +00:00
__messages: [],
__echo: function(msg) {
if (typeof(WScript) !== "undefined") {
2020-07-03 11:14:20 +00:00
WScript.echo(msg);
}
2020-07-23 02:41:33 +00:00
this.__messages.push(msg);
2020-07-07 15:55:50 +00:00
},
2020-07-23 02:41:33 +00:00
log: function(msg) {
this.__echo(msg);
},
error: function(msg) {
var msg = "[ERROR] " + msg;
this.__echo(msg);
2020-07-07 17:06:38 +00:00
},
info: function(msg) {
2020-07-23 02:41:33 +00:00
var msg = "[INFO] " + msg;
this.__echo(msg);
2020-07-07 17:06:38 +00:00
},
warn: function(msg) {
2020-07-23 02:41:33 +00:00
var msg = "[WARN] " + msg;
this.__echo(msg);
2020-07-23 02:47:17 +00:00
},
debug: function(msg) {
var msg = "[DEBUG] " + msg;
this.__echo(msg);
2020-07-07 17:06:38 +00:00
}
2020-06-28 14:22:57 +00:00
};
2020-07-23 09:03:58 +00:00
if(typeof(GetObject) !== "function") {
function GetObject(pathName, className) {
console.warn("Not supported function GetObject() in GUI mode");
};
}
if(typeof(CreateObject) !== "function") {
function CreateObject(className) {
return new ActiveXObject(className);
};
2020-06-28 14:22:57 +00:00
}
function require(FN) {
var cache = require.__cache = require.__cache || {};
if (FN.substr(FN.length - 3) !== '.js') FN += ".js";
if (cache[FN]) return cache[FN];
2020-07-23 02:05:57 +00:00
// get directory name
var getDirName = function(path) {
var pos = path.lastIndexOf("\\");
return path.substring(0, pos);
};
2020-07-21 09:01:38 +00:00
// get current script directory
var getCurrentScriptDirectory = function() {
if(typeof(WScript) !== "undefined") {
var path = WScript.ScriptFullName;
2020-07-23 02:05:57 +00:00
return getDirName(path);
2020-07-21 09:01:38 +00:00
} else {
return ".";
}
};
2020-07-23 02:05:57 +00:00
// get file and directory name
var __filename = getCurrentScriptDirectory() + "\\" + FN;
var __dirname = getDirName(__filename);
// load script file
2020-06-28 14:22:57 +00:00
var FSO = CreateObject("Scripting.FileSystemObject");
var T = null;
try {
2020-07-23 02:05:57 +00:00
TS = FSO.OpenTextFile(__filename, 1);
2020-06-28 14:22:57 +00:00
if (TS.AtEndOfStream) return "";
T = TS.ReadAll();
TS.Close();
TS = null;
} catch (e) {
2020-07-07 15:55:50 +00:00
console.error("LOAD ERROR! " + e.number + ", " + e.description + ", FN=" + FN, 1);
2020-06-28 14:22:57 +00:00
return;
}
// make global function
2020-06-28 14:22:57 +00:00
FSO = null;
2020-07-23 02:05:57 +00:00
T = "(function(global){var module={exports:{}};return(function(exports,require,module,__filename,__dirname){" + '"use strict";' + T + "\nreturn exports})(module.exports,global.require,module,__filename,__dirname)})(this);\n\n////@ sourceURL=" + FN;
2020-06-28 14:22:57 +00:00
try {
cache[FN] = eval(T);
} catch (e) {
2020-07-07 15:55:50 +00:00
console.error("PARSE ERROR! " + e.number + ", " + e.description + ", FN=" + FN, 1);
2020-06-28 14:22:57 +00:00
}
// check type of callback return
if(typeof(cache[FN]) === "object") {
if ("VERSIONINFO" in cache[FN]) console.log(cache[FN].VERSIONINFO);
2020-07-07 23:03:02 +00:00
}
return cache[FN];
2020-07-07 23:03:02 +00:00
}
2020-07-23 09:03:58 +00:00
/////////////////////////////////////////////////////////////////////////////////
// get configuration variables
/////////////////////////////////////////////////////////////////////////////////
var __config = require("config");
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
// Load script, and call app.main()
/////////////////////////////////////////////////////////////////////////////////
function init_console() {
if(typeof(WScript) === "undefined") {
console.error("Error, WScript is not defined", 1);
}
2020-07-03 09:15:23 +00:00
var n = WScript.arguments.length;
if (n > 0) {
2020-06-28 14:22:57 +00:00
var args = [];
2020-07-03 09:15:23 +00:00
for (var i = 0; i < n; i++) {
2020-06-28 14:22:57 +00:00
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 {
2020-07-07 15:55:50 +00:00
console.error("Error, missing main entry point in " + name + ".js", 1);
2020-06-28 14:22:57 +00:00
}
} else {
2020-07-07 15:55:50 +00:00
console.error("Error, cannot find " + name + ".js", 1);
2020-06-28 14:22:57 +00:00
}
}
}
2020-07-03 09:15:23 +00:00
function init_window(name, args, w, h) {
if(typeof(window) === "undefined") {
console.error("Error, window is not defined", 1);
}
2020-06-28 14:22:57 +00:00
var app = require(name);
2020-07-03 09:15:23 +00:00
2020-07-04 12:49:30 +00:00
// "set default size of window";
2020-07-03 11:14:20 +00:00
if (typeof(w) !== "undefined" && typeof(h) !== "undefined") {
2020-07-03 09:15:23 +00:00
window.resizeTo(w, h);
}
2020-07-04 12:49:30 +00:00
// "load app";
2020-06-28 14:22:57 +00:00
if (app) {
if (app.main) {
2020-07-04 12:49:30 +00:00
var exitstatus = app.main.call(app, args);
if (exitstatus > 0) {
2020-07-23 02:41:33 +00:00
console.error("error");
exit(exitstatus);
2020-07-04 12:49:30 +00:00
}
2020-06-28 14:22:57 +00:00
} else {
2020-07-23 02:41:33 +00:00
console.error("Error, missing main entry point in " + name + ".js");
exit(1);
2020-06-28 14:22:57 +00:00
}
} else {
2020-07-23 02:41:33 +00:00
console.error("Error, cannot find " + name + ".js");
exit(1);
2020-06-28 14:22:57 +00:00
}
}
function main() {
2020-07-04 12:49:30 +00:00
if (typeof(window) === "undefined") {
2020-06-28 14:22:57 +00:00
init_console();
} else {
console.log("welcome");
}
}
2020-07-07 00:47:38 +00:00
main();