-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup-plugin-gen-pi.ts
51 lines (47 loc) · 1.74 KB
/
rollup-plugin-gen-pi.ts
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
import fsp from 'fs/promises';
// @ts-ignore
import formatter from 'html-formatter';
import { Plugin } from "rollup";
function url(file: string) {
return new URL(file, import.meta.url);
}
type GeneratePropertyInspectorOptions = {
base: string;
actionsDir: string;
pluginId: string;
titleRows: {
default: number;
map: Record<string, number>;
}
}
export default function generatePi(options: GeneratePropertyInspectorOptions): Plugin {
return {
name: 'generate-pi',
buildStart() {
this.addWatchFile(options.actionsDir);
this.addWatchFile(options.base);
},
async generateBundle() {
const base = await fsp.readFile(url(options.base), 'utf-8');
const actionFiles = await fsp.readdir(url(options.actionsDir));
await Promise.all(actionFiles.map(async (file) => {
if (!file.endsWith(".html")) {
return;
}
const path = url(`${options.actionsDir}/${file}`);
const stats = await fsp.stat(path);
if (!stats.isFile()) {
return;
}
const action = await fsp.readFile(path, 'utf-8');
const name = file.replace(".html", "");
const rows = options.titleRows.map[name] ?? options.titleRows.default;
const output = base
.replace('{{ACTION_UUID}}', `${options.pluginId}.${name}`)
.replace("{{TITLE_ROWS}}", rows.toString())
.replace("{{ACTION_HTML_TEMPLATE}}", action);
this.emitFile({ fileName: file, source: formatter.render(output), type: "asset" });
}));
}
}
}