-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathumlwatch.js
183 lines (144 loc) · 4.64 KB
/
umlwatch.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict;'
/*
You can also check [this gist](https://gist.github.com/pjsousa/e76183d4ecce7a356ce923bd7966667a)
Or [this repo](https://github.com/dataloudlabs/umlwatcher)
*/
const chokidar = require('chokidar');
const log = console.log.bind(console);
const plantuml = require('node-plantuml');
const plantumlEncoder = require('plantuml-encoder');
const fs = require('fs');
const basename = require('path').basename;
const normalize = require('normalize-path');
const os = require('os');
const extend = require("extend");
var options;
var defaults = {
watchPath: "input"
, resultPath: "result"
, fileMask: "*.uml"
, workPath: os.homedir() + "/.umlwatch"
, png: true
, svg: false
, renderExisting: false
};
function parseArgs(raw_argv){
const progargs = require("commander");
var result;
var options = {};
progargs
.version("0.1.0")
.option("-w, --watch-path <input_path>", "Path to watch for changes")
.option("-r, --result-path <result_path>", "Path to place the diagrams")
.option("-m, --file-mask [mask]", "File mask to watch in watch_path")
.option("--png", "Render to png.")
.option("--svg", "Render to svg.")
.option("--render-existing", "Render existing files in watch-path")
.parse(raw_argv);
options["watchPath"] = progargs["watchPath"];
options["resultPath"] = progargs["resultPath"];
options["fileMask"] = progargs["fileMask"];
options["png"] = progargs["png"];
options["svg"] = progargs["svg"];
options["renderExisting"] = progargs["renderExisting"];
options["png"] == options["png"] || options["svg"] ? options["png"] : defaults["png"];
result = extend({}, defaults, options);
return result;
}
function sanitizePath(path){
var result;
if (!fs.existsSync(path)){
fs.mkdirSync(path);
}
result = fs.existsSync(path);
return result;
}
function sanitizeWatchPath(watchPath){
var result;
result = sanitizePath(watchPath)
return result;
};
function sanitizeTargetPath(target_path){
var result;
result = sanitizePath(target_path);
return result;
};
function sanitizeWorkPath(work_dir){
/*
TODO:
// create work file to log paths and encoded strings.
*/
var result;
result = sanitizePath(work_dir);
return result;
};
function handleChange(options, changed_file){
/*
TODO:
Refactor into read->encode->render methods.
Make it elegantly async.
*/
var input_filename = basename(changed_file, ".uml");
var output_path_png = normalize(options["resultPath"] + "/" + input_filename + ".png");
var output_path_svg = normalize(options["resultPath"] + "/" + input_filename + ".svg");
var encoded = encodeUml(changed_file);
var genPng = plantuml.generate(changed_file);
if (options["png"]){
log(changed_file + " -> " + output_path_png + "\n\n Encoded:\n"+encoded+"\n");
genPng.out.pipe(fs.createWriteStream(output_path_png));
}
if (options["svg"]){
log(changed_file + " -> " + output_path_svg + "\n\n Encoded:\n"+encoded+"\n");
var genSvg = plantuml.generate(changed_file, {format: "svg"});
genSvg.out.pipe(fs.createWriteStream(output_path_svg));
}
genPng.out.pipe(fs.createWriteStream("last_render.png"));
return encoded;
};
function encodeUml(umlPath, format){
var result;
result = fs.readFileSync(umlPath);
result = plantumlEncoder.encode(result.toString());
return result;
};
function renderUml(umlText){
// not being used. here for future reference
var result;
var decode = plantuml.decode(umlText);
var gen = plantuml.generate({format: format});
decode.out.pipe(gen.in);
result = gen.out.toString();
return result;
};
function registerWatchers(watchPath, fileMask, options){
const mask = normalize(watchPath + "/" + fileMask);
const watch_options = {
ignored: /(^|[\/\\])\../,
persitent:true,
ignoreInitial: !options["renderExisting"]
};
chokidar.watch(mask, watch_options)
.on('add', handleChange.bind(null, options))
.on('change', handleChange.bind(null, options));
log("Watching: " + mask);
};
function main(options){
sanitizeWorkPath(options["workPath"]);
sanitizeWatchPath(options["watchPath"])
sanitizeTargetPath(options["resultPath"]);
registerWatchers(options["watchPath"], options["fileMask"], options);
};
if (require.main === module) {
options = parseArgs(process.argv);
main(options);
} else {
//log('required as a module');
}
exports.parseArgs = parseArgs;
exports.sanitizeWatchPath = sanitizeWatchPath;
exports.sanitizeTargetPath = sanitizeTargetPath;
exports.sanitizeWorkPath = sanitizeWorkPath;
exports.handleChange = handleChange;
exports.encodeUml = encodeUml;
exports.renderUml = renderUml;
exports.registerWatchers = registerWatchers;