@astralkit/cli 4.0.0 → 4.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/package.json +2 -2
- package/src/commands/add.js +69 -111
- package/src/commands/init.js +47 -76
- package/src/lib/config.js +65 -94
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astralkit/cli",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "AstralKit CLI — Install and manage UI components in your project",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"registry": "https://registry.npmjs.org"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
|
-
"prepublishOnly": "node -e \"console.log('v' +
|
|
32
|
+
"prepublishOnly": "node -e \"import('./package.json', { with: { type: 'json' } }).then(m => console.log('v' + m.default.version))\""
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@clack/prompts": "^0.9.1",
|
package/src/commands/add.js
CHANGED
|
@@ -5,16 +5,16 @@ import * as p from '@clack/prompts';
|
|
|
5
5
|
import { getComponent, getBooster, getWorkflow } from '../lib/api.js';
|
|
6
6
|
import {
|
|
7
7
|
detectFramework,
|
|
8
|
-
|
|
8
|
+
detectTypeScript,
|
|
9
9
|
isAstralkitInstalled,
|
|
10
10
|
findCssEntryFile,
|
|
11
11
|
getMissingCssImports,
|
|
12
12
|
} from '../lib/config.js';
|
|
13
13
|
import { validateSlug, validateFramework, isSafePath, writeFiles } from '../lib/validators.js';
|
|
14
|
-
import { detectPackageManager,
|
|
14
|
+
import { detectPackageManager, getRunCommand, runInstall } from '../lib/pkg-manager.js';
|
|
15
15
|
import { saveApiKey, getApiKey, getAuth } from '../lib/auth.js';
|
|
16
16
|
|
|
17
|
-
const FRAMEWORK_LABELS = { nextjs: 'Next.js', react: 'React'
|
|
17
|
+
const FRAMEWORK_LABELS = { nextjs: 'Next.js', react: 'React' };
|
|
18
18
|
|
|
19
19
|
export async function addCommand(nameOrSlug, options) {
|
|
20
20
|
if (options.apiKey) {
|
|
@@ -25,7 +25,6 @@ export async function addCommand(nameOrSlug, options) {
|
|
|
25
25
|
saveApiKey(options.apiKey);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
// Auth check
|
|
29
28
|
if (!getApiKey()) {
|
|
30
29
|
const auth = getAuth();
|
|
31
30
|
if (!auth.token) {
|
|
@@ -37,88 +36,27 @@ export async function addCommand(nameOrSlug, options) {
|
|
|
37
36
|
|
|
38
37
|
if (options.booster) return addBooster(nameOrSlug, options);
|
|
39
38
|
if (options.workflow) return addWorkflow(nameOrSlug, options);
|
|
40
|
-
|
|
41
|
-
const type = await resolveItemType(nameOrSlug, options);
|
|
42
|
-
if (type === 'booster') return addBooster(nameOrSlug, options);
|
|
43
|
-
if (type === 'workflow') return addWorkflow(nameOrSlug, options);
|
|
44
39
|
return addComponent(nameOrSlug, options);
|
|
45
40
|
}
|
|
46
41
|
|
|
47
|
-
async function resolveItemType(slug, options) {
|
|
48
|
-
const ora = (await import('ora')).default;
|
|
49
|
-
const spinner = ora('Looking up ' + pc.cyan(slug) + '...').start();
|
|
50
|
-
|
|
51
|
-
const checks = await Promise.allSettled([
|
|
52
|
-
tryFetch('component', slug, options),
|
|
53
|
-
tryFetch('booster', slug, options),
|
|
54
|
-
tryFetch('workflow', slug, options),
|
|
55
|
-
]);
|
|
56
|
-
|
|
57
|
-
const found = ['component', 'booster', 'workflow'].filter((_, i) =>
|
|
58
|
-
checks[i].status === 'fulfilled' && checks[i].value
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
spinner.stop();
|
|
62
|
-
|
|
63
|
-
if (found.length === 1) return found[0];
|
|
64
|
-
|
|
65
|
-
if (found.length > 1) {
|
|
66
|
-
const type = await p.select({
|
|
67
|
-
message: `"${slug}" matches multiple items. What do you want to install?`,
|
|
68
|
-
options: found.map(t => ({
|
|
69
|
-
label: t === 'component' ? 'Component (UI element)'
|
|
70
|
-
: t === 'booster' ? 'Booster (feature module)'
|
|
71
|
-
: 'Workflow (complete app)',
|
|
72
|
-
value: t,
|
|
73
|
-
})),
|
|
74
|
-
});
|
|
75
|
-
if (p.isCancel(type)) { p.cancel('Cancelled.'); process.exit(0); }
|
|
76
|
-
return type;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Default to component — no prompt needed
|
|
80
|
-
return 'component';
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async function tryFetch(type, slug, options) {
|
|
84
|
-
try {
|
|
85
|
-
const framework = options.framework || detectFramework();
|
|
86
|
-
if (type === 'component') await getComponent(slug, framework);
|
|
87
|
-
else if (type === 'booster') await getBooster(slug, framework);
|
|
88
|
-
else await getWorkflow(slug, framework);
|
|
89
|
-
return true;
|
|
90
|
-
} catch (err) {
|
|
91
|
-
if (err.code === 'PRO_REQUIRED') return true;
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Resolve framework for add commands.
|
|
98
|
-
* Auto-detects from project files. TypeScript detection for correct extensions.
|
|
99
|
-
*/
|
|
100
42
|
function resolveFramework(options) {
|
|
101
43
|
if (options.framework) {
|
|
102
44
|
if (!validateFramework(options.framework)) process.exit(1);
|
|
103
45
|
return options.framework;
|
|
104
46
|
}
|
|
105
|
-
|
|
47
|
+
const detected = detectFramework();
|
|
48
|
+
if (detected === 'nextjs' || detected === 'react') return detected;
|
|
49
|
+
return 'react';
|
|
106
50
|
}
|
|
107
51
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
*/
|
|
111
|
-
function getFileExtension(framework) {
|
|
52
|
+
function resolveExtension(framework) {
|
|
53
|
+
const isTS = detectTypeScript();
|
|
112
54
|
if (framework === 'nextjs') return '.tsx';
|
|
113
|
-
|
|
114
|
-
return '.html';
|
|
55
|
+
return isTS ? '.tsx' : '.jsx';
|
|
115
56
|
}
|
|
116
57
|
|
|
117
|
-
// ─── Add Component ──────────────────────────────────
|
|
118
|
-
|
|
119
58
|
async function addComponent(componentSlug, options) {
|
|
120
59
|
if (!validateSlug(componentSlug)) process.exit(1);
|
|
121
|
-
|
|
122
60
|
if (!fs.existsSync(path.join(process.cwd(), 'package.json'))) {
|
|
123
61
|
p.log.warn('No package.json found. Run this inside a project directory.');
|
|
124
62
|
return;
|
|
@@ -126,9 +64,8 @@ async function addComponent(componentSlug, options) {
|
|
|
126
64
|
|
|
127
65
|
const ora = (await import('ora')).default;
|
|
128
66
|
const framework = resolveFramework(options);
|
|
129
|
-
const
|
|
130
|
-
const componentsDir = options.dir ||
|
|
131
|
-
const ext = getFileExtension(framework);
|
|
67
|
+
const ext = resolveExtension(framework);
|
|
68
|
+
const componentsDir = options.dir || 'src/components/ui';
|
|
132
69
|
const targetFile = path.resolve(process.cwd(), componentsDir, `${componentSlug}${ext}`);
|
|
133
70
|
|
|
134
71
|
if (fs.existsSync(targetFile) && !options.overwrite) {
|
|
@@ -137,23 +74,10 @@ async function addComponent(componentSlug, options) {
|
|
|
137
74
|
return;
|
|
138
75
|
}
|
|
139
76
|
|
|
140
|
-
const spinner = ora(`Fetching ${pc.cyan(componentSlug)} (${FRAMEWORK_LABELS[framework]
|
|
77
|
+
const spinner = ora(`Fetching ${pc.cyan(componentSlug)} (${FRAMEWORK_LABELS[framework]})...`).start();
|
|
141
78
|
|
|
142
79
|
try {
|
|
143
|
-
|
|
144
|
-
let actualFramework = framework;
|
|
145
|
-
|
|
146
|
-
try {
|
|
147
|
-
data = await getComponent(componentSlug, framework);
|
|
148
|
-
} catch (fetchErr) {
|
|
149
|
-
// Pro required — tell user to upgrade, no HTML fallback
|
|
150
|
-
if (fetchErr.code === 'PRO_REQUIRED') {
|
|
151
|
-
spinner.fail(`${FRAMEWORK_LABELS[framework] || framework} requires a Pro subscription.`);
|
|
152
|
-
console.log(pc.gray(' Upgrade at ') + pc.cyan('https://astralkit.com/pricing'));
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
throw fetchErr;
|
|
156
|
-
}
|
|
80
|
+
const data = await getComponent(componentSlug, framework);
|
|
157
81
|
|
|
158
82
|
if (!data.files?.length) {
|
|
159
83
|
spinner.fail(`No ${framework} code available for "${componentSlug}".`);
|
|
@@ -163,10 +87,9 @@ async function addComponent(componentSlug, options) {
|
|
|
163
87
|
return;
|
|
164
88
|
}
|
|
165
89
|
|
|
166
|
-
// Dry run
|
|
167
90
|
if (options.dryRun) {
|
|
168
91
|
spinner.stop();
|
|
169
|
-
console.log(pc.bold('\
|
|
92
|
+
console.log(pc.bold('\nDry run — no files will be written:\n'));
|
|
170
93
|
for (const file of data.files) {
|
|
171
94
|
const exists = fs.existsSync(path.resolve(process.cwd(), componentsDir, file.path));
|
|
172
95
|
console.log(` ${exists ? pc.yellow('overwrite') : pc.green('+ create')} ${componentsDir}/${file.path}`);
|
|
@@ -187,11 +110,10 @@ async function addComponent(componentSlug, options) {
|
|
|
187
110
|
fs.writeFileSync(filePath, file.content);
|
|
188
111
|
}
|
|
189
112
|
|
|
190
|
-
spinner.succeed(`Added ${pc.cyan(data.name || componentSlug)} (${FRAMEWORK_LABELS[
|
|
113
|
+
spinner.succeed(`Added ${pc.cyan(data.name || componentSlug)} (${FRAMEWORK_LABELS[framework]})`);
|
|
191
114
|
console.log(pc.gray(` ${componentsDir}/`));
|
|
192
115
|
for (const file of data.files) console.log(pc.gray(` ${file.path}`));
|
|
193
116
|
|
|
194
|
-
// Post-install hints
|
|
195
117
|
if (!isAstralkitInstalled()) {
|
|
196
118
|
p.log.warn('Run ' + pc.cyan('astralkit init') + ' to set up design tokens.');
|
|
197
119
|
}
|
|
@@ -204,9 +126,15 @@ async function addComponent(componentSlug, options) {
|
|
|
204
126
|
}
|
|
205
127
|
}
|
|
206
128
|
|
|
207
|
-
if (data.
|
|
208
|
-
|
|
209
|
-
|
|
129
|
+
if (data.dependencies?.length) {
|
|
130
|
+
const pm = detectPackageManager();
|
|
131
|
+
const depList = data.dependencies.filter(d => d !== 'astralkit' && d !== 'clsx' && d !== 'tailwind-merge');
|
|
132
|
+
if (depList.length > 0) {
|
|
133
|
+
const s = ora(`Installing ${depList.join(', ')}...`).start();
|
|
134
|
+
const result = await runInstall(pm, process.cwd(), depList);
|
|
135
|
+
if (result.success) s.succeed(`Installed ${depList.join(', ')}`);
|
|
136
|
+
else { s.fail(result.error); if (result.hint) console.log(pc.gray(` ${result.hint}`)); }
|
|
137
|
+
}
|
|
210
138
|
}
|
|
211
139
|
|
|
212
140
|
} catch (err) {
|
|
@@ -215,8 +143,6 @@ async function addComponent(componentSlug, options) {
|
|
|
215
143
|
}
|
|
216
144
|
}
|
|
217
145
|
|
|
218
|
-
// ─── Add Booster ────────────────────────────────────
|
|
219
|
-
|
|
220
146
|
async function addBooster(slug, options) {
|
|
221
147
|
if (!validateSlug(slug, 'booster name')) process.exit(1);
|
|
222
148
|
|
|
@@ -242,16 +168,16 @@ async function addBooster(slug, options) {
|
|
|
242
168
|
|
|
243
169
|
if (options.dryRun) {
|
|
244
170
|
spinner.stop();
|
|
245
|
-
console.log(pc.bold('\
|
|
171
|
+
console.log(pc.bold('\nDry run — no files will be written:\n'));
|
|
246
172
|
if (coreFiles.length > 0) {
|
|
247
|
-
console.log(pc.gray(` Core
|
|
173
|
+
console.log(pc.gray(` Core: ${corePath}/`));
|
|
248
174
|
for (const f of coreFiles) {
|
|
249
175
|
const exists = fs.existsSync(path.resolve(coreDir, f.path));
|
|
250
176
|
console.log(` ${exists ? pc.yellow('overwrite') : pc.green('+ create')} ${f.path}`);
|
|
251
177
|
}
|
|
252
178
|
}
|
|
253
179
|
if (adapterFiles.length > 0) {
|
|
254
|
-
console.log(pc.gray(` Adapter
|
|
180
|
+
console.log(pc.gray(` Adapter: ${adapterPath}/`));
|
|
255
181
|
for (const f of adapterFiles) {
|
|
256
182
|
const exists = fs.existsSync(path.resolve(adapterDir, f.path));
|
|
257
183
|
console.log(` ${exists ? pc.yellow('overwrite') : pc.green('+ create')} ${f.path}`);
|
|
@@ -260,17 +186,31 @@ async function addBooster(slug, options) {
|
|
|
260
186
|
return;
|
|
261
187
|
}
|
|
262
188
|
|
|
189
|
+
if (!options.overwrite) {
|
|
190
|
+
const conflicts = [
|
|
191
|
+
...coreFiles.filter(f => fs.existsSync(path.resolve(coreDir, f.path))).map(f => `${corePath}/${f.path}`),
|
|
192
|
+
...adapterFiles.filter(f => fs.existsSync(path.resolve(adapterDir, f.path))).map(f => `${adapterPath}/${f.path}`),
|
|
193
|
+
];
|
|
194
|
+
if (conflicts.length > 0) {
|
|
195
|
+
spinner.stop();
|
|
196
|
+
console.log(pc.yellow('\nThese files already exist:'));
|
|
197
|
+
conflicts.forEach(c => console.log(pc.gray(` ${c}`)));
|
|
198
|
+
const overwrite = await p.confirm({ message: 'Overwrite existing files?', initialValue: false });
|
|
199
|
+
if (p.isCancel(overwrite) || !overwrite) return;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
263
203
|
writeFiles(coreDir, coreFiles);
|
|
264
204
|
writeFiles(adapterDir, adapterFiles);
|
|
265
205
|
|
|
266
206
|
spinner.succeed(`Added booster ${pc.cyan(data.name || slug)} (${FRAMEWORK_LABELS[framework] || framework})`);
|
|
267
207
|
|
|
268
208
|
if (coreFiles.length > 0) {
|
|
269
|
-
console.log(pc.gray(` Core
|
|
209
|
+
console.log(pc.gray(` Core: ${corePath}/`));
|
|
270
210
|
coreFiles.forEach(f => console.log(pc.gray(` ${f.path}`)));
|
|
271
211
|
}
|
|
272
212
|
if (adapterFiles.length > 0) {
|
|
273
|
-
console.log(pc.gray(` Adapter
|
|
213
|
+
console.log(pc.gray(` Adapter: ${adapterPath}/`));
|
|
274
214
|
adapterFiles.forEach(f => console.log(pc.gray(` ${f.path}`)));
|
|
275
215
|
}
|
|
276
216
|
|
|
@@ -302,8 +242,6 @@ async function addBooster(slug, options) {
|
|
|
302
242
|
}
|
|
303
243
|
}
|
|
304
244
|
|
|
305
|
-
// ─── Add Workflow ───────────────────────────────────
|
|
306
|
-
|
|
307
245
|
async function addWorkflow(slug, options) {
|
|
308
246
|
if (!validateSlug(slug, 'workflow name')) process.exit(1);
|
|
309
247
|
|
|
@@ -342,14 +280,25 @@ async function addWorkflow(slug, options) {
|
|
|
342
280
|
dirName = newDir;
|
|
343
281
|
}
|
|
344
282
|
}
|
|
345
|
-
|
|
346
283
|
targetDir = path.resolve(process.cwd(), dirName);
|
|
347
284
|
}
|
|
348
285
|
|
|
286
|
+
if (options.overlay && !options.overwrite) {
|
|
287
|
+
const conflicts = data.files.filter(f => fs.existsSync(path.resolve(targetDir, f.path))).map(f => f.path);
|
|
288
|
+
if (conflicts.length > 0) {
|
|
289
|
+
spinner.stop();
|
|
290
|
+
console.log(pc.yellow(`${conflicts.length} file(s) would be overwritten:`));
|
|
291
|
+
conflicts.slice(0, 10).forEach(c => console.log(pc.gray(` ${c}`)));
|
|
292
|
+
if (conflicts.length > 10) console.log(pc.gray(` ... and ${conflicts.length - 10} more`));
|
|
293
|
+
const overwrite = await p.confirm({ message: 'Overwrite these files?', initialValue: false });
|
|
294
|
+
if (p.isCancel(overwrite) || !overwrite) return;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
349
298
|
if (options.dryRun) {
|
|
350
299
|
spinner.stop();
|
|
351
300
|
const label = options.overlay ? './' : `${path.basename(targetDir)}/`;
|
|
352
|
-
console.log(pc.bold(`\
|
|
301
|
+
console.log(pc.bold(`\nDry run — no files will be written to ${label}:\n`));
|
|
353
302
|
data.files.forEach(f => {
|
|
354
303
|
const exists = fs.existsSync(path.resolve(targetDir, f.path));
|
|
355
304
|
console.log(` ${exists ? pc.yellow('overwrite') : pc.green('+ create')} ${f.path}`);
|
|
@@ -361,7 +310,7 @@ async function addWorkflow(slug, options) {
|
|
|
361
310
|
const fileCount = writeFiles(targetDir, data.files);
|
|
362
311
|
const label = options.overlay ? './' : `${path.basename(targetDir)}/`;
|
|
363
312
|
|
|
364
|
-
spinner.succeed(`Scaffolded workflow ${pc.cyan(data.name || slug)}
|
|
313
|
+
spinner.succeed(`Scaffolded workflow ${pc.cyan(data.name || slug)} in ${label}`);
|
|
365
314
|
console.log(pc.gray(` ${fileCount} files written`));
|
|
366
315
|
|
|
367
316
|
if (!options.overlay) {
|
|
@@ -393,12 +342,21 @@ async function addWorkflow(slug, options) {
|
|
|
393
342
|
}
|
|
394
343
|
}
|
|
395
344
|
|
|
396
|
-
// ─── Error handler ──────────────────────────────────
|
|
397
|
-
|
|
398
345
|
function handleAddError(err, slug, framework) {
|
|
399
|
-
if (err.code === 'AUTH_REQUIRED'
|
|
346
|
+
if (err.code === 'AUTH_REQUIRED') {
|
|
400
347
|
p.log.warn(err.message);
|
|
348
|
+
} else if (err.code === 'PRO_REQUIRED') {
|
|
349
|
+
p.log.warn(`${FRAMEWORK_LABELS[framework] || framework} version requires a Pro subscription.`);
|
|
350
|
+
console.log(pc.gray(' Upgrade at ') + pc.cyan('https://astralkit.com/pricing'));
|
|
401
351
|
} else {
|
|
352
|
+
try {
|
|
353
|
+
const body = JSON.parse(err.message.replace(/^API error \d+: /, ''));
|
|
354
|
+
if (body.supported_frameworks) {
|
|
355
|
+
p.log.warn(`No ${framework} version available for "${slug}".`);
|
|
356
|
+
console.log(pc.gray(`Available: ${body.supported_frameworks.map(f => FRAMEWORK_LABELS[f] || f).join(', ')}`));
|
|
357
|
+
process.exit(1);
|
|
358
|
+
}
|
|
359
|
+
} catch { /* not json */ }
|
|
402
360
|
console.error(pc.red(err.message));
|
|
403
361
|
}
|
|
404
362
|
process.exit(1);
|
package/src/commands/init.js
CHANGED
|
@@ -5,19 +5,18 @@ import * as p from '@clack/prompts';
|
|
|
5
5
|
import {
|
|
6
6
|
detectFramework,
|
|
7
7
|
detectTailwindVersion,
|
|
8
|
-
|
|
8
|
+
detectTypeScript,
|
|
9
9
|
isAstralkitInstalled,
|
|
10
10
|
findCssEntryFile,
|
|
11
11
|
getMissingCssImports,
|
|
12
12
|
addCssImports,
|
|
13
|
+
getRequiredCssImports,
|
|
13
14
|
} from '../lib/config.js';
|
|
14
|
-
import { validateFramework } from '../lib/validators.js';
|
|
15
15
|
import { getAiRulesContent } from '../lib/ai-rules.js';
|
|
16
16
|
import { saveApiKey, getApiKey } from '../lib/auth.js';
|
|
17
17
|
import { detectPackageManager, runInstall } from '../lib/pkg-manager.js';
|
|
18
|
-
import { apiRequest } from '../lib/api.js';
|
|
19
18
|
|
|
20
|
-
const FRAMEWORK_LABELS = { nextjs: 'Next.js', react: 'React'
|
|
19
|
+
const FRAMEWORK_LABELS = { nextjs: 'Next.js', react: 'React' };
|
|
21
20
|
|
|
22
21
|
export async function initCommand(options) {
|
|
23
22
|
p.intro(pc.bgBlue(pc.white(' AstralKit Setup ')));
|
|
@@ -59,104 +58,76 @@ export async function initCommand(options) {
|
|
|
59
58
|
p.log.success('API key saved');
|
|
60
59
|
} else {
|
|
61
60
|
p.log.warn('Skipping API key — some features will be unavailable.');
|
|
62
|
-
p.log.info('Run ' + pc.cyan('astralkit init --api-key <
|
|
61
|
+
p.log.info('Run ' + pc.cyan('astralkit init --api-key <key>') + ' later to add it.');
|
|
63
62
|
}
|
|
64
63
|
} else {
|
|
65
64
|
p.log.success('API key found');
|
|
66
65
|
}
|
|
67
66
|
|
|
68
|
-
// ─── Step 2: Detect
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
let framework = options.framework || detectFramework();
|
|
74
|
-
const twVersion = detectTailwindVersion();
|
|
75
|
-
const isTS = isTypeScriptProject();
|
|
76
|
-
const hasSrc = fs.existsSync(path.join(process.cwd(), 'src'));
|
|
67
|
+
// ─── Step 2: Detect project ────────────────────────────
|
|
68
|
+
const framework = options.framework || detectFramework();
|
|
69
|
+
const tailwindVersion = detectTailwindVersion();
|
|
70
|
+
const isTS = detectTypeScript();
|
|
77
71
|
const pm = detectPackageManager();
|
|
78
72
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
73
|
+
if (framework !== 'react' && framework !== 'nextjs') {
|
|
74
|
+
p.log.warn('AstralKit CLI is for React and Next.js projects.');
|
|
75
|
+
p.log.info('For HTML, copy components from ' + pc.cyan('https://astralkit.com'));
|
|
76
|
+
p.outro('');
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
p.log.step(`Framework: ${pc.cyan(FRAMEWORK_LABELS[framework])}`);
|
|
81
|
+
if (tailwindVersion) p.log.step(`Tailwind: ${pc.cyan('v' + tailwindVersion)}`);
|
|
82
|
+
p.log.step(`TypeScript: ${pc.cyan(isTS ? 'Yes' : 'No')}`);
|
|
82
83
|
p.log.step(`Package manager: ${pc.cyan(pm)}`);
|
|
83
84
|
|
|
84
|
-
// ─── Step 3: Install astralkit
|
|
85
|
+
// ─── Step 3: Install astralkit (real npm dependency) ────
|
|
86
|
+
//
|
|
87
|
+
// `npm install astralkit` — a proper package on the public registry.
|
|
88
|
+
// Contains: design tokens (CSS), cn() utility, AI rules.
|
|
89
|
+
// Listed in package.json. Survives npm ci. Works for teammates and CI/CD.
|
|
90
|
+
|
|
85
91
|
if (!isAstralkitInstalled()) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const buffer = Buffer.from(await response.arrayBuffer());
|
|
97
|
-
fs.writeFileSync(tgzPath, buffer);
|
|
98
|
-
|
|
99
|
-
spinner.text = `Installing astralkit via ${pm}...`;
|
|
100
|
-
|
|
101
|
-
const result = await runInstall(pm, process.cwd(), [`./${tgzFilename}`]);
|
|
102
|
-
|
|
103
|
-
if (result.success) {
|
|
104
|
-
spinner.succeed('Installed astralkit design tokens');
|
|
105
|
-
p.log.info(pc.gray(`Commit ${tgzFilename} to git so teammates get it on npm install`));
|
|
106
|
-
} else {
|
|
107
|
-
spinner.fail('Failed to install astralkit.');
|
|
108
|
-
console.log(pc.gray(' ' + (result.hint || result.error)));
|
|
109
|
-
}
|
|
110
|
-
} catch (err) {
|
|
111
|
-
spinner.fail('Could not download design tokens.');
|
|
112
|
-
if (err.code === 'AUTH_REQUIRED') {
|
|
113
|
-
p.log.info('Add your API key: ' + pc.cyan('astralkit init --api-key <key>'));
|
|
114
|
-
} else if (err.code === 'NETWORK_ERROR' || err.code === 'TIMEOUT') {
|
|
115
|
-
p.log.info('Check your connection and run ' + pc.cyan('astralkit init') + ' again.');
|
|
116
|
-
} else {
|
|
117
|
-
p.log.info(pc.gray(err.message));
|
|
118
|
-
}
|
|
92
|
+
const ora = (await import('ora')).default;
|
|
93
|
+
const spinner = ora('Installing astralkit...').start();
|
|
94
|
+
try {
|
|
95
|
+
const result = await runInstall(pm, process.cwd(), ['astralkit']);
|
|
96
|
+
if (result.success) {
|
|
97
|
+
spinner.succeed('Installed astralkit');
|
|
98
|
+
} else {
|
|
99
|
+
spinner.fail('Failed to install astralkit.');
|
|
100
|
+
console.log(pc.gray(' ' + (result.hint || result.error)));
|
|
101
|
+
console.log(pc.gray(` Retry manually: ${pm === 'npm' ? 'npm install' : pm + ' add'} astralkit`));
|
|
119
102
|
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
|
|
103
|
+
} catch (err) {
|
|
104
|
+
spinner.fail('Failed to install astralkit.');
|
|
105
|
+
console.log(pc.gray(' ' + err.message));
|
|
123
106
|
}
|
|
124
107
|
} else {
|
|
125
|
-
p.log.step('astralkit
|
|
108
|
+
p.log.step('astralkit already installed');
|
|
126
109
|
}
|
|
127
110
|
|
|
128
111
|
// ─── Step 4: CSS imports ─────────────────────────────
|
|
129
112
|
const cssEntry = findCssEntryFile();
|
|
113
|
+
|
|
130
114
|
if (cssEntry) {
|
|
131
|
-
const missing = getMissingCssImports(cssEntry.path);
|
|
115
|
+
const missing = getMissingCssImports(cssEntry.path, tailwindVersion);
|
|
132
116
|
if (missing.length > 0) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
initialValue: true,
|
|
136
|
-
});
|
|
137
|
-
if (!p.isCancel(addImports) && addImports) {
|
|
138
|
-
addCssImports(cssEntry.path, missing);
|
|
139
|
-
p.log.success(`CSS imports added to ${cssEntry.relative}`);
|
|
140
|
-
}
|
|
117
|
+
addCssImports(cssEntry.path, missing);
|
|
118
|
+
p.log.success(`Updated ${pc.cyan(cssEntry.relative)}`);
|
|
141
119
|
} else {
|
|
142
120
|
p.log.step('CSS imports already present');
|
|
143
121
|
}
|
|
144
122
|
} else {
|
|
145
|
-
//
|
|
146
|
-
const defaultCssPath = (
|
|
147
|
-
? path.join(process.cwd(), 'src', 'app', 'globals.css')
|
|
148
|
-
: (framework === 'nextjs')
|
|
149
|
-
? path.join(process.cwd(), 'app', 'globals.css')
|
|
150
|
-
: (hasSrc)
|
|
151
|
-
? path.join(process.cwd(), 'src', 'index.css')
|
|
152
|
-
: path.join(process.cwd(), 'index.css');
|
|
123
|
+
// Create a default CSS file
|
|
124
|
+
const defaultCssPath = path.join(process.cwd(), 'src', 'app', 'globals.css');
|
|
153
125
|
const defaultCssRelative = path.relative(process.cwd(), defaultCssPath);
|
|
154
|
-
|
|
155
|
-
const allImports = getMissingCssImports('/nonexistent'); // get all imports
|
|
156
126
|
const cssDir = path.dirname(defaultCssPath);
|
|
157
127
|
if (!fs.existsSync(cssDir)) fs.mkdirSync(cssDir, { recursive: true });
|
|
158
|
-
|
|
159
|
-
|
|
128
|
+
const imports = getRequiredCssImports(tailwindVersion);
|
|
129
|
+
fs.writeFileSync(defaultCssPath, imports.join('\n') + '\n', 'utf-8');
|
|
130
|
+
p.log.success(`Created ${pc.cyan(defaultCssRelative)}`);
|
|
160
131
|
}
|
|
161
132
|
|
|
162
133
|
// ─── Step 5: AI rules file ───────────────────────────
|
package/src/lib/config.js
CHANGED
|
@@ -1,75 +1,33 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
|
|
4
|
-
//
|
|
5
|
-
// New installs don't create this file; detection is automatic.
|
|
6
|
-
export const CONFIG_FILENAME = 'astralkit.config.json';
|
|
7
|
-
|
|
8
|
-
export function getProjectConfig(dir = process.cwd()) {
|
|
9
|
-
let searchDir = dir;
|
|
10
|
-
while (true) {
|
|
11
|
-
const configPath = path.join(searchDir, CONFIG_FILENAME);
|
|
12
|
-
if (fs.existsSync(configPath)) {
|
|
13
|
-
try {
|
|
14
|
-
const raw = fs.readFileSync(configPath, 'utf8');
|
|
15
|
-
return { ...JSON.parse(raw), _path: configPath };
|
|
16
|
-
} catch { return null; }
|
|
17
|
-
}
|
|
18
|
-
const parent = path.dirname(searchDir);
|
|
19
|
-
if (parent === searchDir) break;
|
|
20
|
-
searchDir = parent;
|
|
21
|
-
}
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
24
|
-
|
|
4
|
+
// ─── CSS entry file candidates (searched in order) ────────
|
|
25
5
|
export const CSS_ENTRY_CANDIDATES = [
|
|
26
|
-
'src/index.css',
|
|
27
6
|
'src/app/globals.css',
|
|
7
|
+
'src/index.css',
|
|
28
8
|
'src/styles/globals.css',
|
|
29
9
|
'src/styles/index.css',
|
|
30
10
|
'app/globals.css',
|
|
31
11
|
'styles/globals.css',
|
|
32
|
-
'styles/global.css',
|
|
33
12
|
'index.css',
|
|
34
13
|
];
|
|
35
14
|
|
|
36
|
-
|
|
37
|
-
* CSS imports needed for AstralKit.
|
|
38
|
-
* Tailwind import is version-dependent — use getRequiredCssImports() instead.
|
|
39
|
-
*/
|
|
40
|
-
export const REQUIRED_CSS_IMPORTS = [
|
|
41
|
-
'@import "tailwindcss";',
|
|
42
|
-
'@import "astralkit/theme";',
|
|
43
|
-
'@import "astralkit/utilities";',
|
|
44
|
-
];
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Get the correct CSS imports for the project's Tailwind version.
|
|
48
|
-
*/
|
|
49
|
-
export function getRequiredCssImports(dir = process.cwd()) {
|
|
50
|
-
const twVersion = detectTailwindVersion(dir);
|
|
51
|
-
const tailwindImport = twVersion === 3
|
|
52
|
-
? '@tailwind base;\n@tailwind components;\n@tailwind utilities;'
|
|
53
|
-
: '@import "tailwindcss";';
|
|
54
|
-
return [
|
|
55
|
-
tailwindImport,
|
|
56
|
-
'@import "astralkit/theme";',
|
|
57
|
-
'@import "astralkit/utilities";',
|
|
58
|
-
];
|
|
59
|
-
}
|
|
15
|
+
// ─── Framework detection ──────────────────────────────────
|
|
60
16
|
|
|
61
17
|
export function detectFramework(dir = process.cwd()) {
|
|
18
|
+
// Next.js
|
|
62
19
|
if (
|
|
63
20
|
fs.existsSync(path.join(dir, 'next.config.js')) ||
|
|
64
21
|
fs.existsSync(path.join(dir, 'next.config.mjs')) ||
|
|
65
22
|
fs.existsSync(path.join(dir, 'next.config.ts'))
|
|
66
23
|
) return 'nextjs';
|
|
67
24
|
|
|
25
|
+
// React (Vite, CRA, etc.)
|
|
68
26
|
const pkgPath = path.join(dir, 'package.json');
|
|
69
27
|
if (fs.existsSync(pkgPath)) {
|
|
70
28
|
try {
|
|
71
|
-
const
|
|
72
|
-
const allDeps = { ...
|
|
29
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
30
|
+
const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
73
31
|
if (allDeps['react']) return 'react';
|
|
74
32
|
} catch { /* ignore */ }
|
|
75
33
|
}
|
|
@@ -77,38 +35,37 @@ export function detectFramework(dir = process.cwd()) {
|
|
|
77
35
|
return 'html';
|
|
78
36
|
}
|
|
79
37
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
* Returns 3, 4, or null.
|
|
83
|
-
*/
|
|
38
|
+
// ─── Tailwind version detection ───────────────────────────
|
|
39
|
+
|
|
84
40
|
export function detectTailwindVersion(dir = process.cwd()) {
|
|
41
|
+
const pkgPath = path.join(dir, 'package.json');
|
|
42
|
+
if (!fs.existsSync(pkgPath)) return null;
|
|
85
43
|
try {
|
|
86
|
-
const pkgPath = path.join(dir, 'package.json');
|
|
87
|
-
if (!fs.existsSync(pkgPath)) return null;
|
|
88
44
|
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
89
|
-
const
|
|
90
|
-
const ver =
|
|
45
|
+
const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
46
|
+
const ver = allDeps['tailwindcss'];
|
|
91
47
|
if (!ver) return null;
|
|
92
48
|
const major = parseInt(ver.replace(/[^0-9]/, ''), 10);
|
|
93
49
|
if (major >= 4) return 4;
|
|
94
|
-
if (major
|
|
95
|
-
return null;
|
|
96
|
-
} catch {
|
|
50
|
+
if (major >= 3) return 3;
|
|
97
51
|
return null;
|
|
98
|
-
}
|
|
52
|
+
} catch { return null; }
|
|
99
53
|
}
|
|
100
54
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
export function isTypeScriptProject(dir = process.cwd()) {
|
|
55
|
+
// ─── TypeScript detection ─────────────────────────────────
|
|
56
|
+
|
|
57
|
+
export function detectTypeScript(dir = process.cwd()) {
|
|
105
58
|
return fs.existsSync(path.join(dir, 'tsconfig.json'));
|
|
106
59
|
}
|
|
107
60
|
|
|
61
|
+
// ─── astralkit installed check ────────────────────────────
|
|
62
|
+
|
|
108
63
|
export function isAstralkitInstalled(dir = process.cwd()) {
|
|
109
64
|
return fs.existsSync(path.join(dir, 'node_modules', 'astralkit', 'package.json'));
|
|
110
65
|
}
|
|
111
66
|
|
|
67
|
+
// ─── CSS entry file search ────────────────────────────────
|
|
68
|
+
|
|
112
69
|
export function findCssEntryFile(dir = process.cwd()) {
|
|
113
70
|
for (const candidate of CSS_ENTRY_CANDIDATES) {
|
|
114
71
|
const filePath = path.join(dir, candidate);
|
|
@@ -117,40 +74,54 @@ export function findCssEntryFile(dir = process.cwd()) {
|
|
|
117
74
|
return null;
|
|
118
75
|
}
|
|
119
76
|
|
|
120
|
-
|
|
77
|
+
// ─── CSS imports (Tailwind v3 vs v4 aware) ────────────────
|
|
78
|
+
|
|
79
|
+
export function getRequiredCssImports(tailwindVersion) {
|
|
80
|
+
if (tailwindVersion === 3) {
|
|
81
|
+
return [
|
|
82
|
+
'@tailwind base;',
|
|
83
|
+
'@tailwind components;',
|
|
84
|
+
'@tailwind utilities;',
|
|
85
|
+
'@import "astralkit/theme";',
|
|
86
|
+
'@import "astralkit/utilities";',
|
|
87
|
+
];
|
|
88
|
+
}
|
|
89
|
+
// v4 or unknown — default to v4 syntax
|
|
90
|
+
return [
|
|
91
|
+
'@import "tailwindcss";',
|
|
92
|
+
'@import "astralkit/theme";',
|
|
93
|
+
'@import "astralkit/utilities";',
|
|
94
|
+
];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function getMissingCssImports(cssFilePath, tailwindVersion) {
|
|
121
98
|
try {
|
|
122
99
|
const content = fs.readFileSync(cssFilePath, 'utf8');
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
100
|
+
const required = getRequiredCssImports(tailwindVersion);
|
|
101
|
+
|
|
102
|
+
return required.filter(imp => {
|
|
103
|
+
// For tailwind directives, check if either v3 or v4 syntax is present
|
|
104
|
+
if (imp === '@import "tailwindcss";') {
|
|
105
|
+
return !content.includes('@import "tailwindcss"') && !content.includes('@tailwind base');
|
|
106
|
+
}
|
|
107
|
+
if (imp === '@tailwind base;') {
|
|
108
|
+
return !content.includes('@tailwind base') && !content.includes('@import "tailwindcss"');
|
|
109
|
+
}
|
|
110
|
+
if (imp === '@tailwind components;') {
|
|
111
|
+
return !content.includes('@tailwind components');
|
|
112
|
+
}
|
|
113
|
+
if (imp === '@tailwind utilities;') {
|
|
114
|
+
return !content.includes('@tailwind utilities');
|
|
115
|
+
}
|
|
116
|
+
// AstralKit imports — check directly
|
|
117
|
+
return !content.includes(imp);
|
|
118
|
+
});
|
|
133
119
|
} catch {
|
|
134
|
-
return
|
|
120
|
+
return getRequiredCssImports(tailwindVersion);
|
|
135
121
|
}
|
|
136
122
|
}
|
|
137
123
|
|
|
138
|
-
export function addCssImports(cssFilePath,
|
|
124
|
+
export function addCssImports(cssFilePath, imports) {
|
|
139
125
|
const existing = fs.existsSync(cssFilePath) ? fs.readFileSync(cssFilePath, 'utf8') : '';
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
const lines = [];
|
|
143
|
-
for (const imp of missing) {
|
|
144
|
-
if (imp === 'tailwind') {
|
|
145
|
-
if (twVersion === 3) {
|
|
146
|
-
lines.push('@tailwind base;', '@tailwind components;', '@tailwind utilities;');
|
|
147
|
-
} else {
|
|
148
|
-
lines.push('@import "tailwindcss";');
|
|
149
|
-
}
|
|
150
|
-
} else {
|
|
151
|
-
lines.push(imp);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
fs.writeFileSync(cssFilePath, lines.join('\n') + '\n\n' + existing);
|
|
126
|
+
fs.writeFileSync(cssFilePath, imports.join('\n') + '\n\n' + existing);
|
|
156
127
|
}
|