Permalink
Browse files

cleaned code, added example, added tests, rewrote moduleLoader, writi…

…ng dependencyMapper, writing core
  • Loading branch information...
1 parent d3c0fb4 commit 0618792cd658614f9d79cb8af7f4274b6aa29c92 @Raynos committed May 6, 2012
View
@@ -0,0 +1,27 @@
+var ncore = require(".."),
+ extend = require("pd").extend,
+ Core = extend({}, ncore).constructor({
+ "main": {
+ "hello": "hello",
+ "world": "world"
+ }
+ })
+
+Core.add("hello", {
+ hello: function () {
+ return "hello"
+ },
+ expose: ["hello"]
+})
+
+Core.add("world", function () {
+ return "world"
+})
+
+Core.add("main", {
+ init: function () {
+ console.log(this.hello.hello() + " " + this.world())
+ }
+})
+
+Core.init()
View
@@ -48,17 +48,18 @@ module.exports = {
all modules finish setup.
*/
init: function (callback) {
- var self = this,
- modules = self._modules,
- counter = 1
-
injectDependencies(this)
setupModules(this, callback)
}
}
function makeInterface(core, name, module) {
- var _interface = {}
+ var _interface
+ if (typeof module === "function") {
+ _interface = proxyModule
+ } else {
+ _interface = {}
+ }
bindAll(module)
if (module.expose) {
module.expose.forEach(addToInterface)
@@ -67,19 +68,33 @@ function makeInterface(core, name, module) {
}
return _interface
+ function proxyModule() {
+ return module.apply(this, arguments)
+ }
+
function addToInterface(name) {
_interface[name] = module[name]
}
}
function makeProxy(core, name, _interface) {
- var proxy = bindAll({}, EventEmitter),
- interfaces = core._interfaces
+ var interfaces = core._interfaces,
+ proxy
+
+ if (typeof _interface === "function") {
+ proxy = bindAll(proxyForFunction, EventEmitter)
+ } else {
+ proxy = bindAll({}, EventEmitter)
+ }
Object.keys(_interface).forEach(proxyProperty)
return proxy
+ function proxyForFunction() {
+ return interfaces[name].apply(this, arguments)
+ }
+
function proxyProperty(key) {
if (typeof _interface[key] === "function") {
proxy[key] = proxyFunction(key)
View
@@ -0,0 +1,32 @@
+var moduleLoader = require("./moduleLoader"),
+ extend = require("pd").extend,
+ ncore = require("..")
+
+var CORE_DEFAULTS = {
+ core: extend({}, ncore).constructor()
+}
+
+module.exports = {
+ core: function (options, callback) {
+ options = extend(CORE_DEFAULTS, options)
+ if (!options.moduleLoader.core) {
+ options.moduleLoader.core = options.core
+ }
+
+ var Core = options.core
+
+ var moduleLoader = Core.add("moduleLoader", moduleLoader)
+
+ moduleLoader.load(options.moduleLoader, next)
+
+ function next(err) {
+ if (err) {
+ if (callback) {
+ return callback(err)
+ }
+ console.log("Error occurred in core.core", err)
+ }
+ Core.init(callback)
+ }
+ }
+}
@@ -0,0 +1,42 @@
+var fs = require("fs"),
+ path = require("path"),
+ extend = require("pd").extend,
+ after = require("after")
+
+var DEPENDENCY_WRITE_DEFAULTS = {
+ jsonUri: path.join(process.cwd(), "dependencies.json"),
+ uri: path.join(process.cwd(), "modules")
+ },
+ isFile = /.js$/
+
+module.exports = {
+ map: function (options, callback) {
+ var json = require(options.jsonUri),
+ coreDependenciesResult = {}
+
+ after.forEach(json, mapToDependencies, returnResult)
+
+ function mapToDependencies(dependencies, moduleName, callback) {
+ var dependenciesResult = {}
+ if (isFile.test(moduleName)) {
+ after.forEach(dependencies, mapToProxyName, done)
+ } else {
+ done()
+ }
+
+ function mapToProxyName(proxyName, propertyName, callback) {
+ dependenciesResult[propertyName] = proxyName
+ callback()
+ }
+
+ function done() {
+ coreDependenciesResult[moduleName] = dependenciesResult
+ callback()
+ }
+ }
+
+ function returnResult(err) {
+ callback(err, coreDependenciesResult)
+ }
+ }
+}
View
@@ -1,146 +1,84 @@
var fs = require("fs"),
- pd = require("pd"),
- after = require("after"),
- DependencyWriter = require("./dependencyWriter"),
- path = require("path");
+ path = require("path"),
+ extend = require("pd").extend,
+ ncore = require(".."),
+ after = require("after")
-pd.extend(ModuleLoader, {
- getStatsOfURI: function getStatsOfURI() {
- if (!this.originalUri) {
- this.originalUri = this.uri;
- }
- fs.stat(this.uri, this.readFileOrFolder);
- },
- readFileOrFolder: function readFileOrFolder(err, stats) {
- if (err) {
- return this.callback(err);
- }
- if (stats.isFile()) {
- if (this.uri.substr(-3) !== ".js") {
- return this.callback();
- }
- if (this.skip) {
- if (this.uri.indexOf(this.skip) !== -1) {
- return this.callback()
- }
- }
- var module = require(this.uri),
- relative = path.relative(this.originalUri, this.uri);
- relative = relative.replace(".js", "");
- relative = relative.replace(/\//g, ".");
- this.core.add(relative, module);
- this.callback();
- } else if (stats.isDirectory()) {
- if (this.skip) {
- if (this.uri.indexOf(this.skip) !== -1) {
- return this.callback()
- }
- }
- fs.readdir(this.uri, this.readFiles);
- }
+var MODULE_LOADER_DEFAULTS = {
+ uri: path.join(process.cwd(), "modules"),
+ skip: /client/
},
- readFiles: function readFiles(err, files) {
- if (err) {
- return this.callback(err);
- }
- after.forEach(files, readFile, this, this.callback);
-
- function readFile(fileName, done) {
- ModuleLoader({
- uri: path.join(this.uri, fileName),
- core: this.core,
- skip: this.skip,
- originalUri: this.originalUri,
- callback: done
- });
- }
- }
-});
+ isJsFile = /.js$/
module.exports = {
/*
- loads all files in the folder. Each file is expected to be a export
- a module which is then attached to the core under a filename
-
- Modules are stored by the naming convention of <fileName> if directly
- in the folder or <folder>.<fileName>, <folder>.<folder>.<fileName>
- if stored nested in the folder
+ Loads modules and adds them to the core
- @param {Object} options -
+ @param {Object} options
{
- dependencies: {Object} - A dependency mapping for the
- files in the folder (uri). This is based on
- files/folderNames
- uri: {String} - uri of the folder to load
- skip: {String} - string to match the folder name again to skip
- callback: {Function} - callback to invoke when all modules
- are loaded and attached to core. Passes an error if
- the error occurs
- core: {Object} - instance of the core to attach to
+ Core: The core
+ skip: RegExp to skip by
+ uri: root uri to load
}
-
*/
- load: function load(options) {
- var counter = 2;
-
- after.forEach(options.dependencies, writeDependencies, next)
+ load: function (options, callback) {
+ options = extend(MODULE_LOADER_DEFAULTS, options)
+ var modulesFolder = options.uri
- ModuleLoader({
- uri: options.uri,
- skip: options.skip,
- core: options.core,
- callback: next
- })
+ iterateFiles(modulesFolder, loadModule, callback)
- function next(err) {
+ function loadModule(err, fileName) {
if (err) {
- return options.callback(err)
+ return callback(err)
}
- if (--counter === 0) {
- options.callback();
+ if (options.skip && options.skip.test(fileName)) {
+ return
}
+ var module = require(fileName)
+ var name = path.relative(options.uri, fileName)
+ options.core.add(name, module)
}
+ }
+}
+
+function iterateFiles(uri, callback, done) {
+ var counter = 1
+ fs.readdir(uri, readFiles)
- function writeDependencies(depObject, fileName, callback) {
- DependencyWriter({
- uri: path.join(options.uri, fileName),
- originalUri: options.uri,
- depObject: depObject,
- core: options.core,
- callback: callback
- });
+ function readFiles(err, files) {
+ if (err) {
+ return callback(err)
}
- },
- /*
- core reduces boilerplate by doing default actions
- */
- core: function core(uri, callback) {
- var Core = Object.create(require("..")).constructor()
- Core.use("moduleLoader", this)
+ counter += files.length
+ files.forEach(isDirOrFile)
+ next()
+ }
+
+ function isDirOrFile(fileName) {
+ fileName = path.join(uri, fileName)
- this.load({
- uri: uri,
- core: Core,
- skip: "client",
- dependencies: require(path.join(uri, "dependency.json")),
- callback: init
- })
+ fs.stat(fileName, readOrRecurse)
- function init(err) {
+ function readOrRecurse(err, stat) {
if (err) {
- if (callback) {
- return callback(err)
- }
- console.log("Error occurred in moduleLoader.core", err)
+ return callback(err)
}
- Core.init(callback)
- }
- },
- expose: ["load", "core"]
-};
+ if (stat.isDirectory()) {
+ iterateFiles(fileName, callback, next)
+ } else if (stat.isFile() && isJsFile.test(fileName)) {
+ callback(null, fileName)
+ next()
+ } else {
+ next()
+ }
+ }
+ }
-function ModuleLoader(options) {
- pd.bindAll({}, ModuleLoader, options).getStatsOfURI();
+ function next() {
+ if (--counter === 0) {
+ done(null)
+ }
+ }
}
Oops, something went wrong.

0 comments on commit 0618792

Please sign in to comment.