Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added additional error handling, added 'root' option to require

  • Loading branch information...
commit 4bc9633009e6445c242172a5f764dd7acbf34870 1 parent cd718be
Chris Powers authored
79 lib/air-drop.js
View
@@ -170,8 +170,11 @@ var packageMethods = {
var wrap = this._fetchWrappedFile();
glob(path.path, function(err, filePaths) {
if(err) { return cb(err); }
+ if(filePaths.length === 0) {
+ return cb("No files found in path " + path.path);
+ }
var expandedPaths = _(filePaths || []).map(function(filepath) {
- return new Path({type: path.type, path: filepath, name: path.name});
+ return path.clone({path: filepath});
});
orderedAsyncMap(expandedPaths, wrap, function(err, parts) {
err ? cb(err) : cb(null, parts.join("\n"))
@@ -188,7 +191,7 @@ var packageMethods = {
}
else {
return function(path, cb) {
- encodedPath = path.path.replace(/\//g, "|");
+ encodedPath = path.fetchPath.replace(/\//g, "|");
cb(null, "document.write('<scr'+'ipt src=\"/air-drop/" + package.packageName + "/" + path.type + "/" + encodedPath + "\" type=\"text/javascript\"></scr'+'ipt>');");
}
}
@@ -218,25 +221,38 @@ function readWrapFile(path, cb) {
function expandPaths(paths, cb) {
orderedAsyncConcat(paths, expandPath, function(err, allPaths) {
- if(err) { return cb(err); }
- var flattened = allPaths,
- compacted = [],
- nameIndex = [];
- for(var i=0; i < flattened.length; i++) {
- var path = allPaths[i];
- if(nameIndex.indexOf(path.path) === -1) {
- nameIndex.push(path.path);
- compacted.push(path);
+ try {
+ if(err) { throw err; }
+ var flattened = allPaths,
+ compacted = [],
+ nameIndex = [];
+ for(var i=0; i < flattened.length; i++) {
+ var path = allPaths[i];
+ if(nameIndex.indexOf(path.path) === -1) {
+ nameIndex.push(path.path);
+ compacted.push(path);
+ }
}
+ cb(null, compacted);
+ }
+ catch(e) {
+ if(err) { return cb(err); }
}
- cb(null, compacted);
});
}
function expandPath(path, cb) {
glob(path.path, function(err, filePaths) {
- if(err) { return cb(err); }
- cb(null, _(filePaths).map(function(newPath) { return new Path({type: path.type, path: newPath, name: path.name}); }));
+ try {
+ if(err) { throw err; }
+ var clonedPaths = _(filePaths).map(function(newPath) {
+ return path.clone({path: newPath});
+ });
+ cb(null, clonedPaths);
+ }
+ catch(e) {
+ if(err) { return cb(err); }
+ }
});
}
@@ -251,24 +267,33 @@ function orderedAsyncMap(arr, iterator, origCb) {
};
async.map(arrWithIndexes, wrappedIterator, function(err, tuples) {
- if(err) { return origCb(err); }
- var orderedItems = [];
- _(tuples).each(function(tuple) {
- var item = tuple[0],
- i = tuple[1];
- orderedItems[i] = item;
- });
- origCb(null, orderedItems);
+ try {
+ if(err) { throw err }
+ var orderedItems = [];
+ _(tuples).each(function(tuple) {
+ var item = tuple[0],
+ i = tuple[1];
+ orderedItems[i] = item;
+ });
+ origCb(null, orderedItems);
+ } catch(e) {
+ origCb(e);
+ }
});
}
function orderedAsyncConcat(arr, iterator, cb) {
orderedAsyncMap(arr, iterator, function(err, items) {
- if(err) { return cb(err); }
- var head = items.shift();
- for(var i=0; i < items.length; i++) {
- head = head.concat(items[i]);
+ try {
+ if(err) { throw err; }
+ var head = items.shift();
+ for(var i=0; i < items.length; i++) {
+ head = head.concat(items[i]);
+ }
+ cb(null, head);
+ }
+ catch(e) {
+ if(err) { return cb(err); }
}
- cb(null, head);
});
}
18 lib/compilers.js
View
@@ -1,17 +1,19 @@
-var compilers = [];
+var compilers = [],
+ _ = require("underscore");
var add = exports.add = function(compiler, test) {
- compilers.unshift([compiler, test]);
+ compilers.push([compiler, test]);
};
exports.choose = function(path) {
- for(var i=0; i < compilers.length; i++) {
- var tuple = compilers[i],
- compiler = tuple[0],
+ var matchingCompiler = None;
+
+ _(compilers).each(function(tuple) {
+ var compiler = tuple[0],
test = tuple[1];
- if(test(path)) { return compiler; }
- }
- return None;
+ if(test(path)) { matchingCompiler = compiler; }
+ });
+ return matchingCompiler;
};
var None = function(data, cb) { cb(null, data) };
39 lib/path.js
View
@@ -1,21 +1,54 @@
var pathLib = require("path"),
- Compilers = require("./compilers");
+ Compilers = require("./compilers"),
+ _ = require("underscore");
function Path(options) {
this.type = options.type;
this.path = options.path;
this.name = options.name;
this.explicitCompiler = options.compiler;
+ if(options.root) {
+ this.root = pathLib.resolve(options.root);
+ if(this.path === pathLib.resolve(this.path)) {
+ this.relativePath = pathLib.relative(this.root, this.path);
+ }
+ else {
+ this.relativePath = this.path;
+ this.path = pathLib.join(this.root, this.relativePath);
+ }
+ }
+ else {
+ this.path = pathLib.resolve(this.path);
+ this.relativePath = pathLib.relative(this.root, this.path);
+ }
+ this.fetchPath = pathLib.relative(process.cwd(), this.path);
+ this.options = options;
};
module.exports = Path;
Path.prototype = {
moduleName: function() {
- if(this.name) { return this.name; }
- return pathLib.dirname(this.path) + "/" + pathLib.basename(this.path, pathLib.extname(this.path));
+ return this.name || implicitModuleName(this.relativePath);
},
compiler: function() {
return this.explicitCompiler || Compilers.choose(this);
+ },
+
+ clone: function(opts) {
+ var newOpts = {};
+ _.extend(newOpts, this.options);
+ _.extend(newOpts, opts);
+ return new Path(newOpts);
}
};
+
+function implicitModuleName(path) {
+ var out = "",
+ dirname = pathLib.dirname(path);
+ if(dirname !== ".") {
+ out = dirname + "/";
+ }
+ out = out + pathLib.basename(path, pathLib.extname(path));
+ return out;
+}
19 spec/air-drop-spec.coffee
View
@@ -10,7 +10,8 @@ expectSourceToMatch = (drop, content) ->
error = err
actual = data
)
- waitsFor -> actual || error
+ waitsFor ->
+ actual || error
runs ->
throw error if error
expect(actual.replace(trailingNewline, "")).toEqual(expected)
@@ -88,6 +89,22 @@ describe "AirDrop", ->
it "uses the name in the define statement rather than the path", ->
expectSourceToMatchFile drop, "#{__dirname}/fixtures/packaged/d-with-name.js"
+ describe "with a root", ->
+ beforeEach ->
+ drop = AirDrop("drop").useBrowserRequire(false).package().require("d.js", {root: "#{__dirname}/fixtures/requires"})
+ console.log(drop)
+
+ it "finds the path relative to the root", ->
+ expectSourceToMatchFile drop, "#{__dirname}/fixtures/packaged/d-with-name.js"
+
+# describe "with a root and redundant path", ->
+# beforeEach ->
+# drop = AirDrop("drop").useBrowserRequire(false).package().require("#{__dirname}/fixtures/requires/d.js", {root: "#{__dirname}/fixtures/requires"})
+#
+# it "finds the path relative to the root", ->
+# expectSourceToMatchFile drop, "#{__dirname}/fixtures/packaged/d-with-name.js"
+
+
describe "#minimize", ->
describe "with no args", ->
beforeEach ->
Please sign in to comment.
Something went wrong with that request. Please try again.