@coolicons/cli 0.0.0 → 5.0.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.
- package/LICENSE +21 -0
- package/README.md +39 -1
- package/dist/cli.js +122 -0
- package/dist/core.js +134 -0
- package/dist/data/bulk-regular.json +1 -0
- package/dist/data/bulk-round.json +1 -0
- package/dist/data/bulk-sharp.json +1 -0
- package/dist/data/duocolor-regular.json +1 -0
- package/dist/data/duocolor-round.json +1 -0
- package/dist/data/duocolor-sharp.json +1 -0
- package/dist/data/duotone-regular.json +1 -0
- package/dist/data/duotone-round.json +1 -0
- package/dist/data/duotone-sharp.json +1 -0
- package/dist/data/fill-regular.json +1 -0
- package/dist/data/fill-round.json +1 -0
- package/dist/data/fill-sharp.json +1 -0
- package/dist/data/index.json +1 -0
- package/dist/data/line-regular.json +1 -0
- package/dist/data/line-round.json +1 -0
- package/dist/data/line-sharp.json +1 -0
- package/dist/search.js +44 -0
- package/package.json +20 -2
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,41 @@
|
|
|
1
1
|
# @coolicons/cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<a href="https://coolicons.cool"><img src="https://raw.githubusercontent.com/krystonschwarze/coolicons/main/.github/assets/banner.png" alt="coolicons: Every icon for your next project. Free, wherever you build." width="100%"></a>
|
|
4
|
+
|
|
5
|
+
Search [coolicons](https://coolicons.cool) and copy them into your project from the terminal: 1,220 icons in five styles and three corner radii. No install, no dependencies, no network. The whole set ships inside the package.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npx @coolicons/cli search delete
|
|
9
|
+
npx @coolicons/cli show trash --style fill > trash.svg
|
|
10
|
+
npx @coolicons/cli add trash bell --style bulk --radius round --out src/icons
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Commands
|
|
14
|
+
|
|
15
|
+
| Command | |
|
|
16
|
+
| --- | --- |
|
|
17
|
+
| `search <query>` | Find icons by name or meaning, ranked like the search on coolicons.cool |
|
|
18
|
+
| `show <name>` | Print one icon's SVG to stdout |
|
|
19
|
+
| `add <name...>` | Write SVG files into a folder |
|
|
20
|
+
| `list` | Print every icon name |
|
|
21
|
+
|
|
22
|
+
| Option | |
|
|
23
|
+
| --- | --- |
|
|
24
|
+
| `-s, --style` | `line`, `duocolor`, `duotone`, `fill` or `bulk`. Default `line` |
|
|
25
|
+
| `-r, --radius` | `sharp`, `regular` or `round`. Default `regular` |
|
|
26
|
+
| `-o, --out` | Folder for `add`. Default `./icons` |
|
|
27
|
+
| `-l, --limit` | Results for `search`. Default 20 |
|
|
28
|
+
| `-c, --category` | Only one category, for `list` |
|
|
29
|
+
| `--size`, `--stroke` | Size and stroke width of the SVG. Default 24 and 2 |
|
|
30
|
+
| `--json` | Machine readable output for `search` and `list` |
|
|
31
|
+
|
|
32
|
+
`show` and `list` write only their result to stdout, everything else goes to stderr, so you can pipe them:
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
npx @coolicons/cli list | grep chart
|
|
36
|
+
npx @coolicons/cli show mail --style fill | pbcopy
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
A name that does not exist suggests the closest ones: `show chekc` answers with `check`.
|
|
40
|
+
|
|
41
|
+
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,134 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { matchIcons } from "./search.js";
|
|
3
|
+
|
|
4
|
+
export const STYLES = ["line", "duocolor", "duotone", "fill", "bulk"];
|
|
5
|
+
export const RADII = ["sharp", "regular", "round"];
|
|
6
|
+
export const STROKED = new Set(["line", "duocolor", "duotone"]);
|
|
7
|
+
export const SITE = "https://coolicons.cool";
|
|
8
|
+
|
|
9
|
+
const read = (file) => JSON.parse(readFileSync(new URL(`./data/${file}`, import.meta.url), "utf8"));
|
|
10
|
+
|
|
11
|
+
let set;
|
|
12
|
+
const chunks = new Map();
|
|
13
|
+
let byName;
|
|
14
|
+
|
|
15
|
+
export function iconSet() {
|
|
16
|
+
set ??= read("index.json");
|
|
17
|
+
return set;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function lookup() {
|
|
21
|
+
byName ??= new Map(iconSet().icons.map((icon) => [icon.name, icon]));
|
|
22
|
+
return byName;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class LookupError extends Error {
|
|
26
|
+
constructor(message, suggestions = []) {
|
|
27
|
+
super(message);
|
|
28
|
+
this.suggestions = suggestions;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function toName(input) {
|
|
33
|
+
return String(input)
|
|
34
|
+
.trim()
|
|
35
|
+
.replace(/Icon$/, "")
|
|
36
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
37
|
+
.replace(/[\s_]+/g, "-")
|
|
38
|
+
.toLowerCase();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function findIcon(input) {
|
|
42
|
+
const name = toName(input);
|
|
43
|
+
const icon = lookup().get(name);
|
|
44
|
+
if (icon) return icon;
|
|
45
|
+
const suggestions = suggest(name);
|
|
46
|
+
throw new LookupError(
|
|
47
|
+
`No coolicons icon is named "${name}".${suggestions.length ? ` Did you mean ${suggestions.join(", ")}?` : ""}`,
|
|
48
|
+
suggestions,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function checkVariant(style = "line", radius = "regular") {
|
|
53
|
+
if (!STYLES.includes(style)) throw new LookupError(`Unknown style "${style}". Styles: ${STYLES.join(", ")}.`);
|
|
54
|
+
if (!RADII.includes(radius)) throw new LookupError(`Unknown radius "${radius}". Radii: ${RADII.join(", ")}.`);
|
|
55
|
+
return { style, radius };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function search(query, limit = 10) {
|
|
59
|
+
return matchIcons(iconSet().icons, query).slice(0, limit);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function distance(a, b) {
|
|
63
|
+
const d = Array.from({ length: a.length + 1 }, (_, i) => Array.from({ length: b.length + 1 }, (_, j) => (i === 0 ? j : j === 0 ? i : 0)));
|
|
64
|
+
for (let i = 1; i <= a.length; i++) {
|
|
65
|
+
for (let j = 1; j <= b.length; j++) {
|
|
66
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
67
|
+
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
|
68
|
+
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);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return d[a.length][b.length];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function suggest(input, limit = 5) {
|
|
75
|
+
const name = toName(input);
|
|
76
|
+
if (!name) return [];
|
|
77
|
+
const found = search(name.replaceAll("-", " "), limit).map((icon) => icon.name);
|
|
78
|
+
if (found.length >= limit) return found;
|
|
79
|
+
const near = iconSet()
|
|
80
|
+
.icons.map((icon) => ({ name: icon.name, cost: distance(name, icon.name) }))
|
|
81
|
+
.filter((entry) => entry.cost <= Math.max(2, Math.floor(name.length / 3)) && !found.includes(entry.name))
|
|
82
|
+
.sort((a, b) => a.cost - b.cost || a.name.localeCompare(b.name))
|
|
83
|
+
.map((entry) => entry.name);
|
|
84
|
+
return [...found, ...near].slice(0, limit);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function component(icon) {
|
|
88
|
+
const pascal = icon.name
|
|
89
|
+
.split("-")
|
|
90
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
91
|
+
.join("");
|
|
92
|
+
return Object.hasOwn(iconSet().clashes, icon.name) ? `${pascal}Icon` : pascal;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function svg(input, { style = "line", radius = "regular", size = 24, strokeWidth = 2 } = {}) {
|
|
96
|
+
const icon = findIcon(input);
|
|
97
|
+
checkVariant(style, radius);
|
|
98
|
+
const key = `${style}-${radius}`;
|
|
99
|
+
if (!chunks.has(key)) chunks.set(key, read(`${key}.json`));
|
|
100
|
+
let inner = chunks.get(key)[icon.name];
|
|
101
|
+
if (STROKED.has(style) && Number(strokeWidth) !== 2) inner = inner.replaceAll('stroke-width="2"', `stroke-width="${strokeWidth}"`);
|
|
102
|
+
return `<svg width="${size}" height="${size}" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">${inner}</svg>\n`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const entry = (style, radius) => (style === "line" && radius === "regular" ? "" : radius === "regular" ? `/${style}` : `/${style}/${radius}`);
|
|
106
|
+
|
|
107
|
+
export function registryItem(icon, style = "line", radius = "regular") {
|
|
108
|
+
return [style === "line" && radius === "regular" ? "" : style, radius === "regular" ? "" : radius, icon.name].filter(Boolean).join("/");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function usage(inputs, { framework = "react", style = "line", radius = "regular" } = {}) {
|
|
112
|
+
checkVariant(style, radius);
|
|
113
|
+
const icons = [inputs].flat().map(findIcon);
|
|
114
|
+
const names = icons.map(component);
|
|
115
|
+
if (framework === "react" || framework === "vue") {
|
|
116
|
+
const from = `@coolicons/${framework}${entry(style, radius)}`;
|
|
117
|
+
const imports = `import { ${names.join(", ")} } from "${from}";`;
|
|
118
|
+
const tags = names.map((name) => (framework === "react" ? `<${name} size={20} />` : `<${name} :size="20" />`)).join("\n");
|
|
119
|
+
const code = framework === "react" ? `${imports}\n\n${tags}` : `<script setup>\n${imports}\n</script>\n\n<template>\n${tags}\n</template>`;
|
|
120
|
+
return { install: `npm install @coolicons/${framework}`, code };
|
|
121
|
+
}
|
|
122
|
+
if (framework === "svg") {
|
|
123
|
+
return {
|
|
124
|
+
install: "npm install @coolicons/svg",
|
|
125
|
+
code: icons.map((icon) => `@coolicons/svg/${style}/${radius}/${icon.name}.svg`).join("\n"),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
if (framework === "shadcn") {
|
|
129
|
+
const items = icons.map((icon) => `${SITE}/r/${registryItem(icon, style, radius)}.json`);
|
|
130
|
+
const files = icons.map((icon) => `components/icons/${registryItem(icon, style, radius)}.tsx`);
|
|
131
|
+
return { install: `npx shadcn@latest add ${items.join(" ")}`, code: `Adds ${files.join(", ")} exporting ${names.join(", ")}.` };
|
|
132
|
+
}
|
|
133
|
+
throw new LookupError(`Unknown framework "${framework}". Use react, vue, svg or shadcn.`);
|
|
134
|
+
}
|