welsonjs/app.js

369 lines
11 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-11-18 04:13:38 +00:00
2020-07-23 02:41:33 +00:00
var exit = function(status) {
2021-08-10 07:44:37 +00:00
console.error("Exit caused by status " + status);
2021-04-28 07:28:43 +00:00
2020-07-23 02:41:33 +00:00
if (typeof(WScript) !== "undefined") {
WScript.quit(status);
2021-04-28 07:28:43 +00:00
} else if (typeof(window) !== "undefined") {
2021-04-28 07:32:17 +00:00
window.close();
}
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 = {
2021-04-29 07:49:36 +00:00
_timers: {},
2021-04-29 08:05:53 +00:00
_counters: {},
2020-11-04 07:41:56 +00:00
_messages: [],
2020-11-25 03:16:42 +00:00
_join: function(args, sep) {
args = args || [];
sep = sep || ' ';
var res = '';
for (var i = args.length - 1; i > -1; i--) {
res = (i ? sep : '') + args[i] + res;
}
return res;
},
2022-01-27 01:34:48 +00:00
_echoCallback: null,
2020-11-18 04:13:38 +00:00
_echo: function(args, type) {
2022-02-09 10:09:33 +00:00
var message = "";
var params = {
type: type,
channel: 'default',
messsage: ''
};
if (args.length > 0) {
if (typeof args[0] === "string") {
// if not type is "log", then "{type}: {message}"
if (typeof type !== "undefined") {
message += (type + ": " + this._join(args));
} else {
message += this._join(args);
}
if (typeof WScript !== "undefined") {
WScript.echo(" * " + message)
}
this._messages.push(message);
2022-02-24 08:13:31 +00:00
params.message = message;
2022-02-09 10:09:33 +00:00
} else if (typeof args[0] === "object") {
if ('message' in args[0]) {
2022-02-24 08:13:31 +00:00
if (typeof type !== "undefined") {
message += (type + ": " + args[0].message);
} else {
message += args[0].message;
}
2022-02-09 10:09:33 +00:00
}
if (typeof WScript !== "undefined") {
WScript.echo(" * " + message);
}
this._messages.push(args[0].message);
for (var k in args[0]) {
params[k] = args[0][k];
}
}
2020-07-03 11:14:20 +00:00
}
2022-01-27 01:34:48 +00:00
// after calling echo
2022-01-27 02:10:39 +00:00
if (['error', 'info', 'warn'].indexOf(type) > -1 && typeof this._echoCallback === "function") {
2022-02-25 07:17:29 +00:00
try {
this._echoCallback(params);
} catch (e) {
2022-02-24 08:13:31 +00:00
if (typeof WScript !== "undefined") {
WScript.echo(" * Exception of _echoCallback:", e.message);
}
2022-02-25 07:17:29 +00:00
}
2022-01-27 01:34:48 +00:00
}
2020-07-07 15:55:50 +00:00
},
2021-04-28 07:45:56 +00:00
assert: function(assertion) {
if (arguments.length > 1 && assertion === arguments[0]) {
if(!assertion) {
this.error("Assertion failed: " + this._join(arguments.slice(1)));
}
}
},
2021-04-28 07:28:43 +00:00
clear: function() {
this._messages = [];
},
2020-11-18 04:13:38 +00:00
log: function() {
this._echo(arguments);
2020-07-23 02:41:33 +00:00
},
2020-11-18 04:13:38 +00:00
error: function() {
this._echo(arguments, "error");
2020-07-07 17:06:38 +00:00
},
2020-11-18 04:13:38 +00:00
info: function() {
this._echo(arguments, "info");
2020-07-07 17:06:38 +00:00
},
2020-11-18 04:13:38 +00:00
warn: function() {
this._echo(arguments, "warn");
2020-07-23 02:47:17 +00:00
},
2020-11-18 04:13:38 +00:00
debug: function() {
this._echo(arguments, "debug");
2021-04-29 07:49:36 +00:00
},
time: function(label) {
2021-04-29 08:05:53 +00:00
label = label || "default";
if (!(label in this._timers)) {
this._timers[label] = new Date();
}
2021-04-29 07:49:36 +00:00
},
timeLog: function(label, end) {
2021-04-29 08:05:53 +00:00
label = label || "default";
2021-04-29 07:49:36 +00:00
if (label in this._timers) {
2021-08-10 08:48:47 +00:00
console.debug(label + ":", ((new Date()).getTime() - this._timers[label].getTime()) + "ms", (end ? " - timer ended" : ""));
2021-04-29 07:49:36 +00:00
}
},
timeEnd: function(label) {
2021-04-29 08:05:53 +00:00
label = label || "default";
2021-04-29 07:49:36 +00:00
if (label in this._timers) {
this.timeLog();
delete this._timers[label];
}
2021-04-29 08:05:53 +00:00
},
count: function(label) {
label = label || "default";
if (!(label in this._counters)) {
this._counters[label] = 1;
}
},
countReset: function(label) {
label = label || "default";
if (label in this._counters) {
this.timeLog();
delete this._counters[label];
}
2020-07-07 17:06:38 +00:00
}
2020-06-28 14:22:57 +00:00
};
2021-08-10 07:46:02 +00:00
if (typeof(CreateObject) === "undefined") {
2020-11-10 09:13:41 +00:00
var CreateObject = function(progId, serverName, callback) {
2021-08-10 06:27:47 +00:00
var progIds = (progId instanceof Array ? progId : [progId]);
2020-10-20 05:18:00 +00:00
var _CreateObject = function(p, s) {
if (typeof(WScript) !== "undefined") {
return WScript.CreateObject(p, s);
} else {
2020-11-04 07:30:52 +00:00
return new ActiveXObject(p);
2020-10-20 05:18:00 +00:00
}
};
for (var i = 0; i < progIds.length; i++) {
try {
2020-11-10 09:13:41 +00:00
var obj = _CreateObject(progIds[i], serverName);
if (typeof(callback) === "function") {
callback(obj, progIds[i]);
}
return obj;
2020-11-04 07:30:52 +00:00
} catch (e) {
console.error(e.message);
};
2020-10-20 05:18:00 +00:00
}
2020-07-23 10:39:24 +00:00
};
2020-07-23 10:35:53 +00:00
}
/**
* @FN {string} The name of the file.
*/
2021-08-10 06:27:47 +00:00
function include(FN) {
2021-08-10 06:33:57 +00:00
if (FN.substr(FN.length - 3) !== '.js') FN += ".js";
return eval(require.__load__(FN));
2021-08-10 06:27:47 +00:00
}
2021-06-20 18:17:58 +00:00
2021-08-10 06:27:47 +00:00
/**
* @FN {string} The name of the file.
*/
function require(FN) {
var cache = require.__cache__ = require.__cache__ || {};
2021-08-10 06:32:32 +00:00
2020-06-28 14:22:57 +00:00
if (FN.substr(FN.length - 3) !== '.js') FN += ".js";
if (cache[FN]) return cache[FN];
2021-08-10 06:33:57 +00:00
2020-07-23 02:05:57 +00:00
// get file and directory name
2021-08-10 06:27:47 +00:00
var __filename__ = require.__getCurrentScriptDirectory__() + "\\" + FN;
var __dirname__ = require.__getDirName__(__filename__);
2021-08-10 06:33:57 +00:00
var T = require.__load__(FN);
2021-08-10 06:32:32 +00:00
// build
2021-08-10 06:27:47 +00:00
T = "(function(global){var module=new require.__ModuleObject__();return(function(exports,require,module,__filename,__dirname){"
2021-04-29 12:17:15 +00:00
+ '"use strict";'
+ T
2021-08-10 06:27:47 +00:00
+ "\n\nreturn module.exports})(module.exports,global.require,module,__filename__,__dirname__)})(require.__global__);\n\n////@ sourceURL="
+ FN
2021-08-10 06:33:57 +00:00
;
2021-08-10 06:32:32 +00:00
// execute
2021-08-10 06:33:57 +00:00
try {
cache[FN] = eval(T);
} catch (e) {
2021-08-10 07:41:25 +00:00
console.error("PARSE ERROR!", e.number + ",", e.description + ",", "FN=" + FN);
2021-08-10 06:33:57 +00:00
}
2020-06-28 14:22:57 +00:00
2021-08-10 06:32:32 +00:00
// print VERSIONINFO
2020-07-23 10:39:24 +00:00
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
}
2021-08-10 06:27:47 +00:00
require.__global__ = this;
require.__ModuleObject__ = function() {
this.exports = {};
};
require.__getDirName__ = function(path) {
2021-08-10 06:33:57 +00:00
var delimiter = "\\";
var pos = path.lastIndexOf(delimiter);
return (pos > -1 ? path.substring(0, pos) : "");
2021-08-10 06:27:47 +00:00
};
require.__getCurrentScriptDirectory__ = function() {
2021-08-10 06:33:57 +00:00
if (typeof(WScript) !== "undefined") {
return require.__getDirName__(WScript.ScriptFullName);
} else if (typeof(document) !== "undefined") {
return require.__getDirName__(document.location.pathname);
} else {
return ".";
}
2021-08-10 06:27:47 +00:00
};
require.__load__ = function(FN) {
2021-08-10 06:33:57 +00:00
// get filename
var __filename__ = require.__getCurrentScriptDirectory__() + "\\" + FN;
// load script file
// use ADODB.Stream instead of Scripting.FileSystemObject, because of UTF-8 (unicode)
var objStream = CreateObject("ADODB.Stream");
var T = null;
try {
objStream.charSet = "utf-8";
objStream.open();
objStream.loadFromFile(__filename__);
T = objStream.readText();
objStream.close();
} catch (e) {
2021-08-10 07:41:25 +00:00
console.error("LOAD ERROR!", e.number + ",", e.description + ",", "FN=" + FN);
2021-08-10 06:33:57 +00:00
return;
}
return T;
2021-08-10 06:27:47 +00:00
};
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
// Load script, and call app.main()
/////////////////////////////////////////////////////////////////////////////////
2021-08-10 06:27:47 +00:00
function initializeConsole() {
2020-07-23 10:39:24 +00:00
if (typeof(WScript) === "undefined") {
2020-07-27 02:31:52 +00:00
console.error("Error, WScript is not defined");
exit(1);
}
2020-07-27 02:31:52 +00:00
var argl = WScript.arguments.length;
if (argl > 0) {
2020-06-28 14:22:57 +00:00
var args = [];
2020-07-27 02:31:52 +00:00
for (var i = 0; i < argl; 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) {
2021-08-10 06:35:32 +00:00
var exitStatus = app.main.call(this, args);
if (typeof(exitStatus) !== "undefined") {
exit(exitStatus);
2020-06-28 14:22:57 +00:00
}
} else {
2021-08-10 06:27:47 +00:00
console.error("Error, missing main entry point in", name + ".js");
2020-06-28 14:22:57 +00:00
}
} else {
2021-08-10 06:27:47 +00:00
console.error("Error, cannot find", name + ".js");
2020-06-28 14:22:57 +00:00
}
}
}
2021-08-10 06:27:47 +00:00
function initializeWindow(name, args, w, h) {
2020-07-23 10:39:24 +00:00
if (typeof(window) === "undefined") {
2020-07-27 02:31:52 +00:00
console.error("Error, window is not defined");
exit(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-11-25 03:32:04 +00:00
var exitStatus = app.main.call(app, args);
if (exitStatus > 0) {
exit(exitStatus);
2020-07-04 12:49:30 +00:00
}
2020-06-28 14:22:57 +00:00
} else {
2021-08-10 06:36:55 +00:00
console.error("Error, missing main entry point in", name + ".js");
2020-07-23 02:41:33 +00:00
exit(1);
2020-06-28 14:22:57 +00:00
}
} else {
2021-08-10 06:36:55 +00:00
console.error("Error, cannot find", name + ".js");
2020-07-23 02:41:33 +00:00
exit(1);
2020-06-28 14:22:57 +00:00
}
}
// JSON 2
2021-08-10 06:27:47 +00:00
include("app/assets/js/json2");
// JSON 3 was a JSON polyfill for older JavaScript platforms
2021-06-20 18:17:58 +00:00
//var JSON = require("app/assets/js/json3-3.3.2.min");
2021-08-10 07:08:54 +00:00
// core-js (formerly babel-polyfill)
require("app/assets/js/corejs-build-20210810");
2021-08-10 07:08:54 +00:00
// es5-shims
//require("app/assets/js/es5-shim-4.5.15.min");
//require("app/assets/js/es5-sham-4.5.15.min");
2020-11-14 14:32:30 +00:00
// Squel.js SQL query string builder for Javascript
2021-08-10 06:27:47 +00:00
var squel = require("app/assets/js/squel-basic-5.13.0.hiddentao-afa1cb5.wsh");
2020-11-14 14:32:30 +00:00
2022-02-08 02:47:07 +00:00
// JavaScript YAML parser and dumper.
var yaml = require("app/assets/js/js-yaml-4.1.0.wsh");
// Dive into entrypoint
2021-08-10 06:27:47 +00:00
function __main__() {
2020-07-04 12:49:30 +00:00
if (typeof(window) === "undefined") {
2021-08-10 06:27:47 +00:00
initializeConsole();
2020-06-28 14:22:57 +00:00
} else {
console.log("welcome");
}
}
2021-08-10 06:27:47 +00:00
__main__();