blob: 02800bcfd5e3cf7db8fe9c1c171a31a968979d42 [file] [log] [blame]
Matteo Scandolo280dcd32016-05-16 09:59:38 -07001'use strict';
2
3var _ = require('lodash'),
4 fs = require('fs-extra'),
5 path = require('path');
6
7var file = require('../common/file'),
8 mapping = require('../common/mapping');
9
10var templatePath = path.join(__dirname, 'template/doc'),
11 template = file.globTemplate(path.join(templatePath, '*.jst'));
12
13var argNames = ['a', 'b', 'c', 'd'];
14
15var templateData = {
16 'mapping': mapping,
17 'toArgOrder': toArgOrder,
18 'toFuncList': toFuncList
19};
20
21function toArgOrder(array) {
22 var reordered = [];
23 _.each(array, function(newIndex, index) {
24 reordered[newIndex] = argNames[index];
25 });
26 return '`(' + reordered.join(', ') + ')`';
27}
28
29function toFuncList(array) {
30 var chunks = _.chunk(array.slice().sort(), 5),
31 lastChunk = _.last(chunks),
32 last = lastChunk ? lastChunk.pop() : undefined;
33
34 chunks = _.reject(chunks, _.isEmpty);
35 lastChunk = _.last(chunks);
36
37 var result = '`' + _.map(chunks, function(chunk) {
38 return chunk.join('`, `') + '`';
39 }).join(',\n`');
40
41 if (last == null) {
42 return result;
43 }
44 if (_.size(chunks) > 1 || _.size(lastChunk) > 1) {
45 result += ',';
46 }
47 result += ' &';
48 result += _.size(lastChunk) < 5 ? ' ' : '\n';
49 return result + '`' + last + '`';
50}
51
52/*----------------------------------------------------------------------------*/
53
54function onComplete(error) {
55 if (error) {
56 throw error;
57 }
58}
59
60function build(target) {
61 target = path.resolve(target);
62 fs.writeFile(target, template.wiki(templateData), onComplete);
63}
64
65build(_.last(process.argv));