@astralkit/cli 3.0.1 → 3.0.2

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.2",
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,7 +82,8 @@ 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();
68
87
 
69
88
  // Create node_modules/astralkit/ directory structure
70
89
  const sdkDir = path.join(process.cwd(), 'node_modules', 'astralkit');
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 };