1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.loadFontAwesomeIcons = exports.generateSVG = void 0;
|
4 | const path_1 = require("path");
|
5 | function camelCase(source) {
|
6 |
|
7 | if (typeof source !== 'string' || !source.length) {
|
8 | return source;
|
9 | }
|
10 | return source
|
11 | .toLowerCase()
|
12 | .replace(/[^a-z0-9\s]/g, ' ')
|
13 | .replace(/(^.|\s.)/g, (...t) => t[1].toUpperCase());
|
14 | }
|
15 | function generateSVG(icon, tag) {
|
16 | const { width, height, svgPathData } = icon;
|
17 | return `
|
18 | <svg id="${tag}" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}">
|
19 | <path fill="currentColor" d="${svgPathData}"></path>
|
20 | </svg>
|
21 | `;
|
22 | }
|
23 | exports.generateSVG = generateSVG;
|
24 | async function loadFontAwesomeIcons(icons, toLoad) {
|
25 |
|
26 | const dependencies = require(path_1.resolve(process.cwd(), './package.json')).dependencies;
|
27 | icons.tags = toLoad.reduce((accu, entry, index) => {
|
28 |
|
29 | const [alias, rawName] = entry.includes('@') ? entry.split('@') : [entry.replace(/:.+/, ''), entry];
|
30 | const [name, section] = rawName.includes(':') ? rawName.split(':') : [rawName, 'solid'];
|
31 | const tag = `i${index}`;
|
32 | const iconPackage = `@fortawesome/free-${section}-svg-icons`;
|
33 |
|
34 | if (!(iconPackage in dependencies)) {
|
35 | throw new Error(`In order to load the "${entry}" icon, please add ${iconPackage} to the package.json dependencies.`);
|
36 | }
|
37 |
|
38 |
|
39 | const icon = require(path_1.resolve(process.cwd(), `node_modules/${iconPackage}/fa${camelCase(`${name}`).replace(/\s/g, '')}`));
|
40 | icons.definitions += generateSVG(icon, tag);
|
41 | accu[alias] = tag;
|
42 | return accu;
|
43 | }, {});
|
44 | }
|
45 | exports.loadFontAwesomeIcons = loadFontAwesomeIcons;
|