@foundrykit/cli 0.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.
@@ -0,0 +1,318 @@
1
+ import { Command } from 'commander';
2
+ import chalk from 'chalk';
3
+ import fs from 'fs-extra';
4
+ import path from 'path';
5
+ import { execSync } from 'child_process';
6
+ import inquirer from 'inquirer';
7
+
8
+ interface InitOptions {
9
+ force?: boolean;
10
+ skipInstall?: boolean;
11
+ }
12
+
13
+ export function initCommand() {
14
+ return new Command('init')
15
+ .description('Initialize FoundryKit in your Next.js project')
16
+ .option('-f, --force', 'overwrite existing files')
17
+ .option('--skip-install', 'skip installing dependencies')
18
+ .action(async (options: InitOptions) => {
19
+ try {
20
+ console.log(chalk.blue('šŸš€ Initializing FoundryKit...'));
21
+
22
+ // Check if it's a Next.js project
23
+ await validateNextJsProject();
24
+
25
+ // Check if already initialized
26
+ if (!options.force && (await isFoundryKitInitialized())) {
27
+ const { shouldContinue } = await inquirer.prompt([
28
+ {
29
+ type: 'confirm',
30
+ name: 'shouldContinue',
31
+ message: 'FoundryKit is already initialized. Continue anyway?',
32
+ default: false,
33
+ },
34
+ ]);
35
+
36
+ if (!shouldContinue) {
37
+ console.log(chalk.yellow('āŒ Initialization cancelled.'));
38
+ return;
39
+ }
40
+ }
41
+
42
+ // Install dependencies
43
+ if (!options.skipInstall) {
44
+ await installDependencies();
45
+ }
46
+
47
+ // Setup Tailwind config
48
+ await setupTailwindConfig();
49
+
50
+ // Create components directory
51
+ await createDirectories();
52
+
53
+ // Create example files
54
+ await createExampleFiles();
55
+
56
+ // Update package.json scripts
57
+ await updatePackageScripts();
58
+
59
+ console.log(chalk.green('\nāœ… FoundryKit initialized successfully!'));
60
+ console.log(chalk.cyan('\nšŸ“¦ Next steps:'));
61
+ console.log(
62
+ chalk.gray(
63
+ ' 1. Add a component: npx @foundrykit/cli add hero-minimal'
64
+ )
65
+ );
66
+ console.log(
67
+ chalk.gray(' 2. List available components: npx @foundrykit/cli list')
68
+ );
69
+ console.log(
70
+ chalk.gray(' 3. Check out the docs: https://foundrykit.dev')
71
+ );
72
+ } catch (error) {
73
+ console.error(chalk.red('āŒ Failed to initialize FoundryKit:'));
74
+ console.error(
75
+ chalk.red(error instanceof Error ? error.message : String(error))
76
+ );
77
+ process.exit(1);
78
+ }
79
+ });
80
+ }
81
+
82
+ async function validateNextJsProject(): Promise<void> {
83
+ const packageJsonPath = path.join(process.cwd(), 'package.json');
84
+
85
+ if (!fs.existsSync(packageJsonPath)) {
86
+ throw new Error(
87
+ 'No package.json found. Please run this command in a Next.js project root.'
88
+ );
89
+ }
90
+
91
+ const packageJson = await fs.readJson(packageJsonPath);
92
+ const dependencies = {
93
+ ...packageJson.dependencies,
94
+ ...packageJson.devDependencies,
95
+ };
96
+
97
+ if (!dependencies.next) {
98
+ throw new Error(
99
+ 'Next.js not detected. This command should be run in a Next.js project.'
100
+ );
101
+ }
102
+
103
+ console.log(chalk.green('āœ… Next.js project detected'));
104
+ }
105
+
106
+ async function isFoundryKitInitialized(): Promise<boolean> {
107
+ const tailwindConfigPath = path.join(process.cwd(), 'tailwind.config.js');
108
+ const tailwindConfigTsPath = path.join(process.cwd(), 'tailwind.config.ts');
109
+
110
+ if (fs.existsSync(tailwindConfigPath)) {
111
+ const config = await fs.readFile(tailwindConfigPath, 'utf-8');
112
+ return config.includes('@foundrykit/tailwind-config');
113
+ }
114
+
115
+ if (fs.existsSync(tailwindConfigTsPath)) {
116
+ const config = await fs.readFile(tailwindConfigTsPath, 'utf-8');
117
+ return config.includes('@foundrykit/tailwind-config');
118
+ }
119
+
120
+ return false;
121
+ }
122
+
123
+ async function installDependencies(): Promise<void> {
124
+ console.log(chalk.blue('šŸ“¦ Installing FoundryKit dependencies...'));
125
+
126
+ const dependencies = [
127
+ '@foundrykit/primitives',
128
+ '@foundrykit/components',
129
+ '@foundrykit/blocks',
130
+ '@foundrykit/hooks',
131
+ '@foundrykit/animation',
132
+ '@foundrykit/tokens',
133
+ '@foundrykit/tailwind-config',
134
+ ];
135
+
136
+ try {
137
+ // Detect package manager
138
+ const packageManager = detectPackageManager();
139
+ console.log(chalk.gray(`Using ${packageManager}...`));
140
+
141
+ const installCmd =
142
+ packageManager === 'npm'
143
+ ? `npm install ${dependencies.join(' ')}`
144
+ : packageManager === 'yarn'
145
+ ? `yarn add ${dependencies.join(' ')}`
146
+ : `pnpm add ${dependencies.join(' ')}`;
147
+
148
+ execSync(installCmd, { stdio: 'inherit' });
149
+ console.log(chalk.green('āœ… Dependencies installed'));
150
+ } catch (error) {
151
+ throw new Error(
152
+ 'Failed to install dependencies. Please install them manually.'
153
+ );
154
+ }
155
+ }
156
+
157
+ function detectPackageManager(): string {
158
+ if (fs.existsSync('pnpm-lock.yaml')) return 'pnpm';
159
+ if (fs.existsSync('yarn.lock')) return 'yarn';
160
+ return 'npm';
161
+ }
162
+
163
+ async function setupTailwindConfig(): Promise<void> {
164
+ console.log(chalk.blue('āš™ļø Setting up Tailwind configuration...'));
165
+
166
+ const configPath = path.join(process.cwd(), 'tailwind.config.ts');
167
+ const configContent = `import type { Config } from 'tailwindcss';
168
+ import { withFoundryKit } from '@foundrykit/tailwind-config';
169
+
170
+ const config: Config = withFoundryKit({
171
+ content: [
172
+ './pages/**/*.{js,ts,jsx,tsx,mdx}',
173
+ './components/**/*.{js,ts,jsx,tsx,mdx}',
174
+ './app/**/*.{js,ts,jsx,tsx,mdx}',
175
+ './src/**/*.{js,ts,jsx,tsx,mdx}',
176
+ ],
177
+ theme: {
178
+ extend: {},
179
+ },
180
+ plugins: [],
181
+ });
182
+
183
+ export default config;
184
+ `;
185
+
186
+ await fs.writeFile(configPath, configContent);
187
+ console.log(chalk.green('āœ… Tailwind config created'));
188
+ }
189
+
190
+ async function createDirectories(): Promise<void> {
191
+ console.log(chalk.blue('šŸ“ Creating component directories...'));
192
+
193
+ const dirs = ['components/ui', 'components/blocks', 'lib', 'hooks'];
194
+
195
+ for (const dir of dirs) {
196
+ await fs.ensureDir(path.join(process.cwd(), dir));
197
+ }
198
+
199
+ console.log(chalk.green('āœ… Directories created'));
200
+ }
201
+
202
+ async function createExampleFiles(): Promise<void> {
203
+ console.log(chalk.blue('šŸ“ Creating example files...'));
204
+
205
+ // Create lib/utils.ts
206
+ const utilsPath = path.join(process.cwd(), 'lib/utils.ts');
207
+ const utilsContent = `import { type ClassValue, clsx } from 'clsx';
208
+ import { twMerge } from 'tailwind-merge';
209
+
210
+ export function cn(...inputs: ClassValue[]) {
211
+ return twMerge(clsx(inputs));
212
+ }
213
+ `;
214
+
215
+ await fs.writeFile(utilsPath, utilsContent);
216
+
217
+ // Create a basic globals.css if it doesn't exist
218
+ const globalsCssPath = path.join(process.cwd(), 'app/globals.css');
219
+ const srcGlobalsCssPath = path.join(process.cwd(), 'src/app/globals.css');
220
+
221
+ const globalsCssContent = `@import "tailwindcss";
222
+
223
+ @layer base {
224
+ :root {
225
+ --background: 0 0% 100%;
226
+ --foreground: 222.2 84% 4.9%;
227
+ --card: 0 0% 100%;
228
+ --card-foreground: 222.2 84% 4.9%;
229
+ --popover: 0 0% 100%;
230
+ --popover-foreground: 222.2 84% 4.9%;
231
+ --primary: 222.2 47.4% 11.2%;
232
+ --primary-foreground: 210 40% 98%;
233
+ --secondary: 210 40% 96%;
234
+ --secondary-foreground: 222.2 84% 4.9%;
235
+ --muted: 210 40% 96%;
236
+ --muted-foreground: 215.4 16.3% 46.9%;
237
+ --accent: 210 40% 96%;
238
+ --accent-foreground: 222.2 84% 4.9%;
239
+ --destructive: 0 84.2% 60.2%;
240
+ --destructive-foreground: 210 40% 98%;
241
+ --border: 214.3 31.8% 91.4%;
242
+ --input: 214.3 31.8% 91.4%;
243
+ --ring: 222.2 84% 4.9%;
244
+ --radius: 0.5rem;
245
+ }
246
+
247
+ .dark {
248
+ --background: 222.2 84% 4.9%;
249
+ --foreground: 210 40% 98%;
250
+ --card: 222.2 84% 4.9%;
251
+ --card-foreground: 210 40% 98%;
252
+ --popover: 222.2 84% 4.9%;
253
+ --popover-foreground: 210 40% 98%;
254
+ --primary: 210 40% 98%;
255
+ --primary-foreground: 222.2 47.4% 11.2%;
256
+ --secondary: 217.2 32.6% 17.5%;
257
+ --secondary-foreground: 210 40% 98%;
258
+ --muted: 217.2 32.6% 17.5%;
259
+ --muted-foreground: 215 20.2% 65.1%;
260
+ --accent: 217.2 32.6% 17.5%;
261
+ --accent-foreground: 210 40% 98%;
262
+ --destructive: 0 62.8% 30.6%;
263
+ --destructive-foreground: 210 40% 98%;
264
+ --border: 217.2 32.6% 17.5%;
265
+ --input: 217.2 32.6% 17.5%;
266
+ --ring: 212.7 26.8% 83.9%;
267
+ }
268
+ }
269
+
270
+ @layer base {
271
+ body {
272
+ @apply bg-background text-foreground;
273
+ }
274
+ }
275
+ `;
276
+
277
+ // Try to update existing globals.css or create new one
278
+ if (fs.existsSync(globalsCssPath)) {
279
+ const existing = await fs.readFile(globalsCssPath, 'utf-8');
280
+ if (!existing.includes('--background')) {
281
+ await fs.writeFile(globalsCssPath, globalsCssContent);
282
+ }
283
+ } else if (fs.existsSync(srcGlobalsCssPath)) {
284
+ const existing = await fs.readFile(srcGlobalsCssPath, 'utf-8');
285
+ if (!existing.includes('--background')) {
286
+ await fs.writeFile(srcGlobalsCssPath, globalsCssContent);
287
+ }
288
+ } else {
289
+ // Create in app directory by default
290
+ await fs.ensureDir(path.join(process.cwd(), 'app'));
291
+ await fs.writeFile(globalsCssPath, globalsCssContent);
292
+ }
293
+
294
+ console.log(chalk.green('āœ… Example files created'));
295
+ }
296
+
297
+ async function updatePackageScripts(): Promise<void> {
298
+ console.log(chalk.blue('šŸ“‹ Updating package.json scripts...'));
299
+
300
+ const packageJsonPath = path.join(process.cwd(), 'package.json');
301
+ const packageJson = await fs.readJson(packageJsonPath);
302
+
303
+ if (!packageJson.scripts) {
304
+ packageJson.scripts = {};
305
+ }
306
+
307
+ // Add FoundryKit CLI scripts if they don't exist
308
+ if (!packageJson.scripts['foundrykit']) {
309
+ packageJson.scripts['foundrykit'] = 'foundrykit';
310
+ }
311
+
312
+ if (!packageJson.scripts['fk']) {
313
+ packageJson.scripts['fk'] = 'foundrykit';
314
+ }
315
+
316
+ await fs.writeJson(packageJsonPath, packageJson, { spaces: 2 });
317
+ console.log(chalk.green('āœ… Package scripts updated'));
318
+ }
@@ -0,0 +1,255 @@
1
+ import { Command } from 'commander';
2
+ import chalk from 'chalk';
3
+ import fetch from 'node-fetch';
4
+
5
+ interface Component {
6
+ name: string;
7
+ description: string;
8
+ category: 'primitives' | 'components' | 'blocks';
9
+ dependencies?: string[];
10
+ tags?: string[];
11
+ }
12
+
13
+ interface Registry {
14
+ primitives: Component[];
15
+ components: Component[];
16
+ blocks: Component[];
17
+ }
18
+
19
+ export function listCommand() {
20
+ return new Command('list')
21
+ .description('List all available FoundryKit components and blocks')
22
+ .option(
23
+ '-c, --category <category>',
24
+ 'filter by category (primitives, components, blocks)'
25
+ )
26
+ .option('-s, --search <term>', 'search components by name or description')
27
+ .action(async (options: { category?: string; search?: string }) => {
28
+ try {
29
+ console.log(chalk.blue('šŸ“¦ Available FoundryKit components:'));
30
+
31
+ const registry = await getRegistry();
32
+
33
+ // Filter by category if specified
34
+ let categories: ('primitives' | 'components' | 'blocks')[] = [
35
+ 'primitives',
36
+ 'components',
37
+ 'blocks',
38
+ ];
39
+ if (options.category) {
40
+ if (
41
+ !['primitives', 'components', 'blocks'].includes(options.category)
42
+ ) {
43
+ console.error(chalk.red(`Invalid category: ${options.category}`));
44
+ console.log(
45
+ chalk.gray('Available categories: primitives, components, blocks')
46
+ );
47
+ return;
48
+ }
49
+ categories = [
50
+ options.category as 'primitives' | 'components' | 'blocks',
51
+ ];
52
+ }
53
+
54
+ // Display components
55
+ for (const category of categories) {
56
+ const items = registry[category];
57
+ const filteredItems = options.search
58
+ ? items.filter(
59
+ item =>
60
+ item.name
61
+ .toLowerCase()
62
+ .includes(options.search!.toLowerCase()) ||
63
+ item.description
64
+ .toLowerCase()
65
+ .includes(options.search!.toLowerCase())
66
+ )
67
+ : items;
68
+
69
+ if (filteredItems.length === 0) continue;
70
+
71
+ console.log(`\n${chalk.cyan.bold(category.toUpperCase())}`);
72
+ console.log(chalk.gray('─'.repeat(40)));
73
+
74
+ for (const item of filteredItems) {
75
+ console.log(`${chalk.green('ā—')} ${chalk.bold(item.name)}`);
76
+ console.log(` ${chalk.gray(item.description)}`);
77
+ if (item.tags && item.tags.length > 0) {
78
+ console.log(
79
+ ` ${chalk.blue(item.tags.map(tag => `#${tag}`).join(' '))}`
80
+ );
81
+ }
82
+ console.log();
83
+ }
84
+ }
85
+
86
+ console.log(chalk.cyan('\nšŸ’” Usage:'));
87
+ console.log(chalk.gray(' foundrykit add <component-name>'));
88
+ console.log(chalk.gray(' foundrykit add button --with-animation'));
89
+ } catch (error) {
90
+ console.error(chalk.red('āŒ Failed to fetch component registry:'));
91
+ console.error(
92
+ chalk.red(error instanceof Error ? error.message : String(error))
93
+ );
94
+
95
+ // Fallback to local registry
96
+ console.log(chalk.yellow('\nšŸ“‹ Using local registry:'));
97
+ displayLocalRegistry(options);
98
+ }
99
+ });
100
+ }
101
+
102
+ async function getRegistry(): Promise<Registry> {
103
+ try {
104
+ // Try to fetch from registry API
105
+ const response = await fetch('http://localhost:3001/api/components');
106
+ if (response.ok) {
107
+ const data = await response.json();
108
+ return data as Registry;
109
+ }
110
+ } catch (error) {
111
+ // Fall back to local registry
112
+ }
113
+
114
+ // Return local/fallback registry
115
+ return getLocalRegistry();
116
+ }
117
+
118
+ function getLocalRegistry(): Registry {
119
+ return {
120
+ primitives: [
121
+ {
122
+ name: 'button',
123
+ description:
124
+ 'Displays a button or a component that looks like a button.',
125
+ category: 'primitives',
126
+ tags: ['form', 'interactive'],
127
+ },
128
+ {
129
+ name: 'input',
130
+ description:
131
+ 'Displays a form input field or a component that looks like an input.',
132
+ category: 'primitives',
133
+ tags: ['form'],
134
+ },
135
+ {
136
+ name: 'badge',
137
+ description: 'Displays a badge or a component that looks like a badge.',
138
+ category: 'primitives',
139
+ tags: ['status', 'label'],
140
+ },
141
+ {
142
+ name: 'card',
143
+ description: 'Displays a card with header, content, and footer.',
144
+ category: 'primitives',
145
+ tags: ['layout', 'container'],
146
+ },
147
+ {
148
+ name: 'avatar',
149
+ description:
150
+ 'An image element with a fallback for representing the user.',
151
+ category: 'primitives',
152
+ tags: ['user', 'profile'],
153
+ },
154
+ ],
155
+ components: [
156
+ {
157
+ name: 'search-bar',
158
+ description: 'A search input with suggestions and keyboard navigation.',
159
+ category: 'components',
160
+ dependencies: ['input', 'popover'],
161
+ tags: ['search', 'form'],
162
+ },
163
+ {
164
+ name: 'combobox',
165
+ description:
166
+ 'Combines a text input with a listbox, allowing users to filter a list.',
167
+ category: 'components',
168
+ dependencies: ['input', 'popover', 'command'],
169
+ tags: ['form', 'selection'],
170
+ },
171
+ {
172
+ name: 'date-picker',
173
+ description: 'A date picker component with calendar popup.',
174
+ category: 'components',
175
+ dependencies: ['input', 'popover', 'calendar'],
176
+ tags: ['form', 'date'],
177
+ },
178
+ ],
179
+ blocks: [
180
+ {
181
+ name: 'hero-minimal',
182
+ description:
183
+ 'A minimal hero section with heading, description, and CTA button.',
184
+ category: 'blocks',
185
+ dependencies: ['button'],
186
+ tags: ['landing', 'marketing'],
187
+ },
188
+ {
189
+ name: 'hero-with-image',
190
+ description:
191
+ 'A hero section with background image and content overlay.',
192
+ category: 'blocks',
193
+ dependencies: ['button'],
194
+ tags: ['landing', 'marketing', 'image'],
195
+ },
196
+ {
197
+ name: 'pricing',
198
+ description:
199
+ 'A pricing section with multiple tiers and feature comparison.',
200
+ category: 'blocks',
201
+ dependencies: ['card', 'button', 'badge'],
202
+ tags: ['pricing', 'marketing'],
203
+ },
204
+ {
205
+ name: 'testimonials',
206
+ description: 'A testimonials section with customer quotes and avatars.',
207
+ category: 'blocks',
208
+ dependencies: ['avatar', 'card'],
209
+ tags: ['social-proof', 'marketing'],
210
+ },
211
+ ],
212
+ };
213
+ }
214
+
215
+ function displayLocalRegistry(options: { category?: string; search?: string }) {
216
+ const registry = getLocalRegistry();
217
+
218
+ let categories: ('primitives' | 'components' | 'blocks')[] = [
219
+ 'primitives',
220
+ 'components',
221
+ 'blocks',
222
+ ];
223
+ if (options.category) {
224
+ categories = [options.category as 'primitives' | 'components' | 'blocks'];
225
+ }
226
+
227
+ for (const category of categories) {
228
+ const items = registry[category];
229
+ const filteredItems = options.search
230
+ ? items.filter(
231
+ item =>
232
+ item.name.toLowerCase().includes(options.search!.toLowerCase()) ||
233
+ item.description
234
+ .toLowerCase()
235
+ .includes(options.search!.toLowerCase())
236
+ )
237
+ : items;
238
+
239
+ if (filteredItems.length === 0) continue;
240
+
241
+ console.log(`\n${chalk.cyan.bold(category.toUpperCase())}`);
242
+ console.log(chalk.gray('─'.repeat(40)));
243
+
244
+ for (const item of filteredItems) {
245
+ console.log(`${chalk.green('ā—')} ${chalk.bold(item.name)}`);
246
+ console.log(` ${chalk.gray(item.description)}`);
247
+ if (item.tags && item.tags.length > 0) {
248
+ console.log(
249
+ ` ${chalk.blue(item.tags.map(tag => `#${tag}`).join(' '))}`
250
+ );
251
+ }
252
+ console.log();
253
+ }
254
+ }
255
+ }
@@ -0,0 +1,12 @@
1
+ import { Command } from 'commander';
2
+ import chalk from 'chalk';
3
+
4
+ export function updateCommand() {
5
+ return new Command('update')
6
+ .description('Update an existing component or block')
7
+ .argument('<id>', 'Component or block ID')
8
+ .action(async id => {
9
+ console.log(chalk.blue(`šŸ”„ Updating ${id}...`));
10
+ console.log(chalk.green(`āœ… ${id} updated successfully!`));
11
+ });
12
+ }
package/src/index.ts ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { Command } from 'commander';
4
+ import { initCommand } from './commands/init.js';
5
+ import { listCommand } from './commands/list.js';
6
+ import { addCommand } from './commands/add.js';
7
+ import { updateCommand } from './commands/update.js';
8
+ import { doctorCommand } from './commands/doctor.js';
9
+
10
+ const program = new Command();
11
+
12
+ program
13
+ .name('foundrykit')
14
+ .description('FoundryKit CLI - Component library management tool')
15
+ .version('0.1.0');
16
+
17
+ // Add commands
18
+ program.addCommand(initCommand());
19
+ program.addCommand(listCommand());
20
+ program.addCommand(addCommand());
21
+ program.addCommand(updateCommand());
22
+ program.addCommand(doctorCommand());
23
+
24
+ program.parse();
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "@foundrykit/typescript-config/react-library.json",
3
+ "compilerOptions": {
4
+ "baseUrl": ".",
5
+ "paths": {
6
+ "@foundrykit/cli/*": ["./src/*"]
7
+ }
8
+ },
9
+ "include": ["."],
10
+ "exclude": ["node_modules", "dist"]
11
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "@foundrykit/typescript-config/react-library.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist"
5
+ },
6
+ "include": ["src", "turbo"],
7
+ "exclude": ["node_modules", "dist"]
8
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ export default defineConfig({
4
+ entry: ['src/index.ts'],
5
+ format: ['esm'],
6
+ dts: true,
7
+ splitting: false,
8
+ sourcemap: true,
9
+ clean: true,
10
+ external: [],
11
+ });