2020-11-05 12:28:02 +01:00
|
|
|
|
import * as fs from "fs"
|
2024-07-10 11:40:00 +02:00
|
|
|
|
import Script from "./Script"
|
2023-11-19 04:38:34 +01:00
|
|
|
|
|
2021-11-21 02:44:35 +01:00
|
|
|
|
function genImages(dryrun = false) {
|
2020-11-06 01:58:26 +01:00
|
|
|
|
console.log("Generating images")
|
|
|
|
|
const dir = fs.readdirSync("./assets/svg")
|
|
|
|
|
for (const path of dir) {
|
2021-09-09 00:05:51 +02:00
|
|
|
|
if (path.endsWith("license_info.json")) {
|
2021-04-10 15:01:28 +02:00
|
|
|
|
continue
|
|
|
|
|
}
|
2023-07-27 14:15:30 +02:00
|
|
|
|
if (path.endsWith(".license")) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2021-09-09 00:05:51 +02:00
|
|
|
|
|
2020-11-06 01:58:26 +01:00
|
|
|
|
if (!path.endsWith(".svg")) {
|
|
|
|
|
throw "Non-svg file detected in the svg files: " + path
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-26 11:38:10 +02:00
|
|
|
|
let svg: string = fs
|
|
|
|
|
.readFileSync("./assets/svg/" + path, "utf-8")
|
2020-11-14 02:54:33 +01:00
|
|
|
|
.replace(/<\?xml.*?>/, "")
|
2023-11-19 04:38:34 +01:00
|
|
|
|
.replace(/<!DOCTYPE [^>]*>/, "")
|
2021-09-09 00:05:51 +02:00
|
|
|
|
.replace(/fill: ?none;/g, "fill: none !important;") // This is such a brittle hack...
|
2020-11-06 01:58:26 +01:00
|
|
|
|
.replace(/\n/g, " ")
|
|
|
|
|
.replace(/\r/g, "")
|
|
|
|
|
.replace(/\\/g, "\\")
|
2024-07-21 10:52:51 +02:00
|
|
|
|
.replace(/"/g, '\\"')
|
2023-10-26 14:13:04 +02:00
|
|
|
|
.replaceAll(" ", " ")
|
2022-07-26 11:38:10 +02:00
|
|
|
|
|
2023-10-26 14:13:04 +02:00
|
|
|
|
let hasNonAsciiChars = Array.from(svg)
|
|
|
|
|
.filter((char) => char.charCodeAt(0) > 127)
|
|
|
|
|
.map((char) => char.charCodeAt(0))
|
|
|
|
|
.join(", ")
|
|
|
|
|
if (hasNonAsciiChars.length > 0) {
|
|
|
|
|
throw "The svg '" + path + "' has non-ascii characters: " + hasNonAsciiChars
|
2022-07-26 11:38:10 +02:00
|
|
|
|
}
|
2022-09-19 12:34:05 +02:00
|
|
|
|
const name = path.substring(0, path.length - 4).replace(/[ -]/g, "_")
|
2021-11-21 02:44:35 +01:00
|
|
|
|
|
2023-10-06 23:56:50 +02:00
|
|
|
|
const nameUC = name.toUpperCase().at(0) + name.substring(1)
|
|
|
|
|
const svelteCode =
|
2024-07-21 10:52:51 +02:00
|
|
|
|
'<script>\nexport let color = "#000000"\n</script>\n' +
|
2023-11-16 03:30:29 +01:00
|
|
|
|
svg
|
|
|
|
|
.replace(
|
|
|
|
|
"<svg ",
|
2024-07-21 10:52:51 +02:00
|
|
|
|
"<svg {...$$$$restProps} on:click on:mouseover on:mouseenter on:mouseleave on:keydown on:focus "
|
2023-11-16 03:30:29 +01:00
|
|
|
|
)
|
2024-07-21 10:52:51 +02:00
|
|
|
|
.replace(/\\"/g, '"')
|
2023-11-16 03:30:29 +01:00
|
|
|
|
.replace(/(rgb\(0%,0%,0%\)|#000000|#000)/g, "{color}")
|
2023-10-06 23:56:50 +02:00
|
|
|
|
fs.writeFileSync("./src/assets/svg/" + nameUC + ".svelte", svelteCode, "utf8")
|
2024-07-10 11:40:00 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-19 04:38:34 +01:00
|
|
|
|
|
2024-07-10 11:40:00 +02:00
|
|
|
|
class GenerateIncludedImages extends Script {
|
|
|
|
|
constructor() {
|
|
|
|
|
super("Converts all images from assets/svg into svelte-classes.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async main(args: string[]): Promise<void> {
|
|
|
|
|
genImages()
|
2020-11-06 01:58:26 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-09 00:05:51 +02:00
|
|
|
|
|
2024-07-10 11:40:00 +02:00
|
|
|
|
new GenerateIncludedImages().run()
|