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