@astralkit/cli 3.0.1 → 3.0.3

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/bin/index.js CHANGED
@@ -26,6 +26,7 @@ program
26
26
  program
27
27
  .command('init')
28
28
  .description('Initialize AstralKit in your project')
29
+ .option('--api-key <key>', 'Your API key (from astralkit.com/account)')
29
30
  .option('--framework <framework>', 'Framework to use (auto-detected if omitted)')
30
31
  .option('--dir <directory>', 'Component output directory', 'src/components/ui')
31
32
  .action(initCommand);
@@ -34,6 +35,7 @@ program
34
35
  program
35
36
  .command('add <name>')
36
37
  .description('Add a component, booster, or workflow to your project')
38
+ .option('--api-key <key>', 'Your API key (from astralkit.com/account)')
37
39
  .option('-f, --framework <framework>', 'Framework version (html, react, nextjs)')
38
40
  .option('--dir <directory>', 'Override output directory')
39
41
  .option('--overwrite', 'Overwrite existing files', false)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astralkit/cli",
3
- "version": "3.0.1",
3
+ "version": "3.0.3",
4
4
  "description": "AstralKit CLI — Install and manage UI components in your project",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "bin": {
@@ -15,6 +15,7 @@ const {
15
15
  } = require('../lib/config');
16
16
  const { validateSlug, validateFramework, isSafePath, writeFiles } = require('../lib/validators');
17
17
  const { detectPackageManager, getInstallCommand, getRunCommand, runInstall } = require('../lib/pkg-manager');
18
+ const { saveApiKey, getApiKey } = require('../lib/auth');
18
19
 
19
20
  const FRAMEWORK_LABELS = {
20
21
  nextjs: 'Next.js',
@@ -27,6 +28,26 @@ const FRAMEWORK_LABELS = {
27
28
  * If no --booster or --workflow flag, try auto-detection then prompt.
28
29
  */
29
30
  async function addCommand(nameOrSlug, options) {
31
+ // Save API key if provided via --api-key flag
32
+ if (options.apiKey) {
33
+ if (!options.apiKey.startsWith('ak_')) {
34
+ console.log(chalk.red('Invalid API key format. Keys start with ak_'));
35
+ process.exit(1);
36
+ }
37
+ saveApiKey(options.apiKey);
38
+ }
39
+
40
+ // Require API key or token
41
+ if (!getApiKey()) {
42
+ const { getAuth } = require('../lib/auth');
43
+ const auth = getAuth();
44
+ if (!auth.token) {
45
+ console.log(chalk.yellow('No API key found. Run `astralkit init --api-key <key>` first.'));
46
+ console.log(chalk.gray('Get your key at'), chalk.cyan('astralkit.com/account'));
47
+ process.exit(1);
48
+ }
49
+ }
50
+
30
51
  // If explicit flags, use them directly
31
52
  if (options.booster) {
32
53
  return addBooster(nameOrSlug, options);
@@ -18,11 +18,29 @@ const {
18
18
  } = require('../lib/config');
19
19
  const { validateFramework } = require('../lib/validators');
20
20
  const { getAiRulesContent } = require('../lib/ai-rules');
21
+ const { saveApiKey, getApiKey } = require('../lib/auth');
21
22
 
22
23
  async function initCommand(options) {
23
24
  console.log(chalk.blue.bold('AstralKit — Project Setup'));
24
25
  console.log();
25
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
+
26
44
  // Validate --framework if provided
27
45
  if (options.framework && !validateFramework(options.framework)) {
28
46
  process.exit(1);
@@ -64,30 +82,40 @@ async function initCommand(options) {
64
82
  const spinner = ora('Installing astralkit design tokens...').start();
65
83
  try {
66
84
  const { apiRequest } = require('../lib/api');
67
- const data = await apiRequest('/api/cli/sdk');
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() });
68
97
 
69
- // Create node_modules/astralkit/ directory structure
98
+ // Now write astralkit token files to node_modules/astralkit/
99
+ // (after npm install so they don't get pruned)
70
100
  const sdkDir = path.join(process.cwd(), 'node_modules', 'astralkit');
71
101
  const libDir = path.join(sdkDir, 'lib');
72
102
  fs.mkdirSync(libDir, { recursive: true });
73
103
 
74
- // Write all files
75
- for (const [filePath, content] of Object.entries(data.files)) {
104
+ let filesWritten = 0;
105
+ for (const [filePath, content] of Object.entries(data.files || {})) {
76
106
  const fullPath = path.join(sdkDir, filePath);
77
107
  const dir = path.dirname(fullPath);
78
108
  if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
79
109
  fs.writeFileSync(fullPath, content, 'utf-8');
110
+ filesWritten++;
80
111
  }
81
112
 
82
- // Also install the runtime dependencies (clsx + tailwind-merge)
83
- const pm = detectPackageManager();
84
- const depsCmd = pm === 'yarn' ? 'yarn add clsx tailwind-merge'
85
- : pm === 'pnpm' ? 'pnpm add clsx tailwind-merge'
86
- : pm === 'bun' ? 'bun add clsx tailwind-merge'
87
- : 'npm install clsx tailwind-merge';
88
- execSync(depsCmd, { stdio: 'pipe', cwd: process.cwd() });
89
-
90
- spinner.succeed('Installed astralkit design tokens');
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
+ }
91
119
  } catch (err) {
92
120
  spinner.fail('Failed to install astralkit');
93
121
  if (err.message?.includes('Authentication')) {
@@ -122,12 +150,18 @@ async function initCommand(options) {
122
150
  console.log(chalk.gray(' CSS imports already present'));
123
151
  }
124
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
+
125
159
  console.log();
126
- console.log(chalk.yellow('No CSS entry file found.'));
127
- console.log(chalk.gray('Add these imports to your main CSS file:'));
128
- for (const imp of REQUIRED_CSS_IMPORTS) {
129
- console.log(chalk.cyan(` ${imp}`));
130
- }
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'));
131
165
  }
132
166
 
133
167
  // ─── Step 3: Write config ──────────────────────────────
package/src/lib/api.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const path = require('path');
2
- const { getAuth } = require('./auth');
2
+ const { getAuth, getApiKey } = require('./auth');
3
3
 
4
4
  const pkg = require(path.join(__dirname, '..', '..', 'package.json'));
5
5
  const CLI_VERSION = pkg.version;
@@ -32,7 +32,6 @@ const API_BASE = (() => {
32
32
  */
33
33
  async function apiRequest(urlPath, options = {}) {
34
34
  const url = `${API_BASE}${urlPath}`;
35
- const auth = getAuth();
36
35
 
37
36
  const headers = {
38
37
  'Accept': 'application/json',
@@ -41,8 +40,15 @@ async function apiRequest(urlPath, options = {}) {
41
40
  ...options.headers,
42
41
  };
43
42
 
44
- if (auth.token) {
45
- headers['Authorization'] = `Bearer ${auth.token}`;
43
+ // Prefer API key (short, permanent) over Bearer JWT (legacy)
44
+ const apiKey = getApiKey();
45
+ if (apiKey) {
46
+ headers['x-api-key'] = apiKey;
47
+ } else {
48
+ const auth = getAuth();
49
+ if (auth.token) {
50
+ headers['Authorization'] = `Bearer ${auth.token}`;
51
+ }
46
52
  }
47
53
 
48
54
  // Request timeout
package/src/lib/auth.js CHANGED
@@ -90,6 +90,29 @@ function saveAuth({ token, email, plan }) {
90
90
  fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), { mode: 0o600 });
91
91
  }
92
92
 
93
+ /**
94
+ * Save an API key (replaces Bearer token auth — simpler, permanent).
95
+ */
96
+ function saveApiKey(apiKey) {
97
+ ensureConfigDir();
98
+ const existing = getFullConfig();
99
+ const config = { ...existing, apiKey };
100
+ // Clear legacy JWT fields when switching to API key
101
+ delete config.token;
102
+ fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), { mode: 0o600 });
103
+ }
104
+
105
+ /**
106
+ * Get the stored API key (if any).
107
+ */
108
+ function getApiKey() {
109
+ const envKey = process.env.ASTRALKIT_API_KEY;
110
+ if (envKey) return envKey;
111
+
112
+ const config = getFullConfig();
113
+ return config.apiKey || null;
114
+ }
115
+
93
116
  /**
94
117
  * Remove stored auth.
95
118
  */
@@ -116,4 +139,4 @@ function getFullConfig() {
116
139
  return {};
117
140
  }
118
141
 
119
- module.exports = { getAuth, saveAuth, clearToken, isTokenExpired, CONFIG_DIR, CONFIG_FILE };
142
+ module.exports = { getAuth, saveAuth, saveApiKey, getApiKey, clearToken, isTokenExpired, CONFIG_DIR, CONFIG_FILE };