🔥 Schema Generator in bibata-core package

This commit is contained in:
ful1e5 2020-08-31 18:05:50 +05:30
parent 8baf50a6e1
commit 2130840ab3
3 changed files with 148 additions and 1 deletions

115
packages/core/src/schema.ts Normal file
View file

@ -0,0 +1,115 @@
import fs from "fs";
import path from "path";
import { ColorSchemes, Config, PathConfig } from "./types";
import { writeSchemaData } from "./utils/writeSchema";
export interface GenerateConfigsArgs {
pathConfig: PathConfig;
colorSchemes: ColorSchemes;
themeName: string;
}
// --------------------------------------- Key Colors Configs 🛠 (Colors for replacement)
const watchKeyColor = "#FF0000";
const baseKeyColor = "#00FF00";
const outlineKeyColor = "#0000FF";
const generateConfigs = ({
pathConfig,
colorSchemes,
themeName
}: GenerateConfigsArgs) => {
const {
rawSvgsDir,
schemesPath,
bitmapsPath,
animatedCursors,
staticCursors
} = pathConfig;
if (!fs.existsSync(rawSvgsDir)) {
console.error(`🚨 .svg files not found in ${rawSvgsDir}`);
process.exit(1);
}
const configs: Record<string, Config> = {};
for (let [schema] of Object.entries(colorSchemes)) {
const schemaName = `${themeName}-${schema}`;
const schemaSvgsPath = path.resolve(schemesPath, schemaName);
fs.mkdirSync(schemaSvgsPath, { recursive: true });
const { base, outline, watch } = colorSchemes[schema];
const sCursors = staticCursors.map((cursor: string) => {
// Read file
let content = fs.readFileSync(cursor, "utf-8").toString();
content = content
.replace(new RegExp(baseKeyColor, "g"), base)
.replace(new RegExp(outlineKeyColor, "g"), outline);
// Save Schema
const cursorPath = path.resolve(
schemaSvgsPath,
"static",
path.basename(cursor)
);
writeSchemaData(cursorPath, content);
return cursorPath;
});
const aCursors = animatedCursors.map((cursor: string) => {
// Read file
let content = fs
.readFileSync(path.resolve(rawSvgsDir, cursor), "utf-8")
.toString();
// Animated Cursors have two parts:
// 1) Cursor Color
// 2) Watch Color
content = content
.replace(new RegExp(baseKeyColor, "g"), base)
.replace(new RegExp(outlineKeyColor, "g"), outline);
// try => replace `customize` colors
// onError => replace `schema` main colors
try {
if (!watch) throw new Error("");
const { background: b } = watch;
content = content.replace(new RegExp(watchKeyColor, "g"), b); // Watch Background
} catch (error) {
content = content.replace(new RegExp(watchKeyColor, "g"), base); // on error=> replace as base
}
// Save Schema
const cursorPath = path.resolve(
schemaSvgsPath,
"animated",
path.basename(cursor)
);
writeSchemaData(cursorPath, content);
return cursorPath;
});
// Creating Dir for store bitmaps
const bitmapsDir = path.resolve(bitmapsPath, schemaName);
fs.mkdirSync(bitmapsDir, { recursive: true });
// push config to Object
configs[schema] = {
bitmapsDir,
animatedCursors: aCursors,
staticCursors: sCursors
};
}
return configs;
};
export { generateConfigs };

View file

@ -4,10 +4,28 @@ interface Config {
bitmapsDir: string;
}
interface PathConfig {
rawSvgsDir: string;
schemesPath: string;
bitmapsPath: string;
animatedCursors: string[];
staticCursors: string[];
}
interface Frames {
[fileName: string]: {
buffer: Buffer;
};
}
export { Config, Frames };
interface ColorSchemes {
[name: string]: {
base: string;
outline: string;
watch?: {
background: string;
};
};
}
export { Config, Frames, ColorSchemes, PathConfig };

View file

@ -0,0 +1,14 @@
import { exists, mkdir, writeFile } from "fs";
const writeSchemaData = (location: string, content: string) => {
exists(location, (exists) => {
if (!exists) {
mkdir(location, { recursive: true }, () => {});
}
});
writeFile(location, content, "utf-8", () => {
throw new Error(`Oops Something went wrong with Schema Generator`);
});
};
export { writeSchemaData };