config-builder.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. transformPathArray(json.paths);
  37. }
  38. function transformPathArray(array) {
  39. var paths = Object.keys(array);
  40. for (var i = 0; i < paths.length; i++) {
  41. var key = paths[i];
  42. var pathValue = array[key];
  43. switch (typeof pathValue) {
  44. case "string":
  45. array[key] = url.resolve(dir, pathValue) || ".";
  46. break;
  47. case "object":
  48. Array.isArray(pathValue) && transformPathArray(pathValue);
  49. break;
  50. }
  51. }
  52. }
  53. }
  54. function isLessDeep(a, b) {
  55. var bDepth = b ? b.split(path.sep).length : Infinity;
  56. var aDepth = a ? a.split(path.sep).length : Infinity;
  57. return aDepth < bDepth;
  58. }
  59. function constructDirMap(dirs) {
  60. var result = {};
  61. for (var i = 0; i < dirs.length; i++) {
  62. var dir = dirs[i];
  63. var basename = path.basename(dir);
  64. if (isLessDeep(dir, result[basename])) {
  65. result[basename] = dir;
  66. }
  67. }
  68. return result;
  69. }
  70. function queoeOfFilesToProcess(files, dirMap) {
  71. var firstPass = [], secondPass = [];
  72. for (var i = 0; i < files.length; i++) {
  73. var file = files[i];
  74. var basename = path.basename(file);
  75. var specifier = basename.replace(/\.amd\.json$/, "");
  76. if (specifier == "" || specifier[0] == ".") {
  77. continue;
  78. } else if (specifier == "local") {
  79. firstPass.push(file);
  80. } else if (dirMap[specifier]) {
  81. secondPass.push(file);
  82. } else {
  83. throw new Error("No location for " + file);
  84. }
  85. }
  86. return firstPass.concat(secondPass).reverse();
  87. }
  88. exports.produceConfigObject = function (root, callback) {
  89. root = path.resolve(root);
  90. eachConfigFile(root, function (err, filesAndDirs) {
  91. if (err) {
  92. callback(err);
  93. return;
  94. }
  95. var dirMap = constructDirMap(Object.keys(filesAndDirs.dirs));
  96. var queue;
  97. try {
  98. queue = queoeOfFilesToProcess(Object.keys(filesAndDirs.files), dirMap);
  99. } catch (e) {
  100. callback(e);
  101. return;
  102. }
  103. if (!queue.length) {
  104. callback(new Error("No .amd.json-type file found"));
  105. return;
  106. }
  107. var result = {};
  108. for (var i = 0; i < queue.length; i++) {
  109. var file = queue[i];
  110. var json = require(file);
  111. transformShimDeps(json);
  112. transformPaths(json, root, file, dirMap);
  113. _.merge(result, json, function (a, b) {
  114. return _.isArray(a) ? b.concat(a) : undefined;
  115. });
  116. }
  117. callback(null, result);
  118. })
  119. };