Matteo Scandolo | 280dcd3 | 2016-05-16 09:59:38 -0700 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | var _ = require('lodash'), |
| 4 | fs = require('fs-extra'), |
| 5 | path = require('path'); |
| 6 | |
| 7 | var file = require('../common/file'), |
| 8 | mapping = require('../common/mapping'); |
| 9 | |
| 10 | var templatePath = path.join(__dirname, 'template/doc'), |
| 11 | template = file.globTemplate(path.join(templatePath, '*.jst')); |
| 12 | |
| 13 | var argNames = ['a', 'b', 'c', 'd']; |
| 14 | |
| 15 | var templateData = { |
| 16 | 'mapping': mapping, |
| 17 | 'toArgOrder': toArgOrder, |
| 18 | 'toFuncList': toFuncList |
| 19 | }; |
| 20 | |
| 21 | function toArgOrder(array) { |
| 22 | var reordered = []; |
| 23 | _.each(array, function(newIndex, index) { |
| 24 | reordered[newIndex] = argNames[index]; |
| 25 | }); |
| 26 | return '`(' + reordered.join(', ') + ')`'; |
| 27 | } |
| 28 | |
| 29 | function 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 | |
| 54 | function onComplete(error) { |
| 55 | if (error) { |
| 56 | throw error; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | function build(target) { |
| 61 | target = path.resolve(target); |
| 62 | fs.writeFile(target, template.wiki(templateData), onComplete); |
| 63 | } |
| 64 | |
| 65 | build(_.last(process.argv)); |