diff --git a/bitmapper/packages/core/src/BitmapsGenerator.ts b/bitmapper/packages/core/src/BitmapsGenerator.ts index 8d6b946c..9580f2fa 100644 --- a/bitmapper/packages/core/src/BitmapsGenerator.ts +++ b/bitmapper/packages/core/src/BitmapsGenerator.ts @@ -65,7 +65,7 @@ class BitmapsGenerator { const page = await browser.newPage(); const svg = await this.getSvgElement(page, content); - const out = path.resolve(this.bitmapsDir, key); + const out = path.resolve(this.bitmapsDir, `${key}.png`); console.log("Saving", key, "..."); await svg.screenshot({ omitBackground: true, path: out }); diff --git a/bitmapper/packages/core/src/SVGHandler/SvgDirectoryParser.ts b/bitmapper/packages/core/src/SVGHandler/SvgDirectoryParser.ts index 33f90202..65756dad 100644 --- a/bitmapper/packages/core/src/SVGHandler/SvgDirectoryParser.ts +++ b/bitmapper/packages/core/src/SVGHandler/SvgDirectoryParser.ts @@ -1,6 +1,11 @@ import fs from "fs"; import path from "path"; +interface Svg { + key: string; + content: string; +} + class SvgDirectoryParser { /** * Manage and Parse SVG file path in `absolute` fashion. @@ -18,10 +23,16 @@ class SvgDirectoryParser { } } + private readData(f: string): Svg { + const content = fs.readFileSync(f, "utf-8"); + const key = path.basename(f, ".svg"); + return { content, key }; + } + /** - * Return absolute paths array of SVG files located inside '@svgDir/static' + * Return absolute paths array of SVG files data located inside '@svgDir/static' */ - public getStatic(): string[] { + public getStatic(): Svg[] { const staticDir = path.resolve(this.svgDir, "static"); if (!fs.existsSync(staticDir)) { @@ -29,38 +40,37 @@ class SvgDirectoryParser { this.semiAnimated = true; return []; } else { - const staticCursors = fs + const svgs = fs .readdirSync(staticDir) - .map((f) => path.resolve(staticDir, f)); + .map((f) => this.readData(path.resolve(staticDir, f))); - if (staticCursors.length == 0) { + if (svgs.length == 0) { throw new Error("Static Cursors directory is empty"); } - return staticCursors; + return svgs; } } - /** - * Return absolute paths array of SVG files located inside '@svgDir/animated' + * Return absolute paths array of SVG files data located inside '@svgDir/animated' */ - public getAnimated(): string[] { + public getAnimated(): Svg[] { const animatedDir = path.resolve(this.svgDir, "animated"); if (!fs.existsSync(animatedDir)) { throw new Error("Animated Cursors directory not found"); } - const animatedCursors = fs + const svgs = fs .readdirSync(animatedDir) - .map((f) => path.resolve(animatedDir, f)); + .map((f) => this.readData(path.resolve(animatedDir, f))); - if (animatedCursors.length == 0 && this.semiAnimated) { + if (svgs.length == 0 && this.semiAnimated) { throw new Error( `Can't parse svg directory ${this.svgDir} as semi-animated theme` ); } - return animatedCursors; + return svgs; } } diff --git a/bitmapper/packages/modern/src/index.ts b/bitmapper/packages/modern/src/index.ts index cc63480e..4ad91827 100644 --- a/bitmapper/packages/modern/src/index.ts +++ b/bitmapper/packages/modern/src/index.ts @@ -1,5 +1,4 @@ import path from "path"; -import fs from "fs"; import { BitmapsGenerator, SVGHandler } from "bibata-core"; import { Colors } from "bibata-core/src/types"; @@ -24,21 +23,13 @@ const main = async () => { const png = new BitmapsGenerator(bitmapsDir); const browser = await png.getBrowser(); - for (const svg of SVG.getStatic()) { - const key = `${path.basename(svg, ".svg")}.png`; - - let content = fs.readFileSync(svg, "utf-8"); + for (let { key, content } of SVG.getStatic()) { content = SVGHandler.colorSvg(content, color); - await png.generateStatic(browser, content, key); } - for (const svg of SVG.getAnimated()) { - const key = `${path.basename(svg, ".svg")}.png`; - - let content = fs.readFileSync(svg, "utf-8"); + for (let { key, content } of SVG.getAnimated()) { content = SVGHandler.colorSvg(content, color); - await png.generateAnimated(browser, content, key); }