config-builder.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. eachConfigFile(root, function (err, filesAndDirs) {
  79. if (err) {
  80. callback(err);
  81. return;
  82. }
  83. var dirMap = constructDirMap(Object.keys(filesAndDirs.dirs));
  84. var queue;
  85. try {
  86. queue = queoeOfFilesToProcess(Object.keys(filesAndDirs.files), dirMap);
  87. } catch (e) {
  88. callback(e);
  89. return;
  90. }
  91. if (!queue.length) {
  92. callback(new Error("No .amd.json-type file found"));
  93. return;
  94. }
  95. var result = {};
  96. for (var i = 0; i < queue.length; i++) {
  97. var file = queue[i];
  98. var json = require(file);
  99. transformShimDeps(json);
  100. transformPaths(json, root, file, dirMap);
  101. _.merge(result, json, function (a, b) {
  102. return _.isArray(a) ? a.concat(b) : undefined;
  103. });
  104. }
  105. callback(null, result);
  106. })
  107. };