Skip to content

Commit

Permalink
Updated command line tool to remove bugs. Updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Raynos committed Mar 18, 2012
1 parent 9cf25a3 commit 511cf67
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 12 deletions.
77 changes: 72 additions & 5 deletions bin/ncore
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#!/usr/bin/env node

// bugs
// - /node_modules/ncore/node_modules
// - doesn't include files not picked up by dependencySet

var program = require("commander"),
_package = require("../package.json"),
path = require("path"),
pd = require("pd"),
fs = require("fs"),
after = require("after"),
DependencyWriter = require("../modules/dependencyWriter"),
wrench = require("wrench"),
browserify = require("browserify")

program
Expand All @@ -25,9 +30,31 @@ var code = "(function () { \n" +
"var Core = Object.create(nCore).constructor()\n" +
"delete window.nCore \n"

after.forEach(dependencies, writeDependencies, next)
var done = after(2, next)

after.forEach(dependencies, writeDependencies, done)

readDir(moduleUri, readFiles)

function readFiles(err, files) {
if (err) {
throw err
}
after.forEach(files.files, tunnelWriteDependencies, done)
}

function tunnelWriteDependencies(fileName, callback) {
var relative = path.relative(moduleUri, fileName)
if (relative.indexOf(".json") === -1) {
writeDependencies({},
path.relative(moduleUri, fileName), callback)
} else {
callback()
}
}

function writeDependencies(depObject, fileName, callback) {
console.log("writeDependencies", depObject, fileName)
var dw = DependencyWriter({
uri: path.join(moduleUri, fileName),
originalUri: moduleUri,
Expand All @@ -46,9 +73,6 @@ function writeDependencies(depObject, fileName, callback) {
})
}




function next() {
var bundle = browserify({
debug: true
Expand All @@ -58,13 +82,56 @@ function next() {
code += "Core.init()\n"
code += "})()"

console.log(__dirname)
bundle.addEntry(path.join(__dirname, "../lib/browser.js"))

bundle.addEntry(path.join(moduleUri, "dummy.js"), { body: code })

code = bundle.bundle()

code.replace(/\/ncore\/node_modules/g, "")

fs.writeFile(path.join(process.cwd(), program.out), code)
}

function readDir(start, callback) {
// Use lstat to resolve symlink if we are passed a symlink
fs.lstat(start, function(err, stat) {
if(err) {
return callback(err);
}
var found = {dirs: [], files: []},
total = 0,
processed = 0;
function isDir(abspath) {
fs.stat(abspath, function(err, stat) {
if(stat.isDirectory()) {
found.dirs.push(abspath);
// If we found a directory, recurse!
readDir(abspath, function(err, data) {
found.dirs = found.dirs.concat(data.dirs);
found.files = found.files.concat(data.files);
if(++processed == total) {
callback(null, found);
}
});
} else {
found.files.push(abspath);
if(++processed == total) {
callback(null, found);
}
}
});
}
// Read through all the files in this directory
if(stat.isDirectory()) {
fs.readdir(start, function (err, files) {
total = files.length;
for(var x=0, l=files.length; x<l; x++) {
isDir(path.join(start, files[x]));
}
});
} else {
return callback(new Error("path: " + start + " is not a directory"));
}
});
}
2 changes: 1 addition & 1 deletion modules/dependencyWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pd.extend(DependencyWriter, {
}
if (stats.isFile()) {
this.relative = makeRelative.call(this)
this.emitter.emit("dependencySet", this.relative, this.uri)
after.forEach(this.depObject, this.injectDependency,
this, this.callback)
} else if (stats.isDirectory()) {
Expand Down Expand Up @@ -73,7 +74,6 @@ pd.extend(DependencyWriter, {
if (!dependencies[this.relative]) {
dependencies[this.relative] = {}
}
this.emitter.emit("dependencySet", this.relative, this.uri)
dependencies[this.relative][name] = deps
callback()
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"after": "0.3.2",
"eventemitter-light": "0.2.1",
"commander": "0.5.1",
"wrench": "1.3.7",
"browserify": "1.10.3"
},
"engines": {
Expand Down
11 changes: 8 additions & 3 deletions test/browser/build.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions test/browser/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ suite("nCore", function () {
var foo = window.foo
var bar = window["bar.bar"]
var barfoo = window["bar.foo"]
var baz = window.baz

assert(foo.has("bar") === bar,
"bar depedency on foo did not work")
assert(foo.has("foo") === barfoo,
"foo dependency on foo did not work")
console.log(foo.has("baz"), baz)
assert(foo.has("baz") === baz,
"baz dependency on foo did not work")
testBars(bar, "bar")
testBars(barfoo, "barfoo")
assert(bar.has("foobar") === bar,
Expand All @@ -25,9 +29,9 @@ suite("nCore", function () {
function testBars(_bar, text) {
assert(_bar.has("foo") === foo,
"foo dependency on "+text+" did not work")
assert(_bar.has("bars").indexOf(bar) !== -1,
assert(_bar.has("bars").bar,
"bars array does not contain bar on "+text)
assert(_bar.has("bars").indexOf(barfoo) !== -1,
assert(_bar.has("bars").barfoo !== -1,
"bars array does not contain barfoo on "+text)
}

Expand Down
5 changes: 5 additions & 0 deletions test/modules/baz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
has: function (name) {
return this[name];
}
};
3 changes: 2 additions & 1 deletion test/modules/dependency.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"./foo.js": {
"bar": "./bar/bar.js",
"foo": "./bar/"
"foo": "./bar/",
"baz": "./baz.js"
},
"./bar/": {
"foo": "./foo.js",
Expand Down
1 change: 1 addition & 0 deletions test/modules/foo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
init: function () {
typeof window !== "undefined" && (window["bar.bar"] = this.bar)
typeof window !== "undefined" && (window["bar.foo"] = this.foo)
typeof window !== "undefined" && (window["baz"] = this.baz)
},
has: function (name) {
return this[name];
Expand Down

0 comments on commit 511cf67

Please sign in to comment.