1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | const fs = require('fs-extra');
|
18 | const klaw = require('klaw');
|
19 | const path = require('path');
|
20 | const program = require('commander');
|
21 | const PlantUMLGenerator = require('./fromjs/plantumlgenerator');
|
22 | const APISignatureGenerator = require('./fromjs/apisignaturegenerator');
|
23 | const JavaScriptParser = require('./javascriptparser');
|
24 | const JSONGenerator = require('./fromjs/jsongenerator');
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function processFile(file, fileProcessor) {
|
34 | let filePath = path.parse(file);
|
35 | if (filePath.ext === '.js' && filePath.base !== 'parser.js') {
|
36 | let fileContents = fs.readFileSync(file, 'utf8');
|
37 |
|
38 | const parser = new JavaScriptParser(fileContents, program.private, 8, false);
|
39 | fileProcessor.generate(program, file, parser.getIncludes(), parser.getClasses(), parser.getFunctions());
|
40 | }
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function processDirectory(path, fileProcessor) {
|
51 | let items = [];
|
52 | klaw(path)
|
53 | .on('readable', function (item) {
|
54 | while ((item = this.read())) {
|
55 | if (item.stats.isFile()) {
|
56 | items.push(item.path);
|
57 | }
|
58 | }
|
59 | })
|
60 | .on('end', () => {
|
61 | items.sort();
|
62 | items.forEach((item) => {
|
63 | processFile(item, fileProcessor);
|
64 | });
|
65 | });
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | program
|
76 | .version('0.0.1')
|
77 | .description('Parses Javascript source and generates output from class and method definitions')
|
78 | .usage('[options]')
|
79 | .option('-o, --outputDir <outputDir>', 'Output directory')
|
80 | .option('-i, --inputDir <inputDir>', 'Input source directory')
|
81 | .option('-s, --single <singlefile>', 'Single file to process')
|
82 | .option('-f, --format <format>', 'Format of code to generate. Defaults to PlantUML.', 'PlantUML')
|
83 | .option('-p, --private', 'Include classes that have the @private JSDoc annotation')
|
84 | .parse(process.argv);
|
85 |
|
86 | let fileProcessor;
|
87 |
|
88 | switch (program.format) {
|
89 | case 'PlantUML':
|
90 | fileProcessor = new PlantUMLGenerator();
|
91 | break;
|
92 | case 'APISignature':
|
93 | fileProcessor = new APISignatureGenerator();
|
94 | break;
|
95 | case 'JSON':
|
96 | fileProcessor = new JSONGenerator();
|
97 | break;
|
98 | }
|
99 |
|
100 |
|
101 | if (program.inputDir) {
|
102 |
|
103 | console.log('Input dir ' + program.inputDir);
|
104 | processDirectory(program.inputDir, fileProcessor);
|
105 | } else if (program.single) {
|
106 | console.log('Single file ' + program.single);
|
107 | processFile(path.resolve(program.single), fileProcessor);
|
108 | } else {
|
109 | console.log('no file option given');
|
110 | }
|