welsonjs/app.js

287 lines
8.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-11-18 04:13:38 +00:00
2020-07-23 02:41:33 +00:00
var exit = function(status) {
if (typeof(WScript) !== "undefined") {
WScript.quit(status);
}
2020-11-04 09:32:25 +00:00
console.warn("Exit caused: " + 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-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;
},
2020-11-18 04:13:38 +00:00
_echo: function(args, type) {
2020-11-25 03:16:42 +00:00
msg = (typeof(type) !== "undefined" ? type + ": " : "") + this._join(args);
2020-07-23 02:41:33 +00:00
if (typeof(WScript) !== "undefined") {
2020-11-05 07:50:05 +00:00
WScript.echo(" * " + msg);
2020-07-03 11:14:20 +00:00
}
2020-11-04 07:41:56 +00:00
this._messages.push(msg);
2020-07-07 15:55:50 +00:00
},
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");
2020-07-07 17:06:38 +00:00
}
2020-06-28 14:22:57 +00:00
};
2020-07-23 10:39:24 +00:00
if (typeof(CreateObject) !== "function") {
2020-11-10 09:13:41 +00:00
var CreateObject = function(progId, serverName, callback) {
2020-10-20 05:18:00 +00:00
var progIds = [];
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
}
};
if (typeof(progId) == "object") {
progIds = progId;
} else {
progIds.push(progId);
}
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.
* @escapeToGlobal {bool} Specifies the scope of code to execute.
*/
function _require(FN, escapeToGlobal) {
2020-06-28 14:22:57 +00:00
var cache = require.__cache = require.__cache || {};
if (FN.substr(FN.length - 3) !== '.js') FN += ".js";
if (cache[FN]) return cache[FN];
if (typeof(escapeToGlobal) != "boolean") escapeToGlobal = true;
2020-07-23 02:05:57 +00:00
// get directory name
var getDirName = function(path) {
2020-08-05 07:35:43 +00:00
var delimiter = "\\";
var pos = path.lastIndexOf(delimiter);
return (pos > -1 ? path.substring(0, pos) : "");
2020-07-23 02:05:57 +00:00
};
2020-07-21 09:01:38 +00:00
// get current script directory
var getCurrentScriptDirectory = function() {
2020-07-23 10:39:24 +00:00
if (typeof(WScript) !== "undefined") {
2020-08-05 07:35:43 +00:00
return getDirName(WScript.ScriptFullName);
} else if (typeof(document) !== "undefined") {
return getDirName(document.location.pathname);
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-08-04 09:29:42 +00:00
// use ADODB.Stream instead of Scripting.FileSystemObject, because of UTF-8 (unicode)
var objStream = CreateObject("ADODB.Stream");
2020-06-28 14:22:57 +00:00
var T = null;
try {
2020-08-04 09:29:42 +00:00
objStream.charSet = "utf-8";
objStream.open();
objStream.loadFromFile(__filename);
T = objStream.readText();
objStream.close();
2020-06-28 14:22:57 +00:00
} catch (e) {
2020-11-25 03:32:04 +00:00
console.error("LOAD ERROR! ", e.number, ", ", e.description, ", FN=", FN);
2020-06-28 14:22:57 +00:00
return;
}
// make function
if (!escapeToGlobal) {
T = "(function(global){var module=new ModuleObject();return(function(exports,require,module,__filename,__dirname){"
+ '"use strict";'
+ T
+ "\n\nreturn module.exports})(module.exports,global.require,module,__filename,__dirname)})(this);\n\n////@ sourceURL="
+ FN;
}
// execute function
2020-06-28 14:22:57 +00:00
try {
cache[FN] = eval(T);
} catch (e) {
2020-11-25 03:32:04 +00:00
console.error("PARSE ERROR! ", e.number, ", ", e.description, ", FN=", FN);
2020-06-28 14:22:57 +00:00
}
// check type of callback return
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
}
/**
* @FN {string} The name of the file.
*/
function require(FN) {
return _require(FN, false);
}
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
// Load script, and call app.main()
/////////////////////////////////////////////////////////////////////////////////
function init_console() {
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) {
var exitstatus = app.main.call(this, args);
2020-07-27 02:31:52 +00:00
if (typeof(exitstatus) !== "undefined") {
exit(exitstatus);
2020-06-28 14:22:57 +00:00
}
} else {
2020-11-25 03:32:04 +00:00
console.error("Error, missing main entry point in ", name, ".js");
2020-06-28 14:22:57 +00:00
}
} else {
2020-11-25 03:32:04 +00:00
console.error("Error, cannot find ", name, ".js");
2020-06-28 14:22:57 +00:00
}
}
}
2020-07-03 09:15:23 +00:00
function init_window(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) {
2020-07-23 02:41:33 +00:00
console.error("error");
2020-11-25 03:32:04 +00:00
exit(exitStatus);
2020-07-04 12:49:30 +00:00
}
2020-06-28 14:22:57 +00:00
} else {
2020-11-25 03:32:04 +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 {
2020-11-25 03:32:04 +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
}
}
2020-11-14 15:55:58 +00:00
// define module object
var ModuleObject = function() {
this.exports = {};
};
// JSON 2
_require("app/assets/js/json2");
// Babel Browser Polyfill (6.22.0)
require("app/assets/js/babel-browser-polyfill-6.22.0");
2020-11-14 14:32:30 +00:00
// ECMAScript 5 compatibility shims for legacy (and modern) JavaScript engines
require("app/assets/js/es5-shim-4.5.14.min");
require("app/assets/js/es5-sham-4.5.14.min");
// Squel.js SQL query string builder for Javascript
var squel = require("app/assets/js/welsonjs-squel-basic-afa1cb5-modified");
// (Optional)
2020-11-14 14:32:30 +00:00
// JSON 3 was a JSON polyfill for older JavaScript platforms
//var JSON = require("app/assets/js/json3-3.3.2.min");
2020-11-14 14:32:30 +00:00
// (Optional)
2020-11-14 14:32:30 +00:00
// ECMAScript 6 compatibility shims for legacy JS engines
//require("app/assets/js/es6-shim-0.35.5.min");
//require("app/assets/js/es6-sham-0.35.5.min");
2020-11-14 14:32:30 +00:00
// Dive into entrypoint
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-26 09:53:25 +00:00
main();