@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,186 +1,46 @@
1
- const chalk = require('chalk');
2
- const ora = require('ora');
3
- const { listComponents, listBoosters, listWorkflows } = require('../lib/api');
4
- const { validateFramework } = require('../lib/validators');
5
-
6
- /**
7
- * Route to the correct list handler based on flags.
8
- */
9
- async function listCommand(options) {
10
- if (options.framework && !validateFramework(options.framework)) {
11
- process.exit(1);
12
- }
13
-
14
- if (options.boosters) {
15
- return listBoostersCommand(options);
16
- }
17
- if (options.workflows) {
18
- return listWorkflowsCommand(options);
19
- }
20
- return listComponentsCommand(options);
21
- }
22
-
23
- /**
24
- * List UI components (existing behavior).
25
- */
26
- async function listComponentsCommand(options) {
27
- const spinner = ora('Fetching components...').start();
28
-
29
- try {
30
- const data = await listComponents({
31
- category: options.category,
32
- framework: options.framework,
33
- pro: options.pro,
34
- free: options.free,
35
- });
36
-
37
- spinner.stop();
38
-
39
- if (!data.components || data.components.length === 0) {
40
- console.log(chalk.yellow('No components found.'));
41
- return;
42
- }
43
-
44
- const grouped = {};
45
- for (const comp of data.components) {
46
- const cat = comp.category || 'Uncategorized';
47
- if (!grouped[cat]) grouped[cat] = [];
48
- grouped[cat].push(comp);
49
- }
50
-
51
- console.log(chalk.blue.bold(`AstralKit Components (${data.components.length} total)`));
52
- console.log();
53
-
54
- for (const [category, components] of Object.entries(grouped)) {
55
- console.log(chalk.white.bold(` ${category}`));
56
- for (const comp of components) {
57
- const proTag = comp.is_pro ? chalk.magenta(' PRO') : '';
58
- const frameworks = comp.supported_frameworks?.length
59
- ? chalk.gray(` [${comp.supported_frameworks.join(', ')}]`)
60
- : '';
61
- console.log(` ${chalk.cyan(comp.slug)}${proTag}${frameworks}`);
62
- if (comp.short_description) {
63
- console.log(` ${chalk.gray(comp.short_description)}`);
64
- }
65
- }
66
- console.log();
67
- }
68
-
69
- console.log(chalk.gray('Run'), chalk.cyan('astralkit add <component>'), chalk.gray('to install.'));
70
-
71
- } catch (err) {
72
- spinner.fail('Failed to list components.');
73
- console.error(chalk.red(err.message));
74
- process.exit(1);
75
- }
76
- }
77
-
78
- /**
79
- * List available boosters.
80
- */
81
- async function listBoostersCommand(options) {
82
- const spinner = ora('Fetching boosters...').start();
83
-
84
- try {
85
- const data = await listBoosters({
86
- framework: options.framework,
87
- pro: options.pro,
88
- free: options.free,
89
- });
90
-
91
- spinner.stop();
92
-
93
- if (!data.boosters || data.boosters.length === 0) {
94
- console.log(chalk.yellow('No boosters found.'));
95
- return;
96
- }
97
-
98
- const grouped = {};
99
- for (const booster of data.boosters) {
100
- const cat = booster.category || 'Uncategorized';
101
- if (!grouped[cat]) grouped[cat] = [];
102
- grouped[cat].push(booster);
103
- }
104
-
105
- console.log(chalk.blue.bold(`AstralKit Boosters (${data.boosters.length} total)`));
106
- console.log();
107
-
108
- for (const [category, boosters] of Object.entries(grouped)) {
109
- console.log(chalk.white.bold(` ${category}`));
110
- for (const b of boosters) {
111
- const proTag = b.is_pro ? chalk.magenta(' PRO') : '';
112
- const frameworks = b.frameworks?.length
113
- ? chalk.gray(` [${b.frameworks.join(', ')}]`)
114
- : '';
115
- console.log(` ${chalk.cyan(b.slug)}${proTag}${frameworks}`);
116
- if (b.description) {
117
- console.log(` ${chalk.gray(b.description)}`);
118
- }
119
- }
120
- console.log();
121
- }
122
-
123
- console.log(chalk.gray('Run'), chalk.cyan('astralkit add --booster <name>'), chalk.gray('to install.'));
124
-
125
- } catch (err) {
126
- spinner.fail('Failed to list boosters.');
127
- console.error(chalk.red(err.message));
128
- process.exit(1);
129
- }
130
- }
131
-
132
- /**
133
- * List available workflows.
134
- */
135
- async function listWorkflowsCommand(options) {
136
- const spinner = ora('Fetching workflows...').start();
137
-
138
- try {
139
- const data = await listWorkflows({
140
- framework: options.framework,
141
- pro: options.pro,
142
- free: options.free,
143
- });
144
-
145
- spinner.stop();
146
-
147
- if (!data.workflows || data.workflows.length === 0) {
148
- console.log(chalk.yellow('No workflows found.'));
149
- return;
150
- }
151
-
152
- const grouped = {};
153
- for (const workflow of data.workflows) {
154
- const cat = workflow.category || 'Uncategorized';
155
- if (!grouped[cat]) grouped[cat] = [];
156
- grouped[cat].push(workflow);
157
- }
158
-
159
- console.log(chalk.blue.bold(`AstralKit Workflows (${data.workflows.length} total)`));
160
- console.log();
161
-
162
- for (const [category, workflows] of Object.entries(grouped)) {
163
- console.log(chalk.white.bold(` ${category}`));
164
- for (const w of workflows) {
165
- const proTag = w.is_pro ? chalk.magenta(' PRO') : '';
166
- const frameworks = w.frameworks?.length
167
- ? chalk.gray(` [${w.frameworks.join(', ')}]`)
168
- : '';
169
- console.log(` ${chalk.cyan(w.slug)}${proTag}${frameworks}`);
170
- if (w.description) {
171
- console.log(` ${chalk.gray(w.description)}`);
172
- }
173
- }
174
- console.log();
175
- }
176
-
177
- console.log(chalk.gray('Run'), chalk.cyan('astralkit add --workflow <name>'), chalk.gray('to scaffold.'));
178
-
179
- } catch (err) {
180
- spinner.fail('Failed to list workflows.');
181
- console.error(chalk.red(err.message));
182
- process.exit(1);
183
- }
184
- }
185
-
186
- module.exports = { listCommand };
1
+ import pc from 'picocolors';
2
+ import * as p from '@clack/prompts';
3
+ import { listComponents, listBoosters, listWorkflows } from '../lib/api.js';
4
+
5
+ const FRAMEWORK_LABELS = { nextjs: 'Next.js', react: 'React', html: 'HTML' };
6
+
7
+ export async function listCommand(options) {
8
+ const ora = (await import('ora')).default;
9
+ const type = options.boosters ? 'boosters' : options.workflows ? 'workflows' : 'components';
10
+ const spinner = ora(`Fetching ${type}...`).start();
11
+
12
+ try {
13
+ let items;
14
+ const filters = { framework: options.framework, pro: options.pro, free: options.free, category: options.category };
15
+
16
+ if (options.boosters) items = await listBoosters(filters);
17
+ else if (options.workflows) items = await listWorkflows(filters);
18
+ else items = await listComponents(filters);
19
+
20
+ spinner.stop();
21
+
22
+ if (!items?.length) {
23
+ p.log.info(`No ${type} found.`);
24
+ return;
25
+ }
26
+
27
+ console.log();
28
+ console.log(pc.bold(` ${items.length} ${type} available\n`));
29
+
30
+ for (const item of items) {
31
+ const proTag = item.is_pro ? pc.magenta(' [PRO]') : pc.green(' [FREE]');
32
+ const fw = item.frameworks?.map(f => FRAMEWORK_LABELS[f] || f).join(', ') || '';
33
+ console.log(` ${pc.cyan(item.slug || item.name)}${proTag}`);
34
+ if (item.description) console.log(pc.gray(` ${item.description}`));
35
+ if (fw) console.log(pc.gray(` Frameworks: ${fw}`));
36
+ console.log();
37
+ }
38
+
39
+ console.log(pc.gray(` astralkit add <name> to install`));
40
+ } catch (err) {
41
+ spinner.fail('Failed to fetch items.');
42
+ if (err.code === 'AUTH_REQUIRED') p.log.warn(err.message);
43
+ else console.error(pc.red(err.message));
44
+ process.exit(1);
45
+ }
46
+ }
@@ -1,157 +1,133 @@
1
- const http = require('http');
2
- const crypto = require('crypto');
3
- const chalk = require('chalk');
4
- const ora = require('ora');
5
- const open = require('open');
6
- const { getAuth, saveAuth } = require('../lib/auth');
7
- const { API_BASE } = require('../lib/api');
8
-
9
- const DEFAULT_CALLBACK_PORT = 9876;
10
-
11
- async function loginCommand() {
12
- console.log(chalk.blue.bold('AstralKit — Login'));
13
- console.log();
14
-
15
- // CI/CD shortcut: if ASTRALKIT_TOKEN is set, skip browser login
16
- if (process.env.ASTRALKIT_TOKEN) {
17
- const auth = getAuth();
18
- if (auth.token) {
19
- console.log(chalk.green('Authenticated via ASTRALKIT_TOKEN environment variable.'));
20
- } else if (auth.expired) {
21
- console.log(chalk.yellow('ASTRALKIT_TOKEN is expired. Please generate a new token.'));
22
- process.exit(1);
23
- } else {
24
- console.log(chalk.red('ASTRALKIT_TOKEN is set but invalid.'));
25
- process.exit(1);
26
- }
27
- return;
28
- }
29
-
30
- const callbackPort = parseInt(process.env.ASTRALKIT_LOGIN_PORT || String(DEFAULT_CALLBACK_PORT), 10);
31
- if (isNaN(callbackPort) || callbackPort < 1024 || callbackPort > 65535) {
32
- console.error(chalk.red('Invalid ASTRALKIT_LOGIN_PORT. Must be between 1024 and 65535.'));
33
- process.exit(1);
34
- }
35
-
36
- // Generate CSRF state token
37
- const state = crypto.randomBytes(32).toString('hex');
38
-
39
- const spinner = ora('Waiting for browser authentication...').start();
40
-
41
- try {
42
- const token = await new Promise((resolve, reject) => {
43
- const server = http.createServer((req, res) => {
44
- const url = new URL(req.url, `http://localhost:${callbackPort}`);
45
-
46
- // Restrict CORS to AstralKit domain only
47
- const origin = req.headers['origin'] || '';
48
- const allowedOrigin = API_BASE.startsWith('http://localhost')
49
- ? origin // Allow localhost in development
50
- : 'https://astralkit.com';
51
-
52
- res.setHeader('Access-Control-Allow-Origin', allowedOrigin);
53
- res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
54
- res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
55
-
56
- // Handle CORS preflight
57
- if (req.method === 'OPTIONS') {
58
- res.writeHead(204);
59
- res.end();
60
- return;
61
- }
62
-
63
- if (url.pathname === '/callback' && req.method === 'POST') {
64
- let body = '';
65
-
66
- // Limit request body size (1MB max)
67
- let bodySize = 0;
68
- req.on('data', (chunk) => {
69
- bodySize += chunk.length;
70
- if (bodySize > 1_048_576) {
71
- res.writeHead(413);
72
- res.end(JSON.stringify({ error: 'Request body too large' }));
73
- server.close();
74
- reject(new Error('Received oversized callback payload.'));
75
- return;
76
- }
77
- body += chunk;
78
- });
79
-
80
- req.on('end', () => {
81
- try {
82
- const data = JSON.parse(body);
83
-
84
- // Verify CSRF state token
85
- if (data.state !== state) {
86
- res.writeHead(403, { 'Content-Type': 'application/json' });
87
- res.end(JSON.stringify({ error: 'State mismatch' }));
88
- server.close();
89
- reject(new Error('Authentication failed — state mismatch. Please try again.'));
90
- return;
91
- }
92
-
93
- const token = data.token;
94
- const email = data.email;
95
- const plan = data.plan;
96
-
97
- if (token) {
98
- res.writeHead(200, { 'Content-Type': 'application/json' });
99
- res.end(JSON.stringify({ ok: true }));
100
- server.close();
101
- resolve({ token, email, plan });
102
- } else {
103
- res.writeHead(400, { 'Content-Type': 'application/json' });
104
- res.end(JSON.stringify({ error: 'Missing token' }));
105
- server.close();
106
- reject(new Error('Authentication failed — no token received.'));
107
- }
108
- } catch {
109
- res.writeHead(400, { 'Content-Type': 'application/json' });
110
- res.end(JSON.stringify({ error: 'Invalid request body' }));
111
- }
112
- });
113
- } else {
114
- res.writeHead(404);
115
- res.end();
116
- }
117
- });
118
-
119
- server.listen(callbackPort, () => {
120
- const loginUrl = `${API_BASE}/cli-auth?callback=${encodeURIComponent(`http://localhost:${callbackPort}/callback`)}&state=${state}`;
121
- open(loginUrl);
122
- });
123
-
124
- // Timeout after 5 minutes
125
- setTimeout(() => {
126
- server.close();
127
- reject(new Error('Authentication timed out. Please try again.'));
128
- }, 5 * 60 * 1000);
129
-
130
- server.on('error', (err) => {
131
- if (err.code === 'EADDRINUSE') {
132
- reject(new Error(
133
- `Port ${callbackPort} is in use. ` +
134
- `Close the conflicting process or set ASTRALKIT_LOGIN_PORT to a different port.`
135
- ));
136
- } else {
137
- reject(err);
138
- }
139
- });
140
- });
141
-
142
- // Save credentials
143
- saveAuth(token);
144
- spinner.succeed('Authenticated successfully!');
145
- console.log(chalk.gray(` Email: ${token.email || 'unknown'}`));
146
- console.log(chalk.gray(` Plan: ${token.plan || 'free'}`));
147
- console.log();
148
- console.log(chalk.green('You can now install pro components:'));
149
- console.log(chalk.cyan(' astralkit add pricing-table'));
150
-
151
- } catch (err) {
152
- spinner.fail(err.message);
153
- process.exit(1);
154
- }
155
- }
156
-
157
- module.exports = { loginCommand };
1
+ import http from 'http';
2
+ import crypto from 'crypto';
3
+ import pc from 'picocolors';
4
+ import * as p from '@clack/prompts';
5
+ import { getAuth, saveAuth } from '../lib/auth.js';
6
+ import { API_BASE } from '../lib/api.js';
7
+
8
+ const DEFAULT_CALLBACK_PORT = 9876;
9
+
10
+ export async function loginCommand() {
11
+ p.intro(pc.bgBlue(pc.white(' AstralKit Login ')));
12
+
13
+ // CI/CD shortcut
14
+ if (process.env.ASTRALKIT_TOKEN) {
15
+ const auth = getAuth();
16
+ if (auth.token) {
17
+ p.outro(pc.green('Authenticated via ASTRALKIT_TOKEN'));
18
+ } else if (auth.expired) {
19
+ p.log.warn('ASTRALKIT_TOKEN is expired. Generate a new one.');
20
+ process.exit(1);
21
+ } else {
22
+ p.log.error('ASTRALKIT_TOKEN is set but invalid.');
23
+ process.exit(1);
24
+ }
25
+ return;
26
+ }
27
+
28
+ const callbackPort = parseInt(process.env.ASTRALKIT_LOGIN_PORT || String(DEFAULT_CALLBACK_PORT), 10);
29
+ if (isNaN(callbackPort) || callbackPort < 1024 || callbackPort > 65535) {
30
+ p.log.error('Invalid ASTRALKIT_LOGIN_PORT. Must be 1024–65535.');
31
+ process.exit(1);
32
+ }
33
+
34
+ const state = crypto.randomBytes(32).toString('hex');
35
+ const ora = (await import('ora')).default;
36
+ const { default: open } = await import('open');
37
+
38
+ const spinner = ora('Waiting for browser authentication...').start();
39
+
40
+ try {
41
+ const authData = await new Promise((resolve, reject) => {
42
+ const server = http.createServer((req, res) => {
43
+ const url = new URL(req.url, `http://localhost:${callbackPort}`);
44
+
45
+ const origin = req.headers['origin'] || '';
46
+ const allowedOrigin = API_BASE.startsWith('http://localhost') ? origin : 'https://astralkit.com';
47
+ res.setHeader('Access-Control-Allow-Origin', allowedOrigin);
48
+ res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
49
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
50
+
51
+ if (req.method === 'OPTIONS') { res.writeHead(204); res.end(); return; }
52
+
53
+ if (url.pathname === '/callback' && req.method === 'POST') {
54
+ let body = '';
55
+ let bodySize = 0;
56
+
57
+ req.on('data', chunk => {
58
+ bodySize += chunk.length;
59
+ if (bodySize > 1_048_576) {
60
+ res.writeHead(413);
61
+ res.end(JSON.stringify({ error: 'Request body too large' }));
62
+ server.close();
63
+ reject(new Error('Received oversized callback payload.'));
64
+ return;
65
+ }
66
+ body += chunk;
67
+ });
68
+
69
+ req.on('end', () => {
70
+ try {
71
+ const data = JSON.parse(body);
72
+ if (data.state !== state) {
73
+ res.writeHead(403, { 'Content-Type': 'application/json' });
74
+ res.end(JSON.stringify({ error: 'State mismatch' }));
75
+ server.close();
76
+ reject(new Error('Authentication failed — state mismatch.'));
77
+ return;
78
+ }
79
+
80
+ if (data.token) {
81
+ res.writeHead(200, { 'Content-Type': 'application/json' });
82
+ res.end(JSON.stringify({ ok: true }));
83
+ server.close();
84
+ resolve(data);
85
+ } else {
86
+ res.writeHead(400, { 'Content-Type': 'application/json' });
87
+ res.end(JSON.stringify({ error: 'Missing token' }));
88
+ server.close();
89
+ reject(new Error('Authentication failed — no token received.'));
90
+ }
91
+ } catch {
92
+ res.writeHead(400);
93
+ res.end(JSON.stringify({ error: 'Invalid body' }));
94
+ }
95
+ });
96
+ } else {
97
+ res.writeHead(404); res.end();
98
+ }
99
+ });
100
+
101
+ server.listen(callbackPort, () => {
102
+ const loginUrl = `${API_BASE}/cli-auth?callback=${encodeURIComponent(`http://localhost:${callbackPort}/callback`)}&state=${state}`;
103
+ open(loginUrl);
104
+ });
105
+
106
+ setTimeout(() => {
107
+ server.close();
108
+ reject(new Error('Authentication timed out. Please try again.'));
109
+ }, 5 * 60 * 1000);
110
+
111
+ server.on('error', err => {
112
+ if (err.code === 'EADDRINUSE') {
113
+ reject(new Error(
114
+ `Port ${callbackPort} is in use. Set ASTRALKIT_LOGIN_PORT to a different port.`
115
+ ));
116
+ } else {
117
+ reject(err);
118
+ }
119
+ });
120
+ });
121
+
122
+ saveAuth(authData);
123
+ spinner.succeed('Authenticated successfully!');
124
+ console.log(pc.gray(` Email: ${authData.email || 'unknown'}`));
125
+ console.log(pc.gray(` Plan: ${authData.plan || 'free'}`));
126
+ console.log();
127
+ p.outro(pc.green('You can now install Pro components: ' + pc.cyan('astralkit add pricing-table')));
128
+
129
+ } catch (err) {
130
+ spinner.fail(err.message);
131
+ process.exit(1);
132
+ }
133
+ }