Permalink
Browse files

cleanups and change api

  • Loading branch information...
drewlesueur committed Jul 22, 2012
1 parent 86359b0 commit 406338e2f79f3b0f13a0a1617de0317250154051
View
@@ -1,7 +1,7 @@
poor-module
===========
-a less than 50 line module for node.js and the browser.
+a less than 20 line module system for node.js and the browser.
This is a simple module system for use in node.js and the browser.
It doesn't try to load files. It just is for defining modules so you don't polute the global scope.
View
@@ -5,7 +5,6 @@ var fs = require('fs');
var http = require("https")
var exec = require("child_process").exec
-console.log("poor module")
var args = process.argv.slice(2)
var makePoorModulesDir = function (cb) {
@@ -42,9 +41,31 @@ var install = function (name) {
})
}
+var outputNode = function () {
+ exec("ls poor_modules", function (err, stdout){
+ files = stdout.split("\n")
+ for (var i = 0; i < files.length; i++) {
+ var file = files[i]
+ if (file.length) console.log("require(\"./poor_modules/"+file+"\")")
+ }
+ })
+}
+
+var outputBrowser = function () {
+ exec("ls poor_modules", function (err, stdout){
+ files = stdout.split("\n")
+ for (var i = 0; i < files.length; i++) {
+ var file = files[i]
+ if (file.length) console.log("<script src=\"/poor_modules/"+file+"\"></script>")
+ }
+ })
+}
var commands = {
- install : install
+ install : install,
+ "node": outputNode,
+ browser: outputBrowser
+
}
var command = args[0]
View
@@ -19,7 +19,9 @@ modules =
'poor-module':
name: 'poor-module'
url: 'https://raw.github.com/drewlesueur/poor-module/master/poor-module.js'
-
+ 'peppermint-expressions':
+ name: 'peppermint-expressoins'
+ url: 'https://raw.github.com/drewlesueur/peppermint-expressions/master/peppermint-expressions.js'
console.log JSON.stringify modules
View
@@ -1 +1 @@
-{"red-yarn":{"name":"red-yarn","dependencies":["underscore","rpc"],"url":"https://raw.github.com/drewlesueur/red-yarn/master/red-yarn.js"},"underscore":{"name":"underscore","url":"https://raw.github.com/documentcloud/underscore/master/underscore.js","poor-module":false},"rpc":{"name":"rpc","dependencies":["underscore"],"url":"https://raw.github.com/drewlesueur/red-yarn/master/rpc.js"},"pm":{"name":"poor-module","url":"https://raw.github.com/drewlesueur/poor-module/master/poor-module.js"},"poor-module":{"name":"poor-module","url":"https://raw.github.com/drewlesueur/poor-module/master/poor-module.js"}}
+{"red-yarn":{"name":"red-yarn","dependencies":["underscore","rpc"],"url":"https://raw.github.com/drewlesueur/red-yarn/master/red-yarn.js"},"underscore":{"name":"underscore","url":"https://raw.github.com/documentcloud/underscore/master/underscore.js","poor-module":false},"rpc":{"name":"rpc","dependencies":["underscore"],"url":"https://raw.github.com/drewlesueur/red-yarn/master/rpc.js"},"pm":{"name":"poor-module","url":"https://raw.github.com/drewlesueur/poor-module/master/poor-module.js"},"poor-module":{"name":"poor-module","url":"https://raw.github.com/drewlesueur/poor-module/master/poor-module.js"},"peppermint-expressions":{"name":"peppermint-expressoins","url":"https://raw.github.com/drewlesueur/peppermint-expressions/master/peppermint-expressions.js"}}
View
@@ -1,26 +1,13 @@
-//get global object, either window or global
-//if window? then window else if global? then global else this
-var root = typeof window !== "undefined" && window !== null ? window : typeof global !== "undefined" && global !== null ? global : this;
-if (!root.poorModule) {
- var poorModule = root.poorModule = {};
- var defs = poorModule.defs = {};
- var modules = poorModule.modules = {};
-
- var setModule = poorModule.setModule = function (name, fn) {
- defs[name] = fn;
- delete modules[name];
- }
-
- var getModule = poorModule.getModule = function (name) {
+;(function (root) {
+var _poorModule = root._poorModule = {}
+, defs = _poorModule.defs = {}, modules = _poorModule.modules = {}
+, poorModule = root.poorModule = _poorModule.poorModule = function (name, fn) {
+ if (fn) { defs[name] = fn; delete modules[name]; }
+ else {
if (modules.hasOwnProperty(name)) return modules[name];
if (defs.hasOwnProperty(name)) {
var fn = defs[name];
defs[name] = function () { throw new Error("Circular Dependency"); }
return modules[name] = fn();
- }
- throw new Error("Module not found: " + name);
- }
-
- if (!root.setModule) { root.setModule = setModule; }
- if (!root.getModule) { root.getModule = getModule; }
-}
+ } throw new Error("Module not found: " + name); }
+}})(typeof window !== "undefined" && window !== null ? window : typeof global !== "undefined" && global !== null ? global : this);
View
@@ -0,0 +1,15 @@
+require("../poor-module.js")
+require("./poor_modules/colors.js")
+require("./poor_modules/letters.js")
+require("./poor_modules/numbers.js")
+
+var colors = poorModule("colors")
+var letters = poorModule("letters")
+
+console.assert(colors() == "red")
+console.assert(colors() == "green")
+console.assert(letters.first() == "a")
+console.assert(letters.second() == "b")
+console.assert(letters.third() == "c")
+
+console.log("All tests passed")
View
No changes.
@@ -0,0 +1,10 @@
+poorModule("colors", function () {
+ var colors = ["red", "green", "blue"]
+ var index = 0
+ return function () {
+ var color = colors[index];
+ index += 1;
+ if (index > colors.length) index = 0;
+ return color
+ }
+});
@@ -0,0 +1,9 @@
+poorModule("letters", function () {
+ var numbers = poorModule("numbers")
+ var letters = "abc"
+ return {
+ first: function () { return letters.charAt(numbers.first() - 1)},
+ second: function () { return letters.charAt(numbers.second() - 1)},
+ third: function () { return letters.charAt(numbers.third() - 1)},
+ }
+});
@@ -0,0 +1,7 @@
+poorModule("numbers", function () {
+ return {
+ first: function () { return 1; },
+ second: function () { return 2; },
+ third: function () { return 3; }
+ }
+});

0 comments on commit 406338e

Please sign in to comment.