comparison modules.js @ 41:bc717575e66a

Much better handling of modules.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 20 Feb 2011 15:51:12 +0100
parents b2faacfefb90
children 023f767662d3
comparison
equal deleted inserted replaced
40:741110cdcfcf 41:bc717575e66a
1 var modules = exports; 1 var modules = exports;
2 2
3 var files = require('fs').readdirSync('modules'); 3 var fs = require('fs');
4 var dir = require('./configuration').pluginsDir;
5 var files = fs.readdirSync(dir);
4 var regex = /^mod_.*\.js/; 6 var regex = /^mod_.*\.js/;
7
8 var Script = process.binding('evals').Script;
9 var sandbox = {
10 process: process,
11 config: require('./configuration'),
12 xmpp: require('xmpp'),
13 errors: require('./errors'),
14 forms: require('./forms'),
15 makeError: require('./errors').makeError,
16 notifs: require('./notifs'),
17 NS: require('./namespaces'),
18 require: require,
19 storage: require('./storage'),
20 toBareJID: require('./fdsq').toBareJID,
21 exports: {}
22 };
5 23
6 for (var i in files) { 24 for (var i in files) {
7 var file = files[i]; 25 var file = files[i];
8 if (!regex.test(file)) 26 if (!regex.test(file))
9 continue; 27 continue;
10 28
11 var module = require('./modules/' + files[i]); 29 fs.readFile(dir + '/' + file, function(err, data) {
12 for (var j in module) { 30 if (err) {
13 if (typeof modules[j] != 'undefined') 31 console.log('ERROR: loading module “'+file+'”.'); // FIXME: file is always the last file of the list…
14 console.log('WARNING: module '+j+' already loaded.'); 32 return;
33 }
15 34
16 modules[j] = module[j]; 35 Script.runInNewContext(data, sandbox, dir + '/' + file); // FIXME: the same.
17 } 36 var module = sandbox.exports;
37 sandbox.exports = {};
38
39 for (var j in module) {
40 if (typeof modules[j] != 'undefined')
41 console.log('WARNING: module '+j+' already loaded.');
42
43 modules[j] = module[j];
44 }
45 });
18 } 46 }