forked from MapComplete/MapComplete
		
	
		
			
				
	
	
		
			72 lines
		
	
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			72 lines
		
	
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import * as fs from "fs"
 | 
						||
import Script from "./Script"
 | 
						||
 | 
						||
function genImages() {
 | 
						||
    console.log("Generating images")
 | 
						||
    const dir = fs.readdirSync("./assets/svg")
 | 
						||
    for (const path of dir) {
 | 
						||
        if (path.endsWith("license_info.json")) {
 | 
						||
            continue
 | 
						||
        }
 | 
						||
        if (path.endsWith(".license")) {
 | 
						||
            continue
 | 
						||
        }
 | 
						||
 | 
						||
        if (!path.endsWith(".svg")) {
 | 
						||
            throw "Non-svg file detected in the svg files: " + path
 | 
						||
        }
 | 
						||
 | 
						||
        let svg: string = fs
 | 
						||
            .readFileSync("./assets/svg/" + path, "utf-8")
 | 
						||
            .replace(/<\?xml.*?>/, "")
 | 
						||
            .replace(/<!DOCTYPE [^>]*>/, "")
 | 
						||
            .replace(/fill: ?none;/g, "fill: none !important;") // This is such a brittle hack...
 | 
						||
            .replace(/\n/g, " ")
 | 
						||
            .replace(/\r/g, "")
 | 
						||
            .replace(/\\/g, "\\")
 | 
						||
            .replace(/"/g, '\\"')
 | 
						||
            .replaceAll(" ", " ")
 | 
						||
 | 
						||
        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
 | 
						||
        }
 | 
						||
        const name = path.substring(0, path.length - 4).replace(/[ -]/g, "_")
 | 
						||
 | 
						||
        const nameUC = name.toUpperCase().at(0) + name.substring(1)
 | 
						||
 | 
						||
        const svgBody = svg
 | 
						||
            .replace(
 | 
						||
                "<svg ",
 | 
						||
                "<svg {...$$$$restProps} on:click on:mouseover on:mouseenter on:mouseleave on:keydown on:focus "
 | 
						||
            )
 | 
						||
            .replace(/\\"/g, '"')
 | 
						||
            .replace(/(rgb\(0%,0%,0%\)|#000000|#000)/g, "{color}")
 | 
						||
 | 
						||
        const hasColor = svgBody.indexOf("{color}") >= 0
 | 
						||
 | 
						||
        const svelteCode = [
 | 
						||
            "<script>",
 | 
						||
            "export " + (hasColor ? "let" : "const") + ' color = "#000000"',
 | 
						||
            "</script>",
 | 
						||
            svgBody,
 | 
						||
        ].join("\n")
 | 
						||
 | 
						||
        fs.writeFileSync("./src/assets/svg/" + nameUC + ".svelte", svelteCode, "utf8")
 | 
						||
    }
 | 
						||
}
 | 
						||
 | 
						||
class GenerateIncludedImages extends Script {
 | 
						||
    constructor() {
 | 
						||
        super("Converts all images from assets/svg into svelte-classes.")
 | 
						||
    }
 | 
						||
 | 
						||
    async main(): Promise<void> {
 | 
						||
        genImages()
 | 
						||
    }
 | 
						||
}
 | 
						||
 | 
						||
new GenerateIncludedImages().run()
 |