forked from pietervdvn/MapComplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateIncludedImages.ts
53 lines (43 loc) · 1.7 KB
/
generateIncludedImages.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
52
53
import * as fs from "fs";
function genImages(dryrun = false) {
console.log("Generating images")
const dir = fs.readdirSync("./assets/svg")
let module = "import Img from \"./UI/Base/Img\";\nimport {FixedUiElement} from \"./UI/Base/FixedUiElement\";\n\nexport default class Svg {\n\n\n";
const allNames: string[] = [];
for (const path of dir) {
if (path.endsWith("license_info.json")) {
continue;
}
if (!path.endsWith(".svg")) {
throw "Non-svg file detected in the svg files: " + path;
}
let svg = fs.readFileSync("./assets/svg/" + path, "utf-8")
.replace(/<\?xml.*?>/, "")
.replace(/fill: ?none;/g, "fill: none !important;") // This is such a brittle hack...
.replace(/\n/g, " ")
.replace(/\r/g, "")
.replace(/\\/g, "\\")
.replace(/"/g, "\\\"")
const name = path.substr(0, path.length - 4)
.replace(/[ -]/g, "_");
if (dryrun) {
svg = "xxx"
}
let rawName = name;
if (dryrun) {
rawName = "add";
}
module += ` public static ${name} = "${svg}"\n`
module += ` public static ${name}_img = Img.AsImageElement(Svg.${rawName})\n`
module += ` public static ${name}_svg() { return new Img(Svg.${rawName}, true);}\n`
module += ` public static ${name}_ui() { return new FixedUiElement(Svg.${rawName}_img);}\n\n`
if (!dryrun) {
allNames.push(`"${path}": Svg.${name}`)
}
}
module += `public static All = {${allNames.join(",")}};`
module += "}\n";
fs.writeFileSync("Svg.ts", module);
console.log("Done")
}
genImages()