UNPKG

2.56 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 path = require('path');
19const yargs = require('yargs');
20const plantuml = require('node-plantuml');
21
22
23
24/**
25 * Generate an image file from a PlantUML source file
26 * @private
27 */
28let program = yargs
29 .usage('$0 [options]')
30 .options({
31 'inputDir' : {alias: 'i', required: true, describe: 'Input directory containing PlantUML files', type: 'string' },
32 'outputDir' : {alias: 'o', required: true, describe:'Output directory to store generated images', type: 'string'},
33 'format' : {alias: 'f', required: true, describe:'Image format, defaults to SVG',type:'string',default:'svg'}
34 })
35 .argv;
36
37/**
38 * Processes a single UML file (.uml extension)
39 *
40 * @param {string} file - the file to process
41 * @private
42 */
43function processFile(file) {
44 let filePath = path.parse(file);
45 if (filePath.ext === '.uml') {
46 let gen = plantuml.generate(file, {
47 format: program.format
48 });
49 const imageFile = program.outputDir + '/' + filePath.name + '.' + program.format;
50 fs.ensureFileSync(imageFile);
51 gen.out.pipe(fs.createWriteStream(imageFile));
52 }
53}
54
55/**
56 * Processes all the UML files within a directory.
57 *
58 * @param {string} path - the path to process
59 * @private
60 */
61function processDirectory(path) {
62 //console.log( 'Processing ' + path );
63 fs.readdir(path, function(err, files) {
64 if (err) {
65 console.error('Could not list the directory.', err);
66 process.exit(1);
67 }
68
69 files.forEach(function(file, index) {
70 let stats = fs.statSync(path + '/' + file);
71 if (stats.isFile()) {
72 processFile(path + '/' + file);
73 } else if (stats.isDirectory()) {
74 processDirectory(path + '/' + file);
75 }
76 });
77 });
78}
79
80console.log('Input dir ' + program.inputDir);
81
82// Loop through all the files in the input directory
83processDirectory(program.inputDir);