mapcomplete/scripts/generateIncludedImages.ts

40 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-11-05 11:28:02 +00:00
import * as fs from "fs";
2020-11-06 00:58:26 +00:00
function genImages() {
2020-11-05 11:28:02 +00:00
2020-11-06 00:58:26 +00:00
console.log("Generating images")
const dir = fs.readdirSync("./assets/svg")
2020-11-05 11:28:02 +00:00
2021-01-02 23:19:42 +00:00
let module = "import Img from \"./UI/Base/Img\";\nimport {FixedUiElement} from \"./UI/Base/FixedUiElement\";\n\nexport default class Svg {\n\n\n";
2020-11-17 01:22:48 +00:00
const allNames: string[] = [];
2020-11-06 00:58:26 +00:00
for (const path of dir) {
2021-04-10 13:01:28 +00:00
if(path.endsWith("license_info.json")){
continue;
}
2020-11-06 00:58:26 +00:00
if (!path.endsWith(".svg")) {
throw "Non-svg file detected in the svg files: " + path;
}
const svg = fs.readFileSync("./assets/svg/" + path, "utf-8")
.replace(/<\?xml.*?>/, "")
.replace(/fill: ?none;/g,"fill: none !important;") // This is such a brittle hack...
2020-11-06 00:58:26 +00:00
.replace(/\n/g, " ")
.replace(/\r/g, "")
.replace(/\\/g, "\\")
.replace(/"/g, "\\\"")
const name = path.substr(0, path.length - 4)
.replace(/[ -]/g, "_");
module += ` public static ${name} = "${svg}"\n`
module += ` public static ${name}_img = Img.AsImageElement(Svg.${name})\n`
module += ` public static ${name}_svg() { return new FixedUiElement(Svg.${name});}\n`
2020-11-06 00:58:26 +00:00
module += ` public static ${name}_ui() { return new FixedUiElement(Svg.${name}_img);}\n\n`
2020-11-17 01:22:48 +00:00
allNames.push(`"${path}": Svg.${name}`)
2020-11-06 00:58:26 +00:00
}
2020-11-17 01:22:48 +00:00
module += `public static All = {${allNames.join(",")}};`
2020-11-06 00:58:26 +00:00
module += "}\n";
fs.writeFileSync("Svg.ts", module);
console.log("Done")
}
genImages()