@uxf/icons-generator 10.0.0 → 11.0.0
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/README.md +97 -59
- package/package.json +3 -3
- package/src/adapters/fa-pro.d.ts +5 -0
- package/src/adapters/fa-pro.js +41 -0
- package/src/fa-pro-types.d.ts +1 -0
- package/src/fa-pro-types.js +2 -0
- package/src/scripts/gen.js +20 -2
- package/src/types.d.ts +31 -11
- package/src/types.js +0 -11
- package/src/utils/_compareSprites.d.ts +3 -2
- package/src/utils/_compareSprites.js +2 -2
- package/src/utils/_createDefinitionFileContent.d.ts +3 -2
- package/src/utils/_createDefinitionFileContent.js +21 -8
- package/src/utils/_createSpriteContent.d.ts +3 -2
- package/src/utils/_createSpriteContent.js +16 -4
- package/src/utils/_generateAdaptersFallbackFiles.d.ts +3 -0
- package/src/utils/_generateAdaptersFallbackFiles.js +27 -0
- package/src/utils/_generateDefinitionFile.d.ts +3 -2
- package/src/utils/_generateDefinitionFile.js +2 -1
- package/src/utils/_generateSeparateIconFiles.d.ts +3 -2
- package/src/utils/_generateSeparateIconFiles.js +19 -4
- package/src/utils/_generateSpriteFile.d.ts +3 -2
- package/src/utils/_generateSpriteFile.js +2 -2
- package/src/utils/_getConfig.d.ts +2 -2
- package/src/utils/_getConfig.js +5 -3
- package/src/utils/_getIconFromAdapterName.d.ts +1 -0
- package/src/utils/_getIconFromAdapterName.js +5 -0
- package/src/utils/_removeUnusedSeparateIconFiles.d.ts +3 -2
- package/src/utils/_removeUnusedSeparateIconFiles.js +13 -2
- package/src/utils/check/_check.d.ts +2 -2
- package/src/utils/check/_check.js +7 -1
- package/src/utils/check/_validateDefinitions.d.ts +3 -2
- package/src/utils/check/_validateDefinitions.js +2 -1
- package/src/utils/check/_validateSeparateIcons.js +2 -3
- package/src/utils/check/_validateSourceData.js +2 -3
- package/src/utils/check/_validateSprite.d.ts +3 -2
- package/src/utils/check/_validateSprite.js +2 -2
- package/tests/icons.config.d.ts +1 -0
- package/tests/icons.config.js +11 -1
package/README.md
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
# @uxf/icons-generator
|
|
2
|
+
A well configurable tool for generating and managing custom SVG icons and sprites in web projects.
|
|
2
3
|
|
|
3
|
-
## Quick
|
|
4
|
+
## Quick Start
|
|
4
5
|
|
|
5
|
-
###
|
|
6
|
-
|
|
6
|
+
### Installation
|
|
7
|
+
Use npm or yarn to install:
|
|
8
|
+
```bash
|
|
7
9
|
npm i -D @uxf/icons-generator
|
|
8
10
|
```
|
|
9
|
-
|
|
10
|
-
or use yarn
|
|
11
|
-
```
|
|
11
|
+
```bash
|
|
12
12
|
yarn add -D @uxf/icons-generator
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
###
|
|
16
|
-
|
|
15
|
+
### Configuration
|
|
16
|
+
Create a `icons.config.ts` file in your project's root:
|
|
17
|
+
```ts
|
|
17
18
|
module.exports = {
|
|
18
19
|
icons: {
|
|
19
20
|
test: {
|
|
@@ -24,35 +25,37 @@ module.exports = {
|
|
|
24
25
|
}
|
|
25
26
|
}
|
|
26
27
|
```
|
|
27
|
-
|
|
28
|
+
See the full configuration options [here](#config).
|
|
28
29
|
|
|
29
|
-
###
|
|
30
|
-
|
|
30
|
+
### Generating Icons
|
|
31
|
+
Run the generator:
|
|
32
|
+
```bash
|
|
31
33
|
icons-gen
|
|
32
34
|
```
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
```
|
|
35
|
+
For a custom config file name:
|
|
36
|
+
```bash
|
|
36
37
|
icons-gen --configFile=yourConfigFileName.js
|
|
37
38
|
```
|
|
38
39
|
|
|
39
|
-
##
|
|
40
|
-
|
|
41
|
-
|
|
|
42
|
-
|
|
43
|
-
| mode | `"simple"
|
|
44
|
-
| typescript | `boolean`
|
|
45
|
-
| configDirectory | `string`
|
|
46
|
-
| generatedDirectory | `string`
|
|
47
|
-
| spriteFileName | `string`
|
|
48
|
-
| typeName | `string`
|
|
49
|
-
| moduleDefinition | `ModuleDefinition`
|
|
50
|
-
| customDefinitionContent | `string`
|
|
51
|
-
| icons | `Record<string, SimpleIcon
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
40
|
+
## Configuration Details
|
|
41
|
+
|
|
42
|
+
| Key | Type | Default | Required | Description |
|
|
43
|
+
|-------------------------|-------------------------------------------|-----------------------------------------------------------------------|----------|----------------------------------|
|
|
44
|
+
| mode | `"simple" \| "sizes"` | `"simple"` | No | - |
|
|
45
|
+
| typescript | `boolean` | `true` | No | - |
|
|
46
|
+
| configDirectory | `string` | `"/src/config/"` | No | - |
|
|
47
|
+
| generatedDirectory | `string` | `"/public/icons-generated/"` | No | - |
|
|
48
|
+
| spriteFileName | `string` | `undefined` | No | Specify custom sprite file name |
|
|
49
|
+
| typeName | `string` | `IconsSet` | No | - |
|
|
50
|
+
| moduleDefinition | `ModuleDefinition` | `"{ moduleName: "icons", typeName: "IconsSet", format: "interface"}"` | No | Custom module definition |
|
|
51
|
+
| customDefinitionContent | `string` | `"_icon-sprite.svg"` | No | - |
|
|
52
|
+
| icons | `Record<string, SimpleIcon \| SizedIcon>` | - | Yes | More details [here](#icon-types) |
|
|
53
|
+
| adapters | - | - | No | More details [here](#adapters) |
|
|
54
|
+
| fallbackFilesDirectory | - | - | No | More details [here](#adapters) |
|
|
55
|
+
|
|
56
|
+
## Icon Types
|
|
57
|
+
|
|
58
|
+
### SimpleIcon (mode "simple")
|
|
56
59
|
```ts
|
|
57
60
|
type SimpleIcon = {
|
|
58
61
|
data: string;
|
|
@@ -60,8 +63,7 @@ type SimpleIcon = {
|
|
|
60
63
|
width: number;
|
|
61
64
|
};
|
|
62
65
|
```
|
|
63
|
-
|
|
64
|
-
#### Example
|
|
66
|
+
Example:
|
|
65
67
|
```js
|
|
66
68
|
test: {
|
|
67
69
|
width: 50,
|
|
@@ -70,13 +72,11 @@ test: {
|
|
|
70
72
|
}
|
|
71
73
|
```
|
|
72
74
|
|
|
73
|
-
### SizedIcon (
|
|
74
|
-
#### Type
|
|
75
|
+
### SizedIcon (mode "sizes")
|
|
75
76
|
```ts
|
|
76
77
|
type SizedIcon = Record<number, string>;
|
|
77
78
|
```
|
|
78
|
-
|
|
79
|
-
#### Example
|
|
79
|
+
Example:
|
|
80
80
|
```js
|
|
81
81
|
test: {
|
|
82
82
|
24: `<path fill="#fecd09" d="M33.03 17.44c-1.68 0-3.12-.99-3.79-2.42h7.58a4.181 4.181 0 01-3.79 2.42z" />`,
|
|
@@ -84,24 +84,69 @@ test: {
|
|
|
84
84
|
}
|
|
85
85
|
```
|
|
86
86
|
|
|
87
|
-
##
|
|
88
|
-
|
|
87
|
+
## Adapters
|
|
88
|
+
Extend icon generation with adapters for external sources.
|
|
89
89
|
|
|
90
|
-
|
|
90
|
+
### Adapter Structure
|
|
91
|
+
An adapter in this context is a function designed to retrieve specific icon information, such as size and SVG path, based on the icon's name.
|
|
91
92
|
|
|
92
|
-
|
|
93
|
+
#### Example
|
|
94
|
+
```ts
|
|
95
|
+
export const getIconFromMyCustomAdapter = (iconName: string) => {
|
|
96
|
+
const icon = findIconDefinition({ iconName });
|
|
97
|
+
return { width: icon.width, height: icon.height, path: icon.data };
|
|
98
|
+
};
|
|
99
|
+
```
|
|
93
100
|
|
|
94
|
-
|
|
101
|
+
Integration into your icons configuration is straightforward:
|
|
102
|
+
```ts
|
|
103
|
+
import { getIconFromMyCustomAdapter } from "./my-custom-adapter";
|
|
104
|
+
|
|
105
|
+
adapters: {
|
|
106
|
+
myAdapter: {
|
|
107
|
+
icons: ["user"],
|
|
108
|
+
getIcon: getIconFromMyCustomAdapter,
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
For type safety, you can create a typed configuration. Use a generated type from your icon list as a generic for the icons configuration:
|
|
114
|
+
```ts
|
|
115
|
+
const iconConfig: IconsConfig<{ myAdapter: MyAdapterIconNames; }> = {};
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Predefined adapters
|
|
95
119
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
120
|
+
### Font Awesome Pro Adapter
|
|
121
|
+
#### Local Development Setup
|
|
122
|
+
```bash
|
|
123
|
+
// Set registry and token
|
|
124
|
+
npm config set "@fortawesome:registry" https://npm.fontawesome.com/
|
|
125
|
+
npm config set "//npm.fontawesome.com/:_authToken" YOUR_TOKEN
|
|
100
126
|
|
|
101
|
-
|
|
127
|
+
// Install package
|
|
128
|
+
npm install --save-dev @fortawesome/fontawesome-pro
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
#### Without Private Key
|
|
132
|
+
If your project already contains generated icons, installing the Font Awesome Pro package is not necessary.
|
|
133
|
+
The package automatically generates a 'fallback file' for the icons in use, ensuring functionality even without
|
|
134
|
+
Font Awesome Pro. However, while this setup allows for the continued use of existing icons, adding new icons
|
|
135
|
+
from the adapter will not be possible.
|
|
136
|
+
|
|
137
|
+
## Recommended Usage (Step-by-Step)
|
|
138
|
+
|
|
139
|
+
### 1. Configuration
|
|
140
|
+
Create and configure your icon settings in a config file at the project's root.
|
|
141
|
+
|
|
142
|
+
### 2. Icon Generation
|
|
143
|
+
Execute `icons-gen` to produce:
|
|
144
|
+
- SVG sprite and individual files.
|
|
145
|
+
- Definition file with icon list and sizes.
|
|
146
|
+
- Config file with icon version and sprite path.
|
|
102
147
|
|
|
103
|
-
|
|
104
|
-
Example for
|
|
148
|
+
### 3. Icon Component Setup
|
|
149
|
+
Incorporate the generated output into your project. Example for TypeScript and React:
|
|
105
150
|
|
|
106
151
|
```tsx
|
|
107
152
|
import { ICONS, IconsSet } from "src/config/icons";
|
|
@@ -128,8 +173,7 @@ export const Icon: FC<IconProps> = ({ name }) => {
|
|
|
128
173
|
};
|
|
129
174
|
```
|
|
130
175
|
|
|
131
|
-
### 4.
|
|
132
|
-
|
|
176
|
+
### 4. Sprite Preloading (Optional)
|
|
133
177
|
```tsx
|
|
134
178
|
import { ICON_SPRITE, ICONS_VERSION } from "src/config/icons-config";
|
|
135
179
|
|
|
@@ -145,11 +189,5 @@ import { ICON_SPRITE, ICONS_VERSION } from "src/config/icons-config";
|
|
|
145
189
|
</Head>
|
|
146
190
|
```
|
|
147
191
|
|
|
148
|
-
### 5.
|
|
149
|
-
|
|
150
|
-
To be sure, you have generated the last version of icons, run:
|
|
151
|
-
```
|
|
152
|
-
icons-check
|
|
153
|
-
```
|
|
154
|
-
This command compares icons with config file and fails, when icons doesn't match current config.
|
|
155
|
-
We strongly recommend add this command to your CI.
|
|
192
|
+
### 5. Verification
|
|
193
|
+
Run `icons-check` to ensure icon consistency with the config. It's recommended to integrate this check into your CI process.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uxf/icons-generator",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "11.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"author": "",
|
|
23
23
|
"license": "ISC",
|
|
24
24
|
"bin": {
|
|
25
|
-
"icons-gen": "./scripts/gen.js",
|
|
26
|
-
"icons-check": "./scripts/check.js"
|
|
25
|
+
"icons-gen": "./src/scripts/gen.js",
|
|
26
|
+
"icons-check": "./src/scripts/check.js"
|
|
27
27
|
},
|
|
28
28
|
"bugs": {
|
|
29
29
|
"url": "https://gitlab.com/uxf-npm/icons-generator/issues"
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getFaProIcon = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const _getConfig_1 = require("../utils/_getConfig");
|
|
6
|
+
const removeComments = (str) => str.replace(/<!--([\s\S]*?)-->/g, "");
|
|
7
|
+
const removeSvg = (str) => str.replace(/<svg[^>]*>/, "").replace(/<\/svg>/, "");
|
|
8
|
+
const addFillAttribute = (str) => str.replace(/\/>/g, ' fill="currentColor"/>');
|
|
9
|
+
const parseDimensions = (str) => {
|
|
10
|
+
const viewBox = str.match(/viewBox="([^"]*)"/);
|
|
11
|
+
let width, height;
|
|
12
|
+
if (viewBox && viewBox[1]) {
|
|
13
|
+
[, , width, height] = viewBox[1].split(" ");
|
|
14
|
+
}
|
|
15
|
+
if (!width || !height) {
|
|
16
|
+
throw new Error("Can't parse width or height.");
|
|
17
|
+
}
|
|
18
|
+
return { width, height };
|
|
19
|
+
};
|
|
20
|
+
const getFaProIcon = (adapterKey) => (iconName) => {
|
|
21
|
+
const config = (0, _getConfig_1._getConfig)();
|
|
22
|
+
if ((0, fs_1.existsSync)("./node_modules/@fortawesome/fontawesome-pro")) {
|
|
23
|
+
const [namespace, icon] = iconName.split(".");
|
|
24
|
+
let svg = (0, fs_1.readFileSync)(`./node_modules/@fortawesome/fontawesome-pro/svgs/${namespace}/${icon}.svg`).toString();
|
|
25
|
+
svg = removeComments(svg);
|
|
26
|
+
const { width, height } = parseDimensions(svg);
|
|
27
|
+
svg = removeSvg(svg);
|
|
28
|
+
svg = addFillAttribute(svg);
|
|
29
|
+
return { width, height, path: svg };
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
const fallbackPath = config.fallbackFilesDirectory + adapterKey + ".json";
|
|
33
|
+
const fileContent = (0, fs_1.readFileSync)(fallbackPath).toString();
|
|
34
|
+
const icon = JSON.parse(fileContent)[iconName];
|
|
35
|
+
return { width: icon.width, height: icon.height, path: decodeURIComponent(icon.path) };
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
throw new Error(`Can't find icon "${iconName}" in "${adapterKey}" fallback file.`);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
exports.getFaProIcon = getFaProIcon;
|