build-doc.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. var _ = require('lodash'),
  3. fs = require('fs-extra'),
  4. path = require('path');
  5. var file = require('../common/file'),
  6. mapping = require('../common/mapping'),
  7. util = require('../common/util');
  8. var templatePath = path.join(__dirname, 'template/doc'),
  9. template = file.globTemplate(path.join(templatePath, '*.jst'));
  10. var argNames = ['a', 'b', 'c', 'd'];
  11. var templateData = {
  12. 'mapping': mapping,
  13. 'toArgOrder': toArgOrder,
  14. 'toFuncList': toFuncList
  15. };
  16. /**
  17. * Converts arranged argument `indexes` into a named argument string
  18. * representation of their order.
  19. *
  20. * @private
  21. * @param {number[]} indexes The arranged argument indexes.
  22. * @returns {string} Returns the named argument string.
  23. */
  24. function toArgOrder(indexes) {
  25. var reordered = [];
  26. _.each(indexes, function(newIndex, index) {
  27. reordered[newIndex] = argNames[index];
  28. });
  29. return '`(' + reordered.join(', ') + ')`';
  30. }
  31. /**
  32. * Converts `funcNames` into a chunked list string representation.
  33. *
  34. * @private
  35. * @param {string[]} funcNames The function names.
  36. * @returns {string} Returns the function list string.
  37. */
  38. function toFuncList(funcNames) {
  39. var chunks = _.chunk(funcNames.slice().sort(), 5),
  40. lastChunk = _.last(chunks),
  41. last = lastChunk ? lastChunk.pop() : undefined;
  42. chunks = _.reject(chunks, _.isEmpty);
  43. lastChunk = _.last(chunks);
  44. var result = '`' + _.map(chunks, function(chunk) {
  45. return chunk.join('`, `') + '`';
  46. }).join(',\n`');
  47. if (last == null) {
  48. return result;
  49. }
  50. if (_.size(chunks) > 1 || _.size(lastChunk) > 1) {
  51. result += ',';
  52. }
  53. result += ' &';
  54. result += _.size(lastChunk) < 5 ? ' ' : '\n';
  55. return result + '`' + last + '`';
  56. }
  57. /*----------------------------------------------------------------------------*/
  58. /**
  59. * Creates the FP-Guide wiki at the `target` path.
  60. *
  61. * @private
  62. * @param {string} target The output file path.
  63. */
  64. function build(target) {
  65. target = path.resolve(target);
  66. fs.writeFile(target, template.wiki(templateData), util.pitch);
  67. }
  68. build(_.last(process.argv));