|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +var program = require('commander'), |
| 4 | + fs = require('fs'), |
| 5 | + path = require('path'), |
| 6 | + _ = require('lodash'), |
| 7 | + |
| 8 | + dir = require('../lib/dir'), |
| 9 | + parse = require('../lib/parser'), |
| 10 | + compile = require('../lib/compile'), |
| 11 | + symbols = require('../lib/symbols'), |
| 12 | + |
| 13 | + version = require('../package').version; |
| 14 | + |
| 15 | +/** |
| 16 | + * Options & Defaults |
| 17 | + */ |
| 18 | +var ignoredDirs = 'test,public,static,views,templates'; |
| 19 | + |
| 20 | +program |
| 21 | + .version(version) |
| 22 | + // .option('-r, --raw', 'output \'raw\' comments, leaving the markdown intact') |
| 23 | + .option('-d, --debug', 'output parsed comments for debugging') |
| 24 | + .option('-t, --title <string>', 'The title for the page produced') |
| 25 | + .option('-s, --source <source>', 'The folder which should get parsed') |
| 26 | + .option('-i, --ignore <directories>', 'Comma seperated list of directories to ignore. Default: ' + ignoredDirs) |
| 27 | + .option('-T, --target <target>', 'The folder which will contain the results. Default: <process.cwd()>/docs') |
| 28 | + .option('--template <jade template>', 'The jade template file to use'); |
| 29 | + |
| 30 | +function showHelp(){ |
| 31 | + console.log(' Examples:\n'); |
| 32 | + console.log(' # parse a whole folder'); |
| 33 | + console.log(' $ doxx --source ./lib --target ./docs'); |
| 34 | +} |
| 35 | + |
| 36 | +// examples |
| 37 | +program.on('--help', showHelp); |
| 38 | + |
| 39 | +// parse argv |
| 40 | +program.parse(process.argv); |
| 41 | + |
| 42 | +if(program.template){ |
| 43 | + compile.tpl = fs.readFileSync(program.template).toString(); |
| 44 | +} |
| 45 | + |
| 46 | +if (!program.source) { |
| 47 | + console.error(" Error you must define a source\n"); |
| 48 | + return showHelp(); |
| 49 | +} |
| 50 | + |
| 51 | +var target = path.resolve(process.cwd(), program.target) || process.cwd() + '/docs', |
| 52 | + source = path.resolve(process.cwd(), program.source), |
| 53 | + ignore = program.ignore || ignoredDirs; |
| 54 | + |
| 55 | +// Cleanup and turn into an array the ignoredDirs |
| 56 | +ignore = ignore.trim().replace(' ','').split(','); |
| 57 | + |
| 58 | +// Find, cleanup and validate all potential files |
| 59 | +var files = dir.collectFiles(source, ignore); |
| 60 | + |
| 61 | +dir.createTargetFolders(target, files); |
| 62 | + |
| 63 | +// Parse each file |
| 64 | +files = files.map(function(file) { |
| 65 | + var dox = parse(path.join(source, file), {}); |
| 66 | + return { |
| 67 | + name: file, |
| 68 | + dox: dox, |
| 69 | + symbols: symbols(dox) |
| 70 | + }; |
| 71 | +}); |
| 72 | + |
| 73 | +// Compute all symboles |
| 74 | +var allSymbols = files.reduce(function(m, a, b){ |
| 75 | + m = (a.symbols || []).concat(b.symbols || []); |
| 76 | + return m; |
| 77 | +}, []); |
| 78 | + |
| 79 | +// Render and write each file |
| 80 | +files.forEach(function(file){ |
| 81 | + |
| 82 | + var options = _.extend({}, file, { |
| 83 | + title: program.title || require(process.cwd() + '/package').name, |
| 84 | + allSymbols: allSymbols |
| 85 | + }); |
| 86 | + |
| 87 | + var compiled = compile(options); |
| 88 | + fs.writeFileSync(path.join(target, file.name+".html"), compiled); |
| 89 | +}); |
0 commit comments