-
Notifications
You must be signed in to change notification settings - Fork 97
/
utils.js
161 lines (144 loc) · 4.46 KB
/
utils.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
const fs = require('fs');
const zlib = require('zlib');
const makeDirectory = (path) =>
new Promise((resolve) => {
fs.mkdir(path, () => {
return resolve();
});
});
const readFile = (path, type) =>
new Promise((resolve, reject) => {
fs.readFile(path, type, (err, data) => {
if (err) return reject(err);
return resolve(data);
});
});
const writeFile = (path, data) =>
new Promise((resolve, reject) => {
fs.writeFile(path, data, (err, res) => {
if (err) return reject(err);
return resolve(res);
});
});
const writeOnSameLine = async (message, fn) => {
process.stdout.write(`${message}\r`);
};
const encodeURIPath = (path) => {
path = path.split('\\').join('/');
return encodeURI(path);
};
/**
* From
* https://github.com/qjebbs/vscode-plantuml/blob/master/src/plantuml/renders/plantumlServer.ts
*
The MIT License (MIT)
Copyright (c) 2016 jebbs
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
const urlTextFrom = (s) => {
let opt = { level: 9 };
let d = zlib.deflateRawSync(new Buffer.from(s), opt);
let b = encode64(String.fromCharCode(...d.subarray(0)));
return b;
// from synchro.js
/* Copyright (C) 1999 Masanao Izumo <[email protected]>
* Version: 1.0.1
* LastModified: Dec 25 1999
*/
function encode64(data) {
let r = '';
for (let i = 0; i < data.length; i += 3) {
if (i + 2 == data.length) {
r += append3bytes(data.charCodeAt(i), data.charCodeAt(i + 1), 0);
} else if (i + 1 == data.length) {
r += append3bytes(data.charCodeAt(i), 0, 0);
} else {
r += append3bytes(data.charCodeAt(i), data.charCodeAt(i + 1), data.charCodeAt(i + 2));
}
}
return r;
}
function append3bytes(b1, b2, b3) {
let c1 = b1 >> 2;
let c2 = ((b1 & 0x3) << 4) | (b2 >> 4);
let c3 = ((b2 & 0xf) << 2) | (b3 >> 6);
let c4 = b3 & 0x3f;
let r = '';
r += encode6bit(c1 & 0x3f);
r += encode6bit(c2 & 0x3f);
r += encode6bit(c3 & 0x3f);
r += encode6bit(c4 & 0x3f);
return r;
}
function encode6bit(b) {
if (b < 10) {
return String.fromCharCode(48 + b);
}
b -= 10;
if (b < 26) {
return String.fromCharCode(65 + b);
}
b -= 26;
if (b < 26) {
return String.fromCharCode(97 + b);
}
b -= 26;
if (b == 0) {
return '-';
}
if (b == 1) {
return '_';
}
return '?';
}
};
const plantUmlServerUrl = (baseURL, imageFormat, content) =>
`${baseURL}/${imageFormat}/0/${urlTextFrom(content)}`;
const clearConsole = () => {
process.stdout.write('\x1b[2J');
process.stdout.write('\x1b[0f');
};
const plantumlVersions = [
{
version: '1.2020.07',
jar: 'plantuml-1.2020.7.jar'
},
{
version: '1.2020.17',
jar: 'plantuml.1.2020.17.jar'
},
{
version: '1.2021.7',
jar: 'plantuml.1.2021.7.jar'
},
{
version: '1.2021.12',
jar: 'plantuml.1.2021.12.jar'
},
{
version: '1.2022.3',
isLatest: true,
jar: 'plantuml-1.2022.3.jar'
}
];
module.exports = {
makeDirectory,
readFile,
writeFile,
encodeURIPath,
writeOnSameLine,
clearConsole,
plantUmlServerUrl,
plantumlVersions
};