diff --git a/packages/core/src/schema.ts b/packages/core/src/schema.ts new file mode 100644 index 00000000..848776be --- /dev/null +++ b/packages/core/src/schema.ts @@ -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 = {}; + + 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 }; diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 16c28233..168e2f0e 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -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 }; diff --git a/packages/core/src/utils/writeSchema.ts b/packages/core/src/utils/writeSchema.ts new file mode 100644 index 00000000..e9c2c90d --- /dev/null +++ b/packages/core/src/utils/writeSchema.ts @@ -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 };