-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
58 lines (53 loc) · 2.13 KB
/
app.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
import { Page } from "./includes/page";
const fs = require('fs');
const path = require('path');
const {ArgumentParser} = require('argparse');
import {$} from "bun";
import { getIndex, writeIndex } from "./includes/files";
import { saveAsPNG } from "./includes/export";
const argparser = new ArgumentParser({
description: "MML Chart render service"
});
argparser.add_argument('-m', {help: 'Mode', type: 'str', choices: ['batch', 'single']});
argparser.add_argument('-i', {help: 'Input file/directory'});
argparser.add_argument('-e', {help: 'Export', choices: ['svg', 'png'], default: 'svg'});
argparser.add_argument('-f', {help: 'Force update', action: 'store_true'});
const args = argparser.parse_args();
const index = getIndex();
let filesChanged = 0;
const dirPath = './input/'+args.i;
const outPath = './output/'+args.i;
if (args.m === 'single') {
const input = require(dirPath);
const fileDirName = path.dirname(args.i);
const page = new Page(input, fileDirName);
const name = path.parse(dirPath).name;
fs.writeFileSync("./output/"+fileDirName+"/"+name+".svg", page.render());
if (args.e === 'png')
saveAsPNG('./output/'+fileDirName, name);
} else if (args.m === 'batch') {
const files = fs.readdirSync(dirPath).filter(f => path.extname(f) === '.json').map(f => path.join(dirPath, f));
if (!fs.existsSync(outPath))
fs.mkdirSync(outPath, {recursive: true});
if (!index?.[args.i])
index[args.i] = {};
const processFile = async (f) => {
const checksum = await $`shasum -a 512 ${f}`.text().then(i => i.split(' ')[0]);
if (index?.[args.i]?.[f] === checksum && !args.f)
return;
else
index[args.i][f] = checksum;
filesChanged++;
const page = new Page(require('./'+f), args.i);
const name = path.parse(f).name;
fs.writeFileSync(outPath+"/"+name+".svg", page.render());
if (args.e === 'png')
saveAsPNG(outPath, name);
};
Promise.all(files.map(processFile)).then(() => {
if (filesChanged > 0)
writeIndex(index);
else
console.log("No files changed!");
});
}