@coolicons/cli 0.0.0 → 5.0.0-beta.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kryston Schwarze
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,39 @@
1
1
  # @coolicons/cli
2
2
 
3
- Reserved for coolicons 5. Use version 5 or later, see [github.com/krystonschwarze/coolicons](https://github.com/krystonschwarze/coolicons).
3
+ Search [coolicons](https://coolicons.cool) and copy them into your project from the terminal: 1220 icons in five styles and three corner radii. No install, no dependencies, no network. The whole set ships inside the package.
4
+
5
+ ```sh
6
+ npx @coolicons/cli search delete
7
+ npx @coolicons/cli show trash --style fill > trash.svg
8
+ npx @coolicons/cli add trash bell --style bulk --radius round --out src/icons
9
+ ```
10
+
11
+ ## Commands
12
+
13
+ | Command | |
14
+ | --- | --- |
15
+ | `search <query>` | Find icons by name or meaning, ranked like the search on coolicons.cool |
16
+ | `show <name>` | Print one icon's SVG to stdout |
17
+ | `add <name...>` | Write SVG files into a folder |
18
+ | `list` | Print every icon name |
19
+
20
+ | Option | |
21
+ | --- | --- |
22
+ | `-s, --style` | `line`, `duocolor`, `duotone`, `fill` or `bulk`. Default `line` |
23
+ | `-r, --radius` | `sharp`, `regular` or `round`. Default `regular` |
24
+ | `-o, --out` | Folder for `add`. Default `./icons` |
25
+ | `-l, --limit` | Results for `search`. Default 20 |
26
+ | `-c, --category` | Only one category, for `list` |
27
+ | `--size`, `--stroke` | Size and stroke width of the SVG. Default 24 and 2 |
28
+ | `--json` | Machine readable output for `search` and `list` |
29
+
30
+ `show` and `list` write only their result to stdout, everything else goes to stderr, so you can pipe them:
31
+
32
+ ```sh
33
+ npx @coolicons/cli list | grep chart
34
+ npx @coolicons/cli show mail --style fill | pbcopy
35
+ ```
36
+
37
+ A name that does not exist suggests the closest ones: `show chekc` answers with `check`.
38
+
39
+ More in the [coolicons repository](https://github.com/krystonschwarze/coolicons#readme). MIT license.
package/dist/cli.js ADDED
@@ -0,0 +1,122 @@
1
+ #!/usr/bin/env node
2
+ import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
3
+ import path from "node:path";
4
+ import { component, findIcon, iconSet, LookupError, RADII, search, STYLES, svg } from "./core.js";
5
+
6
+ const { version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
7
+
8
+ const HELP = `coolicons ${version}
9
+
10
+ Search coolicons and copy them into your project. No network, the whole set ships in this package.
11
+
12
+ Usage
13
+ coolicons search <query> Find icons by name or meaning, ranked
14
+ coolicons show <name> Print one icon's SVG to stdout
15
+ coolicons add <name...> Write SVG files into a folder
16
+ coolicons list Print every icon name
17
+
18
+ Options
19
+ -s, --style <style> ${STYLES.join(", ")} (default line)
20
+ -r, --radius <radius> ${RADII.join(", ")} (default regular)
21
+ -o, --out <dir> Folder for add (default ./icons)
22
+ -l, --limit <n> Results for search (default 20)
23
+ -c, --category <name> Only this category, for list
24
+ --size <px> Width and height of the SVG (default 24)
25
+ --stroke <px> Stroke width for line, duocolor and duotone (default 2)
26
+ --json Machine readable output for search and list
27
+ -h, --help Show this help
28
+ -v, --version Show the version
29
+
30
+ Examples
31
+ npx @coolicons/cli search delete
32
+ npx @coolicons/cli show trash --style fill > trash.svg
33
+ npx @coolicons/cli add trash bell --style bulk --radius round --out src/icons
34
+ `;
35
+
36
+ const SHORT = { s: "style", r: "radius", o: "out", l: "limit", c: "category", h: "help", v: "version" };
37
+ const VALUED = new Set(["style", "radius", "out", "limit", "category", "size", "stroke"]);
38
+
39
+ function parse(argv) {
40
+ const options = {};
41
+ const words = [];
42
+ for (let i = 0; i < argv.length; i++) {
43
+ const arg = argv[i];
44
+ if (!arg.startsWith("-") || arg === "-") {
45
+ words.push(arg);
46
+ continue;
47
+ }
48
+ const [raw, inline] = arg.replace(/^--?/, "").split("=", 2);
49
+ const key = arg.startsWith("--") ? raw : (SHORT[raw] ?? raw);
50
+ if (VALUED.has(key)) {
51
+ const value = inline ?? argv[++i];
52
+ if (value === undefined) throw new LookupError(`--${key} needs a value.`);
53
+ options[key] = value;
54
+ } else options[key] = true;
55
+ }
56
+ return { command: words[0], words: words.slice(1), options };
57
+ }
58
+
59
+ const out = (text) => process.stdout.write(text.endsWith("\n") ? text : `${text}\n`);
60
+ const say = (text) => process.stderr.write(`${text}\n`);
61
+
62
+ function variant(options) {
63
+ return { style: options.style ?? "line", radius: options.radius ?? "regular", size: options.size ?? 24, strokeWidth: options.stroke ?? 2 };
64
+ }
65
+
66
+ function run({ command, words, options }) {
67
+ if (options.version) return out(version);
68
+ if (options.help || !command || command === "help") return out(HELP);
69
+
70
+ if (command === "search") {
71
+ const query = words.join(" ");
72
+ if (!query) throw new LookupError("search needs a query, like: coolicons search delete");
73
+ const results = search(query, Number(options.limit ?? 20));
74
+ if (options.json) return out(JSON.stringify(results.map((icon) => ({ ...icon, component: component(icon) })), null, 2));
75
+ if (!results.length) {
76
+ say(`No icon matches "${query}".`);
77
+ process.exitCode = 1;
78
+ return;
79
+ }
80
+ const width = Math.max(...results.map((icon) => icon.name.length));
81
+ for (const icon of results) out(`${icon.name.padEnd(width)} ${component(icon)} ${icon.category}${icon.keywords.length ? ` ${icon.keywords.join(", ")}` : ""}`);
82
+ say(`${results.length} of ${iconSet().icons.length} icons`);
83
+ return;
84
+ }
85
+
86
+ if (command === "show") {
87
+ if (words.length !== 1) throw new LookupError("show needs exactly one name, like: coolicons show trash");
88
+ return out(svg(words[0], variant(options)));
89
+ }
90
+
91
+ if (command === "add") {
92
+ if (!words.length) throw new LookupError("add needs at least one name, like: coolicons add trash bell");
93
+ const icons = words.map(findIcon);
94
+ const folder = path.resolve(options.out ?? "icons");
95
+ mkdirSync(folder, { recursive: true });
96
+ for (const icon of icons) {
97
+ const file = path.join(folder, `${icon.name}.svg`);
98
+ writeFileSync(file, svg(icon.name, variant(options)));
99
+ say(`Wrote ${path.relative(process.cwd(), file) || file}`);
100
+ }
101
+ return;
102
+ }
103
+
104
+ if (command === "list") {
105
+ const icons = options.category ? iconSet().icons.filter((icon) => icon.category.toLowerCase() === String(options.category).toLowerCase()) : iconSet().icons;
106
+ if (!icons.length) throw new LookupError(`No category "${options.category}". Categories: ${[...new Set(iconSet().icons.map((icon) => icon.category))].join(", ")}.`);
107
+ if (options.json) return out(JSON.stringify(icons.map((icon) => icon.name)));
108
+ out(icons.map((icon) => icon.name).join("\n"));
109
+ say(`${icons.length} icons`);
110
+ return;
111
+ }
112
+
113
+ throw new LookupError(`Unknown command "${command}". Run coolicons --help.`);
114
+ }
115
+
116
+ try {
117
+ run(parse(process.argv.slice(2)));
118
+ } catch (error) {
119
+ if (!(error instanceof LookupError)) throw error;
120
+ say(error.message);
121
+ process.exitCode = 1;
122
+ }
package/dist/core.js ADDED
@@ -0,0 +1,163 @@
1
+ import { readFileSync } from "node:fs";
2
+
3
+ export const STYLES = ["line", "duocolor", "duotone", "fill", "bulk"];
4
+ export const RADII = ["sharp", "regular", "round"];
5
+ export const STROKED = new Set(["line", "duocolor", "duotone"]);
6
+ export const SITE = "https://coolicons.cool";
7
+
8
+ const read = (file) => JSON.parse(readFileSync(new URL(`./data/${file}`, import.meta.url), "utf8"));
9
+
10
+ let set;
11
+ const chunks = new Map();
12
+ let byName;
13
+
14
+ export function iconSet() {
15
+ set ??= read("index.json");
16
+ return set;
17
+ }
18
+
19
+ function lookup() {
20
+ byName ??= new Map(iconSet().icons.map((icon) => [icon.name, icon]));
21
+ return byName;
22
+ }
23
+
24
+ export class LookupError extends Error {
25
+ constructor(message, suggestions = []) {
26
+ super(message);
27
+ this.suggestions = suggestions;
28
+ }
29
+ }
30
+
31
+ export function toName(input) {
32
+ return String(input)
33
+ .trim()
34
+ .replace(/Icon$/, "")
35
+ .replace(/([a-z0-9])([A-Z])/g, "$1-$2")
36
+ .replace(/[\s_]+/g, "-")
37
+ .toLowerCase();
38
+ }
39
+
40
+ export function findIcon(input) {
41
+ const name = toName(input);
42
+ const icon = lookup().get(name);
43
+ if (icon) return icon;
44
+ const suggestions = suggest(name);
45
+ throw new LookupError(
46
+ `No coolicons icon is named "${name}".${suggestions.length ? ` Did you mean ${suggestions.join(", ")}?` : ""}`,
47
+ suggestions,
48
+ );
49
+ }
50
+
51
+ export function checkVariant(style = "line", radius = "regular") {
52
+ if (!STYLES.includes(style)) throw new LookupError(`Unknown style "${style}". Styles: ${STYLES.join(", ")}.`);
53
+ if (!RADII.includes(radius)) throw new LookupError(`Unknown radius "${radius}". Radii: ${RADII.join(", ")}.`);
54
+ return { style, radius };
55
+ }
56
+
57
+ function scoreIcon(icon, terms) {
58
+ let total = 0;
59
+ const tokens = icon.name.split("-");
60
+ const groups = icon.category.toLowerCase().split(/[^a-z0-9]+/);
61
+ for (const term of terms) {
62
+ let best = 0;
63
+ const exact = icon.keywords.indexOf(term);
64
+ const prefix = icon.keywords.findIndex((word) => word.startsWith(term));
65
+ if (icon.name === term) best = 100;
66
+ else if (icon.name.startsWith(term)) best = 80;
67
+ else if (tokens.includes(term)) best = 70;
68
+ else if (tokens.some((token) => token.startsWith(term))) best = 55;
69
+ else if ((term.length > 3 || term.includes("-")) && icon.name.includes(term)) best = 40;
70
+ else if (exact >= 0) best = 35 - Math.min(exact, 9);
71
+ else if (groups.includes(term)) best = 30;
72
+ else if (prefix >= 0) best = 25 - Math.min(prefix, 9);
73
+ else if (icon.category.toLowerCase().includes(term)) best = 15;
74
+ if (!best) return 0;
75
+ total += best;
76
+ }
77
+ return total;
78
+ }
79
+
80
+ export function search(query, limit = 10) {
81
+ const terms = String(query).toLowerCase().trim().split(/\s+/).filter(Boolean);
82
+ if (!terms.length) return [];
83
+ return iconSet()
84
+ .icons.map((icon) => ({ icon, rank: scoreIcon(icon, terms) }))
85
+ .filter((entry) => entry.rank)
86
+ .sort((a, b) => b.rank - a.rank || a.icon.name.length - b.icon.name.length || a.icon.name.localeCompare(b.icon.name))
87
+ .slice(0, limit)
88
+ .map((entry) => entry.icon);
89
+ }
90
+
91
+ function distance(a, b) {
92
+ const d = Array.from({ length: a.length + 1 }, (_, i) => Array.from({ length: b.length + 1 }, (_, j) => (i === 0 ? j : j === 0 ? i : 0)));
93
+ for (let i = 1; i <= a.length; i++) {
94
+ for (let j = 1; j <= b.length; j++) {
95
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
96
+ d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
97
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
98
+ }
99
+ }
100
+ return d[a.length][b.length];
101
+ }
102
+
103
+ export function suggest(input, limit = 5) {
104
+ const name = toName(input);
105
+ if (!name) return [];
106
+ const found = search(name.replaceAll("-", " "), limit).map((icon) => icon.name);
107
+ if (found.length >= limit) return found;
108
+ const near = iconSet()
109
+ .icons.map((icon) => ({ name: icon.name, cost: distance(name, icon.name) }))
110
+ .filter((entry) => entry.cost <= Math.max(2, Math.floor(name.length / 3)) && !found.includes(entry.name))
111
+ .sort((a, b) => a.cost - b.cost || a.name.localeCompare(b.name))
112
+ .map((entry) => entry.name);
113
+ return [...found, ...near].slice(0, limit);
114
+ }
115
+
116
+ export function component(icon) {
117
+ const pascal = icon.name
118
+ .split("-")
119
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
120
+ .join("");
121
+ return Object.hasOwn(iconSet().clashes, icon.name) ? `${pascal}Icon` : pascal;
122
+ }
123
+
124
+ export function svg(input, { style = "line", radius = "regular", size = 24, strokeWidth = 2 } = {}) {
125
+ const icon = findIcon(input);
126
+ checkVariant(style, radius);
127
+ const key = `${style}-${radius}`;
128
+ if (!chunks.has(key)) chunks.set(key, read(`${key}.json`));
129
+ let inner = chunks.get(key)[icon.name];
130
+ if (STROKED.has(style) && Number(strokeWidth) !== 2) inner = inner.replaceAll('stroke-width="2"', `stroke-width="${strokeWidth}"`);
131
+ return `<svg width="${size}" height="${size}" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">${inner}</svg>\n`;
132
+ }
133
+
134
+ const entry = (style, radius) => (style === "line" && radius === "regular" ? "" : radius === "regular" ? `/${style}` : `/${style}/${radius}`);
135
+
136
+ export function registryItem(icon, style = "line", radius = "regular") {
137
+ return [style === "line" && radius === "regular" ? "" : style, radius === "regular" ? "" : radius, icon.name].filter(Boolean).join("/");
138
+ }
139
+
140
+ export function usage(inputs, { framework = "react", style = "line", radius = "regular" } = {}) {
141
+ checkVariant(style, radius);
142
+ const icons = [inputs].flat().map(findIcon);
143
+ const names = icons.map(component);
144
+ if (framework === "react" || framework === "vue") {
145
+ const from = `@coolicons/${framework}${entry(style, radius)}`;
146
+ const imports = `import { ${names.join(", ")} } from "${from}";`;
147
+ const tags = names.map((name) => (framework === "react" ? `<${name} size={20} />` : `<${name} :size="20" />`)).join("\n");
148
+ const code = framework === "react" ? `${imports}\n\n${tags}` : `<script setup>\n${imports}\n</script>\n\n<template>\n${tags}\n</template>`;
149
+ return { install: `npm install @coolicons/${framework}`, code };
150
+ }
151
+ if (framework === "svg") {
152
+ return {
153
+ install: "npm install @coolicons/svg",
154
+ code: icons.map((icon) => `@coolicons/svg/${style}/${radius}/${icon.name}.svg`).join("\n"),
155
+ };
156
+ }
157
+ if (framework === "shadcn") {
158
+ const items = icons.map((icon) => `${SITE}/r/${registryItem(icon, style, radius)}.json`);
159
+ const files = icons.map((icon) => `components/icons/${registryItem(icon, style, radius)}.tsx`);
160
+ return { install: `npx shadcn@latest add ${items.join(" ")}`, code: `Adds ${files.join(", ")} exporting ${names.join(", ")}.` };
161
+ }
162
+ throw new LookupError(`Unknown framework "${framework}". Use react, vue, svg or shadcn.`);
163
+ }