kist 0.1.36 → 0.1.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@ import { Action } from "../../core/pipeline/Action";
2
2
  import { ActionOptionsType } from "../../types/ActionOptionsType";
3
3
  /**
4
4
  * SvgToPngAction converts SVG content to PNG format.
5
- * Uses `resvg-js` for conversion and `jsdom` for SVG element manipulation.
5
+ * Uses `canvg` for conversion and `jsdom` for SVG element manipulation.
6
6
  */
7
7
  export declare class SvgToPngAction extends Action {
8
8
  /**
@@ -16,21 +16,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
16
16
  };
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  exports.SvgToPngAction = void 0;
19
- const resvg_js_1 = require("@resvg/resvg-js");
19
+ const canvas_1 = require("canvas");
20
+ const canvg_1 = require("canvg");
20
21
  const fs_1 = __importDefault(require("fs"));
21
22
  const jsdom_1 = require("jsdom");
22
23
  const path_1 = __importDefault(require("path"));
23
24
  const Action_1 = require("../../core/pipeline/Action");
24
25
  // ============================================================================
26
+ // Utilities
27
+ // ============================================================================
28
+ function getSafeRenderingContext(canvas) {
29
+ return canvas.getContext("2d");
30
+ }
31
+ // ============================================================================
25
32
  // Classes
26
33
  // ============================================================================
27
34
  /**
28
35
  * SvgToPngAction converts SVG content to PNG format.
29
- * Uses `resvg-js` for conversion and `jsdom` for SVG element manipulation.
36
+ * Uses `canvg` for conversion and `jsdom` for SVG element manipulation.
30
37
  */
31
38
  class SvgToPngAction extends Action_1.Action {
32
- // Methods
33
- // ========================================================================
34
39
  /**
35
40
  * Executes the SVG-to-PNG conversion process.
36
41
  * @param options - Options including SVG content, output path, width,
@@ -63,29 +68,27 @@ class SvgToPngAction extends Action_1.Action {
63
68
  convert(svgContent, outputPath, width, height) {
64
69
  return __awaiter(this, void 0, void 0, function* () {
65
70
  try {
66
- // Ensure the output directory exists
67
71
  const outputDir = path_1.default.dirname(outputPath);
68
72
  if (!fs_1.default.existsSync(outputDir)) {
69
73
  fs_1.default.mkdirSync(outputDir, { recursive: true });
70
74
  }
71
- // Parse SVG and apply width/height
72
75
  const dom = new jsdom_1.JSDOM(svgContent);
73
76
  const svgElement = dom.window.document.querySelector("svg");
74
77
  if (!svgElement) {
75
78
  throw new Error("Invalid SVG content");
76
79
  }
77
- if (width)
78
- svgElement.setAttribute("width", width.toString());
79
- if (height)
80
- svgElement.setAttribute("height", height.toString());
80
+ const w = width ||
81
+ parseInt(svgElement.getAttribute("width") || "800", 10);
82
+ const h = height ||
83
+ parseInt(svgElement.getAttribute("height") || "600", 10);
84
+ svgElement.setAttribute("width", w.toString());
85
+ svgElement.setAttribute("height", h.toString());
81
86
  const updatedSvgContent = svgElement.outerHTML;
82
- // Render using Resvg
83
- const resvg = new resvg_js_1.Resvg(updatedSvgContent, {
84
- fitTo: width && height
85
- ? { mode: "width", value: width }
86
- : undefined,
87
- });
88
- const pngBuffer = resvg.render().asPng();
87
+ const canvas = (0, canvas_1.createCanvas)(w, h);
88
+ const ctx = getSafeRenderingContext(canvas);
89
+ const canvg = yield canvg_1.Canvg.from(ctx, updatedSvgContent);
90
+ yield canvg.render();
91
+ const pngBuffer = canvas.toBuffer("image/png");
89
92
  fs_1.default.writeFileSync(outputPath, pngBuffer);
90
93
  }
91
94
  catch (error) {
@@ -98,7 +101,7 @@ class SvgToPngAction extends Action_1.Action {
98
101
  * @returns A string description of the action.
99
102
  */
100
103
  describe() {
101
- return "Converts SVG content to PNG format with optional resizing using resvg-js.";
104
+ return "Converts SVG content to PNG format with optional resizing using canvg and canvas.";
102
105
  }
103
106
  }
104
107
  exports.SvgToPngAction = SvgToPngAction;
@@ -1,10 +1,8 @@
1
1
  import { ConfigInterface } from "../../interface/ConfigInterface";
2
2
  import { AbstractProcess } from "../abstract/AbstractProcess";
3
3
  /**
4
- * ConfigLoader is responsible for loading and parsing configuration files
5
- * (`kist.yaml` or `kist.yml` by default). It validates the configuration
6
- * structure and provides it in a usable format for the pipeline.
7
- * Extends `AbstractProcess` for consistent logging.
4
+ * ConfigLoader is responsible for loading and parsing configuration files.
5
+ * Supports a custom path via `--config`, and falls back to `kist.yaml` or `kist.yml`.
8
6
  */
9
7
  export declare class ConfigLoader extends AbstractProcess {
10
8
  /**
@@ -15,21 +13,12 @@ export declare class ConfigLoader extends AbstractProcess {
15
13
  * Default filenames to search for configuration files.
16
14
  */
17
15
  private readonly defaultFilenames;
18
- /**
19
- * Constructs a ConfigLoader instance.
20
- * Searches for `kist.yaml` or `kist.yml` in the working directory
21
- * unless a custom path is provided.
22
- *
23
- * @param configPath - Optional custom configuration file path.
24
- */
25
- constructor(configPath?: string);
16
+ constructor();
26
17
  /**
27
18
  * Initializes the loader by locating the configuration file.
28
- * Searches for `kist.yaml` or `kist.yml` by default.
29
- *
30
- * @param configPath - Optional custom configuration file path.
19
+ * Uses `--config` CLI flag if provided, otherwise defaults.
31
20
  */
32
- initialize(configPath?: string): Promise<void>;
21
+ initialize(): Promise<void>;
33
22
  /**
34
23
  * Loads and validates the configuration file.
35
24
  *
@@ -19,27 +19,19 @@ exports.ConfigLoader = void 0;
19
19
  const fs_1 = __importDefault(require("fs"));
20
20
  const js_yaml_1 = __importDefault(require("js-yaml"));
21
21
  const path_1 = __importDefault(require("path"));
22
+ const ArgumentParser_1 = require("../../cli/ArgumentParser");
22
23
  const AbstractProcess_1 = require("../abstract/AbstractProcess");
23
24
  // ============================================================================
24
25
  // Class
25
26
  // ============================================================================
26
27
  /**
27
- * ConfigLoader is responsible for loading and parsing configuration files
28
- * (`kist.yaml` or `kist.yml` by default). It validates the configuration
29
- * structure and provides it in a usable format for the pipeline.
30
- * Extends `AbstractProcess` for consistent logging.
28
+ * ConfigLoader is responsible for loading and parsing configuration files.
29
+ * Supports a custom path via `--config`, and falls back to `kist.yaml` or `kist.yml`.
31
30
  */
32
31
  class ConfigLoader extends AbstractProcess_1.AbstractProcess {
33
32
  // Constructor
34
33
  // ========================================================================
35
- /**
36
- * Constructs a ConfigLoader instance.
37
- * Searches for `kist.yaml` or `kist.yml` in the working directory
38
- * unless a custom path is provided.
39
- *
40
- * @param configPath - Optional custom configuration file path.
41
- */
42
- constructor(configPath) {
34
+ constructor() {
43
35
  super();
44
36
  // Parameters
45
37
  // ========================================================================
@@ -51,27 +43,22 @@ class ConfigLoader extends AbstractProcess_1.AbstractProcess {
51
43
  * Default filenames to search for configuration files.
52
44
  */
53
45
  this.defaultFilenames = ["kist.yaml", "kist.yml"];
54
- if (configPath) {
55
- this.configPath = path_1.default.resolve(process.cwd(), configPath);
56
- this.logDebug(`Custom configuration path set: ${this.configPath}`);
57
- }
58
- else {
59
- this.logDebug("ConfigLoader initialized without custom path.");
60
- }
46
+ this.logDebug("ConfigLoader initialized.");
61
47
  }
62
48
  // Methods
63
49
  // ========================================================================
64
50
  /**
65
51
  * Initializes the loader by locating the configuration file.
66
- * Searches for `kist.yaml` or `kist.yml` by default.
67
- *
68
- * @param configPath - Optional custom configuration file path.
52
+ * Uses `--config` CLI flag if provided, otherwise defaults.
69
53
  */
70
- initialize(configPath) {
54
+ initialize() {
71
55
  return __awaiter(this, void 0, void 0, function* () {
72
- const searchPaths = configPath ? [configPath] : this.defaultFilenames;
56
+ const parser = new ArgumentParser_1.ArgumentParser();
57
+ const cliFlags = parser.getAllFlags();
58
+ const cliPath = typeof cliFlags.config === "string" ? cliFlags.config : undefined;
59
+ const searchPaths = cliPath ? [cliPath] : this.defaultFilenames;
73
60
  this.logDebug(`Current working directory: ${process.cwd()}`);
74
- this.logDebug("Searching for configuration files...");
61
+ this.logDebug(`Searching for config file${cliPath ? ` from --config=${cliPath}` : ""}...`);
75
62
  for (const fileName of searchPaths) {
76
63
  const resolvedPath = path_1.default.resolve(process.cwd(), fileName);
77
64
  this.logDebug(`Checking: ${resolvedPath}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kist",
3
- "version": "0.1.36",
3
+ "version": "0.1.38",
4
4
  "description": "Package Pipeline Processor",
5
5
  "keywords": [
6
6
  "kist",
@@ -60,15 +60,14 @@
60
60
  "@babel/core": "^7.23.6",
61
61
  "@babel/preset-env": "^7.23.6",
62
62
  "@babel/preset-typescript": "^7.23.3",
63
- "@resvg/resvg-js": "^2.6.2",
64
63
  "@types/fs-extra": "^11.0.4",
65
64
  "@types/glob": "^8.1.0",
66
- "@types/jsdom": "^21.1.6",
67
65
  "@types/micromatch": "^4.0.9",
68
- "@types/node": "^22.13.0",
69
66
  "@types/nunjucks": "^3.2.6",
70
67
  "@types/svg-sprite": "^0.0.39",
71
68
  "autoprefixer": "^10.4.21",
69
+ "canvas": "^3.1.0",
70
+ "canvg": "^4.0.3",
72
71
  "chokidar": "^4.0.0",
73
72
  "cssnano": "^7.0.4",
74
73
  "del": "^8.0.0",
@@ -80,7 +79,7 @@
80
79
  "fs-extra": "^11.2.0",
81
80
  "glob": "^11.0.0",
82
81
  "js-yaml": "^4.1.0",
83
- "jsdom": "^26.0.0",
82
+ "jsdom": "^26.1.0",
84
83
  "lodash": "^4.17.21",
85
84
  "nunjucks": "^3.2.4",
86
85
  "postcss": "^8.4.32",
@@ -2,25 +2,31 @@
2
2
  // Imports
3
3
  // ============================================================================
4
4
 
5
- import { Resvg } from "@resvg/resvg-js";
5
+ import { createCanvas } from "canvas";
6
+ import { Canvg } from "canvg";
6
7
  import fs from "fs";
7
8
  import { JSDOM } from "jsdom";
8
9
  import path from "path";
9
10
  import { Action } from "../../core/pipeline/Action";
10
11
  import { ActionOptionsType } from "../../types/ActionOptionsType";
11
12
 
13
+ // ============================================================================
14
+ // Utilities
15
+ // ============================================================================
16
+
17
+ function getSafeRenderingContext(canvas: ReturnType<typeof createCanvas>) {
18
+ return canvas.getContext("2d");
19
+ }
20
+
12
21
  // ============================================================================
13
22
  // Classes
14
23
  // ============================================================================
15
24
 
16
25
  /**
17
26
  * SvgToPngAction converts SVG content to PNG format.
18
- * Uses `resvg-js` for conversion and `jsdom` for SVG element manipulation.
27
+ * Uses `canvg` for conversion and `jsdom` for SVG element manipulation.
19
28
  */
20
29
  export class SvgToPngAction extends Action {
21
- // Methods
22
- // ========================================================================
23
-
24
30
  /**
25
31
  * Executes the SVG-to-PNG conversion process.
26
32
  * @param options - Options including SVG content, output path, width,
@@ -60,13 +66,11 @@ export class SvgToPngAction extends Action {
60
66
  height?: number,
61
67
  ): Promise<void> {
62
68
  try {
63
- // Ensure the output directory exists
64
69
  const outputDir = path.dirname(outputPath);
65
70
  if (!fs.existsSync(outputDir)) {
66
71
  fs.mkdirSync(outputDir, { recursive: true });
67
72
  }
68
73
 
69
- // Parse SVG and apply width/height
70
74
  const dom = new JSDOM(svgContent);
71
75
  const svgElement = dom.window.document.querySelector("svg");
72
76
 
@@ -74,21 +78,25 @@ export class SvgToPngAction extends Action {
74
78
  throw new Error("Invalid SVG content");
75
79
  }
76
80
 
77
- if (width) svgElement.setAttribute("width", width.toString());
78
- if (height) svgElement.setAttribute("height", height.toString());
81
+ const w =
82
+ width ||
83
+ parseInt(svgElement.getAttribute("width") || "800", 10);
84
+ const h =
85
+ height ||
86
+ parseInt(svgElement.getAttribute("height") || "600", 10);
87
+
88
+ svgElement.setAttribute("width", w.toString());
89
+ svgElement.setAttribute("height", h.toString());
79
90
 
80
91
  const updatedSvgContent = svgElement.outerHTML;
81
92
 
82
- // Render using Resvg
83
- const resvg = new Resvg(updatedSvgContent, {
84
- fitTo:
85
- width && height
86
- ? { mode: "width", value: width }
87
- : undefined,
88
- });
93
+ const canvas = createCanvas(w, h);
94
+ const ctx = getSafeRenderingContext(canvas) as unknown as any;
89
95
 
90
- const pngBuffer = resvg.render().asPng();
96
+ const canvg = await Canvg.from(ctx, updatedSvgContent);
97
+ await canvg.render();
91
98
 
99
+ const pngBuffer = canvas.toBuffer("image/png");
92
100
  fs.writeFileSync(outputPath, pngBuffer);
93
101
  } catch (error) {
94
102
  throw new Error(
@@ -102,7 +110,7 @@ export class SvgToPngAction extends Action {
102
110
  * @returns A string description of the action.
103
111
  */
104
112
  describe(): string {
105
- return "Converts SVG content to PNG format with optional resizing using resvg-js.";
113
+ return "Converts SVG content to PNG format with optional resizing using canvg and canvas.";
106
114
  }
107
115
  }
108
116
 
@@ -5,6 +5,7 @@
5
5
  import fs from "fs";
6
6
  import yaml from "js-yaml";
7
7
  import path from "path";
8
+ import { ArgumentParser } from "../../cli/ArgumentParser";
8
9
  import { ConfigInterface } from "../../interface/ConfigInterface";
9
10
  import { AbstractProcess } from "../abstract/AbstractProcess";
10
11
 
@@ -13,10 +14,8 @@ import { AbstractProcess } from "../abstract/AbstractProcess";
13
14
  // ============================================================================
14
15
 
15
16
  /**
16
- * ConfigLoader is responsible for loading and parsing configuration files
17
- * (`kist.yaml` or `kist.yml` by default). It validates the configuration
18
- * structure and provides it in a usable format for the pipeline.
19
- * Extends `AbstractProcess` for consistent logging.
17
+ * ConfigLoader is responsible for loading and parsing configuration files.
18
+ * Supports a custom path via `--config`, and falls back to `kist.yaml` or `kist.yml`.
20
19
  */
21
20
  export class ConfigLoader extends AbstractProcess {
22
21
  // Parameters
@@ -35,21 +34,9 @@ export class ConfigLoader extends AbstractProcess {
35
34
  // Constructor
36
35
  // ========================================================================
37
36
 
38
- /**
39
- * Constructs a ConfigLoader instance.
40
- * Searches for `kist.yaml` or `kist.yml` in the working directory
41
- * unless a custom path is provided.
42
- *
43
- * @param configPath - Optional custom configuration file path.
44
- */
45
- constructor(configPath?: string) {
37
+ constructor() {
46
38
  super();
47
- if (configPath) {
48
- this.configPath = path.resolve(process.cwd(), configPath);
49
- this.logDebug(`Custom configuration path set: ${this.configPath}`);
50
- } else {
51
- this.logDebug("ConfigLoader initialized without custom path.");
52
- }
39
+ this.logDebug("ConfigLoader initialized.");
53
40
  }
54
41
 
55
42
  // Methods
@@ -57,15 +44,20 @@ export class ConfigLoader extends AbstractProcess {
57
44
 
58
45
  /**
59
46
  * Initializes the loader by locating the configuration file.
60
- * Searches for `kist.yaml` or `kist.yml` by default.
61
- *
62
- * @param configPath - Optional custom configuration file path.
47
+ * Uses `--config` CLI flag if provided, otherwise defaults.
63
48
  */
64
- public async initialize(configPath?: string): Promise<void> {
65
- const searchPaths = configPath ? [configPath] : this.defaultFilenames;
49
+ public async initialize(): Promise<void> {
50
+ const parser = new ArgumentParser();
51
+ const cliFlags = parser.getAllFlags();
52
+ const cliPath =
53
+ typeof cliFlags.config === "string" ? cliFlags.config : undefined;
54
+
55
+ const searchPaths = cliPath ? [cliPath] : this.defaultFilenames;
66
56
 
67
57
  this.logDebug(`Current working directory: ${process.cwd()}`);
68
- this.logDebug("Searching for configuration files...");
58
+ this.logDebug(
59
+ `Searching for config file${cliPath ? ` from --config=${cliPath}` : ""}...`,
60
+ );
69
61
 
70
62
  for (const fileName of searchPaths) {
71
63
  const resolvedPath = path.resolve(process.cwd(), fileName);