ramblings/oxygen/js-require/commons.js

55 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-05-01 18:47:34 +00:00
/*
* Global object.
*
2021-05-02 08:57:58 +00:00
* This object can't be initialized when common.js is first read since the authorAccess object isn't available yet.
*
* For the same reason, include() and require() *cannot* be called from here.
2021-05-01 18:47:34 +00:00
*
*/
2021-05-01 21:40:15 +00:00
2021-05-01 22:06:36 +00:00
/*
* Shortcuts (aka imports)
2021-05-01 22:02:46 +00:00
*/
2021-05-01 21:40:15 +00:00
out = Packages.java.lang.System.out;
2021-05-01 18:47:34 +00:00
CommonsOperationsUtil = Packages.ro.sync.ecss.extensions.commons.operations.CommonsOperationsUtil;
2021-05-02 08:50:48 +00:00
/*
* Function include
*
*/
include = function (filepath) {
out.println('include("' + filepath + '")');
filepath = CommonsOperationsUtil.expandAndResolvePath(authorAccess, filepath);
var text = new java.lang.String(java.nio.file.Files.readAllBytes(java.nio.file.Paths. get (java.net.URI(filepath))));
text = String(text);
eval(text);
}
2021-05-01 22:02:46 +00:00
2021-05-01 22:06:36 +00:00
/*
2021-05-01 22:02:46 +00:00
* Function require
2021-05-01 22:06:36 +00:00
*
2021-05-01 22:02:46 +00:00
*/
2021-05-01 18:47:34 +00:00
require = function (filepath) {
2021-05-02 08:50:48 +00:00
out.println('require("' + filepath + '")');
2021-05-01 22:06:36 +00:00
2021-05-01 18:47:34 +00:00
// Check if the object has been initialized
2021-05-01 22:02:46 +00:00
if (Object.keys(require).length == 0) {
2021-05-01 18:47:34 +00:00
var currentPath = CommonsOperationsUtil.expandAndResolvePath(authorAccess, 'commons.js');
2021-05-01 22:02:46 +00:00
require.urisStack =[currentPath];
require.urisSet = {
2021-05-01 18:47:34 +00:00
};
2021-05-01 22:02:46 +00:00
require.urisSet[currentPath] = true;
2021-05-01 18:47:34 +00:00
}
2021-05-01 22:06:36 +00:00
2021-05-01 22:02:46 +00:00
var currentFilePath = require.urisStack[require.urisStack.length - 1];
2021-05-01 21:40:15 +00:00
var currentURI = new java.net.URI(currentFilePath);
var targetURI = currentURI.resolve(filepath);
filepath = targetURI.toString();
2021-05-01 22:02:46 +00:00
if (require.urisSet[filepath] == undefined) {
require.urisStack.push(filepath);
require.urisSet[filepath] = true;
2021-05-02 08:50:48 +00:00
include(filepath);
2021-05-01 22:02:46 +00:00
require.urisStack.pop();
2021-05-01 18:47:34 +00:00
}
2021-05-02 08:50:48 +00:00
}