@astralkit/cli 3.0.5 → 4.0.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.
@@ -1,50 +1,49 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
-
4
- /**
5
- * Load the AI rules content from the bundled template.
6
- * Tries node_modules first, then falls back to embedded.
7
- */
8
- function getAiRulesContent() {
9
- const candidates = [
10
- path.join(process.cwd(), 'node_modules', 'astralkit', 'ai-rules.md'),
11
- path.join(__dirname, '..', '..', '..', '..', 'astral-kit', 'astralkit', 'ai-rules.md'),
12
- ];
13
-
14
- for (const candidate of candidates) {
15
- try {
16
- return fs.readFileSync(candidate, 'utf8');
17
- } catch {
18
- // Try next
19
- }
20
- }
21
-
22
- return getEmbeddedRules();
23
- }
24
-
25
- /**
26
- * Minimal embedded rules fallback.
27
- */
28
- function getEmbeddedRules() {
29
- return `# AstralKit — AI Coding Rules
30
-
31
- ## Core Principle
32
- AstralKit uses a fluid responsive system. Typography and spacing scale continuously with viewport width via CSS clamp(). This eliminates breakpoints for most use cases.
33
-
34
- ## Prefer these patterns:
35
- - Use \`text-ak-fl-*\` for ALL headings and body text (never breakpoint variants like sm:text-lg md:text-xl)
36
- - Use \`*-ak-fl-*\` spacing for section padding, grid gaps, vertical rhythm margins
37
- - Use space pairs (\`py-ak-fl-xl-3xl\`) for section-level padding
38
- - Use semantic colors (\`bg-ak-bg\`, \`text-ak-text\`, \`border-ak-border\`) — never raw Tailwind colors
39
- - Use \`size-ak-icon-*\` and \`size-ak-avatar-*\` for component sizes
40
- - Use \`max-w-ak-page\` for page containers
41
- - Use layout primitives (\`ak-stack\`, \`ak-grid\`, \`ak-switcher\`) instead of breakpoint grids
42
- - Use \`ak-focus-ring\` on all interactive elements
43
- - Static \`ak-*\` tokens are ONLY for small UI internals (button padding, icon gaps)
44
- - Breakpoints are ONLY acceptable for binary layout switches (lg:grid-cols-2, md:hidden)
45
-
46
- For the complete token reference, see: https://astralkit.com/docs/tokens
47
- `;
48
- }
49
-
50
- module.exports = { getAiRulesContent };
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ import { dirname } from 'path';
5
+
6
+ const __dirname = dirname(fileURLToPath(import.meta.url));
7
+
8
+ export function getAiRulesContent() {
9
+ const candidates = [
10
+ path.join(process.cwd(), 'node_modules', 'astralkit', 'ai-rules.md'),
11
+ ];
12
+
13
+ for (const candidate of candidates) {
14
+ try {
15
+ return fs.readFileSync(candidate, 'utf8');
16
+ } catch { /* try next */ }
17
+ }
18
+
19
+ return getEmbeddedRules();
20
+ }
21
+
22
+ function getEmbeddedRules() {
23
+ return `# AstralKit — AI Coding Rules
24
+
25
+ ## Component Usage
26
+ - Import components from the components directory: \`import { Button } from '@/components/ui/button'\`
27
+ - Use the \`cn()\` helper for conditional classes: \`import { cn } from '@/lib/utils'\`
28
+ - All components use Tailwind CSS for styling
29
+ - Components are built on top of Radix UI primitives where applicable
30
+
31
+ ## Design Tokens
32
+ - Use CSS variables for colors: \`var(--color-primary)\`
33
+ - Spacing follows Tailwind's scale
34
+ - All tokens are defined in \`astralkit/theme\`
35
+
36
+ ## File Structure
37
+ - Components live in \`src/components/ui/\`
38
+ - Boosters live in \`src/lib/<booster-name>/\` (core) and \`src/components/<booster-name>/\` (adapters)
39
+ - Configuration is in \`astralkit.config.json\`
40
+
41
+ ## Adding Components
42
+ - Use the CLI: \`astralkit add <component-name>\`
43
+ - Never manually copy components — always use the CLI to get the latest version
44
+
45
+ ## Pro Components
46
+ - Run \`astralkit login\` to access Pro content
47
+ - Pro components are marked with ✨ in the CLI output
48
+ `;
49
+ }
package/src/lib/api.js CHANGED
@@ -1,197 +1,152 @@
1
- const path = require('path');
2
- const { getAuth, getApiKey } = require('./auth');
3
-
4
- const pkg = require(path.join(__dirname, '..', '..', 'package.json'));
5
- const CLI_VERSION = pkg.version;
6
-
7
- const DEFAULT_API_BASE = 'https://astralkit.com';
8
- const REQUEST_TIMEOUT_MS = 30_000;
9
-
10
- const API_BASE = (() => {
11
- const override = process.env.ASTRALKIT_API_URL;
12
- if (!override) return DEFAULT_API_BASE;
13
-
14
- try {
15
- const parsed = new URL(override);
16
- if (parsed.protocol !== 'https:') {
17
- console.warn(
18
- '\x1b[33m⚠ ASTRALKIT_API_URL uses insecure protocol. ' +
19
- 'Auth tokens will be sent in plaintext. Use HTTPS in production.\x1b[0m'
20
- );
21
- }
22
- } catch {
23
- console.error('\x1b[31m✖ Invalid ASTRALKIT_API_URL. Falling back to default.\x1b[0m');
24
- return DEFAULT_API_BASE;
25
- }
26
-
27
- return override;
28
- })();
29
-
30
- /**
31
- * Make an authenticated API request to AstralKit.
32
- */
33
- async function apiRequest(urlPath, options = {}) {
34
- const url = `${API_BASE}${urlPath}`;
35
-
36
- const headers = {
37
- 'Accept': 'application/json',
38
- 'User-Agent': `@astralkit/cli/${CLI_VERSION} node/${process.version}`,
39
- 'X-AstralKit-CLI': CLI_VERSION,
40
- ...options.headers,
41
- };
42
-
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
- }
52
- }
53
-
54
- // Request timeout
55
- const controller = new AbortController();
56
- const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
57
-
58
- let response;
59
- try {
60
- response = await fetch(url, {
61
- ...options,
62
- headers,
63
- signal: controller.signal,
64
- });
65
- } catch (err) {
66
- clearTimeout(timeout);
67
- if (err.name === 'AbortError') {
68
- const error = new Error('Request timed out. Check your network connection and try again.');
69
- error.code = 'TIMEOUT';
70
- throw error;
71
- }
72
- if (err.code === 'ENOTFOUND' || err.cause?.code === 'ENOTFOUND') {
73
- const error = new Error('Could not reach astralkit.com. Check your internet connection.');
74
- error.code = 'NETWORK_ERROR';
75
- throw error;
76
- }
77
- if (err.code === 'ECONNREFUSED' || err.cause?.code === 'ECONNREFUSED') {
78
- const error = new Error('Connection refused. The AstralKit API may be temporarily down.');
79
- error.code = 'NETWORK_ERROR';
80
- throw error;
81
- }
82
- throw err;
83
- } finally {
84
- clearTimeout(timeout);
85
- }
86
-
87
- if (response.status === 401) {
88
- const message = auth.expired
89
- ? 'Session expired. Run `astralkit login` to re-authenticate.'
90
- : 'Authentication required. Run `astralkit login` to authenticate.';
91
- const err = new Error(message);
92
- err.code = 'AUTH_REQUIRED';
93
- throw err;
94
- }
95
-
96
- if (response.status === 403) {
97
- const err = new Error('Pro subscription required for this component. Visit https://astralkit.com/pricing');
98
- err.code = 'PRO_REQUIRED';
99
- throw err;
100
- }
101
-
102
- if (!response.ok) {
103
- const body = await response.text().catch(() => '');
104
- const err = new Error(`API error ${response.status}: ${body || response.statusText}`);
105
- err.code = 'API_ERROR';
106
- throw err;
107
- }
108
-
109
- return response;
110
- }
111
-
112
- /**
113
- * List components with optional filters.
114
- */
115
- async function listComponents({ category, framework, pro, free, search } = {}) {
116
- const params = new URLSearchParams();
117
- if (category) params.set('category', category);
118
- if (framework) params.set('framework', framework);
119
- if (pro) params.set('pro', 'true');
120
- if (free) params.set('free', 'true');
121
- if (search) params.set('search', search);
122
-
123
- const response = await apiRequest(`/api/cli/components?${params}`);
124
- return response.json();
125
- }
126
-
127
- /**
128
- * Get a single component's details + code files for installation.
129
- */
130
- async function getComponent(slug, framework = 'html') {
131
- const response = await apiRequest(
132
- `/api/cli/components/${encodeURIComponent(slug)}?framework=${encodeURIComponent(framework)}`
133
- );
134
- return response.json();
135
- }
136
-
137
- /**
138
- * Verify current authentication with the server.
139
- */
140
- async function verifyAuth() {
141
- const response = await apiRequest('/api/cli/whoami');
142
- return response.json();
143
- }
144
-
145
- /**
146
- * List boosters with optional filters.
147
- */
148
- async function listBoosters({ framework, search, pro, free } = {}) {
149
- const params = new URLSearchParams();
150
- if (framework) params.set('framework', framework);
151
- if (pro) params.set('pro', 'true');
152
- if (free) params.set('free', 'true');
153
- if (search) params.set('search', search);
154
-
155
- const response = await apiRequest(`/api/cli/boosters?${params}`);
156
- return response.json();
157
- }
158
-
159
- /**
160
- * Get a single booster's details + files for installation.
161
- */
162
- async function getBooster(slug, framework = 'nextjs') {
163
- const response = await apiRequest(
164
- `/api/cli/boosters/${encodeURIComponent(slug)}?framework=${encodeURIComponent(framework)}`
165
- );
166
- return response.json();
167
- }
168
-
169
- /**
170
- * List workflows with optional filters.
171
- */
172
- async function listWorkflows({ framework, search, pro, free } = {}) {
173
- const params = new URLSearchParams();
174
- if (framework) params.set('framework', framework);
175
- if (pro) params.set('pro', 'true');
176
- if (free) params.set('free', 'true');
177
- if (search) params.set('search', search);
178
-
179
- const response = await apiRequest(`/api/cli/workflows?${params}`);
180
- return response.json();
181
- }
182
-
183
- /**
184
- * Get a single workflow's details + files for scaffolding.
185
- */
186
- async function getWorkflow(slug, framework = 'nextjs') {
187
- const response = await apiRequest(
188
- `/api/cli/workflows/${encodeURIComponent(slug)}?framework=${encodeURIComponent(framework)}`
189
- );
190
- return response.json();
191
- }
192
-
193
- module.exports = {
194
- apiRequest, listComponents, getComponent, verifyAuth,
195
- listBoosters, getBooster, listWorkflows, getWorkflow,
196
- API_BASE, CLI_VERSION,
197
- };
1
+ import { createRequire } from 'module';
2
+ import { fileURLToPath } from 'url';
3
+ import { dirname, join } from 'path';
4
+ import { getAuth, getApiKey } from './auth.js';
5
+
6
+ const __dirname = dirname(fileURLToPath(import.meta.url));
7
+ const require = createRequire(import.meta.url);
8
+ const pkg = require(join(__dirname, '..', '..', 'package.json'));
9
+
10
+ const CLI_VERSION = pkg.version;
11
+ const DEFAULT_API_BASE = 'https://astralkit.com';
12
+ const REQUEST_TIMEOUT_MS = 30_000;
13
+
14
+ export const API_BASE = (() => {
15
+ const override = process.env.ASTRALKIT_API_URL;
16
+ if (!override) return DEFAULT_API_BASE;
17
+ try {
18
+ const parsed = new URL(override);
19
+ if (parsed.protocol !== 'https:') {
20
+ console.warn('\x1b[33m⚠ ASTRALKIT_API_URL uses insecure protocol.\x1b[0m');
21
+ }
22
+ } catch {
23
+ console.error('\x1b[31m✖ Invalid ASTRALKIT_API_URL. Falling back to default.\x1b[0m');
24
+ return DEFAULT_API_BASE;
25
+ }
26
+ return override;
27
+ })();
28
+
29
+ export { CLI_VERSION };
30
+
31
+ export async function apiRequest(urlPath, options = {}) {
32
+ const url = `${API_BASE}${urlPath}`;
33
+
34
+ const headers = {
35
+ 'Accept': 'application/json',
36
+ 'User-Agent': `@astralkit/cli/${CLI_VERSION} node/${process.version}`,
37
+ 'X-AstralKit-CLI': CLI_VERSION,
38
+ ...options.headers,
39
+ };
40
+
41
+ const apiKey = getApiKey();
42
+ if (apiKey) {
43
+ headers['x-api-key'] = apiKey;
44
+ } else {
45
+ const auth = getAuth();
46
+ if (auth.token) headers['Authorization'] = `Bearer ${auth.token}`;
47
+ }
48
+
49
+ const controller = new AbortController();
50
+ const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
51
+
52
+ let response;
53
+ try {
54
+ response = await fetch(url, { ...options, headers, signal: controller.signal });
55
+ } catch (err) {
56
+ clearTimeout(timeout);
57
+ if (err.name === 'AbortError') {
58
+ const e = new Error('Request timed out. Check your connection and try again.');
59
+ e.code = 'TIMEOUT';
60
+ throw e;
61
+ }
62
+ if (err.code === 'ENOTFOUND' || err.cause?.code === 'ENOTFOUND') {
63
+ const e = new Error('Could not reach astralkit.com. Check your internet connection.');
64
+ e.code = 'NETWORK_ERROR';
65
+ throw e;
66
+ }
67
+ if (err.code === 'ECONNREFUSED' || err.cause?.code === 'ECONNREFUSED') {
68
+ const e = new Error('Connection refused. The AstralKit API may be temporarily down.');
69
+ e.code = 'NETWORK_ERROR';
70
+ throw e;
71
+ }
72
+ throw err;
73
+ } finally {
74
+ clearTimeout(timeout);
75
+ }
76
+
77
+ if (response.status === 401) {
78
+ const auth = getAuth();
79
+ const message = auth.expired
80
+ ? 'Session expired. Run `astralkit login` to re-authenticate.'
81
+ : 'Authentication required. Run `astralkit login` to authenticate.';
82
+ const err = new Error(message);
83
+ err.code = 'AUTH_REQUIRED';
84
+ throw err;
85
+ }
86
+
87
+ if (response.status === 403) {
88
+ const err = new Error('Pro subscription required. Visit https://astralkit.com/pricing');
89
+ err.code = 'PRO_REQUIRED';
90
+ throw err;
91
+ }
92
+
93
+ if (!response.ok) {
94
+ const body = await response.text().catch(() => '');
95
+ const err = new Error(`API error ${response.status}: ${body || response.statusText}`);
96
+ err.code = 'API_ERROR';
97
+ throw err;
98
+ }
99
+
100
+ return response;
101
+ }
102
+
103
+ export async function verifyAuth() {
104
+ const res = await apiRequest('/api/cli/whoami');
105
+ return res.json();
106
+ }
107
+
108
+ export async function listComponents({ category, framework, pro, free, search } = {}) {
109
+ const params = new URLSearchParams();
110
+ if (category) params.set('category', category);
111
+ if (framework) params.set('framework', framework);
112
+ if (pro) params.set('pro', 'true');
113
+ if (free) params.set('free', 'true');
114
+ if (search) params.set('search', search);
115
+ const res = await apiRequest(`/api/cli/components?${params}`);
116
+ return res.json();
117
+ }
118
+
119
+ export async function getComponent(slug, framework = 'html') {
120
+ const res = await apiRequest(`/api/cli/components/${encodeURIComponent(slug)}?framework=${encodeURIComponent(framework)}`);
121
+ return res.json();
122
+ }
123
+
124
+ export async function listBoosters({ framework, search, pro, free } = {}) {
125
+ const params = new URLSearchParams();
126
+ if (framework) params.set('framework', framework);
127
+ if (pro) params.set('pro', 'true');
128
+ if (free) params.set('free', 'true');
129
+ if (search) params.set('search', search);
130
+ const res = await apiRequest(`/api/cli/boosters?${params}`);
131
+ return res.json();
132
+ }
133
+
134
+ export async function getBooster(slug, framework = 'nextjs') {
135
+ const res = await apiRequest(`/api/cli/boosters/${encodeURIComponent(slug)}?framework=${encodeURIComponent(framework)}`);
136
+ return res.json();
137
+ }
138
+
139
+ export async function listWorkflows({ framework, search, pro, free } = {}) {
140
+ const params = new URLSearchParams();
141
+ if (framework) params.set('framework', framework);
142
+ if (pro) params.set('pro', 'true');
143
+ if (free) params.set('free', 'true');
144
+ if (search) params.set('search', search);
145
+ const res = await apiRequest(`/api/cli/workflows?${params}`);
146
+ return res.json();
147
+ }
148
+
149
+ export async function getWorkflow(slug, framework = 'nextjs') {
150
+ const res = await apiRequest(`/api/cli/workflows/${encodeURIComponent(slug)}?framework=${encodeURIComponent(framework)}`);
151
+ return res.json();
152
+ }