@atls-ui-generators/icons 1.1.8 → 1.2.2
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.
- package/CHANGELOG.md +32 -0
- package/dist/commands/base-icons.command.d.ts +10 -0
- package/dist/commands/base-icons.command.js +32 -0
- package/dist/commands/generate-icons.command.d.ts +6 -0
- package/dist/commands/generate-icons.command.js +43 -0
- package/dist/commands/generate-replacements.command.d.ts +7 -0
- package/dist/commands/generate-replacements.command.js +31 -0
- package/dist/commands/index.d.ts +2 -0
- package/dist/commands/index.js +2 -0
- package/dist/icons.generator.js +4 -37
- package/dist/icons.interfaces.d.ts +5 -0
- package/dist/replacement-utils/build-replacements/build-replacements.util.d.ts +3 -0
- package/dist/replacement-utils/build-replacements/build-replacements.util.js +6 -0
- package/dist/replacement-utils/build-replacements/index.d.ts +1 -0
- package/dist/replacement-utils/build-replacements/index.js +1 -0
- package/dist/replacement-utils/create-replacement-icon-colors/create-replacement-icon-colors.constants.d.ts +2 -0
- package/dist/replacement-utils/create-replacement-icon-colors/create-replacement-icon-colors.constants.js +27 -0
- package/dist/replacement-utils/create-replacement-icon-colors/create-replacement-icon-colors.util.d.ts +3 -0
- package/dist/replacement-utils/create-replacement-icon-colors/create-replacement-icon-colors.util.js +15 -0
- package/dist/replacement-utils/create-replacement-icon-colors/index.d.ts +1 -0
- package/dist/replacement-utils/create-replacement-icon-colors/index.js +1 -0
- package/dist/replacement-utils/create-replacements/create-replacements.util.cjs +15 -8
- package/dist/replacement-utils/create-replacements/create-replacements.util.d.cts +1 -1
- package/dist/replacement-utils/create-replacements/create-replacements.util.d.ts +3 -2
- package/dist/replacement-utils/create-replacements/create-replacements.util.js +15 -8
- package/dist/replacement-utils/index.d.ts +2 -0
- package/dist/replacement-utils/index.js +2 -0
- package/dist/replacement-utils/write-replacements-file/index.d.ts +1 -0
- package/dist/replacement-utils/write-replacements-file/index.js +1 -0
- package/dist/replacement-utils/write-replacements-file/write-replacements-file.util.d.ts +2 -0
- package/dist/replacement-utils/write-replacements-file/write-replacements-file.util.js +8 -0
- package/dist/svgr-utils/{svgr-build.util.d.ts → build-svgr.util.d.ts} +1 -1
- package/dist/svgr-utils/build-svgr.util.js +8 -0
- package/dist/svgr-utils/compile-icons/compile-icon-jsx.util.d.ts +2 -0
- package/dist/svgr-utils/compile-icons/compile-icon-jsx.util.js +7 -0
- package/dist/svgr-utils/compile-icons/compile-icons.constants.d.ts +7 -0
- package/dist/svgr-utils/compile-icons/compile-icons.constants.js +16 -0
- package/dist/svgr-utils/compile-icons/compile-icons.template.d.ts +2 -0
- package/dist/svgr-utils/compile-icons/compile-icons.template.js +19 -0
- package/dist/svgr-utils/compile-icons/compile-icons.util.d.ts +4 -0
- package/dist/svgr-utils/compile-icons/compile-icons.util.js +24 -0
- package/dist/svgr-utils/compile-icons/index.d.ts +3 -0
- package/dist/svgr-utils/compile-icons/index.js +3 -0
- package/dist/svgr-utils/discover-icons.util.d.ts +2 -0
- package/dist/svgr-utils/discover-icons.util.js +13 -0
- package/dist/svgr-utils/get-svg-colors/extract-color-from-element.helper.d.ts +4 -0
- package/dist/svgr-utils/get-svg-colors/extract-color-from-element.helper.js +16 -0
- package/dist/svgr-utils/get-svg-colors/get-svg-colors.constants.d.ts +1 -0
- package/dist/svgr-utils/get-svg-colors/get-svg-colors.constants.js +1 -0
- package/dist/svgr-utils/get-svg-colors/get-svg-colors.interfaces.d.ts +4 -0
- package/dist/svgr-utils/get-svg-colors/get-svg-colors.util.d.ts +2 -0
- package/dist/svgr-utils/get-svg-colors/get-svg-colors.util.js +13 -0
- package/dist/svgr-utils/get-svg-colors/index.d.ts +2 -0
- package/dist/svgr-utils/get-svg-colors/index.js +1 -0
- package/dist/svgr-utils/get-svg-colors/validate-color.helper.d.ts +2 -0
- package/dist/svgr-utils/get-svg-colors/validate-color.helper.js +9 -0
- package/dist/svgr-utils/index.d.ts +4 -1
- package/dist/svgr-utils/index.js +4 -1
- package/dist/svgr-utils/write-compiled-icons.util.d.ts +2 -0
- package/dist/svgr-utils/{create-files.util.js → write-compiled-icons.util.js} +1 -1
- package/package.json +7 -4
- package/dist/replacement-utils/create-replacements/create-replacements.interfaces.d.ts +0 -2
- package/dist/svgr-utils/compile-icons.util.d.ts +0 -4
- package/dist/svgr-utils/compile-icons.util.js +0 -36
- package/dist/svgr-utils/create-files.util.d.ts +0 -2
- package/dist/svgr-utils/read-files.util.d.ts +0 -2
- package/dist/svgr-utils/read-files.util.js +0 -7
- package/dist/svgr-utils/svgr-build.util.js +0 -18
- /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,37 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
+
## [1.2.2](https://github.com/atls/hyperion/compare/@atls-ui-generators/icons@1.2.1...@atls-ui-generators/icons@1.2.2) (2025-10-09)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## [1.2.2] (2025-10-10)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### BREAKING CHANGES
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
* Requires React version 18 or higher for proper component functionality
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
## [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)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Features
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
* **generator:** add check what output directory exists ([01c40fb](https://github.com/atls/hyperion/commit/01c40fb7d3e8c601569b8c6b9be23b30bcfc2027))
|
|
28
|
+
* **generator:** add clipanion generate icons command ([e7fa72b](https://github.com/atls/hyperion/commit/e7fa72beb296402c7b14340b62543994e24b8d3e))
|
|
29
|
+
* **generator:** add generate replacements command to icons generator ([70529f3](https://github.com/atls/hyperion/commit/70529f3a4fc1c179349ea76bcd4ff145da11e353))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
3
35
|
## [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)
|
|
4
36
|
|
|
5
37
|
|
|
@@ -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,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
|
+
}
|
package/dist/icons.generator.js
CHANGED
|
@@ -1,37 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
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,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,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
|
+
};
|
package/dist/replacement-utils/create-replacement-icon-colors/create-replacement-icon-colors.util.js
ADDED
|
@@ -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 = (
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
|
13
|
-
}
|
|
19
|
+
return replacements;
|
|
20
|
+
};
|
|
14
21
|
exports.createReplacements = createReplacements;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const createReplacements: (
|
|
1
|
+
export declare const createReplacements: (replacementIconColors: Record<string, Array<string> | string>) => Record<string, Record<string, string>>;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
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 = (
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
|
10
|
-
}
|
|
16
|
+
return replacements;
|
|
17
|
+
};
|
|
@@ -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,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
|
|
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,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,19 @@
|
|
|
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 { clsx } from 'clsx'
|
|
7
|
+
|
|
8
|
+
import { iconSprinkles } from '../icon.css.js'
|
|
9
|
+
|
|
10
|
+
export const ${componentName} = (props: IconProps): ReactNode => {
|
|
11
|
+
const { className, style, otherProps } = iconSprinkles(props)
|
|
12
|
+
|
|
13
|
+
const iconStyle = { ...style, ...otherProps.style }
|
|
14
|
+
|
|
15
|
+
return (
|
|
16
|
+
${jsx}
|
|
17
|
+
)
|
|
18
|
+
}
|
|
19
|
+
`;
|
|
@@ -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,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,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 @@
|
|
|
1
|
+
export * from "./get-svg-colors.util.js";
|
package/dist/svgr-utils/index.js
CHANGED
|
@@ -14,7 +14,7 @@ const writeIndexFile = (sources, targetDir) => {
|
|
|
14
14
|
.join('\n')}\n`;
|
|
15
15
|
fs.writeFileSync(filename, data);
|
|
16
16
|
};
|
|
17
|
-
export const
|
|
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.
|
|
3
|
+
"version": "1.2.2",
|
|
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
|
-
"
|
|
39
|
-
"
|
|
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
|
-
"
|
|
45
|
+
"domhandler": "5.0.3",
|
|
46
|
+
"tsx": "4.20.5",
|
|
44
47
|
"typescript": "5.5.4"
|
|
45
48
|
},
|
|
46
49
|
"publishConfig": {
|
|
@@ -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') }).then((value) => value.replace('...props', '...otherProps')),
|
|
35
|
-
withReplacement: Boolean(replacements[icon.name]),
|
|
36
|
-
})));
|
|
@@ -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
|
-
};
|