-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (112 loc) · 3.21 KB
/
Copy pathindex.js
File metadata and controls
122 lines (112 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import chokidar from 'chokidar';
import plantUml from 'node-plantuml-latest';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const style = path.resolve(__dirname, '.plantstyles');
/**
* Logs a dated message to the console.
* @param {string} message The message to log.
*/
function log(message) {
const now = new Date();
console.log(
`[${now.getHours().toString().padStart(2, '0')}:${now
.getMinutes()
.toString()
.padStart(2, '0')}:${now
.getSeconds()
.toString()
.padStart(2, '0')}] ${message}`
);
}
/**
* Generates/regenerates a diagram based on a PlantUML file.
* @param {string} pathToFile The absolute file path to the PlantUML file.
*/
function generateDiagram(pathToFile) {
cleanupDiagram(pathToFile);
const diagramPath = calculateDiagramPath(pathToFile);
log('Generating ' + diagramPath);
try {
const content = stripRelativeIncludes(pathToFile);
const gen = plantUml.generate(content, {
format: 'png',
config: style,
pragma: 'layout=smetana'
});
gen.out.pipe(fs.createWriteStream(diagramPath));
} catch (error) {
console.error(error);
}
}
/**
* Reads the content of the puml file and removes relative !include statements
* for .plantstyles files.
* @param {string} pathToFile The absolute file path to the PlantUML file.
* @returns {string} The stripped content without relative !include statements.
*/
function stripRelativeIncludes(pathToFile) {
let stripped = '';
fs.readFileSync(pathToFile, 'utf-8')
.split(/\r?\n/)
.forEach((line) => {
if (!/^!include [./]*.plantstyles$/.test(line)) {
stripped += line + '\n';
}
});
return stripped;
}
/**
* Removes a diagram based on a PlantUML file.
* @param {string} pathToFile The absolute file path to the PlantUML file.
*/
function cleanupDiagram(pathToFile) {
const diagramPath = calculateDiagramPath(pathToFile);
try {
if (fs.existsSync(diagramPath)) {
log('Removing ' + diagramPath);
fs.unlinkSync(diagramPath);
}
} catch (error) {
console.error(error);
}
}
/**
* Determines the path to the generated visual diagram based on the location of
* a PlantUML file.
* @param {string} pathToFile The absolute file path to the PlantUML file.
* @returns {string} The absolute path to the diagram associated with the
* PlantUML file.
*/
function calculateDiagramPath(pathToFile) {
const filename = path.basename(pathToFile, '.puml') + '.png';
return path.join(path.dirname(pathToFile), filename);
}
const watcher = chokidar
.watch('.', {
// Ignore all files except .puml files that are not in node_modules.
ignored: (path, stats) =>
stats?.isFile() &&
!path.endsWith('.puml') &&
path.indexOf('node_modules') < 0,
persistent: true,
cwd: __dirname,
ignoreInitial: true
})
.on('all', (event, path) => {
switch (event) {
case 'add':
case 'change':
generateDiagram(path);
break;
case 'unlink':
cleanupDiagram(path);
break;
default:
log(`Ignoring event ${event} on ${path}`);
break;
}
});