config-builder.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. var path = require('path'),
  2. url = require('url'),
  3. _ = require('lodash'),
  4. findit = require('findit'),
  5. fs = require('fs');
  6. function eachConfigFile(root, callback) {
  7. var result = {files: [], dirs: []};
  8. var find = findit(root, {followSymlinks: true});
  9. find.on('file', function (file, stat) {
  10. if (file.match(/\.amd\.json$/)) {
  11. result.files[file] = stat;
  12. }
  13. });
  14. find.on('directory', function (dir, stat, stop) {
  15. result.dirs[dir] = stat;
  16. });
  17. find.on('end', function () {
  18. callback(null, result);
  19. });
  20. }
  21. function transformShimDeps(json) {
  22. var shimmedList = json.shim && Object.keys(json.shim);
  23. if (!shimmedList) return;
  24. for (var i = 0; i < shimmedList.length; i++) {
  25. var shim = json.shim[shimmedList[i]];
  26. if (Array.isArray(shim)) {
  27. json.shim[shimmedList[i]] = {deps: shim};
  28. }
  29. }
  30. }
  31. function transformPaths(json, root, file, dirs) {
  32. if (json.paths) {
  33. var specifier = path.basename(file).replace(/\.amd\.json$/, "");
  34. var modulePath = specifier === "local" ? path.dirname(file) : dirs[specifier];
  35. var dir = (path.relative(root, modulePath).split(path.sep).join('/') || ".") + '/';
  36. var paths = Object.keys(json.paths);
  37. for (var i = 0; i < paths.length; i++) {
  38. var key = paths[i];
  39. json.paths[key] = url.resolve(dir, json.paths[key]) || ".";
  40. }
  41. }
  42. }
  43. function isLessDeep(a, b) {
  44. var bDepth = b ? b.split(path.sep).length : Infinity;
  45. var aDepth = a ? a.split(path.sep).length : Infinity;
  46. return aDepth < bDepth;
  47. }
  48. function constructDirMap(dirs) {
  49. var result = {};
  50. for (var i = 0; i < dirs.length; i++) {
  51. var dir = dirs[i];
  52. var basename = path.basename(dir);
  53. if (isLessDeep(dir, result[basename])) {
  54. result[basename] = dir;
  55. }
  56. }
  57. return result;
  58. }
  59. function queoeOfFilesToProcess(files, dirMap) {
  60. var firstPass = [], secondPass = [];
  61. for (var i = 0; i < files.length; i++) {
  62. var file = files[i];
  63. var basename = path.basename(file);
  64. var specifier = basename.replace(/\.amd\.json$/, "");
  65. if (specifier == "" || specifier[0] == ".") {
  66. continue;
  67. } else if (specifier == "local") {
  68. firstPass.push(file);
  69. } else if (dirMap[specifier]) {
  70. secondPass.push(file);
  71. } else {
  72. throw new Error("No location for " + file);
  73. }
  74. }
  75. return firstPass.concat(secondPass);
  76. }
  77. exports.produceConfigObject = function (root, callback) {
  78. root = path.resolve(root);
  79. eachConfigFile(root, function (err, filesAndDirs) {
  80. if (err) {
  81. callback(err);
  82. return;
  83. }
  84. var dirMap = constructDirMap(Object.keys(filesAndDirs.dirs));
  85. var queue;
  86. try {
  87. queue = queoeOfFilesToProcess(Object.keys(filesAndDirs.files), dirMap);
  88. } catch (e) {
  89. callback(e);
  90. return;
  91. }
  92. if (!queue.length) {
  93. callback(new Error("No .amd.json-type file found"));
  94. return;
  95. }
  96. var result = {};
  97. for (var i = 0; i < queue.length; i++) {
  98. var file = queue[i];
  99. var json = require(file);
  100. transformShimDeps(json);
  101. transformPaths(json, root, file, dirMap);
  102. _.merge(result, json, function (a, b) {
  103. return _.isArray(a) ? a.concat(b) : undefined;
  104. });
  105. }
  106. callback(null, result);
  107. })
  108. };