-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
111 lines (94 loc) · 2.81 KB
/
index.tsx
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
import { renderToStaticMarkup } from "react-dom/server";
import { $ } from "bun";
import { parseArgs } from "util";
import { readdir } from "node:fs/promises";
import { format } from "prettier";
import config from "./config.json";
const { values: options, positionals } = parseArgs({
args: Bun.argv,
options: {
list: {
type: "boolean",
default: false,
},
"use-latest": {
short: "l",
type: "boolean",
default: config["use-latest"] ?? true,
},
output: {
short: "o",
type: "string",
default: config.output ?? "eDP-1",
},
"render-to-desktop": {
short: "r",
type: "boolean",
default: config["render-to-desktop"] ?? false,
},
"render-to-png": {
short: "p",
type: "boolean",
default: config["render-to-png"] ?? false,
},
},
allowPositionals: true,
});
// console.log(JSON.stringify(options));
const drawings = await readdir("./src/drawings");
if (options.list) {
console.log(drawings.sort());
process.exit();
}
const mostRecentlyEditedDrawing = async () => {
const { stdout } =
await $`find src/drawings -type f -print | xargs stat -c %y=%n | sort -r | head -1 | cut -d= -f 2 | cut -d\/ -f 3`;
return stdout.toString().trim();
};
let drawing = positionals[2];
if (!drawing && options["use-latest"]) {
drawing = await mostRecentlyEditedDrawing();
console.info(
`Did not specify drawing. Using most recently edited drawing '${drawing}'.`,
);
}
if (!drawings.includes(drawing)) {
console.error(`The drawing "${drawing}" does not exist.`);
console.info({ drawings });
process.exit();
}
const modulePath = `./src/drawings/${drawing}`;
console.info(`Loading "${modulePath}"`);
const module = await import(modulePath);
const SVG = module.default;
const svgPath = `./images/svg/${SVG.name}.svg`;
const pngPath = `./images/png/${SVG.name}.png`;
console.time("renderSVG");
const svg = await format(renderToStaticMarkup(<SVG />), { parser: "html" });
console.timeEnd("renderSVG");
console.time("write svgPath");
await Bun.write(svgPath, svg);
console.timeEnd("write svgPath");
async function renderToPNG() {
console.time("convert pngPath");
const { stdout, stderr, exitCode } =
await $`rsvg-convert --dpi-x 150 --dpi-y 150 -o ${pngPath} ${svgPath}`;
if (exitCode) console.log(exitCode, stderr.toString());
console.timeEnd("convert pngPath");
}
async function setDesktopBackground() {
const { stdout, stderr, exitCode } =
await $`swww img -o ${options.output} -t top --resize fit --transition-duration 1 ${pngPath}`;
if (exitCode) console.log(stderr.toString());
}
if (options["render-to-png"]) {
try {
await renderToPNG();
} catch (e) {}
}
if (options["render-to-desktop"]) {
try {
await setDesktopBackground();
} catch (e) {}
}
console.info(`${new Date()}: Rendered: ${SVG.name}`);