@astralkit/cli 2.2.0 → 3.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.
@@ -17,6 +17,7 @@ const {
17
17
  REQUIRED_CSS_IMPORTS,
18
18
  } = require('../lib/config');
19
19
  const { validateFramework } = require('../lib/validators');
20
+ const { getAiRulesContent } = require('../lib/ai-rules');
20
21
 
21
22
  async function initCommand(options) {
22
23
  console.log(chalk.blue.bold('AstralKit — Project Setup'));
@@ -71,21 +72,45 @@ async function initCommand(options) {
71
72
  // Component directory
72
73
  const componentDir = options.dir || 'src/components/ui';
73
74
 
74
- // ─── Step 1: Install astralkit npm package ──────────────
75
+ // ─── Step 1: Install astralkit design token package ─────
76
+ // Fetched from AstralKit API (not npm) to keep tokens private.
77
+ // Written to node_modules/astralkit/ so imports resolve normally.
75
78
  if (framework !== 'html') {
76
79
  if (!isAstralkitInstalled()) {
77
- const spinner = ora('Installing astralkit...').start();
80
+ const spinner = ora('Installing astralkit design tokens...').start();
78
81
  try {
82
+ const { apiRequest } = require('../lib/api');
83
+ const data = await apiRequest('/api/cli/sdk');
84
+
85
+ // Create node_modules/astralkit/ directory structure
86
+ const sdkDir = path.join(process.cwd(), 'node_modules', 'astralkit');
87
+ const libDir = path.join(sdkDir, 'lib');
88
+ fs.mkdirSync(libDir, { recursive: true });
89
+
90
+ // Write all files
91
+ for (const [filePath, content] of Object.entries(data.files)) {
92
+ const fullPath = path.join(sdkDir, filePath);
93
+ const dir = path.dirname(fullPath);
94
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
95
+ fs.writeFileSync(fullPath, content, 'utf-8');
96
+ }
97
+
98
+ // Also install the runtime dependencies (clsx + tailwind-merge)
79
99
  const pm = detectPackageManager();
80
- const installCmd = pm === 'yarn' ? 'yarn add astralkit'
81
- : pm === 'pnpm' ? 'pnpm add astralkit'
82
- : pm === 'bun' ? 'bun add astralkit'
83
- : 'npm install astralkit';
84
- execSync(installCmd, { stdio: 'pipe', cwd: process.cwd() });
85
- spinner.succeed('Installed astralkit');
100
+ const depsCmd = pm === 'yarn' ? 'yarn add clsx tailwind-merge'
101
+ : pm === 'pnpm' ? 'pnpm add clsx tailwind-merge'
102
+ : pm === 'bun' ? 'bun add clsx tailwind-merge'
103
+ : 'npm install clsx tailwind-merge';
104
+ execSync(depsCmd, { stdio: 'pipe', cwd: process.cwd() });
105
+
106
+ spinner.succeed('Installed astralkit design tokens');
86
107
  } catch (err) {
87
108
  spinner.fail('Failed to install astralkit');
88
- console.log(chalk.yellow('Run manually: npm install astralkit'));
109
+ if (err.message?.includes('Authentication')) {
110
+ console.log(chalk.yellow('Run `astralkit login` first, then try again.'));
111
+ } else {
112
+ console.log(chalk.yellow('Check your internet connection and try again.'));
113
+ }
89
114
  }
90
115
  } else {
91
116
  console.log(chalk.gray(' astralkit already installed'));
@@ -130,6 +155,18 @@ async function initCommand(options) {
130
155
  console.log(chalk.green('Created'), chalk.cyan(CONFIG_FILENAME));
131
156
  console.log(chalk.gray(` Framework: ${framework}`));
132
157
  console.log(chalk.gray(` Components: ${componentDir}/`));
158
+
159
+ // ─── Step 4: AI coding rules ─────────────────────────
160
+ const rulesFile = 'astralkit-ai-rules.md';
161
+ const rulesPath = path.join(process.cwd(), rulesFile);
162
+ if (!fs.existsSync(rulesPath)) {
163
+ fs.writeFileSync(rulesPath, getAiRulesContent());
164
+ console.log(chalk.green(' Created'), chalk.cyan(rulesFile));
165
+ console.log(chalk.gray(' Paste into your AI tool\'s config (.cursorrules, CLAUDE.md, etc.)'));
166
+ } else {
167
+ console.log(chalk.gray(` ${rulesFile} already exists`));
168
+ }
169
+
133
170
  console.log();
134
171
  console.log(chalk.green('Next steps:'));
135
172
  console.log(chalk.gray(' astralkit add sidebar-expanded-light Add a component'));
@@ -0,0 +1,145 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const chalk = require('chalk');
4
+ const inquirer = require('inquirer');
5
+ const { getProjectConfig } = require('../lib/config');
6
+
7
+ /**
8
+ * Remove command — cleanly uninstall a component, booster, or workflow.
9
+ */
10
+ async function removeCommand(slug, options) {
11
+ const config = getProjectConfig();
12
+ const cwd = process.cwd();
13
+
14
+ // Build a list of paths this slug might live in
15
+ const candidates = [];
16
+
17
+ // Component paths
18
+ const componentsDir = config?.componentsDir || 'src/components/ui';
19
+ for (const ext of ['.tsx', '.jsx', '.html']) {
20
+ const p = path.join(cwd, componentsDir, `${slug}${ext}`);
21
+ if (fs.existsSync(p)) {
22
+ candidates.push({ type: 'component', path: p, relative: `${componentsDir}/${slug}${ext}` });
23
+ }
24
+ }
25
+
26
+ // Booster paths (core + adapter)
27
+ const boosterCoreDirs = [
28
+ `src/lib/${slug}`,
29
+ `lib/${slug}`,
30
+ ];
31
+ const boosterAdapterDirs = [
32
+ `src/components/${slug}`,
33
+ `components/${slug}`,
34
+ ];
35
+
36
+ for (const dir of boosterCoreDirs) {
37
+ const p = path.join(cwd, dir);
38
+ if (fs.existsSync(p) && fs.statSync(p).isDirectory()) {
39
+ candidates.push({ type: 'booster-core', path: p, relative: `${dir}/` });
40
+ }
41
+ }
42
+ for (const dir of boosterAdapterDirs) {
43
+ const p = path.join(cwd, dir);
44
+ if (fs.existsSync(p) && fs.statSync(p).isDirectory()) {
45
+ candidates.push({ type: 'booster-adapter', path: p, relative: `${dir}/` });
46
+ }
47
+ }
48
+
49
+ // Workflow paths (directory with slug name)
50
+ const workflowDir = path.join(cwd, slug);
51
+ if (fs.existsSync(workflowDir) && fs.statSync(workflowDir).isDirectory()) {
52
+ const hasPackageJson = fs.existsSync(path.join(workflowDir, 'package.json'));
53
+ if (hasPackageJson) {
54
+ candidates.push({ type: 'workflow', path: workflowDir, relative: `${slug}/` });
55
+ }
56
+ }
57
+
58
+ if (candidates.length === 0) {
59
+ console.log(chalk.yellow(`Nothing found for "${slug}" in this project.`));
60
+ console.log(chalk.gray('Searched in:'));
61
+ console.log(chalk.gray(` ${componentsDir}/${slug}.*`));
62
+ console.log(chalk.gray(` src/lib/${slug}/`));
63
+ console.log(chalk.gray(` src/components/${slug}/`));
64
+ console.log(chalk.gray(` ${slug}/`));
65
+ return;
66
+ }
67
+
68
+ // Show what will be removed
69
+ console.log(chalk.white.bold(`\nFound files for "${slug}":\n`));
70
+
71
+ for (const c of candidates) {
72
+ const label = c.type === 'component' ? 'Component'
73
+ : c.type === 'workflow' ? 'Workflow'
74
+ : c.type === 'booster-core' ? 'Booster (core)'
75
+ : 'Booster (adapter)';
76
+
77
+ if (fs.statSync(c.path).isDirectory()) {
78
+ const files = getAllFiles(c.path);
79
+ console.log(chalk.red(` ${label}: ${c.relative}`));
80
+ for (const f of files.slice(0, 10)) {
81
+ console.log(chalk.gray(` ${f}`));
82
+ }
83
+ if (files.length > 10) {
84
+ console.log(chalk.gray(` ... and ${files.length - 10} more files`));
85
+ }
86
+ } else {
87
+ console.log(chalk.red(` ${label}: ${c.relative}`));
88
+ }
89
+ }
90
+
91
+ console.log();
92
+
93
+ // Confirm
94
+ if (!options.force) {
95
+ const { confirm } = await inquirer.prompt([{
96
+ type: 'confirm',
97
+ name: 'confirm',
98
+ message: `Remove ${candidates.length} item(s)?`,
99
+ default: false,
100
+ }]);
101
+
102
+ if (!confirm) {
103
+ console.log(chalk.gray('Cancelled.'));
104
+ return;
105
+ }
106
+ }
107
+
108
+ // Delete
109
+ let removed = 0;
110
+ for (const c of candidates) {
111
+ try {
112
+ if (fs.statSync(c.path).isDirectory()) {
113
+ fs.rmSync(c.path, { recursive: true, force: true });
114
+ } else {
115
+ fs.unlinkSync(c.path);
116
+ }
117
+ console.log(chalk.green(` ✓ Removed ${c.relative}`));
118
+ removed++;
119
+ } catch (err) {
120
+ console.log(chalk.red(` ✖ Failed to remove ${c.relative}: ${err.message}`));
121
+ }
122
+ }
123
+
124
+ console.log();
125
+ console.log(chalk.green(`Removed ${removed} item(s) for "${slug}".`));
126
+ }
127
+
128
+ /**
129
+ * Get all file paths (relative) in a directory recursively.
130
+ */
131
+ function getAllFiles(dir, base = '') {
132
+ const results = [];
133
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
134
+ const rel = base ? `${base}/${entry.name}` : entry.name;
135
+ if (entry.name === 'node_modules' || entry.name === '.next') continue;
136
+ if (entry.isDirectory()) {
137
+ results.push(...getAllFiles(path.join(dir, entry.name), rel));
138
+ } else {
139
+ results.push(rel);
140
+ }
141
+ }
142
+ return results;
143
+ }
144
+
145
+ module.exports = { removeCommand };
@@ -0,0 +1,50 @@
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 };
@@ -0,0 +1,112 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const { execSync } = require('child_process');
4
+
5
+ /**
6
+ * Detect the package manager from lockfiles in the project directory.
7
+ * Falls back to npm if nothing detected.
8
+ */
9
+ function detectPackageManager(dir = process.cwd()) {
10
+ if (fs.existsSync(path.join(dir, 'bun.lockb')) || fs.existsSync(path.join(dir, 'bun.lock'))) return 'bun';
11
+ if (fs.existsSync(path.join(dir, 'pnpm-lock.yaml'))) return 'pnpm';
12
+ if (fs.existsSync(path.join(dir, 'yarn.lock'))) return 'yarn';
13
+ return 'npm';
14
+ }
15
+
16
+ /**
17
+ * Get the install command for a package manager.
18
+ */
19
+ function getInstallCommand(pm, packages = []) {
20
+ if (packages.length === 0) {
21
+ // Install all deps from package.json
22
+ switch (pm) {
23
+ case 'bun': return 'bun install';
24
+ case 'pnpm': return 'pnpm install';
25
+ case 'yarn': return 'yarn';
26
+ default: return 'npm install';
27
+ }
28
+ }
29
+
30
+ // Install specific packages
31
+ const pkgList = packages.join(' ');
32
+ switch (pm) {
33
+ case 'bun': return `bun add ${pkgList}`;
34
+ case 'pnpm': return `pnpm add ${pkgList}`;
35
+ case 'yarn': return `yarn add ${pkgList}`;
36
+ default: return `npm install ${pkgList}`;
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Get the run command for a package manager.
42
+ */
43
+ function getRunCommand(pm, script) {
44
+ switch (pm) {
45
+ case 'bun': return `bun run ${script}`;
46
+ case 'pnpm': return `pnpm ${script}`;
47
+ case 'yarn': return `yarn ${script}`;
48
+ default: return `npm run ${script}`;
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Get the exec command (npx equivalent) for a package manager.
54
+ */
55
+ function getExecCommand(pm) {
56
+ switch (pm) {
57
+ case 'bun': return 'bunx';
58
+ case 'pnpm': return 'pnpx';
59
+ case 'yarn': return 'yarn dlx';
60
+ default: return 'npx';
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Run an install command silently, capturing output.
66
+ * Returns { success, error? }
67
+ */
68
+ function runInstall(pm, cwd, packages = []) {
69
+ const cmd = getInstallCommand(pm, packages);
70
+ try {
71
+ execSync(cmd, { cwd, stdio: 'pipe', timeout: 120000 });
72
+ return { success: true };
73
+ } catch (err) {
74
+ const stderr = err.stderr?.toString() || '';
75
+
76
+ // Detect common errors and provide friendly messages
77
+ if (stderr.includes('ERESOLVE') || stderr.includes('peer dep')) {
78
+ return {
79
+ success: false,
80
+ error: 'Dependency conflict detected.',
81
+ hint: pm === 'npm'
82
+ ? `Try: npm install ${packages.join(' ')} --legacy-peer-deps`
83
+ : `Try: ${cmd} --force`,
84
+ raw: stderr,
85
+ };
86
+ }
87
+
88
+ if (stderr.includes('ENOENT') || stderr.includes('not found')) {
89
+ return {
90
+ success: false,
91
+ error: `${pm} is not installed or not in PATH.`,
92
+ hint: `Install ${pm} or use a different package manager.`,
93
+ raw: stderr,
94
+ };
95
+ }
96
+
97
+ return {
98
+ success: false,
99
+ error: 'Install failed.',
100
+ hint: `Run manually: ${cmd}`,
101
+ raw: stderr.slice(0, 500),
102
+ };
103
+ }
104
+ }
105
+
106
+ module.exports = {
107
+ detectPackageManager,
108
+ getInstallCommand,
109
+ getRunCommand,
110
+ getExecCommand,
111
+ runInstall,
112
+ };