@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.
Files changed (39) hide show
  1. package/README.md +97 -59
  2. package/package.json +3 -3
  3. package/src/adapters/fa-pro.d.ts +5 -0
  4. package/src/adapters/fa-pro.js +41 -0
  5. package/src/fa-pro-types.d.ts +1 -0
  6. package/src/fa-pro-types.js +2 -0
  7. package/src/scripts/gen.js +20 -2
  8. package/src/types.d.ts +31 -11
  9. package/src/types.js +0 -11
  10. package/src/utils/_compareSprites.d.ts +3 -2
  11. package/src/utils/_compareSprites.js +2 -2
  12. package/src/utils/_createDefinitionFileContent.d.ts +3 -2
  13. package/src/utils/_createDefinitionFileContent.js +21 -8
  14. package/src/utils/_createSpriteContent.d.ts +3 -2
  15. package/src/utils/_createSpriteContent.js +16 -4
  16. package/src/utils/_generateAdaptersFallbackFiles.d.ts +3 -0
  17. package/src/utils/_generateAdaptersFallbackFiles.js +27 -0
  18. package/src/utils/_generateDefinitionFile.d.ts +3 -2
  19. package/src/utils/_generateDefinitionFile.js +2 -1
  20. package/src/utils/_generateSeparateIconFiles.d.ts +3 -2
  21. package/src/utils/_generateSeparateIconFiles.js +19 -4
  22. package/src/utils/_generateSpriteFile.d.ts +3 -2
  23. package/src/utils/_generateSpriteFile.js +2 -2
  24. package/src/utils/_getConfig.d.ts +2 -2
  25. package/src/utils/_getConfig.js +5 -3
  26. package/src/utils/_getIconFromAdapterName.d.ts +1 -0
  27. package/src/utils/_getIconFromAdapterName.js +5 -0
  28. package/src/utils/_removeUnusedSeparateIconFiles.d.ts +3 -2
  29. package/src/utils/_removeUnusedSeparateIconFiles.js +13 -2
  30. package/src/utils/check/_check.d.ts +2 -2
  31. package/src/utils/check/_check.js +7 -1
  32. package/src/utils/check/_validateDefinitions.d.ts +3 -2
  33. package/src/utils/check/_validateDefinitions.js +2 -1
  34. package/src/utils/check/_validateSeparateIcons.js +2 -3
  35. package/src/utils/check/_validateSourceData.js +2 -3
  36. package/src/utils/check/_validateSprite.d.ts +3 -2
  37. package/src/utils/check/_validateSprite.js +2 -2
  38. package/tests/icons.config.d.ts +1 -0
  39. 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 start
4
+ ## Quick Start
4
5
 
5
- ### Install
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
- ### Create simple JS config file (`icons.config.js`) in your root folder
16
- ```js
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
- Full list of config options is [here](#config).
28
+ See the full configuration options [here](#config).
28
29
 
29
- ### Run generator
30
- ```
30
+ ### Generating Icons
31
+ Run the generator:
32
+ ```bash
31
33
  icons-gen
32
34
  ```
33
-
34
- Or, if you need to use different name for config file:
35
- ```
35
+ For a custom config file name:
36
+ ```bash
36
37
  icons-gen --configFile=yourConfigFileName.js
37
38
  ```
38
39
 
39
- ## Config
40
-
41
- | key | type | default | required | description |
42
- |-------------------------|------------------------------|-----------------------------------------------------------------------|----------|-------------------------------------|
43
- | mode | `"simple" \ | "sizes"` | `"simple"` | | |
44
- | typescript | `boolean` | `true` | | |
45
- | configDirectory | `string` | `"/src/config/"` | | |
46
- | generatedDirectory | `string` | `"/public/icons-generated/"` | | |
47
- | spriteFileName | `string` | `undefined` | | custom content to definition file |
48
- | typeName | `string` | `IconsSet` | | |
49
- | moduleDefinition | `ModuleDefinition` | `"{ moduleName: "icons", typeName: "IconsSet", format: "interface"}"` | | allows to generate module definiton |
50
- | customDefinitionContent | `string` | `"_icon-sprite.svg"` | | |
51
- | icons | `Record<string, SimpleIcon \ | SizedIcon>` | | yes | find out more [here](#icon-types) |
52
-
53
- ## Icon types
54
- ### SimpleIcon (for mode "simple")
55
- #### Type
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 (for mode "sizes")
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
- ## Recommended use (step-by-step)
88
- ### 1. Create config file in root folder of your project...
87
+ ## Adapters
88
+ Extend icon generation with adapters for external sources.
89
89
 
90
- ... and configure all icons you need.
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
- ### 2. Generate icons
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
- Run `icons-gen` to generate:
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
- - SVG sprite file with all icons
97
- - Separate SVG files for each icon
98
- - "Definition file" which contains list of all icons and their sizes
99
- - "Config file" which contains current icons version and path to generated sprite file
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
- ### 3. Setup your Icon component
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
- Implement generator output to your project structure.
104
- Example for `typescript` and `React`:
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. Preload your sprite (optional)
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. Check it
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": "10.0.0",
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,5 @@
1
+ export declare const getFaProIcon: (adapterKey: string) => (iconName: string) => {
2
+ width: any;
3
+ height: any;
4
+ path: string;
5
+ };
@@ -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;