@astralkit/cli 3.0.4 → 3.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astralkit/cli",
3
- "version": "3.0.4",
3
+ "version": "3.1.0",
4
4
  "description": "AstralKit CLI — Install and manage UI components in your project",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "bin": {
@@ -236,6 +236,7 @@ async function addComponent(componentSlug, options) {
236
236
 
237
237
  try {
238
238
  let data;
239
+ let actualFramework = framework;
239
240
  try {
240
241
  data = await getComponent(componentSlug, framework);
241
242
  } catch (fetchErr) {
@@ -243,6 +244,7 @@ async function addComponent(componentSlug, options) {
243
244
  if (fetchErr.code === 'PRO_REQUIRED' && framework !== 'html') {
244
245
  spinner.warn(`${FRAMEWORK_LABELS[framework] || framework} requires a Pro subscription.`);
245
246
  console.log(chalk.gray(' Fetching HTML version instead (free tier).'));
247
+ actualFramework = 'html';
246
248
  const htmlSpinner = ora(`Fetching ${componentSlug} (HTML)...`).start();
247
249
  try {
248
250
  data = await getComponent(componentSlug, 'html');
@@ -289,7 +291,7 @@ async function addComponent(componentSlug, options) {
289
291
  fs.writeFileSync(filePath, file.content);
290
292
  }
291
293
 
292
- spinner.succeed(`Added ${chalk.cyan(data.name || componentSlug)} (${FRAMEWORK_LABELS[framework] || framework})`);
294
+ spinner.succeed(`Added ${chalk.cyan(data.name || componentSlug)} (${FRAMEWORK_LABELS[actualFramework] || actualFramework})`);
293
295
 
294
296
  console.log(chalk.gray(` ${componentsDir}/`));
295
297
  for (const file of data.files) {
@@ -297,7 +299,7 @@ async function addComponent(componentSlug, options) {
297
299
  }
298
300
 
299
301
  // Post-install checks for react/nextjs
300
- if (framework !== 'html') {
302
+ if (actualFramework !== 'html') {
301
303
  console.log();
302
304
 
303
305
  if (!isAstralkitInstalled()) {
@@ -1,205 +1,188 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const chalk = require('chalk');
4
- const inquirer = require('inquirer');
5
- const ora = require('ora');
6
- const { execSync } = require('child_process');
7
- const {
8
- detectFramework,
9
- defaultConfig,
10
- writeProjectConfig,
11
- getProjectConfig,
12
- CONFIG_FILENAME,
13
- isAstralkitInstalled,
14
- findCssEntryFile,
15
- getMissingCssImports,
16
- addCssImports,
17
- REQUIRED_CSS_IMPORTS,
18
- } = require('../lib/config');
19
- const { validateFramework } = require('../lib/validators');
20
- const { getAiRulesContent } = require('../lib/ai-rules');
21
- const { saveApiKey, getApiKey } = require('../lib/auth');
22
-
23
- async function initCommand(options) {
24
- console.log(chalk.blue.bold('AstralKit — Project Setup'));
25
- console.log();
26
-
27
- // ─── Save API key if provided ────────────────────────
28
- if (options.apiKey) {
29
- if (!options.apiKey.startsWith('ak_')) {
30
- console.log(chalk.red('Invalid API key format. Keys start with ak_'));
31
- console.log(chalk.gray('Get your key at https://astralkit.com/account'));
32
- process.exit(1);
33
- }
34
- saveApiKey(options.apiKey);
35
- console.log(chalk.green(' API key saved'));
36
- } else if (!getApiKey()) {
37
- console.log(chalk.yellow('No API key found.'));
38
- console.log(chalk.gray('Get your key at'), chalk.cyan('astralkit.com/account'));
39
- console.log(chalk.gray('Then run:'), chalk.cyan('npx @astralkit/cli init --api-key <your-key>'));
40
- console.log();
41
- process.exit(1);
42
- }
43
-
44
- // Validate --framework if provided
45
- if (options.framework && !validateFramework(options.framework)) {
46
- process.exit(1);
47
- }
48
-
49
- // Check if already initialized
50
- const existing = getProjectConfig();
51
- if (existing) {
52
- console.log(chalk.yellow(`Found existing ${CONFIG_FILENAME} at ${existing._path}`));
53
- const { overwrite } = await inquirer.prompt([{
54
- type: 'confirm',
55
- name: 'overwrite',
56
- message: 'Overwrite existing config?',
57
- default: false,
58
- }]);
59
- if (!overwrite) {
60
- console.log(chalk.gray('Aborted.'));
61
- return;
62
- }
63
- }
64
-
65
- // Auto-detect framework silently — no prompt needed during init.
66
- // Framework selection happens during `add` if auto-detection is wrong.
67
- let framework = options.framework;
68
- if (!framework) {
69
- framework = detectFramework();
70
- }
71
- console.log(chalk.gray(` Framework: ${framework}`));
72
-
73
- // Component directory
74
- const componentDir = options.dir || 'src/components/ui';
75
-
76
- // ─── Step 1: Install astralkit design token package ─────
77
- // Fetched from AstralKit API (not npm) to keep tokens private.
78
- // Written to node_modules/astralkit/ so imports resolve normally.
79
- // ALL frameworks need this — HTML uses @import "astralkit/theme", React/Next.js use import { cn }.
80
- {
81
- if (!isAstralkitInstalled()) {
82
- const spinner = ora('Installing astralkit design tokens...').start();
83
- try {
84
- const { apiRequest } = require('../lib/api');
85
- const response = await apiRequest('/api/cli/sdk');
86
- const data = await response.json();
87
-
88
- // Install runtime dependencies FIRST (clsx + tailwind-merge)
89
- // Must happen before writing astralkit files, because npm prunes
90
- // extraneous folders from node_modules during install.
91
- const pm = detectPackageManager();
92
- const depsCmd = pm === 'yarn' ? 'yarn add clsx tailwind-merge'
93
- : pm === 'pnpm' ? 'pnpm add clsx tailwind-merge'
94
- : pm === 'bun' ? 'bun add clsx tailwind-merge'
95
- : 'npm install clsx tailwind-merge';
96
- execSync(depsCmd, { stdio: 'pipe', cwd: process.cwd() });
97
-
98
- // Now write astralkit token files to node_modules/astralkit/
99
- // (after npm install so they don't get pruned)
100
- const sdkDir = path.join(process.cwd(), 'node_modules', 'astralkit');
101
- const libDir = path.join(sdkDir, 'lib');
102
- fs.mkdirSync(libDir, { recursive: true });
103
-
104
- let filesWritten = 0;
105
- for (const [filePath, content] of Object.entries(data.files || {})) {
106
- const fullPath = path.join(sdkDir, filePath);
107
- const dir = path.dirname(fullPath);
108
- if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
109
- fs.writeFileSync(fullPath, content, 'utf-8');
110
- filesWritten++;
111
- }
112
-
113
- if (filesWritten === 0) {
114
- spinner.fail('No token files received from API');
115
- console.log(chalk.yellow('Try again — the API may be updating.'));
116
- } else {
117
- spinner.succeed(`Installed astralkit design tokens (${filesWritten} files)`);
118
- }
119
- } catch (err) {
120
- spinner.fail('Failed to install astralkit');
121
- if (err.message?.includes('Authentication')) {
122
- console.log(chalk.yellow('Run `astralkit login` first, then try again.'));
123
- } else {
124
- console.log(chalk.yellow('Check your internet connection and try again.'));
125
- }
126
- }
127
- } else {
128
- console.log(chalk.gray(' astralkit already installed'));
129
- }
130
- }
131
-
132
- // ─── Step 2: Add CSS imports ────────────────────────────
133
- const cssEntry = findCssEntryFile();
134
- if (cssEntry) {
135
- const missing = getMissingCssImports(cssEntry.path);
136
- if (missing.length > 0) {
137
- console.log();
138
- console.log(chalk.gray(`Found CSS entry: ${cssEntry.relative}`));
139
- const { addImports } = await inquirer.prompt([{
140
- type: 'confirm',
141
- name: 'addImports',
142
- message: `Add AstralKit imports to ${cssEntry.relative}?`,
143
- default: true,
144
- }]);
145
- if (addImports) {
146
- addCssImports(cssEntry.path, missing);
147
- console.log(chalk.green(' Added CSS imports'));
148
- }
149
- } else {
150
- console.log(chalk.gray(' CSS imports already present'));
151
- }
152
- } else {
153
- // No CSS entry found — create one with AstralKit imports
154
- const defaultCssPath = framework === 'html'
155
- ? path.join(process.cwd(), 'styles.css')
156
- : path.join(process.cwd(), 'src', 'app', 'globals.css');
157
- const defaultCssRelative = path.relative(process.cwd(), defaultCssPath);
158
-
159
- console.log();
160
- const cssContent = REQUIRED_CSS_IMPORTS.join('\n') + '\n';
161
- const cssDir = path.dirname(defaultCssPath);
162
- if (!fs.existsSync(cssDir)) fs.mkdirSync(cssDir, { recursive: true });
163
- fs.writeFileSync(defaultCssPath, cssContent, 'utf-8');
164
- console.log(chalk.green(' Created'), chalk.cyan(defaultCssRelative), chalk.green('with AstralKit imports'));
165
- }
166
-
167
- // ─── Step 3: Write config ──────────────────────────────
168
- const config = defaultConfig(framework);
169
- config.componentsDir = componentDir;
170
- writeProjectConfig(config);
171
-
172
- console.log();
173
- console.log(chalk.green('Created'), chalk.cyan(CONFIG_FILENAME));
174
- console.log(chalk.gray(` Framework: ${framework}`));
175
- console.log(chalk.gray(` Components: ${componentDir}/`));
176
-
177
- // ─── Step 4: AI coding rules ─────────────────────────
178
- const rulesFile = 'astralkit-ai-rules.md';
179
- const rulesPath = path.join(process.cwd(), rulesFile);
180
- if (!fs.existsSync(rulesPath)) {
181
- fs.writeFileSync(rulesPath, getAiRulesContent());
182
- console.log(chalk.green(' Created'), chalk.cyan(rulesFile));
183
- console.log(chalk.gray(' Paste into your AI tool\'s config (.cursorrules, CLAUDE.md, etc.)'));
184
- } else {
185
- console.log(chalk.gray(` ${rulesFile} already exists`));
186
- }
187
-
188
- console.log();
189
- console.log(chalk.green('Next steps:'));
190
- console.log(chalk.gray(' astralkit add sidebar-expanded-light Add a component'));
191
- console.log(chalk.gray(' astralkit list Browse components'));
192
- console.log(chalk.gray(' astralkit login Unlock pro components'));
193
- }
194
-
195
- /**
196
- * Detect which package manager the project uses.
197
- */
198
- function detectPackageManager(dir = process.cwd()) {
199
- if (fs.existsSync(path.join(dir, 'bun.lockb')) || fs.existsSync(path.join(dir, 'bun.lock'))) return 'bun';
200
- if (fs.existsSync(path.join(dir, 'pnpm-lock.yaml'))) return 'pnpm';
201
- if (fs.existsSync(path.join(dir, 'yarn.lock'))) return 'yarn';
202
- return 'npm';
203
- }
204
-
205
- module.exports = { initCommand };
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const chalk = require('chalk');
4
+ const inquirer = require('inquirer');
5
+ const ora = require('ora');
6
+ const { execSync } = require('child_process');
7
+ const {
8
+ detectFramework,
9
+ isAstralkitInstalled,
10
+ findCssEntryFile,
11
+ getMissingCssImports,
12
+ addCssImports,
13
+ REQUIRED_CSS_IMPORTS,
14
+ } = require('../lib/config');
15
+ const { validateFramework } = require('../lib/validators');
16
+ const { getAiRulesContent } = require('../lib/ai-rules');
17
+ const { saveApiKey, getApiKey } = require('../lib/auth');
18
+
19
+ async function initCommand(options) {
20
+ console.log(chalk.blue.bold('AstralKit — Project Setup'));
21
+ console.log();
22
+
23
+ // ─── Save API key if provided ────────────────────────
24
+ if (options.apiKey) {
25
+ if (!options.apiKey.startsWith('ak_')) {
26
+ console.log(chalk.red('Invalid API key format. Keys start with ak_'));
27
+ console.log(chalk.gray('Get your key at https://astralkit.com/account'));
28
+ process.exit(1);
29
+ }
30
+ saveApiKey(options.apiKey);
31
+ console.log(chalk.green(' API key saved'));
32
+ } else if (!getApiKey()) {
33
+ console.log(chalk.yellow('No API key found.'));
34
+ console.log(chalk.gray('Get your key at'), chalk.cyan('astralkit.com/account'));
35
+ console.log(chalk.gray('Then run:'), chalk.cyan('npx @astralkit/cli init --api-key <your-key>'));
36
+ console.log();
37
+ process.exit(1);
38
+ }
39
+
40
+ // Auto-detect framework
41
+ let framework = options.framework;
42
+ if (framework && !validateFramework(framework)) process.exit(1);
43
+ if (!framework) framework = detectFramework();
44
+ console.log(chalk.gray(` Framework: ${framework}`));
45
+
46
+ // Detect Tailwind version
47
+ const twVersion = detectTailwindVersion();
48
+ if (twVersion) console.log(chalk.gray(` Tailwind: v${twVersion}`));
49
+
50
+ // Detect TypeScript
51
+ const isTS = fs.existsSync(path.join(process.cwd(), 'tsconfig.json'));
52
+ if (isTS) console.log(chalk.gray(' TypeScript: Yes'));
53
+
54
+ // Component directory — adapt to project structure
55
+ const hasSrc = fs.existsSync(path.join(process.cwd(), 'src'));
56
+ const componentDir = options.dir || (hasSrc ? 'src/components/ui' : 'components/ui');
57
+
58
+ // ─── Step 1: Install astralkit as a real npm dependency ─────
59
+ // Downloaded as a tarball from the API, installed via npm/pnpm/yarn/bun.
60
+ // This makes it a real dependency in package.json that survives npm ci.
61
+ {
62
+ if (!isAstralkitInstalled()) {
63
+ const spinner = ora('Installing astralkit design tokens...').start();
64
+ try {
65
+ const { apiRequest, API_BASE } = require('../lib/api');
66
+
67
+ // Download the tarball from the API
68
+ const response = await apiRequest('/api/cli/sdk/tarball');
69
+ const arrayBuffer = await response.arrayBuffer();
70
+ const tgzBuffer = Buffer.from(arrayBuffer);
71
+
72
+ // Save tarball to a temp location
73
+ const tgzPath = path.join(process.cwd(), '.astralkit-sdk.tgz');
74
+ fs.writeFileSync(tgzPath, tgzBuffer);
75
+
76
+ // Install via the detected package manager — this makes it a REAL dependency
77
+ const pm = detectPackageManager();
78
+ const installCmd = pm === 'yarn' ? `yarn add ${tgzPath}`
79
+ : pm === 'pnpm' ? `pnpm add ${tgzPath}`
80
+ : pm === 'bun' ? `bun add ${tgzPath}`
81
+ : `npm install ${tgzPath}`;
82
+ execSync(installCmd, { stdio: 'pipe', cwd: process.cwd() });
83
+
84
+ // Clean up the temp tarball
85
+ if (fs.existsSync(tgzPath)) fs.unlinkSync(tgzPath);
86
+
87
+ // Verify it installed
88
+ if (isAstralkitInstalled()) {
89
+ spinner.succeed('Installed astralkit design tokens');
90
+ } else {
91
+ spinner.fail('Install completed but astralkit not found in node_modules');
92
+ }
93
+ } catch (err) {
94
+ spinner.fail('Failed to install astralkit');
95
+ if (err.message?.includes('API key')) {
96
+ console.log(chalk.yellow('Invalid API key. Check your key at astralkit.com/account'));
97
+ } else {
98
+ console.log(chalk.yellow(err.message || 'Check your internet connection and try again.'));
99
+ }
100
+ // Clean up temp file on error
101
+ const tgzPath = path.join(process.cwd(), '.astralkit-sdk.tgz');
102
+ if (fs.existsSync(tgzPath)) fs.unlinkSync(tgzPath);
103
+ return;
104
+ }
105
+ } else {
106
+ console.log(chalk.gray(' astralkit already installed'));
107
+ }
108
+ }
109
+
110
+ // ─── Step 2: Add CSS imports ────────────────────────────
111
+ const cssEntry = findCssEntryFile();
112
+ if (cssEntry) {
113
+ const missing = getMissingCssImports(cssEntry.path);
114
+ if (missing.length > 0) {
115
+ console.log(chalk.gray(` Found CSS entry: ${cssEntry.relative}`));
116
+ addCssImports(cssEntry.path, missing);
117
+ console.log(chalk.green(' Added CSS imports'));
118
+ } else {
119
+ console.log(chalk.gray(' CSS imports already present'));
120
+ }
121
+ } else {
122
+ // No CSS entry found — create one
123
+ const defaultCssPath = (framework === 'nextjs' && hasSrc)
124
+ ? path.join(process.cwd(), 'src', 'app', 'globals.css')
125
+ : (framework === 'nextjs')
126
+ ? path.join(process.cwd(), 'app', 'globals.css')
127
+ : (hasSrc)
128
+ ? path.join(process.cwd(), 'src', 'index.css')
129
+ : path.join(process.cwd(), 'index.css');
130
+ const defaultCssRelative = path.relative(process.cwd(), defaultCssPath);
131
+
132
+ const cssContent = REQUIRED_CSS_IMPORTS.join('\n') + '\n';
133
+ const cssDir = path.dirname(defaultCssPath);
134
+ if (!fs.existsSync(cssDir)) fs.mkdirSync(cssDir, { recursive: true });
135
+ fs.writeFileSync(defaultCssPath, cssContent, 'utf-8');
136
+ console.log(chalk.green(' Created'), chalk.cyan(defaultCssRelative), chalk.green('with AstralKit imports'));
137
+ }
138
+
139
+ // ─── Step 3: AI coding rules ─────────────────────────
140
+ const rulesFile = 'astralkit-ai-rules.md';
141
+ const rulesPath = path.join(process.cwd(), rulesFile);
142
+ if (!fs.existsSync(rulesPath)) {
143
+ fs.writeFileSync(rulesPath, getAiRulesContent());
144
+ console.log(chalk.green(' Created'), chalk.cyan(rulesFile));
145
+ } else {
146
+ console.log(chalk.gray(` ${rulesFile} already exists`));
147
+ }
148
+
149
+ // ─── Done ────────────────────────────────────────────
150
+ console.log();
151
+ console.log(chalk.green('AstralKit initialized.'));
152
+ console.log();
153
+ console.log(chalk.gray(' Add a component:'), chalk.cyan(`astralkit add sidebar-light`));
154
+ console.log(chalk.gray(' Browse components:'), chalk.cyan('astralkit list'));
155
+ }
156
+
157
+ /**
158
+ * Detect which package manager the project uses.
159
+ */
160
+ function detectPackageManager(dir = process.cwd()) {
161
+ if (fs.existsSync(path.join(dir, 'bun.lockb')) || fs.existsSync(path.join(dir, 'bun.lock'))) return 'bun';
162
+ if (fs.existsSync(path.join(dir, 'pnpm-lock.yaml'))) return 'pnpm';
163
+ if (fs.existsSync(path.join(dir, 'yarn.lock'))) return 'yarn';
164
+ return 'npm';
165
+ }
166
+
167
+ /**
168
+ * Detect Tailwind CSS version from package.json.
169
+ * Returns 3, 4, or null.
170
+ */
171
+ function detectTailwindVersion(dir = process.cwd()) {
172
+ try {
173
+ const pkgPath = path.join(dir, 'package.json');
174
+ if (!fs.existsSync(pkgPath)) return null;
175
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
176
+ const deps = { ...pkg.dependencies, ...pkg.devDependencies };
177
+ const ver = deps['tailwindcss'];
178
+ if (!ver) return null;
179
+ const major = parseInt(ver.replace(/[^0-9]/, ''), 10);
180
+ if (major >= 4) return 4;
181
+ if (major === 3) return 3;
182
+ return null;
183
+ } catch {
184
+ return null;
185
+ }
186
+ }
187
+
188
+ module.exports = { initCommand };