-
Notifications
You must be signed in to change notification settings - Fork 10
/
create-example-cache.js
57 lines (50 loc) · 1.67 KB
/
create-example-cache.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
import data from './src/examples/data'
import { createHash } from 'crypto'
import axios from 'axios'
import util from 'util'
import stream from 'stream'
import fs from 'fs'
const pipeline = util.promisify(stream.pipeline)
const fileExists = (path) => new Promise((resolve, reject) => {
fs.access(path, fs.F_OK, (err) => {
if (err) {
resolve(false);
} else {
resolve(true);
}
})
})
const writeToFile = (path, content) => {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(path);
file.write(content);
file.end();
file.on("finish", () => { resolve(true); });
file.on("error", reject);
});
}
const md5 = (s) => createHash('md5').update(s).digest('hex');
const engine = 'https://kroki.io'
const dirname = './public/cache'
const createCache = async (example) => {
const ext = 'svg'
const radical = [example.diagramType, 'svg', example.example].join('/')
const url = `${engine}/${radical}`
const sum = md5(radical)
const response = await axios.get(url, { responseType: 'stream' })
const filename = `${sum}.${ext}`
const fullFilename = `${dirname}/${filename}`
if (! await fileExists(fullFilename)) {
console.log(`${sum} : ${example.title} - ${example.description}`)
await pipeline(response.data, fs.createWriteStream(fullFilename))
}
return filename
}
const main = async () => {
if (! await fileExists(dirname)) {
await fs.promises.mkdir(dirname)
}
const cache = await Promise.all(data.map(createCache))
await writeToFile('./public/cache.js', `window.cache = ${JSON.stringify(cache.sort())};`)
}
main()