@uxf/icons-generator 11.111.2 → 11.118.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.
@@ -2,47 +2,109 @@
2
2
  var _a;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.faPro = void 0;
5
- const is_not_nil_1 = require("@uxf/core/utils/is-not-nil");
6
5
  const fs_1 = require("fs");
7
6
  const _generateAdapterFallback_1 = require("../utils/_generateAdapterFallback");
8
7
  const _getIconNameForProvider_1 = require("../utils/_getIconNameForProvider");
9
8
  const KEY = "faPro";
10
- const removeComments = (str) => str.replace(/<!--([\s\S]*?)-->/g, "");
11
- const removeSvg = (str) => str.replace(/<svg[^>]*>/, "").replace(/<\/svg>/, "");
12
- const parseDimensions = (str) => {
13
- const viewBox = str.match(/viewBox="([^"]*)"/);
14
- let width, height;
15
- const viewBoxValue = viewBox === null || viewBox === void 0 ? void 0 : viewBox.at(1);
16
- if ((0, is_not_nil_1.isNotNil)(viewBoxValue)) {
17
- [, , width, height] = viewBoxValue.split(" ");
18
- }
19
- if (!width || !height) {
20
- throw new Error("Can't parse width or height.");
21
- }
22
- return { width: Number(width), height: Number(height) };
9
+ const LEGACY_PACKAGE = "@fortawesome/fontawesome-pro";
10
+ // FA's npm naming is inconsistent: the legacy duotone solid keeps the `pro-`
11
+ // prefix (`@fortawesome/pro-duotone-svg-icons`) while the v7 duotone-light /
12
+ // duotone-regular / duotone-thin variants are published without it.
13
+ const NAMESPACE_TO_PACKAGE = {
14
+ brands: "@fortawesome/free-brands-svg-icons",
15
+ duotone: "@fortawesome/pro-duotone-svg-icons",
16
+ "duotone-light": "@fortawesome/duotone-light-svg-icons",
17
+ "duotone-regular": "@fortawesome/duotone-regular-svg-icons",
18
+ "duotone-thin": "@fortawesome/duotone-thin-svg-icons",
19
+ light: "@fortawesome/pro-light-svg-icons",
20
+ regular: "@fortawesome/pro-regular-svg-icons",
21
+ "sharp-duotone-light": "@fortawesome/sharp-duotone-light-svg-icons",
22
+ "sharp-duotone-regular": "@fortawesome/sharp-duotone-regular-svg-icons",
23
+ "sharp-duotone-solid": "@fortawesome/sharp-duotone-solid-svg-icons",
24
+ "sharp-duotone-thin": "@fortawesome/sharp-duotone-thin-svg-icons",
25
+ "sharp-light": "@fortawesome/sharp-light-svg-icons",
26
+ "sharp-regular": "@fortawesome/sharp-regular-svg-icons",
27
+ "sharp-solid": "@fortawesome/sharp-solid-svg-icons",
28
+ "sharp-thin": "@fortawesome/sharp-thin-svg-icons",
29
+ solid: "@fortawesome/pro-solid-svg-icons",
30
+ thin: "@fortawesome/pro-thin-svg-icons",
23
31
  };
24
32
  const NODE_MODULES_PATH = (_a = process.env.NODE_MODULES_PATH) !== null && _a !== void 0 ? _a : "./node_modules";
33
+ if ((0, fs_1.existsSync)(`${NODE_MODULES_PATH}/${LEGACY_PACKAGE}`)) {
34
+ throw new Error(`Package "${LEGACY_PACKAGE}" is installed but no longer used. The faPro adapter reads from per-style packages instead. Run \`yarn remove ${LEGACY_PACKAGE}\`.`);
35
+ }
36
+ const kebabToPascal = (name) => name
37
+ .split("-")
38
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
39
+ .join("");
40
+ const tryRequire = (pkg) => {
41
+ try {
42
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
43
+ return require(pkg);
44
+ }
45
+ catch {
46
+ return null;
47
+ }
48
+ };
49
+ const wrapPath = (pathData) => Array.isArray(pathData)
50
+ ? pathData.map((d) => `<path fill="currentColor" d="${d}"/>`).join("")
51
+ : `<path fill="currentColor" d="${pathData}"/>`;
52
+ const resolvePackage = (namespace, iconName) => {
53
+ const pkg = NAMESPACE_TO_PACKAGE[namespace];
54
+ if (!pkg) {
55
+ const supported = Object.keys(NAMESPACE_TO_PACKAGE).join(", ");
56
+ throw new Error(`Unsupported FA namespace "${namespace}" for icon "${iconName}". Supported namespaces: ${supported}.`);
57
+ }
58
+ return pkg;
59
+ };
60
+ const loadIconFromPackage = (iconName) => {
61
+ const [namespace, name] = iconName.split(".");
62
+ const pkg = resolvePackage(namespace, iconName);
63
+ const pkgExports = tryRequire(pkg);
64
+ if (!pkgExports) {
65
+ return null;
66
+ }
67
+ const exportKey = `fa${kebabToPascal(name)}`;
68
+ const def = pkgExports[exportKey];
69
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
70
+ if (!def || !Array.isArray(def.icon)) {
71
+ throw new Error(`Icon "${iconName}" not found in "${pkg}" (looked up export "${exportKey}").`);
72
+ }
73
+ const [width, height, , , pathData] = def.icon;
74
+ return { width: Number(width), height: Number(height), path: wrapPath(pathData) };
75
+ };
76
+ const readFromFallback = (iconName, config) => {
77
+ const fallbackPath = config.fallbackFilesDirectory + KEY + ".json";
78
+ let fileContent;
79
+ try {
80
+ fileContent = (0, fs_1.readFileSync)(fallbackPath).toString();
81
+ }
82
+ catch {
83
+ return null;
84
+ }
85
+ const icon = JSON.parse(fileContent)[iconName];
86
+ if (!icon) {
87
+ return null;
88
+ }
89
+ return {
90
+ width: Number(icon.width),
91
+ height: Number(icon.height),
92
+ path: decodeURIComponent(icon.path),
93
+ };
94
+ };
25
95
  const getIcon = (iconName, config) => {
26
- if ((0, fs_1.existsSync)(NODE_MODULES_PATH + "/@fortawesome/fontawesome-pro")) {
27
- const [namespace, icon] = iconName.split(".");
28
- let svg = (0, fs_1.readFileSync)(`${NODE_MODULES_PATH}/@fortawesome/fontawesome-pro/svgs/${namespace}/${icon}.svg`).toString();
29
- svg = removeComments(svg);
30
- const { width, height } = parseDimensions(svg);
31
- svg = removeSvg(svg);
32
- const iconDefinition = { width, height, path: svg };
96
+ const iconDefinition = loadIconFromPackage(iconName);
97
+ if (iconDefinition) {
33
98
  (0, _generateAdapterFallback_1._generateAdapterFallback)(KEY, iconName, iconDefinition, config);
34
99
  return iconDefinition;
35
100
  }
36
- try {
37
- const fallbackPath = config.fallbackFilesDirectory + KEY + ".json";
38
- const fileContent = (0, fs_1.readFileSync)(fallbackPath).toString();
39
- const icon = JSON.parse(fileContent)[iconName];
40
- return { width: Number(icon.width), height: Number(icon.height), path: decodeURIComponent(icon.path) };
41
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
42
- }
43
- catch (e) {
44
- throw new Error(`Can't find icon "${iconName}" in "${KEY}" fallback file.`);
101
+ const fallbackIcon = readFromFallback(iconName, config);
102
+ if (fallbackIcon) {
103
+ return fallbackIcon;
45
104
  }
105
+ const [namespace] = iconName.split(".");
106
+ const pkg = resolvePackage(namespace, iconName);
107
+ throw new Error(`Can't resolve icon "${iconName}": package "${pkg}" is not installed. Run \`yarn add -D ${pkg}\` to enable "${namespace}" icons.`);
46
108
  };
47
109
  const icon = (iconName) => (config) => getIcon(iconName, config);
48
110
  exports.faPro = {