@astralkit/cli 3.0.5 → 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 +1 -1
- package/src/commands/init.js +188 -205
package/package.json
CHANGED
package/src/commands/init.js
CHANGED
|
@@ -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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
console.log(chalk.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const
|
|
102
|
-
fs.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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 };
|