|
@@ -1,4 +1,5 @@
|
|
var path = require('path'),
|
|
var path = require('path'),
|
|
|
|
+ url = require('url'),
|
|
_ = require('lodash'),
|
|
_ = require('lodash'),
|
|
findit = require('findit'),
|
|
findit = require('findit'),
|
|
fs = require('fs');
|
|
fs = require('fs');
|
|
@@ -30,6 +31,56 @@ function transformShimDeps(json) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+function transformPaths(json, root, file, dirs) {
|
|
|
|
+ if (json.paths) {
|
|
|
|
+ var specifier = path.basename(file).replace(/\.amd\.json$/, "");
|
|
|
|
+ var modulePath = specifier === "local" ? path.dirname(file) : dirs[specifier];
|
|
|
|
+ var dir = (path.relative(root, modulePath).split(path.sep).join('/') || ".") + '/';
|
|
|
|
+ var paths = Object.keys(json.paths);
|
|
|
|
+ for (var i = 0; i < paths.length; i++) {
|
|
|
|
+ var key = paths[i];
|
|
|
|
+ json.paths[key] = url.resolve(dir, json.paths[key]) || ".";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function isLessDeep(a, b) {
|
|
|
|
+ var bDepth = b ? b.split(path.sep).length : Infinity;
|
|
|
|
+ var aDepth = a ? a.split(path.sep).length : Infinity;
|
|
|
|
+ return aDepth < bDepth;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function constructDirMap(dirs) {
|
|
|
|
+ var result = {};
|
|
|
|
+ for (var i = 0; i < dirs.length; i++) {
|
|
|
|
+ var dir = dirs[i];
|
|
|
|
+ var basename = path.basename(dir);
|
|
|
|
+ if (isLessDeep(dir, result[basename])) {
|
|
|
|
+ result[basename] = dir;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function queoeOfFilesToProcess(files, dirMap) {
|
|
|
|
+ var firstPass = [], secondPass = [];
|
|
|
|
+ for (var i = 0; i < files.length; i++) {
|
|
|
|
+ var file = files[i];
|
|
|
|
+ var basename = path.basename(file);
|
|
|
|
+ var specifier = basename.replace(/\.amd\.json$/, "");
|
|
|
|
+ if (specifier == "" || specifier[0] == ".") {
|
|
|
|
+ continue;
|
|
|
|
+ } else if (specifier == "local") {
|
|
|
|
+ firstPass.push(file);
|
|
|
|
+ } else if (dirMap[specifier]) {
|
|
|
|
+ secondPass.push(file);
|
|
|
|
+ } else {
|
|
|
|
+ throw new Error("No location for " + file);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return firstPass.concat(secondPass);
|
|
|
|
+}
|
|
|
|
+
|
|
exports.produceConfigObject = function (root, callback) {
|
|
exports.produceConfigObject = function (root, callback) {
|
|
eachConfigFile(root, function (err, filesAndDirs) {
|
|
eachConfigFile(root, function (err, filesAndDirs) {
|
|
if (err) {
|
|
if (err) {
|
|
@@ -37,39 +88,25 @@ exports.produceConfigObject = function (root, callback) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- var dirExists = {};
|
|
|
|
- var dirs = Object.keys(filesAndDirs.dirs);
|
|
|
|
- for (var i = 0; i < dirs.length; i++) {
|
|
|
|
- dirExists[path.basename(dirs[i])] = true;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- var files = Object.keys(filesAndDirs.files);
|
|
|
|
- var firstPass = [], secondPass = [];
|
|
|
|
- for (var i = 0; i < files.length; i++) {
|
|
|
|
- var file = files[i];
|
|
|
|
- var basename = path.basename(file);
|
|
|
|
- var specifier = basename.replace(/\.amd\.json$/, "");
|
|
|
|
- if (specifier == "" || specifier[0] == ".") {
|
|
|
|
- continue;
|
|
|
|
- } else if (specifier == "local") {
|
|
|
|
- firstPass.push(file);
|
|
|
|
- } else if (dirExists[specifier]) {
|
|
|
|
- secondPass.push(file);
|
|
|
|
- } else {
|
|
|
|
- callback(new Error("No location for " + file));
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
|
|
+ var dirMap = constructDirMap(Object.keys(filesAndDirs.dirs));
|
|
|
|
+ var queue;
|
|
|
|
+ try {
|
|
|
|
+ queue = queoeOfFilesToProcess(Object.keys(filesAndDirs.files), dirMap);
|
|
|
|
+ } catch (e) {
|
|
|
|
+ callback(e);
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
- var bothPasses = [].concat(firstPass).concat(secondPass);
|
|
|
|
- if (!bothPasses.length) {
|
|
|
|
|
|
+ if (!queue.length) {
|
|
callback(new Error("No .amd.json-type file found"));
|
|
callback(new Error("No .amd.json-type file found"));
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
var result = {};
|
|
var result = {};
|
|
- for (var i = 0; i < bothPasses.length; i++) {
|
|
|
|
- var json = require(bothPasses[i]);
|
|
|
|
|
|
+ for (var i = 0; i < queue.length; i++) {
|
|
|
|
+ var file = queue[i];
|
|
|
|
+ var json = require(file);
|
|
transformShimDeps(json);
|
|
transformShimDeps(json);
|
|
- _.merge(result, json, function(a, b) {
|
|
|
|
|
|
+ transformPaths(json, root, file, dirMap);
|
|
|
|
+ _.merge(result, json, function (a, b) {
|
|
return _.isArray(a) ? a.concat(b) : undefined;
|
|
return _.isArray(a) ? a.concat(b) : undefined;
|
|
});
|
|
});
|
|
}
|
|
}
|