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 path = require('path');
|
19 | const yargs = require('yargs');
|
20 | const plantuml = require('node-plantuml');
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | let 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 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function 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 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function processDirectory(path) {
|
62 |
|
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 |
|
80 | console.log('Input dir ' + program.inputDir);
|
81 |
|
82 |
|
83 | processDirectory(program.inputDir);
|