🔧 Dynamic .svg generation with Schema

This commit is contained in:
ful1e5 2020-08-15 19:29:05 +05:30
parent c53544d882
commit 4405467b23

View file

@ -1,35 +1,68 @@
import path from "path";
import fs from "fs";
import path from "path";
import rimraf from "rimraf";
import { staticCursors, animatedCursors, animatedClip } from "./cursors.json";
import { ColorSchemas } from "./types";
// Source Directory
export const rawSvgsDir = path.resolve("./src/svg/raw");
if (!fs.existsSync(rawSvgsDir))
console.error("🚨🚨 Source files not Found 🚨🚨");
// --------------------------------------- 🌈 Cursors Variants 🌈
export const colorSchemes = {
ice: {
const colorSchemes: ColorSchemas = {
Ice: {
base: "#ffffff",
outline: "#000000"
}
};
// --------------------------------------- 🔧 Cursors Config 🔧
// --------------------------------------- 🔧 Render Configs 🔧
const configs: any = [];
// Source Directory
const svgsDir = path.resolve("./src/svg");
if (!fs.existsSync(svgsDir)) console.error("🚨🚨 Source files not Found 🚨🚨");
for (let [schema] of Object.entries(colorSchemes)) {
const { base, outline } = colorSchemes[schema];
const schemaName = `Bibata_${schema}`;
// Resolve Paths for svg
const staticSvgs = staticCursors.map((svg: string) =>
path.resolve(svgsDir, svg)
);
const schemaSvgsPath = path.resolve("./src/svg", schemaName);
// Out Directory
const bitmapsDir = path.resolve(process.cwd(), "bitmaps");
if (!fs.existsSync(bitmapsDir)) fs.mkdirSync(bitmapsDir);
if (fs.existsSync(schemaSvgsPath)) {
rimraf(schemaSvgsPath, function () {});
} else {
fs.mkdirSync(schemaSvgsPath);
}
export const config = {
animatedCursors,
animatedClip,
staticSvgs,
bitmapsDir,
svgsDir
};
// Resolve Paths for svg
const staticSvgs = staticCursors.map((svgFile: string) => {
// Read file
const filePath = path.resolve(schemaSvgsPath, svgFile);
// Replacing colorSchema
let content = fs
.readFileSync(path.resolve(rawSvgsDir, svgFile), "utf-8")
.toString();
content = content.replace("black", base).replace("white", outline);
// Writing new svg
fs.writeFileSync(filePath, content, "utf-8");
return filePath;
});
// Out Directory
const bitmapsDir = path.resolve(process.cwd(), "bitmaps", `Bibata_${schema}`);
if (!fs.existsSync(bitmapsDir)) fs.mkdirSync(bitmapsDir);
configs.push({
svgDir: schemaSvgsPath,
staticSvgs,
bitmapsDir,
animatedCursors,
animatedClip
});
}
export { configs };