linework-cli 0.0.0-stage → 0.1.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/README.md +28 -2
- package/bin/linework-cli.mjs +7 -0
- package/package.json +37 -4
- package/src/cli.mjs +482 -0
package/README.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
-
#
|
|
1
|
+
# linework-cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Official CLI for [Linework](https://linework.rdev.web.id) semantic vector icons and product illustrations.
|
|
4
|
+
|
|
5
|
+
Initialize semantic `--rdev-*` CSS variables and pull live TSX components or full React packages straight into Next.js, Vite, Astro, Remix, or React Router apps.
|
|
6
|
+
|
|
7
|
+
## Quick Start
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# Initialize semantic tokens & linework.json
|
|
11
|
+
pnpm dlx linework-cli@latest init -t next
|
|
12
|
+
|
|
13
|
+
# Add individual zero-dependency TSX icons or illustrations
|
|
14
|
+
pnpm dlx linework-cli@latest add friendly-ghost shield-check
|
|
15
|
+
|
|
16
|
+
# Or install the full auto-updated React packages directly
|
|
17
|
+
pnpm dlx linework-cli@latest init -t next --install
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Commands
|
|
21
|
+
|
|
22
|
+
- `init [-t next|vite|astro|remix|react-router|react] [--dir <path>] [--install] [--no-css] [--yes]`
|
|
23
|
+
Creates `linework.json`, injects `--rdev-*` semantic light & dark tokens into your global CSS, and optionally installs `@rdev/icons-react` + `@rdev/illustrations-react`.
|
|
24
|
+
- `add <slug...> [-o <dir>] [--size <px>] [--overwrite] [--all-illustrations]`
|
|
25
|
+
Fetches ready-to-use PascalCase TSX component files directly from the live Linework registry.
|
|
26
|
+
- `search <query> [--type icon|illustration] [--limit <n>]`
|
|
27
|
+
Searches published assets from the terminal.
|
|
28
|
+
- `sync`
|
|
29
|
+
Updates `@rdev/icons-react` and `@rdev/illustrations-react` in `node_modules` with newly published assets.
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "linework-cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "CLI for initializing Linework semantic color tokens and adding live vector icons and illustrations to React projects.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "RDEV Studio",
|
|
8
|
+
"homepage": "https://linework.rdev.web.id/docs/cli",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"icons",
|
|
11
|
+
"illustrations",
|
|
12
|
+
"svg",
|
|
13
|
+
"react",
|
|
14
|
+
"nextjs",
|
|
15
|
+
"tailwindcss",
|
|
16
|
+
"design-tokens",
|
|
17
|
+
"linework",
|
|
18
|
+
"cli"
|
|
19
|
+
],
|
|
20
|
+
"bin": {
|
|
21
|
+
"linework-cli": "bin/linework-cli.mjs",
|
|
22
|
+
"linework": "bin/linework-cli.mjs"
|
|
23
|
+
},
|
|
24
|
+
"main": "./src/cli.mjs",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./src/cli.mjs"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"bin",
|
|
30
|
+
"src",
|
|
31
|
+
"README.md"
|
|
32
|
+
],
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=18.0.0"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"test": "node --test tests/*.test.mjs"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/cli.mjs
ADDED
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
|
|
5
|
+
export const DEFAULT_REGISTRY_URL = 'https://linework.rdev.web.id';
|
|
6
|
+
export const CLI_VERSION = '0.1.0';
|
|
7
|
+
|
|
8
|
+
const TOKEN_BLOCK_START = '/* linework-tokens:start */';
|
|
9
|
+
const TOKEN_BLOCK_END = '/* linework-tokens:end */';
|
|
10
|
+
|
|
11
|
+
const DEFAULT_TOKENS = {
|
|
12
|
+
primary: '#224A3E',
|
|
13
|
+
secondary: '#90A99A',
|
|
14
|
+
accent: '#E6A56D',
|
|
15
|
+
outline: '#141413',
|
|
16
|
+
highlight: '#F1E5C7',
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const VALID_FRAMEWORKS = new Set(['next', 'vite', 'astro', 'remix', 'react-router', 'react']);
|
|
20
|
+
const SLUG_RE = /^[a-z0-9-]+$/;
|
|
21
|
+
|
|
22
|
+
export function toPascalCase(slug) {
|
|
23
|
+
return String(slug)
|
|
24
|
+
.split(/[^a-zA-Z0-9]+/)
|
|
25
|
+
.filter(Boolean)
|
|
26
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
27
|
+
.join('');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function detectPackageManager(cwd) {
|
|
31
|
+
if (fs.existsSync(path.join(cwd, 'pnpm-lock.yaml'))) return 'pnpm';
|
|
32
|
+
if (fs.existsSync(path.join(cwd, 'yarn.lock'))) return 'yarn';
|
|
33
|
+
if (
|
|
34
|
+
fs.existsSync(path.join(cwd, 'bun.lock')) ||
|
|
35
|
+
fs.existsSync(path.join(cwd, 'bun.lockb'))
|
|
36
|
+
) {
|
|
37
|
+
return 'bun';
|
|
38
|
+
}
|
|
39
|
+
if (fs.existsSync(path.join(cwd, 'package-lock.json'))) return 'npm';
|
|
40
|
+
|
|
41
|
+
const ua = process.env.npm_config_user_agent || '';
|
|
42
|
+
if (ua.startsWith('pnpm/')) return 'pnpm';
|
|
43
|
+
if (ua.startsWith('yarn/')) return 'yarn';
|
|
44
|
+
if (ua.startsWith('bun/')) return 'bun';
|
|
45
|
+
return 'npm';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function detectFramework(cwd, override) {
|
|
49
|
+
if (override) {
|
|
50
|
+
const normalized = String(override).trim().toLowerCase();
|
|
51
|
+
if (VALID_FRAMEWORKS.has(normalized)) return normalized;
|
|
52
|
+
}
|
|
53
|
+
const pkgPath = path.join(cwd, 'package.json');
|
|
54
|
+
if (fs.existsSync(pkgPath)) {
|
|
55
|
+
try {
|
|
56
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
57
|
+
const deps = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
58
|
+
if (deps.next) return 'next';
|
|
59
|
+
if (deps['@react-router/dev'] || deps['react-router']) return 'react-router';
|
|
60
|
+
if (deps['@remix-run/react']) return 'remix';
|
|
61
|
+
if (deps.astro) return 'astro';
|
|
62
|
+
if (deps.vite) return 'vite';
|
|
63
|
+
} catch {
|
|
64
|
+
// Ignore malformed package.json and use generic React fallback
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return 'react';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function detectCssFile(cwd, framework) {
|
|
71
|
+
const candidates = [
|
|
72
|
+
'src/app/globals.css',
|
|
73
|
+
'app/globals.css',
|
|
74
|
+
'src/index.css',
|
|
75
|
+
'src/App.css',
|
|
76
|
+
'src/styles/globals.css',
|
|
77
|
+
'src/styles/global.css',
|
|
78
|
+
'styles/globals.css',
|
|
79
|
+
];
|
|
80
|
+
for (const rel of candidates) {
|
|
81
|
+
if (fs.existsSync(path.join(cwd, rel))) {
|
|
82
|
+
return rel;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (framework === 'next') {
|
|
86
|
+
return fs.existsSync(path.join(cwd, 'src')) ? 'src/app/globals.css' : 'app/globals.css';
|
|
87
|
+
}
|
|
88
|
+
return fs.existsSync(path.join(cwd, 'src')) ? 'src/index.css' : 'styles/globals.css';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function detectComponentsDir(cwd, override) {
|
|
92
|
+
if (override) return override;
|
|
93
|
+
const cfgPath = path.join(cwd, 'linework.json');
|
|
94
|
+
if (fs.existsSync(cfgPath)) {
|
|
95
|
+
try {
|
|
96
|
+
const cfg = JSON.parse(fs.readFileSync(cfgPath, 'utf8'));
|
|
97
|
+
if (cfg.componentsDir && typeof cfg.componentsDir === 'string') {
|
|
98
|
+
return cfg.componentsDir;
|
|
99
|
+
}
|
|
100
|
+
} catch {
|
|
101
|
+
// Fallback cleanly to heuristic
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return fs.existsSync(path.join(cwd, 'src')) ? 'src/components/linework' : 'components/linework';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function buildTokenCssBlock(tokens = DEFAULT_TOKENS) {
|
|
108
|
+
return [
|
|
109
|
+
TOKEN_BLOCK_START,
|
|
110
|
+
':root {',
|
|
111
|
+
` --rdev-primary: ${tokens.primary};`,
|
|
112
|
+
` --rdev-secondary: ${tokens.secondary};`,
|
|
113
|
+
` --rdev-accent: ${tokens.accent};`,
|
|
114
|
+
` --rdev-outline: ${tokens.outline};`,
|
|
115
|
+
` --rdev-highlight: ${tokens.highlight};`,
|
|
116
|
+
'}',
|
|
117
|
+
'',
|
|
118
|
+
'.dark {',
|
|
119
|
+
' --rdev-primary: #52B788;',
|
|
120
|
+
' --rdev-secondary: #74C69D;',
|
|
121
|
+
' --rdev-accent: #F4A261;',
|
|
122
|
+
' --rdev-outline: #E8E6DF;',
|
|
123
|
+
' --rdev-highlight: #1B2622;',
|
|
124
|
+
'}',
|
|
125
|
+
TOKEN_BLOCK_END,
|
|
126
|
+
].join('\n');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function upsertTokenCss(fullCssPath, tokens = DEFAULT_TOKENS) {
|
|
130
|
+
fs.mkdirSync(path.dirname(fullCssPath), { recursive: true });
|
|
131
|
+
const existing = fs.existsSync(fullCssPath) ? fs.readFileSync(fullCssPath, 'utf8') : '';
|
|
132
|
+
const block = buildTokenCssBlock(tokens);
|
|
133
|
+
|
|
134
|
+
if (existing.includes(TOKEN_BLOCK_START) && existing.includes(TOKEN_BLOCK_END)) {
|
|
135
|
+
const startIdx = existing.indexOf(TOKEN_BLOCK_START);
|
|
136
|
+
const endIdx = existing.indexOf(TOKEN_BLOCK_END) + TOKEN_BLOCK_END.length;
|
|
137
|
+
const updated = `${existing.slice(0, startIdx)}${block}${existing.slice(endIdx)}`;
|
|
138
|
+
fs.writeFileSync(fullCssPath, updated, 'utf8');
|
|
139
|
+
return 'updated';
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (existing.includes('--rdev-primary')) {
|
|
143
|
+
return 'skipped';
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const separator = existing && !existing.endsWith('\n') ? '\n\n' : existing ? '\n' : '';
|
|
147
|
+
fs.writeFileSync(fullCssPath, `${existing}${separator}${block}\n`, 'utf8');
|
|
148
|
+
return 'added';
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function resolveRegistryUrl(cwd, override) {
|
|
152
|
+
const candidate = override || readConfig(cwd)?.registry || DEFAULT_REGISTRY_URL;
|
|
153
|
+
let parsed;
|
|
154
|
+
try {
|
|
155
|
+
parsed = new URL(candidate);
|
|
156
|
+
} catch {
|
|
157
|
+
throw new Error(`Invalid registry URL: ${candidate}`);
|
|
158
|
+
}
|
|
159
|
+
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') {
|
|
160
|
+
throw new Error(`Unsupported registry protocol: ${parsed.protocol}`);
|
|
161
|
+
}
|
|
162
|
+
return parsed.origin;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function readConfig(cwd) {
|
|
166
|
+
const cfgPath = path.join(cwd, 'linework.json');
|
|
167
|
+
if (!fs.existsSync(cfgPath)) return null;
|
|
168
|
+
try {
|
|
169
|
+
return JSON.parse(fs.readFileSync(cfgPath, 'utf8'));
|
|
170
|
+
} catch {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function parseArgs(argv) {
|
|
176
|
+
const positionals = [];
|
|
177
|
+
const flags = {};
|
|
178
|
+
|
|
179
|
+
for (let i = 0; i < argv.length; i++) {
|
|
180
|
+
const arg = argv[i];
|
|
181
|
+
if (arg === '--') {
|
|
182
|
+
positionals.push(...argv.slice(i + 1));
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
if (arg.startsWith('--')) {
|
|
186
|
+
const eqIdx = arg.indexOf('=');
|
|
187
|
+
if (eqIdx !== -1) {
|
|
188
|
+
flags[arg.slice(2, eqIdx)] = arg.slice(eqIdx + 1);
|
|
189
|
+
} else {
|
|
190
|
+
const key = arg.slice(2);
|
|
191
|
+
const next = argv[i + 1];
|
|
192
|
+
if (next && !next.startsWith('-')) {
|
|
193
|
+
flags[key] = next;
|
|
194
|
+
i += 1;
|
|
195
|
+
} else {
|
|
196
|
+
flags[key] = true;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
} else if (arg.startsWith('-') && arg.length === 2) {
|
|
200
|
+
const key = arg.slice(1);
|
|
201
|
+
const next = argv[i + 1];
|
|
202
|
+
if (next && !next.startsWith('-')) {
|
|
203
|
+
flags[key] = next;
|
|
204
|
+
i += 1;
|
|
205
|
+
} else {
|
|
206
|
+
flags[key] = true;
|
|
207
|
+
}
|
|
208
|
+
} else {
|
|
209
|
+
positionals.push(arg);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return { positionals, flags };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function buildInstallArgs(pm, packageSpecs) {
|
|
217
|
+
if (pm === 'npm') return ['install', ...packageSpecs];
|
|
218
|
+
return ['add', ...packageSpecs];
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function defaultRunCommand(pm, args, cwd) {
|
|
222
|
+
const res = spawnSync(pm, args, { cwd, stdio: 'inherit', shell: process.platform === 'win32' });
|
|
223
|
+
return { status: res.status ?? 1 };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function formatHelp() {
|
|
227
|
+
return [
|
|
228
|
+
`linework-cli v${CLI_VERSION} — Semantic SVG icons & illustrations for React`,
|
|
229
|
+
'',
|
|
230
|
+
'Usage:',
|
|
231
|
+
' linework-cli init [-t next|vite|astro|remix|react-router|react] [--dir <path>] [--install] [--no-css] [-y]',
|
|
232
|
+
' linework-cli add <slug...> [-o <dir>] [--size <px>] [--overwrite] [--all-illustrations]',
|
|
233
|
+
' linework-cli search <query> [--type icon|illustration] [--limit <n>]',
|
|
234
|
+
' linework-cli sync',
|
|
235
|
+
'',
|
|
236
|
+
'Options:',
|
|
237
|
+
' -t, --template <framework> Framework preset (next, vite, astro, remix, react-router, react)',
|
|
238
|
+
' -d, --dir <path> Target directory for added components (default: components/linework)',
|
|
239
|
+
' --install Install live @rdev/icons-react & @rdev/illustrations-react .tgz packages',
|
|
240
|
+
' --no-css Skip writing --rdev-* CSS custom properties',
|
|
241
|
+
' --size <px> Default icon/illustration width/height for added TSX (default: 48)',
|
|
242
|
+
' --overwrite Overwrite existing local .tsx component files',
|
|
243
|
+
' -h, --help Show help',
|
|
244
|
+
' -v, --version Show version',
|
|
245
|
+
'',
|
|
246
|
+
'Examples:',
|
|
247
|
+
' pnpm dlx linework-cli@latest init -t next',
|
|
248
|
+
' pnpm dlx linework-cli@latest add friendly-ghost shield-check',
|
|
249
|
+
' pnpm dlx linework-cli@latest search astronaut --type illustration',
|
|
250
|
+
].join('\n');
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export async function runCli(argv, deps = {}) {
|
|
254
|
+
const cwd = deps.cwd || process.cwd();
|
|
255
|
+
const fetchFn = deps.fetch || globalThis.fetch;
|
|
256
|
+
const runCommand = deps.runCommand || defaultRunCommand;
|
|
257
|
+
const log = deps.log || console.log;
|
|
258
|
+
const errLog = deps.errLog || console.error;
|
|
259
|
+
|
|
260
|
+
const { positionals, flags } = parseArgs(argv);
|
|
261
|
+
const cmd = positionals[0];
|
|
262
|
+
|
|
263
|
+
if (!cmd || flags.h || flags.help || cmd === 'help') {
|
|
264
|
+
log(formatHelp());
|
|
265
|
+
return 0;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (flags.v || flags.version || cmd === 'version') {
|
|
269
|
+
log(CLI_VERSION);
|
|
270
|
+
return 0;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
let registry;
|
|
274
|
+
try {
|
|
275
|
+
registry = resolveRegistryUrl(cwd, typeof flags.registry === 'string' ? flags.registry : undefined);
|
|
276
|
+
} catch (err) {
|
|
277
|
+
errLog(err instanceof Error ? err.message : String(err));
|
|
278
|
+
return 1;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (cmd === 'init') {
|
|
282
|
+
const framework = detectFramework(cwd, flags.t || flags.template);
|
|
283
|
+
const pm = detectPackageManager(cwd);
|
|
284
|
+
const cssRel = typeof flags.css === 'string' ? flags.css : detectCssFile(cwd, framework);
|
|
285
|
+
const componentsDir = detectComponentsDir(cwd, typeof (flags.d || flags.dir) === 'string' ? (flags.d || flags.dir) : undefined);
|
|
286
|
+
|
|
287
|
+
const cfgPath = path.join(cwd, 'linework.json');
|
|
288
|
+
const cfg = {
|
|
289
|
+
framework,
|
|
290
|
+
css: cssRel,
|
|
291
|
+
componentsDir,
|
|
292
|
+
registry,
|
|
293
|
+
tokens: DEFAULT_TOKENS,
|
|
294
|
+
};
|
|
295
|
+
fs.writeFileSync(cfgPath, `${JSON.stringify(cfg, null, 2)}\n`, 'utf8');
|
|
296
|
+
log(`✔ Created linework.json (framework: ${framework}, components: ${componentsDir})`);
|
|
297
|
+
|
|
298
|
+
if (!flags['no-css']) {
|
|
299
|
+
const cssFullPath = path.resolve(cwd, cssRel);
|
|
300
|
+
const cssStatus = upsertTokenCss(cssFullPath, DEFAULT_TOKENS);
|
|
301
|
+
if (cssStatus === 'added') {
|
|
302
|
+
log(`✔ Added 5 semantic --rdev-* tokens to ${cssRel}`);
|
|
303
|
+
} else if (cssStatus === 'updated') {
|
|
304
|
+
log(`✔ Updated Linework semantic tokens in ${cssRel}`);
|
|
305
|
+
} else {
|
|
306
|
+
log(`• Semantic tokens already configured in ${cssRel}`);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (flags.install || flags.packages) {
|
|
311
|
+
const specs = [
|
|
312
|
+
`${registry}/api/packages/icons-react.tgz`,
|
|
313
|
+
`${registry}/api/packages/illustrations-react.tgz`,
|
|
314
|
+
];
|
|
315
|
+
const args = buildInstallArgs(pm, specs);
|
|
316
|
+
log(`Installing @rdev/icons-react and @rdev/illustrations-react via ${pm}...`);
|
|
317
|
+
const res = runCommand(pm, args, cwd);
|
|
318
|
+
if (res.status !== 0) {
|
|
319
|
+
errLog(`Package installation failed with exit code ${res.status}.`);
|
|
320
|
+
return res.status;
|
|
321
|
+
}
|
|
322
|
+
log('✔ Packages installed.');
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
log('');
|
|
326
|
+
log('Next steps:');
|
|
327
|
+
const dlxPrefix = pm === 'npm' ? 'npx' : pm === 'bun' ? 'bunx --bun' : `${pm} dlx`;
|
|
328
|
+
log(` ${dlxPrefix} linework-cli@latest add friendly-ghost shield-check`);
|
|
329
|
+
return 0;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (cmd === 'add') {
|
|
333
|
+
let slugs = positionals.slice(1).map((s) => s.trim().toLowerCase()).filter(Boolean);
|
|
334
|
+
const outDirRel =
|
|
335
|
+
typeof (flags.o || flags.dir || flags.d) === 'string'
|
|
336
|
+
? String(flags.o || flags.dir || flags.d)
|
|
337
|
+
: detectComponentsDir(cwd);
|
|
338
|
+
const outDirAbs = path.resolve(cwd, outDirRel);
|
|
339
|
+
const overwrite = Boolean(flags.overwrite);
|
|
340
|
+
const requestedSize = flags.size ? Number(flags.size) : undefined;
|
|
341
|
+
|
|
342
|
+
if (flags['all-illustrations']) {
|
|
343
|
+
const res = await fetchFn(`${registry}/api/agent/search?type=illustration&access=free&limit=100`);
|
|
344
|
+
if (!res.ok) {
|
|
345
|
+
errLog(`Failed to fetch illustration list (${res.status}).`);
|
|
346
|
+
return 1;
|
|
347
|
+
}
|
|
348
|
+
const data = await res.json();
|
|
349
|
+
const rawList = Array.isArray(data.results) ? data.results : Array.isArray(data.assets) ? data.assets : [];
|
|
350
|
+
const fetchedSlugs = rawList.map((a) => a.assetId || a.slug).filter(Boolean);
|
|
351
|
+
slugs = Array.from(new Set([...slugs, ...fetchedSlugs]));
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (slugs.length === 0) {
|
|
355
|
+
errLog('Specify at least one asset slug, e.g.: linework-cli add friendly-ghost shield-check');
|
|
356
|
+
return 1;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
fs.mkdirSync(outDirAbs, { recursive: true });
|
|
360
|
+
let failures = 0;
|
|
361
|
+
|
|
362
|
+
for (const slug of slugs) {
|
|
363
|
+
if (!SLUG_RE.test(slug)) {
|
|
364
|
+
errLog(`✖ Invalid slug "${slug}" (expected kebab-case).`);
|
|
365
|
+
failures += 1;
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const defaultSize = Number.isFinite(requestedSize) && requestedSize > 0 ? requestedSize : undefined;
|
|
370
|
+
const url = defaultSize
|
|
371
|
+
? `${registry}/api/agent/code/${slug}?format=json&size=${defaultSize}`
|
|
372
|
+
: `${registry}/api/agent/code/${slug}?format=json`;
|
|
373
|
+
const res = await fetchFn(url);
|
|
374
|
+
|
|
375
|
+
if (!res.ok) {
|
|
376
|
+
errLog(`✖ Asset "${slug}" not found on registry (${res.status}).`);
|
|
377
|
+
const searchRes = await fetchFn(
|
|
378
|
+
`${registry}/api/agent/search?q=${encodeURIComponent(slug)}&limit=3`,
|
|
379
|
+
).catch(() => null);
|
|
380
|
+
if (searchRes?.ok) {
|
|
381
|
+
const searchData = await searchRes.json();
|
|
382
|
+
const hits = Array.isArray(searchData.results) ? searchData.results : searchData.assets;
|
|
383
|
+
if (Array.isArray(hits) && hits.length > 0) {
|
|
384
|
+
errLog(
|
|
385
|
+
` Did you mean: ${hits.map((a) => a.assetId || a.slug).join(', ')}`,
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
failures += 1;
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const payload = await res.json();
|
|
394
|
+
const tsxSource = payload.reactInlineComponent || payload.tsx;
|
|
395
|
+
if (payload.access === 'premium' || !tsxSource) {
|
|
396
|
+
errLog(`✖ "${slug}" requires a commercial Asset Kit (${payload.marketplaceUrl || registry}).`);
|
|
397
|
+
failures += 1;
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const componentName = payload.componentName || toPascalCase(slug);
|
|
402
|
+
const safeFileName = `${path.basename(componentName)}.tsx`;
|
|
403
|
+
const targetFile = path.join(outDirAbs, safeFileName);
|
|
404
|
+
|
|
405
|
+
if (fs.existsSync(targetFile) && !overwrite) {
|
|
406
|
+
log(`• Skipped ${path.relative(cwd, targetFile)} (already exists; pass --overwrite to replace)`);
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
fs.writeFileSync(targetFile, `${tsxSource.trimEnd()}\n`, 'utf8');
|
|
411
|
+
log(`✔ Added ${path.relative(cwd, targetFile)} (<${componentName} />)`);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
return failures > 0 ? 1 : 0;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (cmd === 'search') {
|
|
418
|
+
const query = positionals.slice(1).join(' ').trim();
|
|
419
|
+
const limit = flags.limit ? Math.max(1, Math.min(50, Number(flags.limit) || 15)) : 15;
|
|
420
|
+
const type = typeof flags.type === 'string' ? flags.type : '';
|
|
421
|
+
const params = new URLSearchParams();
|
|
422
|
+
if (query) params.set('q', query);
|
|
423
|
+
if (type === 'icon' || type === 'illustration') params.set('type', type);
|
|
424
|
+
params.set('limit', String(limit));
|
|
425
|
+
|
|
426
|
+
const res = await fetchFn(`${registry}/api/agent/search?${params.toString()}`);
|
|
427
|
+
if (!res.ok) {
|
|
428
|
+
errLog(`Search failed (${res.status}).`);
|
|
429
|
+
return 1;
|
|
430
|
+
}
|
|
431
|
+
const data = await res.json();
|
|
432
|
+
const items = Array.isArray(data.results) ? data.results : Array.isArray(data.assets) ? data.assets : [];
|
|
433
|
+
if (items.length === 0) {
|
|
434
|
+
log(`No assets matched "${query}".`);
|
|
435
|
+
return 0;
|
|
436
|
+
}
|
|
437
|
+
for (const item of items) {
|
|
438
|
+
const id = item.assetId || item.slug;
|
|
439
|
+
log(
|
|
440
|
+
`${String(id).padEnd(28)} <${item.componentName} /> [${item.assetType}, ${item.access}] (${item.collectionSlug})`,
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
return 0;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
if (cmd === 'sync' || cmd === 'update') {
|
|
447
|
+
const pm = detectPackageManager(cwd);
|
|
448
|
+
const pkgPath = path.join(cwd, 'package.json');
|
|
449
|
+
let deps = {};
|
|
450
|
+
if (fs.existsSync(pkgPath)) {
|
|
451
|
+
try {
|
|
452
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
453
|
+
deps = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
454
|
+
} catch {
|
|
455
|
+
// fallback below
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const targets = [];
|
|
460
|
+
if (deps['@rdev/icons-react']) targets.push(`${registry}/api/packages/icons-react.tgz`);
|
|
461
|
+
if (deps['@rdev/illustrations-react']) targets.push(`${registry}/api/packages/illustrations-react.tgz`);
|
|
462
|
+
if (targets.length === 0) {
|
|
463
|
+
targets.push(
|
|
464
|
+
`${registry}/api/packages/icons-react.tgz`,
|
|
465
|
+
`${registry}/api/packages/illustrations-react.tgz`,
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const args = buildInstallArgs(pm, targets);
|
|
470
|
+
log(`Syncing Linework React packages via ${pm}...`);
|
|
471
|
+
const res = runCommand(pm, args, cwd);
|
|
472
|
+
if (res.status !== 0) {
|
|
473
|
+
errLog(`Sync failed with exit code ${res.status}.`);
|
|
474
|
+
return res.status;
|
|
475
|
+
}
|
|
476
|
+
log('✔ Synced latest icons & illustrations into node_modules.');
|
|
477
|
+
return 0;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
errLog(`Unknown command "${cmd}". Run "linework-cli --help" for usage.`);
|
|
481
|
+
return 1;
|
|
482
|
+
}
|