@wkovacs64/add-icon 0.1.0-dev.8525808f → 0.1.0-dev.8f859e16
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/dist/config.js +1 -1
- package/dist/iconify.js +10 -20
- package/dist/index.d.ts +0 -2
- package/dist/index.js +1 -23
- package/package.json +1 -3
- package/dist/transforms.d.ts +0 -13
- package/dist/transforms.js +0 -37
package/dist/config.js
CHANGED
package/dist/iconify.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import { iconToSVG } from '@iconify/utils';
|
|
4
3
|
/**
|
|
5
4
|
* Parses an icon reference into iconSet and iconName
|
|
6
5
|
* @param iconReference - Reference in format 'iconSet:iconName'
|
|
@@ -17,27 +16,24 @@ export function parseIconReference(iconReference) {
|
|
|
17
16
|
};
|
|
18
17
|
}
|
|
19
18
|
/**
|
|
20
|
-
* Fetches icon
|
|
19
|
+
* Fetches icon SVG directly from Iconify API
|
|
21
20
|
* @param iconSet - Icon set name
|
|
22
21
|
* @param iconName - Icon name
|
|
23
|
-
* @returns Promise with
|
|
22
|
+
* @returns Promise with SVG string
|
|
24
23
|
*/
|
|
25
|
-
async function
|
|
26
|
-
|
|
24
|
+
async function fetchIconSvg(iconSet, iconName) {
|
|
25
|
+
// Using width=unset parameter to remove width/height attributes automatically
|
|
26
|
+
const apiUrl = `https://api.iconify.design/${iconSet}/${iconName}.svg?width=unset`;
|
|
27
27
|
try {
|
|
28
28
|
const response = await fetch(apiUrl);
|
|
29
29
|
if (!response.ok) {
|
|
30
30
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
31
31
|
}
|
|
32
|
-
|
|
33
|
-
if (!data || !data.icons || !data.icons[iconName]) {
|
|
34
|
-
throw new Error(`Icon '${iconName}' not found in '${iconSet}' icon set`);
|
|
35
|
-
}
|
|
36
|
-
return data.icons[iconName];
|
|
32
|
+
return await response.text();
|
|
37
33
|
}
|
|
38
34
|
catch (error) {
|
|
39
35
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
40
|
-
throw new Error(`Failed to fetch icon
|
|
36
|
+
throw new Error(`Failed to fetch icon SVG: ${errorMessage}`);
|
|
41
37
|
}
|
|
42
38
|
}
|
|
43
39
|
/**
|
|
@@ -53,15 +49,9 @@ export async function downloadIcon(iconReference, config) {
|
|
|
53
49
|
if (!fs.existsSync(config.outputDir)) {
|
|
54
50
|
fs.mkdirSync(config.outputDir, { recursive: true });
|
|
55
51
|
}
|
|
56
|
-
//
|
|
57
|
-
|
|
58
|
-
//
|
|
59
|
-
const renderData = iconToSVG(iconData, {
|
|
60
|
-
height: 'auto',
|
|
61
|
-
});
|
|
62
|
-
// Create SVG string
|
|
63
|
-
let svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${renderData.attributes.width}" height="${renderData.attributes.height}" viewBox="${renderData.attributes.viewBox}">${renderData.body}</svg>`;
|
|
64
|
-
// Apply transforms if specified
|
|
52
|
+
// Fetch SVG directly with width=unset parameter to remove width/height attributes
|
|
53
|
+
let svg = await fetchIconSvg(iconSet, iconName);
|
|
54
|
+
// Apply custom transforms if specified
|
|
65
55
|
if (config.transforms && config.transforms.length > 0) {
|
|
66
56
|
for (const transform of config.transforms) {
|
|
67
57
|
// Create transform arguments object
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import type { IconTransform, TransformArgs } from './types.js';
|
|
3
|
-
import * as defaultTransforms from './transforms.js';
|
|
4
3
|
export type { IconTransform, TransformArgs };
|
|
5
|
-
export { defaultTransforms as transforms };
|
|
6
4
|
export { downloadIcon, parseIconReference } from './iconify.js';
|
package/dist/index.js
CHANGED
|
@@ -7,9 +7,6 @@ import { execSync } from 'node:child_process';
|
|
|
7
7
|
import { Command } from 'commander';
|
|
8
8
|
import { downloadIcon } from './iconify.js';
|
|
9
9
|
import { loadConfig } from './config.js';
|
|
10
|
-
import * as defaultTransforms from './transforms.js';
|
|
11
|
-
// Re-export transforms for easy importing by users
|
|
12
|
-
export { defaultTransforms as transforms };
|
|
13
10
|
// Re-export other useful functions
|
|
14
11
|
export { downloadIcon, parseIconReference } from './iconify.js';
|
|
15
12
|
// Create CLI program
|
|
@@ -21,9 +18,6 @@ program
|
|
|
21
18
|
.argument('<icon>', 'Icon reference (e.g., heroicons:arrow-up-circle)')
|
|
22
19
|
.option('-o, --output-dir <dir>', 'Directory to save icon')
|
|
23
20
|
.option('-c, --config <path>', 'Path to config file')
|
|
24
|
-
.option('--remove-size', 'Remove width and height attributes')
|
|
25
|
-
.option('--optimize', 'Optimize SVG with SVGO')
|
|
26
|
-
.option('--minify', 'Minify SVG by removing whitespace')
|
|
27
21
|
.option('-t, --transform <path>', 'Path to custom transform module (.js or .ts)')
|
|
28
22
|
.action(async (icon, options) => {
|
|
29
23
|
try {
|
|
@@ -33,18 +27,6 @@ program
|
|
|
33
27
|
if (options.outputDir) {
|
|
34
28
|
config.outputDir = options.outputDir;
|
|
35
29
|
}
|
|
36
|
-
// Prepare transforms array
|
|
37
|
-
const transforms = [];
|
|
38
|
-
// Add requested built-in transforms
|
|
39
|
-
if (options.removeSize) {
|
|
40
|
-
transforms.push(defaultTransforms.removeSize);
|
|
41
|
-
}
|
|
42
|
-
if (options.optimize) {
|
|
43
|
-
transforms.push(defaultTransforms.optimizeSvg);
|
|
44
|
-
}
|
|
45
|
-
if (options.minify) {
|
|
46
|
-
transforms.push(defaultTransforms.minifySvg);
|
|
47
|
-
}
|
|
48
30
|
// Load custom transform if specified
|
|
49
31
|
if (options.transform) {
|
|
50
32
|
try {
|
|
@@ -76,7 +58,7 @@ program
|
|
|
76
58
|
customTransform = await import(`file://${transformPath}`);
|
|
77
59
|
}
|
|
78
60
|
if (customTransform && typeof customTransform.default === 'function') {
|
|
79
|
-
transforms
|
|
61
|
+
config.transforms = [customTransform.default];
|
|
80
62
|
}
|
|
81
63
|
else {
|
|
82
64
|
console.error('Custom transform must export a default function');
|
|
@@ -89,10 +71,6 @@ program
|
|
|
89
71
|
process.exit(1);
|
|
90
72
|
}
|
|
91
73
|
}
|
|
92
|
-
// Add transforms to config
|
|
93
|
-
if (transforms.length > 0) {
|
|
94
|
-
config.transforms = transforms;
|
|
95
|
-
}
|
|
96
74
|
// Download the icon
|
|
97
75
|
console.log(`Downloading icon: ${icon}...`);
|
|
98
76
|
const savedPath = await downloadIcon(icon, config);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wkovacs64/add-icon",
|
|
3
|
-
"version": "0.1.0-dev.
|
|
3
|
+
"version": "0.1.0-dev.8f859e16",
|
|
4
4
|
"description": "CLI tool to download and transform icons from Iconify",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"iconify",
|
|
@@ -43,9 +43,7 @@
|
|
|
43
43
|
"node": ">=20.19.0"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@iconify/utils": "^2.3.0",
|
|
47
46
|
"commander": "^13.1.0",
|
|
48
|
-
"svgo": "^3.3.2",
|
|
49
47
|
"tsx": "^4.19.3",
|
|
50
48
|
"typescript": "^5.8.3"
|
|
51
49
|
},
|
package/dist/transforms.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { IconTransform } from './types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Removes width and height attributes from SVG
|
|
4
|
-
*/
|
|
5
|
-
export declare const removeSize: IconTransform;
|
|
6
|
-
/**
|
|
7
|
-
* Optimizes SVG with SVGO
|
|
8
|
-
*/
|
|
9
|
-
export declare const optimizeSvg: IconTransform;
|
|
10
|
-
/**
|
|
11
|
-
* Minifies SVG by removing whitespace
|
|
12
|
-
*/
|
|
13
|
-
export declare const minifySvg: IconTransform;
|
package/dist/transforms.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { optimize } from 'svgo';
|
|
2
|
-
/**
|
|
3
|
-
* Removes width and height attributes from SVG
|
|
4
|
-
*/
|
|
5
|
-
export const removeSize = (args) => {
|
|
6
|
-
const { svg } = args;
|
|
7
|
-
return svg.replace(/\s+width="[^"]+"/g, '').replace(/\s+height="[^"]+"/g, '');
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* Optimizes SVG with SVGO
|
|
11
|
-
*/
|
|
12
|
-
export const optimizeSvg = (args) => {
|
|
13
|
-
const { svg } = args;
|
|
14
|
-
const result = optimize(svg, {
|
|
15
|
-
plugins: [
|
|
16
|
-
'preset-default',
|
|
17
|
-
'removeXMLNS',
|
|
18
|
-
{
|
|
19
|
-
name: 'removeAttrs',
|
|
20
|
-
params: {
|
|
21
|
-
attrs: '(data-name)',
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
],
|
|
25
|
-
});
|
|
26
|
-
return result.data;
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Minifies SVG by removing whitespace
|
|
30
|
-
*/
|
|
31
|
-
export const minifySvg = (args) => {
|
|
32
|
-
const { svg } = args;
|
|
33
|
-
return svg
|
|
34
|
-
.replace(/>[\s\r\n]+</g, '><') // Remove whitespace between tags
|
|
35
|
-
.replace(/\s{2,}/g, ' ') // Reduce multiple spaces to single space
|
|
36
|
-
.trim();
|
|
37
|
-
};
|