🧹 Clean old functional code

This commit is contained in:
ful1e5 2020-09-28 17:58:49 +05:30
parent f2c467c533
commit 35ba9640d0
2 changed files with 0 additions and 275 deletions

View file

@ -1,144 +0,0 @@
import fs from "fs";
import path from "path";
import ora from "ora";
import chalk from "chalk";
import puppeteer from "puppeteer";
import { generateRenderTemplate } from "./utils/htmlTemplate";
import { matchImages } from "./utils/matchImages";
import { saveFrames } from "./utils/saveFrames";
import { getKeyName } from "./utils/getKeyName";
import { Config, Frames, PixelDiffRate } from "./types";
const pixelDiffRate: PixelDiffRate = {
"left_ptr_watch.svg": {
rate: 100
},
"wait.svg": {
rate: 990
}
};
const renderCursors = async (configs: Record<string, Config>) => {
const browser = await puppeteer.launch({
ignoreDefaultArgs: [" --single-process ", "--no-sandbox"],
headless: true
});
for (let [
schema,
{ bitmapsDir, staticCursors, animatedCursors }
] of Object.entries(configs)) {
// Checking Bitmaps Dir
if (!fs.existsSync(bitmapsDir)) {
fs.mkdirSync(bitmapsDir);
}
// Spinner
const spinner = ora();
spinner.text = ` Preparing ${schema} Color Schema...`;
spinner.start();
// Render
try {
spinner.color = "yellow";
for (let svgPath of staticCursors) {
const buffer = fs.readFileSync(svgPath, "utf8");
if (!buffer) throw new Error(`${svgPath} File Read error`);
// Generating HTML Template
const data = buffer.toString();
const template = generateRenderTemplate(data);
// config
const bitmapName = `${path.basename(svgPath, ".svg")}.png`;
const out = path.resolve(bitmapsDir, bitmapName);
// Render
const page = await browser.newPage();
await page.setContent(template);
await page.waitForSelector("#container");
const svgElement = await page.$("#container svg");
if (!svgElement) throw new Error("svg element not found");
spinner.text = ` Bitmaping ${chalk.greenBright(bitmapName)}`;
await svgElement.screenshot({ omitBackground: true, path: out });
await page.close();
}
for (let svgPath of animatedCursors) {
const buffer = fs.readFileSync(svgPath, "utf8");
if (!buffer) throw new Error(`${svgPath} File Read error`);
// Generating HTML Template
const data = buffer.toString();
const template = generateRenderTemplate(data);
const page = await browser.newPage();
await page.setContent(template, { waitUntil: "networkidle2" });
await page.waitForSelector("#container");
const svgElement = await page.$("#container svg");
if (!svgElement) throw new Error("svg element not found");
// Render Config
let index = 1;
let breakRendering = false;
const frames: Frames = {};
const firstKey = getKeyName(index, svgPath);
// 1st Frame
spinner.text = ` Bitmaping ${chalk.greenBright(firstKey)}`;
frames[firstKey] = {
buffer: await svgElement.screenshot({
omitBackground: true,
encoding: "binary"
})
};
// Pushing frames until it match to 1st frame
index++;
while (!breakRendering) {
const key = getKeyName(index, svgPath);
spinner.text = ` Bitmaping ${chalk.greenBright(key)}`;
const newFrame = await svgElement.screenshot({
omitBackground: true,
encoding: "binary"
});
const diff = matchImages({
img1Buff: frames[firstKey].buffer,
img2Buff: newFrame
});
const { rate } = pixelDiffRate[path.basename(svgPath)];
if (!(diff < rate)) {
frames[key] = { buffer: newFrame };
} else {
breakRendering = true;
}
index++;
}
saveFrames({ frames, bitmapsDir });
await page.close();
}
spinner.text = ` ${chalk.blueBright(
schema
)} Bitmaps stored at ${chalk.greenBright(
`${path.relative("../../../", bitmapsDir)}`
)}`;
spinner.succeed();
} catch (error) {
console.error(error);
spinner.fail();
process.exit(1);
}
}
};
export { renderCursors };

View file

@ -1,131 +0,0 @@
import fs from "fs";
import ora from "ora";
import chalk from "chalk";
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 spinner = ora();
spinner.text = ` Generating ${chalk.blueBright(
schemaName
)} Color Schema ...`;
const schemaSvgsPath = path.resolve(schemesPath, schemaName);
fs.mkdirSync(schemaSvgsPath, { recursive: true });
const { base, outline, watch } = colorSchemes[schema];
try {
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
cursor = path.basename(cursor);
writeSchemaData({
path: schemaSvgsPath,
fileName: cursor,
content
});
return path.resolve(schemaSvgsPath, cursor);
});
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
cursor = path.basename(cursor);
writeSchemaData({
path: schemaSvgsPath,
fileName: cursor,
content
});
return path.resolve(schemaSvgsPath, cursor);
});
// Creating Dir for store bitmaps
const bitmapsDir = path.resolve(bitmapsPath, schemaName);
fs.mkdirSync(bitmapsDir, { recursive: true });
// push config to Object
configs[schemaName] = {
bitmapsDir,
animatedCursors: aCursors,
staticCursors: sCursors
};
spinner.text = ` Saving ${chalk.blueBright(schemaName)} Color Schema ...`;
spinner.succeed();
} catch (error) {
console.log(error);
spinner.fail();
}
}
return configs;
};
export { generateConfigs };