Created
August 26, 2015 07:19
-
-
Save cking/b3ea724ac2c44ccb2c68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var util = require("util") | |
var path = require("path") | |
var fs = require("fs") | |
var debug = util.debuglog("dm-loader") | |
exports.install = function (dm) { | |
if (dm.directory) { | |
debug("dm.directory already defined! skipping") | |
return | |
} | |
debug("binding dm.directory") | |
dm.directory = dmDirectory.bind(dm) | |
} | |
function dmDirectory(dir, recursive) { | |
dir = path.resolve(dir) | |
if (arguments.length === 1) { | |
recursive = true | |
} | |
if (!fs.existsSync(dir)) { | |
debug("directory " + dir + " not found!") | |
return | |
} | |
fs.readdirSync(dir).forEach(function (file) { | |
var fullPath = path.join(dir, file) | |
if (fs.lstatSync(fullPath).isDirectory() && recursive) { | |
return this.dmDirectory(fullPath, recursive) | |
} | |
try { | |
debug("found " + file + ", trying to require") | |
var mod = require("file") | |
if (!mod.provide) { | |
throw new Error("Invalid module in " + file + " found! Please provide at least a provide method!") | |
} | |
this.provide( | |
mod.provide.name || mod.name || path.basename(file, path.extname(file)), // get the name of the module | |
mod.type || 'factory', // type of module, default to a simple factory | |
mod.provide, // the provide method/value/class/... | |
mod.depends || mod.provide.$depends || [] // provide the dependencies, either on the root, on the provide method (to conform to the node-dm standard), or as an empty array | |
) | |
} | |
}, this) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment