🖼️ BitmapsGenerator init

This commit is contained in:
ful1e5 2021-02-19 20:04:20 +05:30
parent 4329aca7aa
commit 99dde92a8a
3 changed files with 90 additions and 2 deletions

View file

@ -0,0 +1,72 @@
import fs from "fs";
import path from "path";
import * as puppeteer from "puppeteer";
class BitmapsGenerator {
/**
* Generate Png files from svg code.
* @param themeName Give name, So all bitmaps files are organized in one directory.
* @param bitmapsDir `absolute` or `relative` path, Where `.png` files will store.
*/
constructor(
private bitmapsDir: string,
private readonly themeName: string
) {
this.bitmapsDir = path.resolve(bitmapsDir, themeName);
this.createDir(this.bitmapsDir);
// TODO
console.log(this.themeName);
}
/**
* Prepare headless browser.
*/
async initialize() {}
static async create(bitmapsDir: string, themeName: string) {
const newObject = new BitmapsGenerator(bitmapsDir, themeName);
await newObject.initialize();
}
/**
* Create directory if it doesn't exists.
* @param dirPath directory `absolute` path.
*/
private createDir(dirPath: string) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
protected async staticPng(page: puppeteer.Page) {
// TODO
console.log("Static");
await page.close();
}
protected async animatedPng(page: puppeteer.Page) {
// TODO
console.log("animated");
await page.close();
}
public async getBrowser() {
return await puppeteer.launch({
ignoreDefaultArgs: [" --single-process ", "--no-sandbox"],
headless: true,
});
}
public async toPng(
browser: puppeteer.Browser,
content: string,
animated: boolean = false
) {
const page = await browser.newPage();
await page.setContent(content);
animated ? this.animatedPng(page) : this.staticPng(page);
}
}
export { BitmapsGenerator };

View file

@ -1,3 +1,4 @@
import { BitmapsGenerator } from "./BitmapsGenerator";
import * as SVGHandler from "./SVGHandler";
export { SVGHandler };
export { BitmapsGenerator, SVGHandler };

View file

@ -1,5 +1,20 @@
import path from "path";
import { BitmapsGenerator, SVGHandler } from "bibata-core";
const root = path.resolve(__dirname, "../../../../");
const svgDir = path.resolve(root, "svg", "modern");
const main = async () => {
console.log("Bibata Modern");
const SVG = new SVGHandler.SvgDirectoryParser(svgDir);
SVG.getStatic().forEach((svg) => {
console.log(svg);
});
SVG.getAnimated().forEach((svg) => {
console.log(svg);
});
};
main();