build-modules.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. var _ = require('lodash'),
  3. async = require('async'),
  4. glob = require('glob'),
  5. path = require('path');
  6. var file = require('../common/file'),
  7. mapping = require('../common/mapping'),
  8. util = require('../common/util');
  9. var templatePath = path.join(__dirname, 'template/modules'),
  10. template = file.globTemplate(path.join(templatePath, '*.jst'));
  11. var aryMethods = _.union(
  12. mapping.aryMethod[1],
  13. mapping.aryMethod[2],
  14. mapping.aryMethod[3],
  15. mapping.aryMethod[4]
  16. );
  17. var categories = [
  18. 'array',
  19. 'collection',
  20. 'date',
  21. 'function',
  22. 'lang',
  23. 'math',
  24. 'number',
  25. 'object',
  26. 'seq',
  27. 'string',
  28. 'util'
  29. ];
  30. var ignored = [
  31. '_*.js',
  32. 'core.js',
  33. 'core.min.js',
  34. 'fp.js',
  35. 'index.js',
  36. 'lodash.js',
  37. 'lodash.min.js'
  38. ];
  39. /**
  40. * Checks if `name` is a method alias.
  41. *
  42. * @private
  43. * @param {string} name The name to check.
  44. * @returns {boolean} Returns `true` if `name` is a method alias, else `false`.
  45. */
  46. function isAlias(name) {
  47. return _.has(mapping.aliasToReal, name);
  48. }
  49. /**
  50. * Checks if `name` is a category name.
  51. *
  52. * @private
  53. * @param {string} name The name to check.
  54. * @returns {boolean} Returns `true` if `name` is a category name, else `false`.
  55. */
  56. function isCategory(name) {
  57. return _.includes(categories, name);
  58. }
  59. /**
  60. * Checks if `name` belongs to a method that's passed thru and not wrapped.
  61. *
  62. * @private
  63. * @param {string} name The name to check.
  64. * @returns {boolean} Returns `true` if `name` is of a pass thru method,
  65. * else `false`.
  66. */
  67. function isThru(name) {
  68. return !_.includes(aryMethods, name);
  69. }
  70. /**
  71. * Gets metadata for `func`.
  72. *
  73. * @private
  74. * @param {Function} func The function to query.
  75. * @returns {*} Returns the metadata for `func`.
  76. */
  77. function getTemplate(moduleName) {
  78. var data = {
  79. 'name': _.get(mapping.aliasToReal, moduleName, moduleName),
  80. 'mapping': mapping
  81. };
  82. if (isAlias(moduleName)) {
  83. return template.alias(data);
  84. }
  85. if (isCategory(moduleName)) {
  86. return template.category(data);
  87. }
  88. if (isThru(moduleName)) {
  89. return template.thru(data);
  90. }
  91. return template.module(data);
  92. }
  93. /*----------------------------------------------------------------------------*/
  94. /**
  95. * Creates FP modules at the `target` path.
  96. *
  97. * @private
  98. * @param {string} target The output directory path.
  99. */
  100. function build(target) {
  101. target = path.resolve(target);
  102. var fpPath = path.join(target, 'fp');
  103. // Glob existing lodash module paths.
  104. var modulePaths = glob.sync(path.join(target, '*.js'), {
  105. 'nodir': true,
  106. 'ignore': ignored.map(function(filename) {
  107. return path.join(target, filename);
  108. })
  109. });
  110. // Add FP alias and remapped module paths.
  111. _.each([mapping.aliasToReal, mapping.remap], function(data) {
  112. _.forOwn(data, function(realName, alias) {
  113. var modulePath = path.join(target, alias + '.js');
  114. if (!_.includes(modulePaths, modulePath)) {
  115. modulePaths.push(modulePath);
  116. }
  117. });
  118. });
  119. var actions = modulePaths.map(function(modulePath) {
  120. var moduleName = path.basename(modulePath, '.js');
  121. return file.write(path.join(fpPath, moduleName + '.js'), getTemplate(moduleName));
  122. });
  123. actions.unshift(file.copy(path.join(__dirname, '../../fp'), fpPath));
  124. actions.push(file.write(path.join(fpPath, '_falseOptions.js'), template._falseOptions()));
  125. actions.push(file.write(path.join(fpPath, '_util.js'), template._util()));
  126. actions.push(file.write(path.join(target, 'fp.js'), template.fp()));
  127. actions.push(file.write(path.join(fpPath, 'convert.js'), template.convert()));
  128. async.series(actions, util.pitch);
  129. }
  130. build(_.last(process.argv));