UNPKG

3.79 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17const fs = require('fs-extra');
18const klaw = require('klaw');
19const path = require('path');
20const program = require('commander');
21const PlantUMLGenerator = require('./fromjs/plantumlgenerator');
22const APISignatureGenerator = require('./fromjs/apisignaturegenerator');
23const JavaScriptParser = require('./javascriptparser');
24const JSONGenerator = require('./fromjs/jsongenerator');
25
26/**
27 * Processes a single Javascript file (.js extension)
28 *
29 * @param {string} file - the file to process
30 * @param {Object} fileProcessor - the processor instance to use to generate code
31 * @private
32 */
33function processFile(file, fileProcessor) {
34 let filePath = path.parse(file);
35 if (filePath.ext === '.js' && filePath.base !== 'parser.js') { //ignore the generated parsers
36 let fileContents = fs.readFileSync(file, 'utf8');
37 // Specify ES2017 (ES8) as that has async/await, which we use in our APIs.
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 * Processes all the Javascript files within a directory.
45 *
46 * @param {string} path - the path to process
47 * @param {Object} fileProcessor - the processor instance to use to generate code
48 * @private
49 */
50function 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 * Generates Plant UML files from Javascript source files
70 *
71 * node ./lib/codegen/umlgen.js
72 * --outputDir <location to write UML files>
73 * --inputDir <location to recursively read .js files>
74 */
75program
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
86let fileProcessor;
87
88switch (program.format) {
89case 'PlantUML':
90 fileProcessor = new PlantUMLGenerator();
91 break;
92case 'APISignature':
93 fileProcessor = new APISignatureGenerator();
94 break;
95case 'JSON':
96 fileProcessor = new JSONGenerator();
97 break;
98}
99
100
101if (program.inputDir) {
102 // Loop through all the files in the input directory
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}