1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Rearranging boot.js

This commit is contained in:
Jeremy Ruston 2013-03-23 16:15:45 +00:00
parent 60bcac289a
commit d8b76f656e

View File

@ -852,6 +852,47 @@ $tw.boot.decryptEncryptedTiddlers = function(callback) {
callback();
};
/*
Execute the module named 'moduleName'. The name can optionally be relative to the module named 'moduleRoot'
*/
$tw.modules.execute = function(moduleName,moduleRoot) {
var name = moduleRoot ? $tw.utils.resolvePath(moduleName,moduleRoot) : moduleName,
moduleInfo = $tw.modules.titles[name],
tiddler = $tw.wiki.getTiddler(name),
sandbox = {
module: moduleInfo,
exports: {},
console: console,
process: process,
$tw: $tw,
require: function(title) {
return $tw.modules.execute(title,name);
}
};
if(!moduleInfo) {
// If we don't have a module with that name, let node.js try to find it
return require(moduleName);
}
// Execute the module if we haven't already done so
if(!moduleInfo.exports) {
try {
// Check the type of the definition
if(typeof moduleInfo.definition === "string") { // String
vm.runInNewContext(moduleInfo.definition,sandbox,tiddler.fields.title);
moduleInfo.exports = sandbox.exports;
} else if(typeof moduleInfo.definition === "function") { // Function
moduleInfo.exports = moduleInfo.definition(moduleInfo,sandbox.require,moduleInfo.exports);
} else { // Object
moduleInfo.exports = moduleInfo.definition;
}
} catch(e) {
throw "Error executing boot module " + name + ":\n" + e;
}
}
// Return the exports of the module
return moduleInfo.exports;
};
/*
Load the tiddlers contained in a particular file (and optionally extract fields from the accompanying .meta file)
*/
@ -950,47 +991,6 @@ $tw.loadBundleFolder = function(filepath,excludeRegExp) {
} : null;
};
/*
Execute the module named 'moduleName'. The name can optionally be relative to the module named 'moduleRoot'
*/
$tw.modules.execute = function(moduleName,moduleRoot) {
var name = moduleRoot ? $tw.utils.resolvePath(moduleName,moduleRoot) : moduleName,
moduleInfo = $tw.modules.titles[name],
tiddler = $tw.wiki.getTiddler(name),
sandbox = {
module: moduleInfo,
exports: {},
console: console,
process: process,
$tw: $tw,
require: function(title) {
return $tw.modules.execute(title,name);
}
};
if(!moduleInfo) {
// If we don't have a module with that name, let node.js try to find it
return require(moduleName);
}
// Execute the module if we haven't already done so
if(!moduleInfo.exports) {
try {
// Check the type of the definition
if(typeof moduleInfo.definition === "string") { // String
vm.runInNewContext(moduleInfo.definition,sandbox,tiddler.fields.title);
moduleInfo.exports = sandbox.exports;
} else if(typeof moduleInfo.definition === "function") { // Function
moduleInfo.exports = moduleInfo.definition(moduleInfo,sandbox.require,moduleInfo.exports);
} else { // Object
moduleInfo.exports = moduleInfo.definition;
}
} catch(e) {
throw "Error executing boot module " + name + ":\n" + e;
}
}
// Return the exports of the module
return moduleInfo.exports;
};
$tw.loadTiddlers = function() {
// On the server, we load tiddlers from specified folders
var folders = [