@atls-ui-generators/icons 1.1.6 → 1.2.1

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.
Files changed (69) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/commands/base-icons.command.d.ts +10 -0
  3. package/dist/commands/base-icons.command.js +32 -0
  4. package/dist/commands/generate-icons.command.d.ts +6 -0
  5. package/dist/commands/generate-icons.command.js +43 -0
  6. package/dist/commands/generate-replacements.command.d.ts +7 -0
  7. package/dist/commands/generate-replacements.command.js +31 -0
  8. package/dist/commands/index.d.ts +2 -0
  9. package/dist/commands/index.js +2 -0
  10. package/dist/icons.generator.js +4 -37
  11. package/dist/icons.interfaces.d.ts +5 -0
  12. package/dist/replacement-utils/build-replacements/build-replacements.util.d.ts +3 -0
  13. package/dist/replacement-utils/build-replacements/build-replacements.util.js +6 -0
  14. package/dist/replacement-utils/build-replacements/index.d.ts +1 -0
  15. package/dist/replacement-utils/build-replacements/index.js +1 -0
  16. package/dist/replacement-utils/create-replacement-icon-colors/create-replacement-icon-colors.constants.d.ts +2 -0
  17. package/dist/replacement-utils/create-replacement-icon-colors/create-replacement-icon-colors.constants.js +27 -0
  18. package/dist/replacement-utils/create-replacement-icon-colors/create-replacement-icon-colors.util.d.ts +3 -0
  19. package/dist/replacement-utils/create-replacement-icon-colors/create-replacement-icon-colors.util.js +15 -0
  20. package/dist/replacement-utils/create-replacement-icon-colors/index.d.ts +1 -0
  21. package/dist/replacement-utils/create-replacement-icon-colors/index.js +1 -0
  22. package/dist/replacement-utils/create-replacements/create-replacements.util.cjs +15 -8
  23. package/dist/replacement-utils/create-replacements/create-replacements.util.d.cts +1 -1
  24. package/dist/replacement-utils/create-replacements/create-replacements.util.d.ts +3 -2
  25. package/dist/replacement-utils/create-replacements/create-replacements.util.js +15 -8
  26. package/dist/replacement-utils/index.d.ts +2 -0
  27. package/dist/replacement-utils/index.js +2 -0
  28. package/dist/replacement-utils/write-replacements-file/index.d.ts +1 -0
  29. package/dist/replacement-utils/write-replacements-file/index.js +1 -0
  30. package/dist/replacement-utils/write-replacements-file/write-replacements-file.util.d.ts +2 -0
  31. package/dist/replacement-utils/write-replacements-file/write-replacements-file.util.js +8 -0
  32. package/dist/svgr-utils/{svgr-build.util.d.ts → build-svgr.util.d.ts} +1 -1
  33. package/dist/svgr-utils/build-svgr.util.js +8 -0
  34. package/dist/svgr-utils/compile-icons/compile-icon-jsx.util.d.ts +2 -0
  35. package/dist/svgr-utils/compile-icons/compile-icon-jsx.util.js +7 -0
  36. package/dist/svgr-utils/compile-icons/compile-icons.constants.d.ts +7 -0
  37. package/dist/svgr-utils/compile-icons/compile-icons.constants.js +16 -0
  38. package/dist/svgr-utils/compile-icons/compile-icons.template.d.ts +2 -0
  39. package/dist/svgr-utils/compile-icons/compile-icons.template.js +20 -0
  40. package/dist/svgr-utils/compile-icons/compile-icons.util.d.ts +4 -0
  41. package/dist/svgr-utils/compile-icons/compile-icons.util.js +24 -0
  42. package/dist/svgr-utils/compile-icons/index.d.ts +3 -0
  43. package/dist/svgr-utils/compile-icons/index.js +3 -0
  44. package/dist/svgr-utils/discover-icons.util.d.ts +2 -0
  45. package/dist/svgr-utils/discover-icons.util.js +13 -0
  46. package/dist/svgr-utils/get-svg-colors/extract-color-from-element.helper.d.ts +4 -0
  47. package/dist/svgr-utils/get-svg-colors/extract-color-from-element.helper.js +16 -0
  48. package/dist/svgr-utils/get-svg-colors/get-svg-colors.constants.d.ts +1 -0
  49. package/dist/svgr-utils/get-svg-colors/get-svg-colors.constants.js +1 -0
  50. package/dist/svgr-utils/get-svg-colors/get-svg-colors.interfaces.d.ts +4 -0
  51. package/dist/svgr-utils/get-svg-colors/get-svg-colors.util.d.ts +2 -0
  52. package/dist/svgr-utils/get-svg-colors/get-svg-colors.util.js +13 -0
  53. package/dist/svgr-utils/get-svg-colors/index.d.ts +2 -0
  54. package/dist/svgr-utils/get-svg-colors/index.js +1 -0
  55. package/dist/svgr-utils/get-svg-colors/validate-color.helper.d.ts +2 -0
  56. package/dist/svgr-utils/get-svg-colors/validate-color.helper.js +9 -0
  57. package/dist/svgr-utils/index.d.ts +4 -1
  58. package/dist/svgr-utils/index.js +4 -1
  59. package/dist/svgr-utils/write-compiled-icons.util.d.ts +2 -0
  60. package/dist/svgr-utils/{create-files.util.js → write-compiled-icons.util.js} +1 -1
  61. package/package.json +7 -4
  62. package/dist/replacement-utils/create-replacements/create-replacements.interfaces.d.ts +0 -2
  63. package/dist/svgr-utils/compile-icons.util.d.ts +0 -4
  64. package/dist/svgr-utils/compile-icons.util.js +0 -36
  65. package/dist/svgr-utils/create-files.util.d.ts +0 -2
  66. package/dist/svgr-utils/read-files.util.d.ts +0 -2
  67. package/dist/svgr-utils/read-files.util.js +0 -7
  68. package/dist/svgr-utils/svgr-build.util.js +0 -18
  69. /package/dist/{replacement-utils/create-replacements/create-replacements.interfaces.js → svgr-utils/get-svg-colors/get-svg-colors.interfaces.js} +0 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,43 @@
1
1
 
2
2
 
3
+ ## [1.2.1](https://github.com/atls/hyperion/compare/@atls-ui-generators/icons@1.1.8...@atls-ui-generators/icons@1.2.1) (2025-09-23)
4
+
5
+
6
+ ### Features
7
+
8
+
9
+ * **generator:** add check what output directory exists ([01c40fb](https://github.com/atls/hyperion/commit/01c40fb7d3e8c601569b8c6b9be23b30bcfc2027))
10
+ * **generator:** add clipanion generate icons command ([e7fa72b](https://github.com/atls/hyperion/commit/e7fa72beb296402c7b14340b62543994e24b8d3e))
11
+ * **generator:** add generate replacements command to icons generator ([70529f3](https://github.com/atls/hyperion/commit/70529f3a4fc1c179349ea76bcd4ff145da11e353))
12
+
13
+
14
+
15
+
16
+
17
+ ## [1.1.8](https://github.com/atls/hyperion/compare/@atls-ui-generators/icons@1.1.6...@atls-ui-generators/icons@1.1.8) (2025-06-30)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+
23
+ * **icons:** generator template ([78c04b6](https://github.com/atls/hyperion/commit/78c04b6951a51f0bbcded17dd2da85082da9a49f))
24
+
25
+
26
+
27
+
28
+
29
+ ## [1.1.6](https://github.com/atls/hyperion/compare/@atls-ui-generators/icons@1.1.6...@atls-ui-generators/icons@1.1.6) (2025-06-30)
30
+
31
+
32
+ ### Bug Fixes
33
+
34
+
35
+ * **icons:** generator template ([78c04b6](https://github.com/atls/hyperion/commit/78c04b6951a51f0bbcded17dd2da85082da9a49f))
36
+
37
+
38
+
39
+
40
+
3
41
  ## [1.1.6](https://github.com/atls/hyperion/compare/@atls-ui-generators/icons@1.1.5...@atls-ui-generators/icons@1.1.6) (2025-06-24)
4
42
 
5
43
 
@@ -0,0 +1,10 @@
1
+ import type { ResolvedIconsPath } from '../icons.interfaces.js';
2
+ import { Command } from 'clipanion';
3
+ export declare class BaseIconsCommand extends Command {
4
+ static readonly usage: import("clipanion").Usage;
5
+ readonly logger: import("pino").Logger;
6
+ readonly outputPath: string;
7
+ readonly iconsPath: string;
8
+ resolvePaths(): ResolvedIconsPath;
9
+ execute(): Promise<void>;
10
+ }
@@ -0,0 +1,32 @@
1
+ import assert from 'node:assert/strict';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import { Command } from 'clipanion';
5
+ import { Option } from 'clipanion';
6
+ import { createLogger } from '@atls-ui-generators/utils';
7
+ export class BaseIconsCommand extends Command {
8
+ static usage = Command.Usage({ category: 'Icons' });
9
+ logger = createLogger('icons-generator');
10
+ outputPath = Option.String({ name: 'output', required: true });
11
+ iconsPath = Option.String('-i,--icons', {
12
+ required: true,
13
+ description: 'Path to icons svg directory (required)',
14
+ });
15
+ resolvePaths() {
16
+ const outputPath = path.resolve(this.outputPath);
17
+ const iconsPath = path.resolve(this.iconsPath);
18
+ this.logger.info(`Resolved paths: output=${outputPath}, icons=${iconsPath}`);
19
+ assert.ok(fs.existsSync(outputPath) && fs.lstatSync(outputPath).isDirectory(), 'Path to save should point to a directory.');
20
+ assert.ok(fs.existsSync(iconsPath) && fs.lstatSync(iconsPath).isDirectory(), 'Icons path should point to a directory');
21
+ return { outputPath, iconsPath };
22
+ }
23
+ async execute() {
24
+ try {
25
+ this.logger.info('Base icons command');
26
+ this.resolvePaths();
27
+ }
28
+ catch (error) {
29
+ this.logger.error(error);
30
+ }
31
+ }
32
+ }
@@ -0,0 +1,6 @@
1
+ import { BaseIconsCommand } from './base-icons.command.js';
2
+ export declare class GenerateIconsCommand extends BaseIconsCommand {
3
+ static readonly usage: import("clipanion").Usage;
4
+ private readonly replacementsPath;
5
+ execute(): Promise<void>;
6
+ }
@@ -0,0 +1,43 @@
1
+ import assert from 'node:assert/strict';
2
+ import { lstatSync } from 'node:fs';
3
+ import path from 'node:path';
4
+ import { Option } from 'clipanion';
5
+ import { processFile } from '@atls-ui-generators/utils';
6
+ import { BaseIconsCommand } from './base-icons.command.js';
7
+ import { buildSvgr } from '../svgr-utils/index.js';
8
+ export class GenerateIconsCommand extends BaseIconsCommand {
9
+ static usage = BaseIconsCommand.Usage({
10
+ description: 'Generate icon components from SVG files',
11
+ details: `
12
+ Reads all SVG files from the provided directory and generates icon components into the specified output directory.
13
+ Optionally applies color replacements.
14
+ `,
15
+ examples: [
16
+ [`Basic example`, `generate-icons ./src/icons -i ./svg`],
17
+ [`With replacements`, `generate-icons ./src/icons -i ./svg -r ./replacements.ts`],
18
+ ],
19
+ });
20
+ replacementsPath = Option.String('-r,--replacements', '', {
21
+ description: 'Path to replacements file (optional)',
22
+ });
23
+ async execute() {
24
+ try {
25
+ this.logger.info('Starting icons generation...');
26
+ const { iconsPath, outputPath } = this.resolvePaths();
27
+ let replacements = {};
28
+ if (this.replacementsPath) {
29
+ const replacementsPath = path.resolve(this.replacementsPath);
30
+ assert.ok(lstatSync(replacementsPath).isFile(), 'Replacements path should point to a file');
31
+ this.logger.info(`Extract replacements from ${replacementsPath}`);
32
+ const exports = processFile(replacementsPath);
33
+ replacements = Object.values(exports)?.[0];
34
+ }
35
+ this.logger.info(`Icon generation started into ${outputPath}`);
36
+ await buildSvgr(iconsPath, outputPath, replacements);
37
+ this.logger.info(`Generated into ${outputPath}`);
38
+ }
39
+ catch (error) {
40
+ this.logger.error(error);
41
+ }
42
+ }
43
+ }
@@ -0,0 +1,7 @@
1
+ import { BaseIconsCommand } from './base-icons.command.js';
2
+ export declare class GenerateReplacementsCommand extends BaseIconsCommand {
3
+ static readonly paths: string[][];
4
+ static readonly usage: import("clipanion").Usage;
5
+ private readonly fileName;
6
+ execute(): Promise<void>;
7
+ }
@@ -0,0 +1,31 @@
1
+ import path from 'node:path';
2
+ import { Option } from 'clipanion';
3
+ import { BaseIconsCommand } from './base-icons.command.js';
4
+ import { buildReplacements } from '../replacement-utils/index.js';
5
+ import { writeReplacementsFile } from '../replacement-utils/index.js';
6
+ import { discoverIcons } from '../svgr-utils/index.js';
7
+ export class GenerateReplacementsCommand extends BaseIconsCommand {
8
+ static paths = [['replacements']];
9
+ static usage = BaseIconsCommand.Usage({
10
+ description: 'Generate replacements for icons generator',
11
+ details: 'Reads all SVG files from the provided directory and generates replacements into the specified output file.',
12
+ examples: [[`Basic example`, `generate-icons replacements ./src -i ./svg -f replacements.ts`]],
13
+ });
14
+ fileName = Option.String('-f,--file-name', 'replacements.ts', {
15
+ description: 'Output file name for generated replacements (default: replacements.ts)',
16
+ });
17
+ async execute() {
18
+ try {
19
+ this.logger.info('Starting replacements generation...');
20
+ const { iconsPath, outputPath } = this.resolvePaths();
21
+ const outputFilePath = path.resolve(outputPath, this.fileName);
22
+ const icons = await discoverIcons(iconsPath);
23
+ const replacements = await buildReplacements(icons);
24
+ writeReplacementsFile(outputFilePath, replacements);
25
+ this.logger.info(`Generated into ${outputFilePath}`);
26
+ }
27
+ catch (error) {
28
+ this.logger.error(error);
29
+ }
30
+ }
31
+ }
@@ -0,0 +1,2 @@
1
+ export * from './generate-icons.command.js';
2
+ export * from './generate-replacements.command.js';
@@ -0,0 +1,2 @@
1
+ export * from "./generate-icons.command.js";
2
+ export * from "./generate-replacements.command.js";
@@ -1,37 +1,4 @@
1
- import assert from 'node:assert/strict';
2
- import { lstatSync } from 'node:fs';
3
- import { join } from 'node:path';
4
- import { program } from 'commander';
5
- import { createLogger } from '@atls-ui-generators/utils';
6
- import { processFile } from '@atls-ui-generators/utils';
7
- import { svgrBuild } from './svgr-utils/index.js';
8
- const logger = createLogger('icons-generator');
9
- try {
10
- program
11
- .name('Icons generator')
12
- .argument('path <string>', 'Path to save the icons')
13
- .requiredOption('-i, --icons path <string>', 'Path to icons svg dir')
14
- .option('-r, --replacements path <string>', 'Path to replacements file')
15
- .parse(process.argv);
16
- const path = program.args.at(0);
17
- const { icons: iconsPath, replacements: replacementsPath } = program.opts();
18
- assert.ok(path && typeof path === 'string', 'Path to save the icons is required');
19
- assert.ok(lstatSync(path).isDirectory(), 'Path to save icons should point to a directory.');
20
- assert.ok(iconsPath && typeof iconsPath === 'string', 'Icons svg path is required');
21
- assert.ok(lstatSync(iconsPath).isDirectory(), 'Icons path should point to a directory');
22
- let replacements = {};
23
- if (replacementsPath) {
24
- assert.ok(typeof replacementsPath === 'string', 'Replacements path should be string');
25
- assert.ok(lstatSync(replacementsPath).isFile(), 'Replacements path should point to a file');
26
- logger.info(`Extract replacements from ${replacementsPath}`);
27
- const absoluteReplacementsPath = join(process.cwd(), replacementsPath);
28
- const exports = processFile(absoluteReplacementsPath);
29
- replacements = Object.values(exports)?.[0];
30
- }
31
- logger.info(`Icon generation started into ${path}`);
32
- await svgrBuild(iconsPath, path, replacements);
33
- logger.info(`Generated into ${path}`);
34
- }
35
- catch (error) {
36
- logger.error(error);
37
- }
1
+ import { runExit } from 'clipanion';
2
+ import { GenerateIconsCommand } from './commands/index.js';
3
+ import { GenerateReplacementsCommand } from './commands/index.js';
4
+ runExit([GenerateIconsCommand, GenerateReplacementsCommand]);
@@ -9,3 +9,8 @@ export interface Source {
9
9
  withReplacement: boolean;
10
10
  }
11
11
  export type Replacements = Record<string, Record<string, string>>;
12
+ export type ReplacementIconColors = Record<string, Array<string> | string>;
13
+ export interface ResolvedIconsPath {
14
+ outputPath: string;
15
+ iconsPath: string;
16
+ }
@@ -0,0 +1,3 @@
1
+ import type { Icon } from '../../icons.interfaces.js';
2
+ import type { Replacements } from '../../icons.interfaces.js';
3
+ export declare const buildReplacements: (icons: Array<Icon>) => Promise<Replacements>;
@@ -0,0 +1,6 @@
1
+ import { createReplacementIconColors } from '../create-replacement-icon-colors/index.js';
2
+ import { createReplacements } from '../create-replacements/index.js';
3
+ export const buildReplacements = async (icons) => {
4
+ const replacementIconColors = await createReplacementIconColors(icons);
5
+ return createReplacements(replacementIconColors);
6
+ };
@@ -0,0 +1 @@
1
+ export * from './build-replacements.util.js';
@@ -0,0 +1 @@
1
+ export * from "./build-replacements.util.js";
@@ -0,0 +1,2 @@
1
+ import type { GetSvgColorsOptions } from '../../svgr-utils/index.js';
2
+ export declare const getSvgColorsOptions: GetSvgColorsOptions;
@@ -0,0 +1,27 @@
1
+ const colorAttributes = new Set([
2
+ 'fill',
3
+ 'stroke',
4
+ 'color',
5
+ 'stop-color',
6
+ 'flood-color',
7
+ 'lighting-color',
8
+ 'solid-color',
9
+ 'background-color',
10
+ 'text-decoration-color',
11
+ 'caret-color',
12
+ 'outline-color',
13
+ 'border-color',
14
+ ]);
15
+ const ignoredColorAttributeValues = new Set([
16
+ 'none',
17
+ 'currentcolor',
18
+ 'currentColor',
19
+ 'inherit',
20
+ 'transparent',
21
+ 'initial',
22
+ 'unset',
23
+ ]);
24
+ export const getSvgColorsOptions = {
25
+ colorAttributes,
26
+ ignoredColorAttributeValues,
27
+ };
@@ -0,0 +1,3 @@
1
+ import type { Icon } from '../../icons.interfaces.js';
2
+ import type { ReplacementIconColors } from '../../icons.interfaces.js';
3
+ export declare const createReplacementIconColors: (icons: Array<Icon>) => Promise<ReplacementIconColors>;
@@ -0,0 +1,15 @@
1
+ import { compileIconJsx } from '../../svgr-utils/index.js';
2
+ import { getSvgColors } from '../../svgr-utils/index.js';
3
+ import { getSvgColorsOptions } from './create-replacement-icon-colors.constants.js';
4
+ export const createReplacementIconColors = async (icons) => {
5
+ const promises = icons.map(async (icon) => {
6
+ const source = await compileIconJsx(icon);
7
+ const colors = getSvgColors(source, getSvgColorsOptions);
8
+ if (colors.length === 1) {
9
+ return [icon.name, colors[0]];
10
+ }
11
+ return [icon.name, colors];
12
+ });
13
+ const entries = (await Promise.all(promises)).filter(([key, value]) => Boolean(value.length));
14
+ return Object.fromEntries(entries);
15
+ };
@@ -0,0 +1 @@
1
+ export * from './create-replacement-icon-colors.util.js';
@@ -0,0 +1 @@
1
+ export * from "./create-replacement-icon-colors.util.js";
@@ -2,13 +2,20 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createReplacements = void 0;
4
4
  const { getColorReplacement } = require('../get-color-replacement/index.cjs');
5
- const createReplacements = (replacementIcons) => Object.entries(replacementIcons).reduce((replacement, [icon, value]) => {
6
- if (Array.isArray(value)) {
7
- return {
8
- ...replacement,
9
- [icon]: value.reduce((colors, color) => ({ ...colors, ...getColorReplacement({ color }) }), {}),
10
- };
5
+ const createReplacements = (replacementIconColors) => {
6
+ const replacements = {};
7
+ for (const [icon, value] of Object.entries(replacementIconColors)) {
8
+ if (Array.isArray(value)) {
9
+ const colors = {};
10
+ for (const color of value) {
11
+ Object.assign(colors, getColorReplacement({ color }));
12
+ }
13
+ replacements[icon] = colors;
14
+ }
15
+ else {
16
+ replacements[icon] = getColorReplacement({ color: value });
17
+ }
11
18
  }
12
- return { ...replacement, [icon]: getColorReplacement({ color: value }) };
13
- }, {});
19
+ return replacements;
20
+ };
14
21
  exports.createReplacements = createReplacements;
@@ -1 +1 @@
1
- export declare const createReplacements: (replacementIcons: Record<string, Array<string> | string>) => Record<string, Record<string, string>>;
1
+ export declare const createReplacements: (replacementIconColors: Record<string, Array<string> | string>) => Record<string, Record<string, string>>;
@@ -1,2 +1,3 @@
1
- import type { CreateReplacements } from './create-replacements.interfaces.js';
2
- export declare const createReplacements: CreateReplacements;
1
+ import type { Replacements } from '../../icons.interfaces.js';
2
+ import type { ReplacementIconColors } from '../../icons.interfaces.js';
3
+ export declare const createReplacements: (replacementIconColors: ReplacementIconColors) => Replacements;
@@ -1,10 +1,17 @@
1
1
  import { getColorReplacement } from '../get-color-replacement/index.js';
2
- export const createReplacements = (replacementIcons) => Object.entries(replacementIcons).reduce((replacement, [icon, value]) => {
3
- if (Array.isArray(value)) {
4
- return {
5
- ...replacement,
6
- [icon]: value.reduce((colors, color) => ({ ...colors, ...getColorReplacement({ color }) }), {}),
7
- };
2
+ export const createReplacements = (replacementIconColors) => {
3
+ const replacements = {};
4
+ for (const [icon, value] of Object.entries(replacementIconColors)) {
5
+ if (Array.isArray(value)) {
6
+ const colors = {};
7
+ for (const color of value) {
8
+ Object.assign(colors, getColorReplacement({ color }));
9
+ }
10
+ replacements[icon] = colors;
11
+ }
12
+ else {
13
+ replacements[icon] = getColorReplacement({ color: value });
14
+ }
8
15
  }
9
- return { ...replacement, [icon]: getColorReplacement({ color: value }) };
10
- }, {});
16
+ return replacements;
17
+ };
@@ -1,2 +1,4 @@
1
+ export * from './build-replacements/index.js';
1
2
  export * from './create-replacements/index.js';
2
3
  export * from './get-color-replacement/index.js';
4
+ export * from './write-replacements-file/index.js';
@@ -1,2 +1,4 @@
1
+ export * from "./build-replacements/index.js";
1
2
  export * from "./create-replacements/index.js";
2
3
  export * from "./get-color-replacement/index.js";
4
+ export * from "./write-replacements-file/index.js";
@@ -0,0 +1 @@
1
+ export * from './write-replacements-file.util.js';
@@ -0,0 +1 @@
1
+ export * from "./write-replacements-file.util.js";
@@ -0,0 +1,2 @@
1
+ import type { Replacements } from '../../icons.interfaces.js';
2
+ export declare const writeReplacementsFile: (targetFile: string, replacements: Replacements) => void;
@@ -0,0 +1,8 @@
1
+ import { writeFileSync } from 'node:fs';
2
+ export const writeReplacementsFile = (targetFile, replacements) => {
3
+ const sorted = Object.keys(replacements)
4
+ .sort((keyA, keyB) => keyA.localeCompare(keyB))
5
+ .reduce((acc, key) => Object.assign(acc, { [key]: replacements[key] }), {});
6
+ const content = `export const replacements = ${JSON.stringify(sorted, null, 2)}\n`;
7
+ writeFileSync(targetFile, content);
8
+ };
@@ -1,2 +1,2 @@
1
1
  import type { Replacements } from '../icons.interfaces.js';
2
- export declare const svgrBuild: (iconsPath: string, targetDir: string, replacements: Replacements) => Promise<void>;
2
+ export declare const buildSvgr: (iconsPath: string, targetDir: string, replacements: Replacements) => Promise<void>;
@@ -0,0 +1,8 @@
1
+ import { compileIcons } from './compile-icons/index.js';
2
+ import { discoverIcons } from './discover-icons.util.js';
3
+ import { writeCompiledIcons } from './write-compiled-icons.util.js';
4
+ export const buildSvgr = async (iconsPath, targetDir, replacements) => {
5
+ const icons = await discoverIcons(iconsPath);
6
+ const sources = await compileIcons(icons, replacements);
7
+ await writeCompiledIcons(sources, targetDir);
8
+ };
@@ -0,0 +1,2 @@
1
+ import type { Icon } from '../../icons.interfaces.js';
2
+ export declare const compileIconJsx: (icon: Icon) => Promise<string>;
@@ -0,0 +1,7 @@
1
+ import { transform } from '@svgr/core';
2
+ import { MASK_ID_PATTERN } from './compile-icons.constants.js';
3
+ import { svgrTransformConfig } from './compile-icons.constants.js';
4
+ export const compileIconJsx = async (icon) => transform(icon.source.replace(MASK_ID_PATTERN, icon.name), {
5
+ ...svgrTransformConfig,
6
+ template: ({ jsx }, { tpl }) => tpl `${jsx}`,
7
+ });
@@ -0,0 +1,7 @@
1
+ import type { Config } from '@svgr/core';
2
+ export declare const MASK_ID_PATTERN: RegExp;
3
+ export declare const PROPS_SPREAD_FROM = "...props";
4
+ export declare const PROPS_SPREAD_TO = "...otherProps";
5
+ export declare const FIFTY_PLUS_PATTERN = "50+";
6
+ export declare const FIFTY_PLUS_REPLACEMENT = "FiftyPlus";
7
+ export declare const svgrTransformConfig: Config;
@@ -0,0 +1,16 @@
1
+ import { svgrTemplate } from './compile-icons.template.js';
2
+ export const MASK_ID_PATTERN = /mask0/g;
3
+ export const PROPS_SPREAD_FROM = '...props';
4
+ export const PROPS_SPREAD_TO = '...otherProps';
5
+ export const FIFTY_PLUS_PATTERN = '50+';
6
+ export const FIFTY_PLUS_REPLACEMENT = 'FiftyPlus';
7
+ export const svgrTransformConfig = {
8
+ icon: true,
9
+ typescript: true,
10
+ template: svgrTemplate,
11
+ plugins: ['@svgr/plugin-svgo', '@svgr/plugin-jsx', '@svgr/plugin-prettier'],
12
+ svgProps: {
13
+ className: `{clsx(className, String(otherProps.className || ''))}`,
14
+ style: '{iconStyle}',
15
+ },
16
+ };
@@ -0,0 +1,2 @@
1
+ import type { Config } from '@svgr/core';
2
+ export declare const svgrTemplate: Config['template'];
@@ -0,0 +1,20 @@
1
+ export const svgrTemplate = ({ jsx, componentName }, { tpl }) => tpl `
2
+ import type { IconProps } from '../icons.interfaces.js'
3
+
4
+ import type { ReactNode } from 'react'
5
+
6
+ import React from 'react'
7
+ import { clsx } from 'clsx'
8
+
9
+ import { iconSprinkles } from '../icon.css.js'
10
+
11
+ export const ${componentName} = (props: IconProps): ReactNode => {
12
+ const { className, style, otherProps } = iconSprinkles(props)
13
+
14
+ const iconStyle = { ...style, ...otherProps.style }
15
+
16
+ return (
17
+ ${jsx}
18
+ )
19
+ }
20
+ `;
@@ -0,0 +1,4 @@
1
+ import type { Icon } from '../../icons.interfaces.js';
2
+ import type { Replacements } from '../../icons.interfaces.js';
3
+ import type { Source } from '../../icons.interfaces.js';
4
+ export declare const compileIcons: (icons: Array<Icon>, replacements: Replacements) => Promise<Array<Source>>;
@@ -0,0 +1,24 @@
1
+ import { transform } from '@svgr/core';
2
+ import { MASK_ID_PATTERN } from './compile-icons.constants.js';
3
+ import { PROPS_SPREAD_FROM } from './compile-icons.constants.js';
4
+ import { PROPS_SPREAD_TO } from './compile-icons.constants.js';
5
+ import { FIFTY_PLUS_PATTERN } from './compile-icons.constants.js';
6
+ import { FIFTY_PLUS_REPLACEMENT } from './compile-icons.constants.js';
7
+ import { svgrTransformConfig } from './compile-icons.constants.js';
8
+ export const compileIcons = async (icons, replacements) => {
9
+ const promises = icons.map(async (icon) => {
10
+ const transformed = await transform(icon.source, {
11
+ ...svgrTransformConfig,
12
+ replaceAttrValues: { ...replacements[icon.name] },
13
+ }, { componentName: icon.name.replace(FIFTY_PLUS_PATTERN, FIFTY_PLUS_REPLACEMENT) });
14
+ const code = transformed
15
+ .replace(MASK_ID_PATTERN, icon.name)
16
+ .replace(PROPS_SPREAD_FROM, PROPS_SPREAD_TO);
17
+ return {
18
+ name: icon.fileName,
19
+ code,
20
+ withReplacement: Boolean(replacements[icon.name]),
21
+ };
22
+ });
23
+ return Promise.all(promises);
24
+ };
@@ -0,0 +1,3 @@
1
+ export * from './compile-icon-jsx.util.js';
2
+ export * from './compile-icons.template.js';
3
+ export * from './compile-icons.util.js';
@@ -0,0 +1,3 @@
1
+ export * from "./compile-icon-jsx.util.js";
2
+ export * from "./compile-icons.template.js";
3
+ export * from "./compile-icons.util.js";
@@ -0,0 +1,2 @@
1
+ import type { Icon } from '../icons.interfaces.js';
2
+ export declare const discoverIcons: (svgDirectory: string) => Promise<Array<Icon>>;
@@ -0,0 +1,13 @@
1
+ import fs from 'node:fs';
2
+ import { glob } from 'glob';
3
+ import isSvg from 'is-svg';
4
+ import { getIconNames } from '@atls-ui-generators/utils';
5
+ const createIcon = (iconPath) => ({
6
+ ...getIconNames(iconPath),
7
+ source: fs.readFileSync(iconPath).toString(),
8
+ });
9
+ export const discoverIcons = async (svgDirectory) => {
10
+ const files = await glob(`${svgDirectory}/**/*.svg`, { nodir: true });
11
+ const icons = files.map(createIcon);
12
+ return icons.filter((icon) => isSvg(icon.source));
13
+ };
@@ -0,0 +1,4 @@
1
+ import type { CheerioAPI } from 'cheerio';
2
+ import type { AnyNode } from 'domhandler';
3
+ import type { GetSvgColorsOptions } from './get-svg-colors.interfaces.js';
4
+ export declare const extractColorFromElement: (cheerio: CheerioAPI, element: AnyNode, options: GetSvgColorsOptions) => Set<string>;
@@ -0,0 +1,16 @@
1
+ import { STYLE_COLOR_ATTRIBUTE_KEY } from './get-svg-colors.constants.js';
2
+ export const extractColorFromElement = (cheerio, element, options) => {
3
+ const colors = new Set();
4
+ const attributes = cheerio(element).attr() ?? {};
5
+ Object.entries(attributes).forEach(([name, value]) => {
6
+ if (options.colorAttributes?.has(name)) {
7
+ colors.add(value);
8
+ }
9
+ if (name === STYLE_COLOR_ATTRIBUTE_KEY) {
10
+ options.colorAttributes?.forEach((colorAttribute) => {
11
+ colors.add(cheerio(element).css(colorAttribute) || '');
12
+ });
13
+ }
14
+ });
15
+ return colors;
16
+ };
@@ -0,0 +1 @@
1
+ export declare const STYLE_COLOR_ATTRIBUTE_KEY = "style";
@@ -0,0 +1 @@
1
+ export const STYLE_COLOR_ATTRIBUTE_KEY = 'style';
@@ -0,0 +1,4 @@
1
+ export interface GetSvgColorsOptions {
2
+ colorAttributes?: Set<string>;
3
+ ignoredColorAttributeValues?: Set<string>;
4
+ }
@@ -0,0 +1,2 @@
1
+ import type { GetSvgColorsOptions } from './get-svg-colors.interfaces.js';
2
+ export declare const getSvgColors: (source: string, options?: GetSvgColorsOptions) => Array<string>;
@@ -0,0 +1,13 @@
1
+ import { load } from 'cheerio';
2
+ import { extractColorFromElement } from './extract-color-from-element.helper.js';
3
+ import { validateColor } from './validate-color.helper.js';
4
+ export const getSvgColors = (source, options = {}) => {
5
+ const colors = new Set();
6
+ const cheerio = load(source, { xml: true });
7
+ cheerio('*').each((index, element) => {
8
+ const elementColors = extractColorFromElement(cheerio, element, options);
9
+ elementColors.forEach((color) => colors.add(color));
10
+ });
11
+ const colorsArray = Array.from(colors);
12
+ return colorsArray.filter((color) => validateColor(color, options));
13
+ };
@@ -0,0 +1,2 @@
1
+ export * from './get-svg-colors.util.js';
2
+ export type * from './get-svg-colors.interfaces.js';
@@ -0,0 +1 @@
1
+ export * from "./get-svg-colors.util.js";
@@ -0,0 +1,2 @@
1
+ import type { GetSvgColorsOptions } from './get-svg-colors.interfaces.js';
2
+ export declare const validateColor: (color: string, options: GetSvgColorsOptions) => boolean;
@@ -0,0 +1,9 @@
1
+ export const validateColor = (color, options) => {
2
+ if (!color) {
3
+ return false;
4
+ }
5
+ if (options.ignoredColorAttributeValues?.has(color)) {
6
+ return false;
7
+ }
8
+ return true;
9
+ };
@@ -1 +1,4 @@
1
- export * from './svgr-build.util.js';
1
+ export * from './build-svgr.util.js';
2
+ export * from './compile-icons/index.js';
3
+ export * from './discover-icons.util.js';
4
+ export * from './get-svg-colors/index.js';
@@ -1 +1,4 @@
1
- export * from "./svgr-build.util.js";
1
+ export * from "./build-svgr.util.js";
2
+ export * from "./compile-icons/index.js";
3
+ export * from "./discover-icons.util.js";
4
+ export * from "./get-svg-colors/index.js";
@@ -0,0 +1,2 @@
1
+ import type { Source } from '../icons.interfaces.js';
2
+ export declare const writeCompiledIcons: (sources: Array<Source>, targetDir: string) => Promise<void>;
@@ -14,7 +14,7 @@ const writeIndexFile = (sources, targetDir) => {
14
14
  .join('\n')}\n`;
15
15
  fs.writeFileSync(filename, data);
16
16
  };
17
- export const createFiles = async (sources, targetDir) => {
17
+ export const writeCompiledIcons = async (sources, targetDir) => {
18
18
  if (!fs.existsSync(targetDir)) {
19
19
  fs.mkdirSync(targetDir, { recursive: true });
20
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atls-ui-generators/icons",
3
- "version": "1.1.6",
3
+ "version": "1.2.1",
4
4
  "license": "BSD-3-Clause",
5
5
  "type": "module",
6
6
  "exports": {
@@ -35,12 +35,15 @@
35
35
  "@svgr/core": "8.1.0",
36
36
  "@swc-node/register": "1.9.0",
37
37
  "@swc/core": "1.6.1",
38
- "commander": "13.1.0",
39
- "glob": "11.0.2"
38
+ "cheerio": "1.1.2",
39
+ "clipanion": "4.0.0-rc.4",
40
+ "glob": "11.0.2",
41
+ "is-svg": "6.1.0"
40
42
  },
41
43
  "devDependencies": {
42
44
  "@types/node": "22.15.3",
43
- "tsx": "4.19.3",
45
+ "domhandler": "5.0.3",
46
+ "tsx": "4.20.5",
44
47
  "typescript": "5.5.4"
45
48
  },
46
49
  "publishConfig": {
@@ -1,2 +0,0 @@
1
- import type { Replacements } from '../../icons.interfaces.js';
2
- export type CreateReplacements = (replacementIcons: Record<string, Array<string> | string>) => Replacements;
@@ -1,4 +0,0 @@
1
- import type { Icon } from '../icons.interfaces.js';
2
- import type { Replacements } from '../icons.interfaces.js';
3
- import type { Source } from '../icons.interfaces.js';
4
- export declare const compileIcons: (icons: Array<Icon>, replacements: Replacements) => Promise<Array<Source>>;
@@ -1,36 +0,0 @@
1
- import { transform } from '@svgr/core';
2
- const svgrTemplate = ({ jsx, componentName }, { tpl }) => tpl `
3
- import type { IconProps } from '../icons.interfaces.js'
4
-
5
- import type { ReactNode } from 'react'
6
-
7
- import React from 'react'
8
- import { clsx } from 'clsx'
9
-
10
- import { iconSprinkles } from '../icon.css.js'
11
-
12
- export const ${componentName} = (props: IconProps): ReactNode => {
13
- const { className, style, otherProps } = iconSprinkles(props)
14
-
15
- const iconStyle = { ...style, ...otherProps.style }
16
-
17
- return (
18
- ${jsx}
19
- )
20
- }
21
- `;
22
- export const compileIcons = async (icons, replacements) => Promise.all(icons.map(async (icon) => ({
23
- name: icon.fileName,
24
- code: await transform(icon.source.replace(/mask0/g, icon.name), {
25
- icon: true,
26
- typescript: true,
27
- template: svgrTemplate,
28
- plugins: ['@svgr/plugin-svgo', '@svgr/plugin-jsx', '@svgr/plugin-prettier'],
29
- svgProps: {
30
- className: `{clsx(className, String(otherProps.className || ''))}`,
31
- style: '{iconStyle}',
32
- },
33
- replaceAttrValues: { ...replacements[icon.name] },
34
- }, { componentName: icon.name.replace('50+', 'FiftyPlus') }),
35
- withReplacement: Boolean(replacements[icon.name]),
36
- })));
@@ -1,2 +0,0 @@
1
- import type { Source } from '../icons.interfaces.js';
2
- export declare const createFiles: (sources: Array<Source>, targetDir: string) => Promise<void>;
@@ -1,2 +0,0 @@
1
- import type { Icon } from '../icons.interfaces.js';
2
- export declare const readFiles: (files: Array<string>) => Array<Icon>;
@@ -1,7 +0,0 @@
1
- import fs from 'node:fs';
2
- import { getIconNames } from '@atls-ui-generators/utils';
3
- const createIcon = (iconPath) => ({
4
- ...getIconNames(iconPath),
5
- source: fs.readFileSync(iconPath).toString(),
6
- });
7
- export const readFiles = (files) => files.map(createIcon);
@@ -1,18 +0,0 @@
1
- import { glob } from 'glob';
2
- import { compileIcons } from './compile-icons.util.js';
3
- import { createFiles } from './create-files.util.js';
4
- import { readFiles } from './read-files.util.js';
5
- export const svgrBuild = async (iconsPath, targetDir, replacements) => {
6
- const prettifyIconsPath = () => {
7
- const parts = iconsPath.split('');
8
- if (parts[parts.length - 1] === '/') {
9
- parts.pop();
10
- return parts.join('');
11
- }
12
- return iconsPath;
13
- };
14
- const files = await glob(`${prettifyIconsPath()}/*.svg`);
15
- const icons = readFiles(files);
16
- const sources = await compileIcons(icons.filter((icon) => icon.source), replacements);
17
- await createFiles(sources, targetDir);
18
- };