@wkovacs64/add-icon 0.1.0-dev.8b16e5c9 → 0.1.0-dev.b5c05efe
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 +32 -24
- package/bin/add-icon.js +19 -0
- package/dist/config.js +36 -11
- package/dist/import-module.d.ts +6 -0
- package/dist/import-module.js +35 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +27 -45
- package/dist/package-info.d.ts +9 -0
- package/dist/package-info.js +34 -0
- package/dist/types.d.ts +4 -4
- package/package.json +7 -5
package/README.md
CHANGED
|
@@ -35,21 +35,7 @@ npx @wkovacs64/add-icon heroicons:arrow-up-circle --output-dir ./my-icons
|
|
|
35
35
|
|
|
36
36
|
### Transformations
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
```bash
|
|
41
|
-
# Remove width and height attributes
|
|
42
|
-
npx @wkovacs64/add-icon heroicons:arrow-up-circle --remove-size
|
|
43
|
-
|
|
44
|
-
# Optimize SVG with SVGO
|
|
45
|
-
npx @wkovacs64/add-icon heroicons:arrow-up-circle --optimize
|
|
46
|
-
|
|
47
|
-
# Minify SVG
|
|
48
|
-
npx @wkovacs64/add-icon heroicons:arrow-up-circle --minify
|
|
49
|
-
|
|
50
|
-
# Apply multiple transformations
|
|
51
|
-
npx @wkovacs64/add-icon heroicons:arrow-up-circle --remove-size --optimize --minify
|
|
52
|
-
```
|
|
38
|
+
The tool fetches SVG icons directly from the Iconify API with width and height attributes removed automatically. You can add custom transformations for more advanced modifications.
|
|
53
39
|
|
|
54
40
|
### Custom Transformations
|
|
55
41
|
|
|
@@ -77,7 +63,7 @@ export default function addTitle(args) {
|
|
|
77
63
|
|
|
78
64
|
#### TypeScript Transform
|
|
79
65
|
|
|
80
|
-
Create a custom transform file (e.g., `my-transform.ts`):
|
|
66
|
+
Create a custom transform file (e.g., `my-transform.ts`) that will be imported directly:
|
|
81
67
|
|
|
82
68
|
```ts
|
|
83
69
|
import type { TransformArgs } from '@wkovacs64/add-icon';
|
|
@@ -105,11 +91,11 @@ npx @wkovacs64/add-icon heroicons:arrow-up-circle --transform ./my-transform.ts
|
|
|
105
91
|
|
|
106
92
|
## Configuration File
|
|
107
93
|
|
|
108
|
-
You can create a configuration file (`add-icon.config.js`)
|
|
94
|
+
You can create a configuration file in your project root, using either JavaScript (`add-icon.config.js`) or TypeScript (`add-icon.config.ts`).
|
|
109
95
|
|
|
110
|
-
|
|
111
|
-
import { transforms } from '@wkovacs64/add-icon';
|
|
96
|
+
### JavaScript Configuration
|
|
112
97
|
|
|
98
|
+
```js
|
|
113
99
|
// Define custom transform
|
|
114
100
|
function addCustomAttribute(args) {
|
|
115
101
|
return args.svg.replace(/<svg/, `<svg data-icon="${args.iconName}"`);
|
|
@@ -117,10 +103,32 @@ function addCustomAttribute(args) {
|
|
|
117
103
|
|
|
118
104
|
export default {
|
|
119
105
|
outputDir: './assets/icons',
|
|
120
|
-
transforms: [
|
|
106
|
+
transforms: [addCustomAttribute],
|
|
121
107
|
};
|
|
122
108
|
```
|
|
123
109
|
|
|
110
|
+
### TypeScript Configuration
|
|
111
|
+
|
|
112
|
+
You can create a TypeScript configuration file and the tool will import it directly:
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import type { IconifyConfig, TransformArgs } from '@wkovacs64/add-icon';
|
|
116
|
+
|
|
117
|
+
// Define custom transform
|
|
118
|
+
function addCustomAttribute(args: TransformArgs): string {
|
|
119
|
+
return args.svg.replace(/<svg/, `<svg data-icon="${args.iconName}"`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const config = {
|
|
123
|
+
outputDir: './assets/icons',
|
|
124
|
+
transforms: [addCustomAttribute],
|
|
125
|
+
} satisfies IconifyConfig;
|
|
126
|
+
|
|
127
|
+
export default config;
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
> **Note:** TypeScript configuration and transform files are compiled in-memory using esbuild, without creating temporary JavaScript files. This provides a seamless TypeScript experience with no extra build steps.
|
|
131
|
+
|
|
124
132
|
## Using as a Library
|
|
125
133
|
|
|
126
134
|
You can also use iconify-cli as a library in your own projects:
|
|
@@ -128,7 +136,7 @@ You can also use iconify-cli as a library in your own projects:
|
|
|
128
136
|
### JavaScript
|
|
129
137
|
|
|
130
138
|
```js
|
|
131
|
-
import { downloadIcon
|
|
139
|
+
import { downloadIcon } from '@wkovacs64/add-icon';
|
|
132
140
|
|
|
133
141
|
// Create custom transform
|
|
134
142
|
function addCustomAttribute(args) {
|
|
@@ -139,7 +147,7 @@ function addCustomAttribute(args) {
|
|
|
139
147
|
async function downloadCustomIcon() {
|
|
140
148
|
const iconPath = await downloadIcon('heroicons:heart', {
|
|
141
149
|
outputDir: './icons',
|
|
142
|
-
transforms: [
|
|
150
|
+
transforms: [addCustomAttribute],
|
|
143
151
|
});
|
|
144
152
|
|
|
145
153
|
console.log(`Icon saved to: ${iconPath}`);
|
|
@@ -151,7 +159,7 @@ downloadCustomIcon();
|
|
|
151
159
|
### TypeScript
|
|
152
160
|
|
|
153
161
|
```ts
|
|
154
|
-
import { downloadIcon,
|
|
162
|
+
import { downloadIcon, type TransformArgs } from '@wkovacs64/add-icon';
|
|
155
163
|
|
|
156
164
|
// Create custom transform
|
|
157
165
|
const addCustomAttribute = (args: TransformArgs): string => {
|
|
@@ -163,7 +171,7 @@ async function downloadCustomIcon(): Promise<void> {
|
|
|
163
171
|
try {
|
|
164
172
|
const iconPath = await downloadIcon('heroicons:heart', {
|
|
165
173
|
outputDir: './icons',
|
|
166
|
-
transforms: [
|
|
174
|
+
transforms: [addCustomAttribute],
|
|
167
175
|
});
|
|
168
176
|
|
|
169
177
|
console.log(`Icon saved to: ${iconPath}`);
|
package/bin/add-icon.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import url from 'node:url';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import os from 'node:os';
|
|
6
|
+
import { runCli } from '../dist/index.js';
|
|
7
|
+
|
|
8
|
+
const __filename = url.fileURLToPath(import.meta.url);
|
|
9
|
+
const __dirname = path.dirname(__filename);
|
|
10
|
+
|
|
11
|
+
// This logic ensures we only run the CLI when this file is called directly
|
|
12
|
+
// and not when it's imported as a module
|
|
13
|
+
if (
|
|
14
|
+
os.platform() === 'win32'
|
|
15
|
+
? process.argv[1] === __filename
|
|
16
|
+
: process.argv[1] === __filename || process.argv[1] === __dirname
|
|
17
|
+
) {
|
|
18
|
+
runCli();
|
|
19
|
+
}
|
package/dist/config.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
+
import { importModule } from './import-module.js';
|
|
3
4
|
/**
|
|
4
5
|
* Default configuration
|
|
5
6
|
*/
|
|
6
7
|
export const defaultConfig = {
|
|
7
|
-
outputDir: '
|
|
8
|
+
outputDir: '.', // Current directory
|
|
8
9
|
};
|
|
9
10
|
/**
|
|
10
11
|
* Loads configuration from file if it exists
|
|
@@ -12,19 +13,43 @@ export const defaultConfig = {
|
|
|
12
13
|
* @returns Configuration object
|
|
13
14
|
*/
|
|
14
15
|
export async function loadConfig(configPath) {
|
|
15
|
-
// Use provided config path or look for default config file
|
|
16
|
-
const configFile = configPath || path.resolve(process.cwd(), 'add-icon.config.js');
|
|
17
16
|
try {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const config = await
|
|
17
|
+
// If a specific config path is provided, use it
|
|
18
|
+
if (configPath) {
|
|
19
|
+
// Use the unified import method for both JS and TS files
|
|
20
|
+
const config = await importModule(configPath);
|
|
22
21
|
return { ...defaultConfig, ...config.default };
|
|
23
22
|
}
|
|
23
|
+
// Try to find a config file in the current directory, checking both JS and TS
|
|
24
|
+
const jsConfigPath = path.resolve(process.cwd(), 'add-icon.config.js');
|
|
25
|
+
const tsConfigPath = path.resolve(process.cwd(), 'add-icon.config.ts');
|
|
26
|
+
// Check for TypeScript config first
|
|
27
|
+
if (existsSync(tsConfigPath)) {
|
|
28
|
+
try {
|
|
29
|
+
const config = await importModule(tsConfigPath);
|
|
30
|
+
return { ...defaultConfig, ...config.default };
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
console.error('Error loading TypeScript config, falling back to default config:', err);
|
|
34
|
+
return defaultConfig;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
// Then check for JavaScript config
|
|
38
|
+
if (existsSync(jsConfigPath)) {
|
|
39
|
+
try {
|
|
40
|
+
const config = await importModule(jsConfigPath);
|
|
41
|
+
return { ...defaultConfig, ...config.default };
|
|
42
|
+
}
|
|
43
|
+
catch (err) {
|
|
44
|
+
console.error('Error loading JavaScript config, falling back to default config:', err);
|
|
45
|
+
return defaultConfig;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// Fall back to default config
|
|
49
|
+
return defaultConfig;
|
|
24
50
|
}
|
|
25
51
|
catch (error) {
|
|
26
|
-
|
|
27
|
-
|
|
52
|
+
console.error('Error loading config, using default config:', error);
|
|
53
|
+
return defaultConfig;
|
|
28
54
|
}
|
|
29
|
-
return defaultConfig;
|
|
30
55
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import * as esbuild from 'esbuild';
|
|
4
|
+
/**
|
|
5
|
+
* Imports a module file (JavaScript or TypeScript) by processing it with esbuild
|
|
6
|
+
* @param filePath - Path to module file (JS or TS)
|
|
7
|
+
* @returns Module exports
|
|
8
|
+
*/
|
|
9
|
+
export async function importModule(filePath) {
|
|
10
|
+
const absolutePath = path.resolve(filePath);
|
|
11
|
+
try {
|
|
12
|
+
// Read the module file content
|
|
13
|
+
const code = await fs.readFile(absolutePath, "utf-8");
|
|
14
|
+
// Determine the appropriate loader based on file extension
|
|
15
|
+
const loader = absolutePath.endsWith('.ts') ? 'ts' : 'js';
|
|
16
|
+
// Use esbuild to transform the code to ESM JS
|
|
17
|
+
const result = await esbuild.transform(code, {
|
|
18
|
+
loader, // Automatically use the appropriate loader
|
|
19
|
+
format: "esm", // Output format
|
|
20
|
+
sourcemap: false, // Disable source maps for data URI
|
|
21
|
+
sourcefile: absolutePath, // Helps with error messages
|
|
22
|
+
target: 'esnext',
|
|
23
|
+
});
|
|
24
|
+
const jsCode = result.code;
|
|
25
|
+
// Create data URI and import
|
|
26
|
+
const base64Code = Buffer.from(jsCode).toString("base64");
|
|
27
|
+
const dataUri = `data:text/javascript;base64,${base64Code}`;
|
|
28
|
+
// Import the transformed code as a module
|
|
29
|
+
return await import(dataUri);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
console.error(`Error importing module ${filePath} with esbuild:`, error);
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,25 +1,28 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import fs from 'node:fs';
|
|
3
1
|
import path from 'node:path';
|
|
4
|
-
import url from 'node:url';
|
|
5
|
-
import os from 'node:os';
|
|
6
|
-
import { execSync } from 'node:child_process';
|
|
7
2
|
import { Command } from 'commander';
|
|
8
3
|
import { downloadIcon } from './iconify.js';
|
|
9
4
|
import { loadConfig } from './config.js';
|
|
5
|
+
import { importModule } from './import-module.js';
|
|
6
|
+
import { getPackageInfo } from './package-info.js';
|
|
10
7
|
// Re-export other useful functions
|
|
11
8
|
export { downloadIcon, parseIconReference } from './iconify.js';
|
|
12
9
|
// Create CLI program
|
|
13
10
|
const program = new Command();
|
|
14
|
-
program
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
11
|
+
// Set up the program with package info
|
|
12
|
+
const setupProgram = async () => {
|
|
13
|
+
const { name, version, description } = await getPackageInfo();
|
|
14
|
+
return program
|
|
15
|
+
.name(name.split('/').pop() || name)
|
|
16
|
+
.description(description)
|
|
17
|
+
.version(version, '-v, --version', 'Output the current version')
|
|
18
|
+
.argument('<icon>', 'Icon reference (e.g., heroicons:arrow-up-circle)')
|
|
19
|
+
.option('-o, --output-dir <dir>', 'Directory to save icon')
|
|
20
|
+
.option('-c, --config <path>', 'Path to config file')
|
|
21
|
+
.option('-t, --transform <path>', 'Path to custom transform module (.js or .ts)');
|
|
22
|
+
};
|
|
23
|
+
// Initialize the program
|
|
24
|
+
const initializedProgram = await setupProgram();
|
|
25
|
+
initializedProgram.action(async (icon, options) => {
|
|
23
26
|
try {
|
|
24
27
|
// Load config (first from config file, then override with CLI options)
|
|
25
28
|
const config = await loadConfig(options.config);
|
|
@@ -32,30 +35,14 @@ program
|
|
|
32
35
|
try {
|
|
33
36
|
const transformPath = path.resolve(process.cwd(), options.transform);
|
|
34
37
|
let customTransform;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const jsPath = transformPath.replace(/\.ts$/, '.js');
|
|
39
|
-
try {
|
|
40
|
-
// Use tsc to compile the TypeScript file
|
|
41
|
-
execSync(`npx tsc "${transformPath}" --outDir "${path.dirname(transformPath)}" --target es2020 --module NodeNext --moduleResolution NodeNext --esModuleInterop`);
|
|
42
|
-
// Import the compiled JS file
|
|
43
|
-
customTransform = await import(`file://${jsPath}`);
|
|
44
|
-
// Clean up temporary JS file if not in dev mode
|
|
45
|
-
if (process.env.NODE_ENV !== 'development') {
|
|
46
|
-
fs.unlinkSync(jsPath);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
catch (err) {
|
|
50
|
-
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
51
|
-
console.error(`Error transpiling TypeScript transform: ${errorMessage}`);
|
|
52
|
-
console.error('Make sure TypeScript is installed or use a JavaScript (.js) transform file.');
|
|
53
|
-
process.exit(1);
|
|
54
|
-
}
|
|
38
|
+
try {
|
|
39
|
+
// Use unified import method for both JS and TS files
|
|
40
|
+
customTransform = await importModule(transformPath);
|
|
55
41
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
42
|
+
catch (err) {
|
|
43
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
44
|
+
console.error(`Error loading transform: ${errorMessage}`);
|
|
45
|
+
process.exit(1);
|
|
59
46
|
}
|
|
60
47
|
if (customTransform && typeof customTransform.default === 'function') {
|
|
61
48
|
config.transforms = [customTransform.default];
|
|
@@ -82,12 +69,7 @@ program
|
|
|
82
69
|
process.exit(1);
|
|
83
70
|
}
|
|
84
71
|
});
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (os.platform() === 'win32'
|
|
89
|
-
? process.argv[1] === __filename
|
|
90
|
-
: process.argv[1] === __filename || process.argv[1] === __dirname) {
|
|
91
|
-
// Parse command line arguments
|
|
92
|
-
program.parse();
|
|
72
|
+
// Parse command line arguments if called directly
|
|
73
|
+
export function runCli() {
|
|
74
|
+
program.parse(process.argv);
|
|
93
75
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import fs from 'node:fs/promises';
|
|
4
|
+
/**
|
|
5
|
+
* Loads package info from package.json
|
|
6
|
+
* @returns Package info with name, version, and description
|
|
7
|
+
*/
|
|
8
|
+
export async function getPackageInfo() {
|
|
9
|
+
// Get the directory of the current module
|
|
10
|
+
const currentFileUrl = import.meta.url;
|
|
11
|
+
const currentFilePath = fileURLToPath(currentFileUrl);
|
|
12
|
+
const currentDir = path.dirname(currentFilePath);
|
|
13
|
+
// Go up one level from src/ to the package root
|
|
14
|
+
const packageJsonPath = path.resolve(currentDir, '..', 'package.json');
|
|
15
|
+
try {
|
|
16
|
+
// Read and parse package.json
|
|
17
|
+
const packageJsonContent = await fs.readFile(packageJsonPath, 'utf-8');
|
|
18
|
+
const packageJson = JSON.parse(packageJsonContent);
|
|
19
|
+
return {
|
|
20
|
+
name: packageJson.name || 'unknown',
|
|
21
|
+
version: packageJson.version || '0.0.0',
|
|
22
|
+
description: packageJson.description || 'unknown',
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
console.warn('Failed to read package.json:', error);
|
|
27
|
+
// Fallback values
|
|
28
|
+
return {
|
|
29
|
+
name: 'unknown',
|
|
30
|
+
version: '0.0.0',
|
|
31
|
+
description: 'unknown',
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Arguments passed to transform functions
|
|
3
3
|
*/
|
|
4
|
-
export
|
|
4
|
+
export type TransformArgs = {
|
|
5
5
|
/** The SVG content as a string */
|
|
6
6
|
svg: string;
|
|
7
7
|
/** The icon set (e.g., 'heroicons') */
|
|
8
8
|
iconSet: string;
|
|
9
9
|
/** The icon name (e.g., 'arrow-up-circle') */
|
|
10
10
|
iconName: string;
|
|
11
|
-
}
|
|
11
|
+
};
|
|
12
12
|
/**
|
|
13
13
|
* SVG transformation function type
|
|
14
14
|
*/
|
|
@@ -16,9 +16,9 @@ export type IconTransform = (args: TransformArgs) => Promise<string> | string;
|
|
|
16
16
|
/**
|
|
17
17
|
* Configuration options for the Iconify CLI
|
|
18
18
|
*/
|
|
19
|
-
export
|
|
19
|
+
export type IconifyConfig = {
|
|
20
20
|
/** Directory to output icons */
|
|
21
21
|
outputDir: string;
|
|
22
22
|
/** Array of transform functions to apply to icons */
|
|
23
23
|
transforms?: IconTransform[];
|
|
24
|
-
}
|
|
24
|
+
};
|
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.b5c05efe",
|
|
4
4
|
"description": "CLI tool to download and transform icons from Iconify",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"iconify",
|
|
@@ -18,10 +18,11 @@
|
|
|
18
18
|
"./package.json": "./package.json"
|
|
19
19
|
},
|
|
20
20
|
"bin": {
|
|
21
|
-
"add-icon": "
|
|
21
|
+
"add-icon": "bin/add-icon.js"
|
|
22
22
|
},
|
|
23
23
|
"files": [
|
|
24
|
-
"dist"
|
|
24
|
+
"dist",
|
|
25
|
+
"bin"
|
|
25
26
|
],
|
|
26
27
|
"scripts": {
|
|
27
28
|
"build": "tsc",
|
|
@@ -44,7 +45,7 @@
|
|
|
44
45
|
},
|
|
45
46
|
"dependencies": {
|
|
46
47
|
"commander": "^13.1.0",
|
|
47
|
-
"
|
|
48
|
+
"esbuild": "~0.25.2",
|
|
48
49
|
"typescript": "^5.8.3"
|
|
49
50
|
},
|
|
50
51
|
"devDependencies": {
|
|
@@ -57,6 +58,7 @@
|
|
|
57
58
|
"del-cli": "6.0.0",
|
|
58
59
|
"eslint": "9.24.0",
|
|
59
60
|
"npm-run-all2": "7.0.2",
|
|
60
|
-
"prettier": "3.5.3"
|
|
61
|
+
"prettier": "3.5.3",
|
|
62
|
+
"tsx": "4.19.3"
|
|
61
63
|
}
|
|
62
64
|
}
|