gnh1201 / welsonjs

WelsonJS - Build a Windows app on the Windows built-in JavaScript engine

Home Page:https://catswords.social/@catswords_oss

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[core] app.js module wrapper

gnh1201 opened this issue · comments

[app] app.js module wrapper

https://nodejs.org/api/modules.html#modules_the_module_wrapper

(function(global) {
    var module = {
        exports: {}
    };
    return (function(exports, require, module, __filename, __dirname) {
        "use strict";
        T;
        return exports;
    })(module.exports, global.require, module, "", "");
})(this);

completed code:

function require(FN) {
    var cache = require.__cache = require.__cache || {};
    if (FN.substr(FN.length - 3) !== '.js') FN += ".js";
    if (cache[FN]) return cache[FN];

    // get directory name
    var getDirName = function(path) {
        var pos = path.lastIndexOf("\\");
        return path.substring(0, pos);
    };

    // get current script directory
    var getCurrentScriptDirectory = function() {
        if(typeof(WScript) !== "undefined") {
            var path = WScript.ScriptFullName;
            return getDirName(path);
        } else {
            return ".";
        }
    };

    // get file and directory name
    var __filename = getCurrentScriptDirectory() + "\\" + FN;
    var __dirname = getDirName(__filename);

    // load script file
    var FSO = CreateObject("Scripting.FileSystemObject");
    var T = null;
    try {
        TS = FSO.OpenTextFile(__filename, 1);
        if (TS.AtEndOfStream) return "";
        T = TS.ReadAll();
        TS.Close();
        TS = null;
    } catch (e) {
        console.error("LOAD ERROR! " + e.number + ", " + e.description + ", FN=" + FN, 1);
        return;
    }

    // make global function
    FSO = null;
    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;
    try {
        cache[FN] = eval(T);
    } catch (e) {
        console.error("PARSE ERROR! " + e.number + ", " + e.description + ", FN=" + FN, 1);
    }

    // check type of callback return
    if(typeof(cache[FN]) === "object") {
        if ("VERSIONINFO" in cache[FN]) console.log(cache[FN].VERSIONINFO);
    }

    return cache[FN];
}