💅 Code refactor

This commit is contained in:
ful1e5 2021-02-20 19:55:35 +05:30
parent 027b2be19b
commit 6c44d15642
3 changed files with 26 additions and 25 deletions

View file

@ -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 });

View file

@ -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;
}
}

View file

@ -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);
}