Skip to content

Commit

Permalink
breaking-change: The default render engine is now resvg-js
Browse files Browse the repository at this point in the history
  • Loading branch information
ful1e5 committed Feb 24, 2024
1 parent a4a801a commit 07ae4f1
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 221 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Breaking Changes

- The default renderer is now [resvg-js](https://github.com/yisibl/resvg-js).
- The default render engine is now [resvg-js](https://github.com/yisibl/resvg-js).
To use Puppeteer, either assign the `--puppeteer` option in the command line or set `"use": "puppeteer"` in the JSON config.

**Example in CLI Argument:**
Expand Down
6 changes: 3 additions & 3 deletions bin/cbmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const cliApp = () => __awaiter(void 0, void 0, void 0, function* () {
})
.option("-d, --dir <path>", "Specify the directory to search for SVG files.")
.option("-o, --out <path>", "Specify the directory where rasterized PNG files will be saved.", "./bitmaps")
.option("--puppeteer", "Render using Puppeteer (which require internet).")
.option("--puppeteer", "Use Puppeteer for rendering (requires internet) or default to '@resvg/resvg-js' for SVG rendering.")
.option("-n, --themeName <string>", `Specify the name of sub-directory inside output directory. ${chalk.yellow("(Deprecated: Use the '-o' option to specify the full output path instead.)")}`)
.option("-bc, --baseColor [string]", "Specifies the CSS color for inner part of cursor. (optional)")
.option("-oc, --outlineColor [string]", "Specifies the CSS color for cursor's ouline. (optional)")
Expand All @@ -53,14 +53,14 @@ const cliApp = () => __awaiter(void 0, void 0, void 0, function* () {
const options = program.opts();
// ---------------------- Config Based Rendering
if (configPath) {
const { use, configs } = parseConfig(configPath);
const configs = parseConfig(configPath);
try {
for (var _e = true, _f = __asyncValues(Object.entries(configs)), _g; _g = yield _f.next(), _a = _g.done, !_a; _e = true) {
_c = _g.value;
_e = false;
const [key, config] = _c;
console.log(`${chalk.blueBright.bold("[+]")} Parsing ${key} Config...`);
if (use === "puppeteer") {
if (options.puppeteer || config.use.toLowerCase() === "puppeteer") {
yield renderer.renderPngsWithPuppeteer(config.dir, config.out, {
colors: config.colors,
fps: options.fps || config.fps,
Expand Down
11 changes: 4 additions & 7 deletions bin/lib/parseConfig.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { Color } from "./colorSvg.js";
export type Config = {
use: "puppeteer" | "default";
colors?: Color[];
dir: string;
out: string;
fps?: number;
colors?: Color[];
out: string;
};
export type Configs = {
[key: string]: Config;
};
export type ParsedConfig = {
use: "puppeteer" | "default";
configs: Configs;
};
export declare const parseConfig: (p: string) => ParsedConfig;
export declare const parseConfig: (p: string) => Configs;
9 changes: 1 addition & 8 deletions bin/lib/parseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,5 @@ import path from "path";
export const parseConfig = (p) => {
const file = path.resolve(p);
const configs = JSON.parse(fs.readFileSync(file, { encoding: "utf-8", flag: "r" }));
const use = configs["use"];
delete configs["use"];
if (use.lowerCase() === "puppeteer") {
return { configs, use: "puppeteer" };
}
else {
return { configs, use: "default" };
}
return configs;
};
2 changes: 1 addition & 1 deletion bin/lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const renderPngs = (dir, out, options) => __awaiter(void 0, void 0, void 0, func
if (options === null || options === void 0 ? void 0 : options.colors) {
code = colorSvg(code, options.colors);
}
subSpinner.text = fmt("Extracting PNG frames...");
subSpinner.text = fmt("Rendering PNG frames...");
const resvg = new Resvg(code);
const pngData = resvg.render();
const pngBuffer = pngData.asPng();
Expand Down
5 changes: 3 additions & 2 deletions bin/modules/PngRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ var __await = (this && this.__await) || function (v) { return this instanceof __
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
return i = {}, verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
Expand Down
2 changes: 1 addition & 1 deletion bin/version.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export declare const LIB_VERSION = "1.0.1";
export declare const LIB_VERSION = "1.1.0";
2 changes: 1 addition & 1 deletion bin/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const LIB_VERSION = "1.0.1";
export const LIB_VERSION = "1.1.0";
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"cbmp": "bin/cbmp.js"
},
"engines": {
"node": "^12.17.0 || ^14.13 || >=16.0.0"
"node": ">=16.16.0"
},
"scripts": {
"prebuild": "node -p \"'export const LIB_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts",
Expand Down
9 changes: 6 additions & 3 deletions src/cbmp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ const cliApp = async () => {
"./bitmaps",
)

.option("--puppeteer", "Render using Puppeteer (which require internet).")
.option(
"--puppeteer",
"Use Puppeteer for rendering (requires internet) or default to '@resvg/resvg-js' for SVG rendering.",
)

.option(
"-n, --themeName <string>",
Expand Down Expand Up @@ -89,12 +92,12 @@ const cliApp = async () => {

// ---------------------- Config Based Rendering
if (configPath) {
const { use, configs } = parseConfig(configPath);
const configs = parseConfig(configPath);

for await (const [key, config] of Object.entries(configs)) {
console.log(`${chalk.blueBright.bold("[+]")} Parsing ${key} Config...`);

if (use === "puppeteer") {
if (options.puppeteer || config.use.toLowerCase() === "puppeteer") {
await renderer.renderPngsWithPuppeteer(config.dir, config.out, {
colors: config.colors,
fps: options.fps || config.fps,
Expand Down
21 changes: 5 additions & 16 deletions src/lib/parseConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,22 @@ import path from "path";
import { Color } from "./colorSvg.js";

export type Config = {
use: "puppeteer" | "default";
colors?: Color[];
dir: string;
out: string;
fps?: number;
colors?: Color[];
out: string;
};

export type Configs = {
[key: string]: Config;
};

export type ParsedConfig = {
use: "puppeteer" | "default";
configs: Configs;
};

export const parseConfig = (p: string): ParsedConfig => {
export const parseConfig = (p: string): Configs => {
const file = path.resolve(p);
const configs = JSON.parse(
fs.readFileSync(file, { encoding: "utf-8", flag: "r" }),
);

const use = configs["use"];
delete configs["use"];

if (use.lowerCase() === "puppeteer") {
return { configs, use: "puppeteer" };
} else {
return { configs, use: "default" };
}
return configs;
};
2 changes: 1 addition & 1 deletion src/lib/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const renderPngs = async (
code = colorSvg(code, options.colors);
}

subSpinner.text = fmt("Extracting PNG frames...");
subSpinner.text = fmt("Rendering PNG frames...");

const resvg = new Resvg(code);
const pngData = resvg.render();
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const LIB_VERSION = "1.0.1";
export const LIB_VERSION = "1.1.0";

0 comments on commit 07ae4f1

Please sign in to comment.