exports.list = function(www, parent, module, callback, listOnly) {
list(www, parent, module, callback, true);
};
exports.debug = false;
function walk(module) {
modules[module.id] = module;
path.push(module.id);
module.index = -1;
module.deps.forEach(function(d) {
d = modules[d.id] || d;
if (d.index === undefined) walk(d);
else if (d.index < 0) {
var str = "Module " + module.id + " is dependent on module " + d.id +
'. However, module ' + d.id + ' is also directly or indirectly dependent on module ' +
module.id + ".\nDependency path to this point: \n" + path.join(' relies on \n') +
' relies on ' + d.id;
console.log(str.red);
}
});
module.index = index++;
path.pop();
}
function endsWith(str, trail) {
return (str.substr(str.length-trail.length, str.length-1) === trail);
};
function trailWith(str, trail) {
return str ? (str + (!endsWith(str, trail) ? trail : '')) : undefined;
};
function list(www, parent, id, cb, listOnly) {
try
{ www = Path.resolve(www);
parent = Path.resolve(www, parent);
debug('Resolving: ' + id + ' in directory ' + parent);
var fileName = Path.resolve(parent, trailWith(id, '.js'));
try {
fs.statSync(fileName);
} catch(e) { cb(e,null);
return;}
required(fileName, {
includeSource: false
}, function(err, deps) {
if (err) throw err;
else {
modules = [];
walk({
id: id,
filename: fileName,
deps: deps,
index: -1
});
var list = Object.keys(modules).map(function(m) {
m = modules[m];
var startWithWwwPath = m.filename.indexOf(www) === 0;
if (!startWithWwwPath)
throw 'Warning: ' + m.id + ' was found outside the www directory (' + www + ')';
m.route = m.filename.slice(www.length);
debug('module:',m);
return m;
}).sort(function(a, b) {
return a.index > b.index;
}).map(function(m) {
return listOnly ?
{ id: m.id, route: m.route, filename: m.filename } :
"<script type=\"text/javascript\" src=\"" + m.route + "\"></script>";
});
debug('Debug:\n', list);
cb(null, list);
}
});
} catch(e) {
cb(e, null);
}
};
function debug() {
if (exports.debug) console.log.apply(console, arguments);
}