@astralkit/cli 2.1.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.
- package/LICENSE +52 -21
- package/README.md +362 -267
- package/bin/index.js +10 -1
- package/package.json +3 -5
- package/src/commands/add.js +642 -373
- package/src/commands/diff.js +1 -1
- package/src/commands/init.js +119 -11
- package/src/commands/remove.js +145 -0
- package/src/commands/update.js +1 -4
- package/src/lib/ai-rules.js +50 -0
- package/src/lib/config.js +70 -23
- package/src/lib/pkg-manager.js +112 -0
- package/src/lib/validators.js +29 -6
package/src/commands/diff.js
CHANGED
|
@@ -79,7 +79,7 @@ async function diffCommand(componentSlug, options) {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
// Check for local files not on remote
|
|
82
|
-
|
|
82
|
+
{
|
|
83
83
|
const localFiles = fs.readdirSync(targetDir);
|
|
84
84
|
const remoteFilenames = new Set(data.files.map(f => f.path));
|
|
85
85
|
for (const localFile of localFiles) {
|
package/src/commands/init.js
CHANGED
|
@@ -1,7 +1,23 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
1
3
|
const chalk = require('chalk');
|
|
2
4
|
const inquirer = require('inquirer');
|
|
3
|
-
const
|
|
5
|
+
const ora = require('ora');
|
|
6
|
+
const { execSync } = require('child_process');
|
|
7
|
+
const {
|
|
8
|
+
detectFramework,
|
|
9
|
+
defaultConfig,
|
|
10
|
+
writeProjectConfig,
|
|
11
|
+
getProjectConfig,
|
|
12
|
+
CONFIG_FILENAME,
|
|
13
|
+
isAstralkitInstalled,
|
|
14
|
+
findCssEntryFile,
|
|
15
|
+
getMissingCssImports,
|
|
16
|
+
addCssImports,
|
|
17
|
+
REQUIRED_CSS_IMPORTS,
|
|
18
|
+
} = require('../lib/config');
|
|
4
19
|
const { validateFramework } = require('../lib/validators');
|
|
20
|
+
const { getAiRulesContent } = require('../lib/ai-rules');
|
|
5
21
|
|
|
6
22
|
async function initCommand(options) {
|
|
7
23
|
console.log(chalk.blue.bold('AstralKit — Project Setup'));
|
|
@@ -42,12 +58,8 @@ async function initCommand(options) {
|
|
|
42
58
|
{ name: `${detected} (detected)`, value: detected },
|
|
43
59
|
{ name: 'HTML + Tailwind', value: 'html' },
|
|
44
60
|
{ name: 'React', value: 'react' },
|
|
45
|
-
{ name: 'Vue', value: 'vue' },
|
|
46
61
|
{ name: 'Next.js', value: 'nextjs' },
|
|
47
|
-
|
|
48
|
-
{ name: 'Svelte', value: 'svelte' },
|
|
49
|
-
{ name: 'Astro', value: 'astro' },
|
|
50
|
-
].filter((c, i, arr) => {
|
|
62
|
+
].filter((c, i) => {
|
|
51
63
|
// Remove duplicate if detected matches a named option
|
|
52
64
|
if (i === 0) return true;
|
|
53
65
|
return c.value !== detected;
|
|
@@ -60,20 +72,116 @@ async function initCommand(options) {
|
|
|
60
72
|
// Component directory
|
|
61
73
|
const componentDir = options.dir || 'src/components/ui';
|
|
62
74
|
|
|
63
|
-
//
|
|
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.
|
|
78
|
+
if (framework !== 'html') {
|
|
79
|
+
if (!isAstralkitInstalled()) {
|
|
80
|
+
const spinner = ora('Installing astralkit design tokens...').start();
|
|
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)
|
|
99
|
+
const pm = detectPackageManager();
|
|
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');
|
|
107
|
+
} catch (err) {
|
|
108
|
+
spinner.fail('Failed to 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
|
+
}
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
console.log(chalk.gray(' astralkit already installed'));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// ─── Step 2: Add CSS imports ────────────────────────────
|
|
121
|
+
const cssEntry = findCssEntryFile();
|
|
122
|
+
if (cssEntry) {
|
|
123
|
+
const missing = getMissingCssImports(cssEntry.path);
|
|
124
|
+
if (missing.length > 0) {
|
|
125
|
+
console.log();
|
|
126
|
+
console.log(chalk.gray(`Found CSS entry: ${cssEntry.relative}`));
|
|
127
|
+
const { addImports } = await inquirer.prompt([{
|
|
128
|
+
type: 'confirm',
|
|
129
|
+
name: 'addImports',
|
|
130
|
+
message: `Add AstralKit imports to ${cssEntry.relative}?`,
|
|
131
|
+
default: true,
|
|
132
|
+
}]);
|
|
133
|
+
if (addImports) {
|
|
134
|
+
addCssImports(cssEntry.path, missing);
|
|
135
|
+
console.log(chalk.green(' Added CSS imports'));
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
console.log(chalk.gray(' CSS imports already present'));
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
console.log();
|
|
142
|
+
console.log(chalk.yellow('No CSS entry file found.'));
|
|
143
|
+
console.log(chalk.gray('Add these imports to your main CSS file:'));
|
|
144
|
+
for (const imp of REQUIRED_CSS_IMPORTS) {
|
|
145
|
+
console.log(chalk.cyan(` ${imp}`));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ─── Step 3: Write config ──────────────────────────────
|
|
64
150
|
const config = defaultConfig(framework);
|
|
65
151
|
config.componentsDir = componentDir;
|
|
66
|
-
|
|
152
|
+
writeProjectConfig(config);
|
|
67
153
|
|
|
68
154
|
console.log();
|
|
69
155
|
console.log(chalk.green('Created'), chalk.cyan(CONFIG_FILENAME));
|
|
70
156
|
console.log(chalk.gray(` Framework: ${framework}`));
|
|
71
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
|
+
|
|
72
170
|
console.log();
|
|
73
171
|
console.log(chalk.green('Next steps:'));
|
|
74
|
-
console.log(chalk.gray(' astralkit add
|
|
75
|
-
console.log(chalk.gray(' astralkit list
|
|
76
|
-
console.log(chalk.gray(' astralkit login
|
|
172
|
+
console.log(chalk.gray(' astralkit add sidebar-expanded-light Add a component'));
|
|
173
|
+
console.log(chalk.gray(' astralkit list Browse components'));
|
|
174
|
+
console.log(chalk.gray(' astralkit login Unlock pro components'));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Detect which package manager the project uses.
|
|
179
|
+
*/
|
|
180
|
+
function detectPackageManager(dir = process.cwd()) {
|
|
181
|
+
if (fs.existsSync(path.join(dir, 'bun.lockb')) || fs.existsSync(path.join(dir, 'bun.lock'))) return 'bun';
|
|
182
|
+
if (fs.existsSync(path.join(dir, 'pnpm-lock.yaml'))) return 'pnpm';
|
|
183
|
+
if (fs.existsSync(path.join(dir, 'yarn.lock'))) return 'yarn';
|
|
184
|
+
return 'npm';
|
|
77
185
|
}
|
|
78
186
|
|
|
79
187
|
module.exports = { initCommand };
|
|
@@ -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 };
|
package/src/commands/update.js
CHANGED
|
@@ -101,10 +101,7 @@ async function updateCommand(componentSlug, options) {
|
|
|
101
101
|
continue;
|
|
102
102
|
}
|
|
103
103
|
const filePath = path.resolve(targetDir, file.path);
|
|
104
|
-
|
|
105
|
-
if (!fs.existsSync(fileDir)) {
|
|
106
|
-
fs.mkdirSync(fileDir, { recursive: true });
|
|
107
|
-
}
|
|
104
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
108
105
|
fs.writeFileSync(filePath, file.content);
|
|
109
106
|
}
|
|
110
107
|
|
|
@@ -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 };
|
package/src/lib/config.js
CHANGED
|
@@ -11,7 +11,6 @@ function defaultConfig(framework = 'html') {
|
|
|
11
11
|
$schema: 'https://astralkit.com/schema/config.json',
|
|
12
12
|
framework,
|
|
13
13
|
componentsDir: 'src/components/ui',
|
|
14
|
-
tokensImport: '@astralkit/tokens/css',
|
|
15
14
|
};
|
|
16
15
|
}
|
|
17
16
|
|
|
@@ -70,33 +69,12 @@ function detectFramework(dir = process.cwd()) {
|
|
|
70
69
|
return 'nextjs';
|
|
71
70
|
}
|
|
72
71
|
|
|
73
|
-
// Check for
|
|
74
|
-
if (fs.existsSync(path.join(dir, 'nuxt.config.ts')) || fs.existsSync(path.join(dir, 'nuxt.config.js'))) {
|
|
75
|
-
return 'vue';
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Check for Svelte
|
|
79
|
-
if (fs.existsSync(path.join(dir, 'svelte.config.js')) || fs.existsSync(path.join(dir, 'svelte.config.ts'))) {
|
|
80
|
-
return 'svelte';
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Check for Astro
|
|
84
|
-
if (fs.existsSync(path.join(dir, 'astro.config.mjs')) || fs.existsSync(path.join(dir, 'astro.config.ts'))) {
|
|
85
|
-
return 'astro';
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Check for Angular
|
|
89
|
-
if (fs.existsSync(path.join(dir, 'angular.json'))) {
|
|
90
|
-
return 'angular';
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Check package.json for Vue/React
|
|
72
|
+
// Check package.json for React
|
|
94
73
|
const pkgPath = path.join(dir, 'package.json');
|
|
95
74
|
if (fs.existsSync(pkgPath)) {
|
|
96
75
|
try {
|
|
97
76
|
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
98
77
|
const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
99
|
-
if (allDeps['vue']) return 'vue';
|
|
100
78
|
if (allDeps['react']) return 'react';
|
|
101
79
|
} catch {
|
|
102
80
|
// Ignore
|
|
@@ -106,6 +84,69 @@ function detectFramework(dir = process.cwd()) {
|
|
|
106
84
|
return 'html';
|
|
107
85
|
}
|
|
108
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Check if the astralkit npm package is installed in the project.
|
|
89
|
+
*/
|
|
90
|
+
function isAstralkitInstalled(dir = process.cwd()) {
|
|
91
|
+
return fs.existsSync(path.join(dir, 'node_modules', 'astralkit', 'package.json'));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Common CSS file locations to search for when adding imports.
|
|
96
|
+
*/
|
|
97
|
+
const CSS_ENTRY_CANDIDATES = [
|
|
98
|
+
'src/index.css',
|
|
99
|
+
'src/app/globals.css',
|
|
100
|
+
'src/styles/globals.css',
|
|
101
|
+
'src/styles/index.css',
|
|
102
|
+
'app/globals.css',
|
|
103
|
+
'styles/globals.css',
|
|
104
|
+
'index.css',
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
const REQUIRED_CSS_IMPORTS = [
|
|
108
|
+
'@import "tailwindcss";',
|
|
109
|
+
'@import "astralkit/theme";',
|
|
110
|
+
'@import "astralkit/utilities";',
|
|
111
|
+
];
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Find the project's CSS entry file.
|
|
115
|
+
*/
|
|
116
|
+
function findCssEntryFile(dir = process.cwd()) {
|
|
117
|
+
for (const candidate of CSS_ENTRY_CANDIDATES) {
|
|
118
|
+
const filePath = path.join(dir, candidate);
|
|
119
|
+
if (fs.existsSync(filePath)) {
|
|
120
|
+
return { path: filePath, relative: candidate };
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Check which of the 3 required CSS imports are missing from a file.
|
|
128
|
+
* Returns array of missing import lines.
|
|
129
|
+
*/
|
|
130
|
+
function getMissingCssImports(cssFilePath) {
|
|
131
|
+
try {
|
|
132
|
+
const content = fs.readFileSync(cssFilePath, 'utf8');
|
|
133
|
+
return REQUIRED_CSS_IMPORTS.filter(imp => !content.includes(imp));
|
|
134
|
+
} catch {
|
|
135
|
+
return REQUIRED_CSS_IMPORTS;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Add missing CSS imports to the top of a file.
|
|
141
|
+
*/
|
|
142
|
+
function addCssImports(cssFilePath, imports) {
|
|
143
|
+
const existing = fs.existsSync(cssFilePath)
|
|
144
|
+
? fs.readFileSync(cssFilePath, 'utf8')
|
|
145
|
+
: '';
|
|
146
|
+
const newContent = imports.join('\n') + '\n\n' + existing;
|
|
147
|
+
fs.writeFileSync(cssFilePath, newContent);
|
|
148
|
+
}
|
|
149
|
+
|
|
109
150
|
module.exports = {
|
|
110
151
|
CONFIG_FILENAME,
|
|
111
152
|
defaultConfig,
|
|
@@ -113,4 +154,10 @@ module.exports = {
|
|
|
113
154
|
getProjectConfig,
|
|
114
155
|
writeProjectConfig,
|
|
115
156
|
detectFramework,
|
|
157
|
+
isAstralkitInstalled,
|
|
158
|
+
CSS_ENTRY_CANDIDATES,
|
|
159
|
+
REQUIRED_CSS_IMPORTS,
|
|
160
|
+
findCssEntryFile,
|
|
161
|
+
getMissingCssImports,
|
|
162
|
+
addCssImports,
|
|
116
163
|
};
|
|
@@ -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
|
+
};
|
package/src/lib/validators.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
1
2
|
const path = require('path');
|
|
2
3
|
const chalk = require('chalk');
|
|
3
4
|
|
|
4
|
-
const VALID_FRAMEWORKS = ['html', 'react', '
|
|
5
|
+
const VALID_FRAMEWORKS = ['html', 'react', 'nextjs'];
|
|
5
6
|
const SLUG_PATTERN = /^[a-z0-9]+(-[a-z0-9]+)*$/;
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -20,23 +21,25 @@ function validateFramework(framework) {
|
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
|
-
* Validate a
|
|
24
|
+
* Validate a slug format.
|
|
25
|
+
* @param {string} slug
|
|
26
|
+
* @param {string} [label='name'] - Content type label for error messages.
|
|
24
27
|
* Returns true if valid, false if invalid (and logs an error).
|
|
25
28
|
*/
|
|
26
|
-
function validateSlug(slug) {
|
|
29
|
+
function validateSlug(slug, label = 'name') {
|
|
27
30
|
if (!slug || typeof slug !== 'string') {
|
|
28
|
-
console.error(chalk.red(
|
|
31
|
+
console.error(chalk.red(`${label.charAt(0).toUpperCase() + label.slice(1)} is required.`));
|
|
29
32
|
return false;
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
if (slug.length > 128) {
|
|
33
|
-
console.error(chalk.red(
|
|
36
|
+
console.error(chalk.red(`${label.charAt(0).toUpperCase() + label.slice(1)} is too long (max 128 characters).`));
|
|
34
37
|
return false;
|
|
35
38
|
}
|
|
36
39
|
|
|
37
40
|
if (!SLUG_PATTERN.test(slug)) {
|
|
38
41
|
console.error(
|
|
39
|
-
chalk.red(`Invalid
|
|
42
|
+
chalk.red(`Invalid ${label} "${slug}".`),
|
|
40
43
|
chalk.gray('Use lowercase letters, numbers, and hyphens only (e.g., "button-sizes").')
|
|
41
44
|
);
|
|
42
45
|
return false;
|
|
@@ -55,10 +58,30 @@ function isSafePath(basedir, filepath) {
|
|
|
55
58
|
return !relative.startsWith('..') && !path.isAbsolute(relative);
|
|
56
59
|
}
|
|
57
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Write files to a base directory with path-traversal protection.
|
|
63
|
+
* Returns the number of files successfully written.
|
|
64
|
+
*/
|
|
65
|
+
function writeFiles(baseDir, files) {
|
|
66
|
+
let count = 0;
|
|
67
|
+
for (const file of files) {
|
|
68
|
+
if (!isSafePath(baseDir, file.path)) {
|
|
69
|
+
console.log(chalk.red(` Skipping suspicious file path: ${file.path}`));
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
const filePath = path.resolve(baseDir, file.path);
|
|
73
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
74
|
+
fs.writeFileSync(filePath, file.content);
|
|
75
|
+
count++;
|
|
76
|
+
}
|
|
77
|
+
return count;
|
|
78
|
+
}
|
|
79
|
+
|
|
58
80
|
module.exports = {
|
|
59
81
|
VALID_FRAMEWORKS,
|
|
60
82
|
SLUG_PATTERN,
|
|
61
83
|
validateFramework,
|
|
62
84
|
validateSlug,
|
|
63
85
|
isSafePath,
|
|
86
|
+
writeFiles,
|
|
64
87
|
};
|