@foundrykit/cli 1.0.4 → 1.0.6

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/dist/index.js CHANGED
@@ -11,10 +11,11 @@ import { Command } from "commander";
11
11
  import chalk from "chalk";
12
12
  import fs from "fs-extra";
13
13
  import path from "path";
14
- import { execSync } from "child_process";
15
14
  import inquirer from "inquirer";
15
+ import { exec } from "child_process";
16
+ import { promisify } from "util";
16
17
  function initCommand() {
17
- return new Command("init").description("Initialize FoundryKit in your project").option("-f, --force", "overwrite existing files").option("--skip-install", "skip installing dependencies").action(async (options) => {
18
+ return new Command("init").description("Initialize FoundryKit in your project").option("-f, --force", "overwrite existing files").option("--skip-install", "skip installing dependencies").option("--skip-prompts", "use default paths without prompting").action(async (options) => {
18
19
  try {
19
20
  console.log(chalk.blue("\u{1F680} Initializing FoundryKit..."));
20
21
  await validateProject();
@@ -32,14 +33,28 @@ function initCommand() {
32
33
  return;
33
34
  }
34
35
  }
36
+ const projectStructure = await detectProjectStructure();
37
+ console.log(
38
+ chalk.gray(
39
+ `Detected project structure: ${projectStructure.hasSrc ? "src/" : "root"} based`
40
+ )
41
+ );
42
+ const pathConfig = await getPathConfiguration(
43
+ projectStructure,
44
+ options.skipPrompts
45
+ );
35
46
  if (!options.skipInstall) {
36
47
  await installDependencies();
37
48
  }
38
- await createConfigFile(options);
39
- await installUtils();
40
- await setupTailwindV4();
41
- await createDirectories();
42
- await createExampleFiles();
49
+ await createConfigFile(options, projectStructure, pathConfig);
50
+ await installUtils(pathConfig);
51
+ await setupTailwindV4(
52
+ pathConfig,
53
+ projectStructure,
54
+ options.skipPrompts
55
+ );
56
+ await createDirectories(pathConfig);
57
+ await createExampleFiles(pathConfig, projectStructure);
43
58
  await updatePackageScripts();
44
59
  console.log(chalk.green("\n\u2705 FoundryKit initialized successfully!"));
45
60
  console.log(chalk.cyan("\n\u{1F4E6} Next steps:"));
@@ -71,56 +86,261 @@ async function validateProject() {
71
86
  const packageJsonPath = path.join(process.cwd(), "package.json");
72
87
  if (!fs.existsSync(packageJsonPath)) {
73
88
  throw new Error(
74
- "No package.json found. Please run this command in a Next.js project root."
89
+ "No package.json found. Please run this command in a project directory."
75
90
  );
76
91
  }
77
92
  const packageJson2 = await fs.readJson(packageJsonPath);
78
- const dependencies = {
93
+ const deps = {
79
94
  ...packageJson2.dependencies,
80
95
  ...packageJson2.devDependencies
81
96
  };
82
- if (!dependencies.next) {
97
+ if (!deps.next) {
83
98
  throw new Error(
84
- "Next.js not detected. This command should be run in a Next.js project."
99
+ "Next.js not detected. FoundryKit currently requires Next.js."
85
100
  );
86
101
  }
87
102
  console.log(chalk.green("\u2705 Next.js project detected"));
88
103
  }
89
104
  async function isFoundryKitInitialized() {
90
- const configPath = path.join(process.cwd(), "foundrykit.config.json");
91
- return fs.existsSync(configPath);
105
+ return fs.existsSync(path.join(process.cwd(), "foundrykit.config.json"));
106
+ }
107
+ async function detectProjectStructure() {
108
+ const cwd = process.cwd();
109
+ const hasSrc = fs.existsSync(path.join(cwd, "src"));
110
+ let baseDir = "./";
111
+ let appPath;
112
+ let componentsPath;
113
+ let libPath;
114
+ let hooksPath;
115
+ let stylesPath;
116
+ if (hasSrc) {
117
+ const srcHasApp = fs.existsSync(path.join(cwd, "src/app"));
118
+ if (srcHasApp) {
119
+ baseDir = "./src/";
120
+ appPath = "./src/app";
121
+ } else {
122
+ const rootHasApp = fs.existsSync(path.join(cwd, "app"));
123
+ if (rootHasApp) {
124
+ appPath = "./app";
125
+ baseDir = "./src/";
126
+ }
127
+ }
128
+ if (fs.existsSync(path.join(cwd, "src/components"))) {
129
+ componentsPath = "./src/components";
130
+ }
131
+ if (fs.existsSync(path.join(cwd, "src/lib"))) {
132
+ libPath = "./src/lib";
133
+ }
134
+ if (fs.existsSync(path.join(cwd, "src/hooks"))) {
135
+ hooksPath = "./src/hooks";
136
+ }
137
+ if (fs.existsSync(path.join(cwd, "src/styles"))) {
138
+ stylesPath = "./src/styles";
139
+ }
140
+ } else {
141
+ const hasApp = fs.existsSync(path.join(cwd, "app"));
142
+ if (hasApp) {
143
+ appPath = "./app";
144
+ }
145
+ if (fs.existsSync(path.join(cwd, "components"))) {
146
+ componentsPath = "./components";
147
+ }
148
+ if (fs.existsSync(path.join(cwd, "lib"))) {
149
+ libPath = "./lib";
150
+ }
151
+ if (fs.existsSync(path.join(cwd, "hooks"))) {
152
+ hooksPath = "./hooks";
153
+ }
154
+ if (fs.existsSync(path.join(cwd, "styles"))) {
155
+ stylesPath = "./styles";
156
+ }
157
+ }
158
+ const isTypeScript = fs.existsSync(path.join(cwd, "tsconfig.json"));
159
+ const packageManager = detectPackageManager();
160
+ return {
161
+ hasSrc,
162
+ hasApp: !!appPath,
163
+ hasComponents: !!componentsPath,
164
+ hasLib: !!libPath,
165
+ hasHooks: !!hooksPath,
166
+ hasStyles: !!stylesPath,
167
+ baseDir,
168
+ framework: "next",
169
+ isTypeScript,
170
+ packageManager,
171
+ appPath,
172
+ componentsPath,
173
+ libPath,
174
+ hooksPath,
175
+ stylesPath
176
+ };
177
+ }
178
+ async function getPathConfiguration(structure, skipPrompts) {
179
+ const ext = structure.isTypeScript ? ".ts" : ".js";
180
+ const jsxExt = structure.isTypeScript ? ".tsx" : ".jsx";
181
+ const defaults = {
182
+ utils: path.join(
183
+ structure.libPath || path.join(structure.baseDir, "lib"),
184
+ `utils${ext}`
185
+ ),
186
+ primitives: path.join(
187
+ structure.componentsPath || path.join(structure.baseDir, "components"),
188
+ "primitives"
189
+ ),
190
+ components: path.join(
191
+ structure.componentsPath || path.join(structure.baseDir, "components"),
192
+ "components"
193
+ ),
194
+ blocks: path.join(
195
+ structure.componentsPath || path.join(structure.baseDir, "components"),
196
+ "blocks"
197
+ ),
198
+ animations: path.join(
199
+ structure.componentsPath || path.join(structure.baseDir, "components"),
200
+ "animations"
201
+ ),
202
+ hooks: structure.hooksPath || path.join(structure.baseDir, "hooks"),
203
+ lib: structure.libPath || path.join(structure.baseDir, "lib"),
204
+ styles: path.join(structure.appPath || structure.baseDir, "globals.css")
205
+ };
206
+ if (structure.appPath) {
207
+ const possibleStylePaths = [
208
+ path.join(structure.appPath, "globals.css"),
209
+ path.join(structure.appPath, "global.css"),
210
+ path.join(structure.stylesPath || "", "globals.css"),
211
+ path.join(structure.stylesPath || "", "global.css")
212
+ ];
213
+ for (const stylePath of possibleStylePaths) {
214
+ if (fs.existsSync(path.join(process.cwd(), stylePath))) {
215
+ defaults.styles = stylePath;
216
+ break;
217
+ }
218
+ }
219
+ }
220
+ if (skipPrompts) {
221
+ console.log(
222
+ chalk.gray("Using smart defaults based on project structure...")
223
+ );
224
+ return defaults;
225
+ }
226
+ console.log(chalk.blue("\n\u{1F4C2} Configure installation paths:"));
227
+ console.log(
228
+ chalk.gray("Press enter to use the default value shown in parentheses\n")
229
+ );
230
+ const answers = await inquirer.prompt([
231
+ {
232
+ type: "input",
233
+ name: "utils",
234
+ message: "Utils file path:",
235
+ default: defaults.utils,
236
+ validate: (input) => {
237
+ if (!input) return "Path cannot be empty";
238
+ return true;
239
+ }
240
+ },
241
+ {
242
+ type: "input",
243
+ name: "components",
244
+ message: "Components directory:",
245
+ default: defaults.components
246
+ },
247
+ {
248
+ type: "input",
249
+ name: "primitives",
250
+ message: "Primitives directory:",
251
+ default: defaults.primitives
252
+ },
253
+ {
254
+ type: "input",
255
+ name: "blocks",
256
+ message: "Blocks directory:",
257
+ default: defaults.blocks
258
+ },
259
+ {
260
+ type: "input",
261
+ name: "hooks",
262
+ message: "Hooks directory:",
263
+ default: defaults.hooks
264
+ },
265
+ {
266
+ type: "input",
267
+ name: "animations",
268
+ message: "Animations directory:",
269
+ default: defaults.animations
270
+ },
271
+ {
272
+ type: "input",
273
+ name: "styles",
274
+ message: "Global styles file:",
275
+ default: defaults.styles
276
+ },
277
+ {
278
+ type: "input",
279
+ name: "lib",
280
+ message: "Lib directory:",
281
+ default: defaults.lib
282
+ }
283
+ ]);
284
+ return answers;
92
285
  }
93
286
  async function installDependencies() {
94
- console.log(chalk.blue("\u{1F4E6} Installing base dependencies..."));
95
- const dependencies = ["clsx", "tailwind-merge"];
287
+ console.log(chalk.blue("\u{1F4E6} Installing dependencies..."));
288
+ const packageManager = detectPackageManager();
289
+ const execAsync2 = promisify(exec);
290
+ const dependencies = [
291
+ "clsx",
292
+ "tailwind-merge",
293
+ "framer-motion",
294
+ "lucide-react",
295
+ "next-themes",
296
+ "cmdk",
297
+ "@radix-ui/react-dialog",
298
+ "@radix-ui/react-dropdown-menu",
299
+ "@radix-ui/react-label",
300
+ "@radix-ui/react-popover",
301
+ "@radix-ui/react-select",
302
+ "@radix-ui/react-separator",
303
+ "@radix-ui/react-slot",
304
+ "@radix-ui/react-switch",
305
+ "@radix-ui/react-tabs",
306
+ "@radix-ui/react-tooltip"
307
+ ];
308
+ const devDependencies = ["tailwindcss", "autoprefixer", "postcss"];
96
309
  try {
97
- const packageManager = detectPackageManager();
98
- console.log(chalk.gray(`Using ${packageManager}...`));
99
- const installCmd = packageManager === "npm" ? `npm install ${dependencies.join(" ")}` : packageManager === "yarn" ? `yarn add ${dependencies.join(" ")}` : packageManager === "bun" ? `bun add ${dependencies.join(" ")}` : `pnpm add ${dependencies.join(" ")}`;
100
- execSync(installCmd, { stdio: "inherit" });
101
- console.log(chalk.green("\u2705 Dependencies installed"));
102
- } catch (error) {
103
- console.log(
104
- chalk.yellow("\u26A0\uFE0F Could not install dependencies automatically")
310
+ const installCmd = packageManager === "npm" ? "install" : "add";
311
+ await execAsync2(
312
+ `${packageManager} ${installCmd} ${dependencies.join(" ")}`
105
313
  );
106
- console.log(chalk.gray("Please install them manually:"));
107
- console.log(
108
- chalk.cyan(` ${detectPackageManager()} add ${dependencies.join(" ")}`)
314
+ const devFlag = packageManager === "npm" ? "--save-dev" : "-D";
315
+ await execAsync2(
316
+ `${packageManager} ${installCmd} ${devFlag} ${devDependencies.join(" ")}`
109
317
  );
318
+ console.log(chalk.green("\u2705 Dependencies installed"));
319
+ } catch (error) {
320
+ console.error(chalk.yellow("\u26A0\uFE0F Failed to install some dependencies"));
321
+ console.error(chalk.gray("You may need to install them manually"));
110
322
  }
111
323
  }
112
324
  function detectPackageManager() {
113
- if (fs.existsSync("pnpm-lock.yaml")) return "pnpm";
114
- if (fs.existsSync("yarn.lock")) return "yarn";
115
- if (fs.existsSync("bun.lockb")) return "bun";
325
+ if (fs.existsSync(path.join(process.cwd(), "pnpm-lock.yaml"))) return "pnpm";
326
+ if (fs.existsSync(path.join(process.cwd(), "yarn.lock"))) return "yarn";
327
+ if (fs.existsSync(path.join(process.cwd(), "bun.lockb"))) return "bun";
116
328
  return "npm";
117
329
  }
118
- async function setupTailwindV4() {
119
- console.log(chalk.blue("\u2699\uFE0F Setting up Tailwind CSS v4..."));
120
- const layoutInfo = await findLayoutFile();
121
- if (layoutInfo) {
122
- const layoutContent = await fs.readFile(layoutInfo.path, "utf-8");
330
+ async function setupTailwindV4(pathConfig, projectStructure, skipPrompts) {
331
+ console.log(chalk.blue("\u{1F3A8} Setting up Tailwind CSS v4..."));
332
+ const layoutPath = await findLayoutFile(projectStructure);
333
+ if (layoutPath) {
334
+ const layoutContent = await fs.readFile(layoutPath, "utf-8");
123
335
  if (layoutContent.includes("@foundrykit/tailwind/globals.css")) {
336
+ if (skipPrompts) {
337
+ console.log(
338
+ chalk.green(
339
+ "\u2705 Keeping existing import from @foundrykit/tailwind/globals.css"
340
+ )
341
+ );
342
+ return;
343
+ }
124
344
  const { importPreference } = await inquirer.prompt([
125
345
  {
126
346
  type: "list",
@@ -147,27 +367,18 @@ async function setupTailwindV4() {
147
367
  );
148
368
  return;
149
369
  }
150
- await createLocalGlobalsAndUpdateLayout(layoutInfo.path, layoutContent);
370
+ await createOrUpdateGlobalsCss(pathConfig, projectStructure);
371
+ await updateLayoutImport(layoutPath, pathConfig);
151
372
  return;
152
373
  }
153
374
  const globalsCssImportMatch = layoutContent.match(
154
- /import\s+['"]([^'"]*globals\.css)['"]/
375
+ /import\s+['"]([^'"]*globals?\.css)['"]/
155
376
  );
156
377
  if (globalsCssImportMatch && globalsCssImportMatch[1]) {
157
378
  const importPath = globalsCssImportMatch[1];
158
- const resolvedPath = importPath.startsWith(".") ? path.join(path.dirname(layoutInfo.path), importPath) : path.join(process.cwd(), importPath.replace(/^@\//, "app/"));
379
+ const resolvedPath = resolveImportPath(layoutPath, importPath);
159
380
  if (fs.existsSync(resolvedPath)) {
160
- const globalsCssContent = await fs.readFile(resolvedPath, "utf-8");
161
- if (globalsCssContent.includes("@foundrykit/tailwind/globals.css")) {
162
- console.log(chalk.green("\u2705 FoundryKit styles already configured"));
163
- return;
164
- }
165
- const updatedContent = `/* Import FoundryKit's Tailwind CSS v4 styles */
166
- @import "@foundrykit/tailwind/globals.css";
167
-
168
- /* Your custom styles below */
169
- ${globalsCssContent}`;
170
- await fs.writeFile(resolvedPath, updatedContent);
381
+ await updateExistingGlobalsCss(resolvedPath);
171
382
  console.log(
172
383
  chalk.green("\u2705 Updated existing globals.css with FoundryKit styles")
173
384
  );
@@ -175,192 +386,188 @@ ${globalsCssContent}`;
175
386
  }
176
387
  }
177
388
  }
178
- const { setupPreference } = await inquirer.prompt([
179
- {
180
- type: "list",
181
- name: "setupPreference",
182
- message: "How would you like to import FoundryKit styles?",
183
- choices: [
184
- {
185
- name: "Create a local globals.css file (recommended for customization)",
186
- value: "local"
187
- },
188
- {
189
- name: "Import directly from @foundrykit/tailwind/globals.css",
190
- value: "direct"
191
- }
192
- ],
193
- default: "local"
194
- }
195
- ]);
389
+ let setupPreference = "local";
390
+ if (!skipPrompts) {
391
+ const answer = await inquirer.prompt([
392
+ {
393
+ type: "list",
394
+ name: "setupPreference",
395
+ message: "How would you like to import FoundryKit styles?",
396
+ choices: [
397
+ {
398
+ name: "Create/update a local globals.css file (recommended for customization)",
399
+ value: "local"
400
+ },
401
+ {
402
+ name: "Import directly from @foundrykit/tailwind/globals.css",
403
+ value: "direct"
404
+ }
405
+ ],
406
+ default: "local"
407
+ }
408
+ ]);
409
+ setupPreference = answer.setupPreference;
410
+ }
196
411
  if (setupPreference === "direct") {
197
- await addDirectImportToLayout();
412
+ await addDirectImportToLayout(layoutPath, pathConfig);
198
413
  } else {
199
- await createLocalGlobalsAndUpdateLayout();
200
- }
201
- }
202
- async function createLocalGlobalsAndUpdateLayout(existingLayoutPath, existingLayoutContent) {
203
- const possiblePaths = [
204
- "app/globals.css",
205
- "src/app/globals.css",
206
- "styles/globals.css",
207
- "src/styles/globals.css"
208
- ];
209
- let globalCssPath = null;
210
- for (const p of possiblePaths) {
211
- const fullPath = path.join(process.cwd(), p);
212
- if (fs.existsSync(fullPath)) {
213
- globalCssPath = fullPath;
214
- break;
414
+ await createOrUpdateGlobalsCss(pathConfig, projectStructure);
415
+ if (layoutPath) {
416
+ await updateLayoutImport(layoutPath, pathConfig);
215
417
  }
216
418
  }
217
- if (!globalCssPath) {
218
- globalCssPath = path.join(process.cwd(), "app/globals.css");
219
- await fs.ensureDir(path.dirname(globalCssPath));
220
- }
221
- const existingContent = fs.existsSync(globalCssPath) ? await fs.readFile(globalCssPath, "utf-8") : "";
222
- if (!existingContent.includes("@foundrykit/tailwind/globals.css")) {
419
+ console.log(chalk.green("\u2705 Tailwind CSS v4 configured"));
420
+ }
421
+ async function createOrUpdateGlobalsCss(pathConfig, projectStructure) {
422
+ const globalsCssPath = path.join(process.cwd(), pathConfig.styles);
423
+ if (fs.existsSync(globalsCssPath)) {
424
+ await updateExistingGlobalsCss(globalsCssPath);
425
+ } else {
223
426
  const cssContent = `/* Import FoundryKit's Tailwind CSS v4 styles */
224
427
  @import "@foundrykit/tailwind/globals.css";
225
428
 
226
429
  /* Your custom styles below */
227
- ${existingContent}`;
228
- await fs.writeFile(globalCssPath, cssContent);
229
- console.log(
230
- chalk.green("\u2705 Created/updated globals.css with FoundryKit styles")
231
- );
232
- }
233
- if (existingLayoutPath && existingLayoutContent) {
234
- const layoutDir = path.dirname(existingLayoutPath);
235
- const cssRelativePath = path.relative(layoutDir, globalCssPath).replace(/\\/g, "/");
236
- const importPath = cssRelativePath.startsWith(".") ? cssRelativePath : `./${cssRelativePath}`;
237
- const updatedContent = existingLayoutContent.replace(
238
- /@foundrykit\/tailwind\/globals\.css/g,
239
- importPath
240
- );
241
- await fs.writeFile(existingLayoutPath, updatedContent);
242
- console.log(chalk.green("\u2705 Updated layout to use local globals.css"));
243
- } else {
244
- await updateLayoutImport(globalCssPath);
430
+ `;
431
+ await fs.ensureDir(path.dirname(globalsCssPath));
432
+ await fs.writeFile(globalsCssPath, cssContent);
433
+ console.log(chalk.green(`\u2705 Created ${pathConfig.styles}`));
245
434
  }
246
435
  }
247
- async function addDirectImportToLayout() {
248
- const layoutInfo = await findLayoutFile();
249
- if (!layoutInfo) {
250
- console.log(
251
- chalk.yellow(
252
- "\u26A0\uFE0F No layout file found. Please add this import to your layout manually:"
253
- )
254
- );
255
- console.log(chalk.gray(` import '@foundrykit/tailwind/globals.css';`));
436
+ async function updateExistingGlobalsCss(globalsCssPath) {
437
+ const content = await fs.readFile(globalsCssPath, "utf-8");
438
+ if (content.includes("@foundrykit/tailwind/globals.css")) {
439
+ console.log(chalk.gray("globals.css already imports FoundryKit styles"));
256
440
  return;
257
441
  }
258
- const layoutContent = await fs.readFile(layoutInfo.path, "utf-8");
259
- if (layoutContent.includes("@foundrykit/tailwind/globals.css")) {
260
- console.log(
261
- chalk.gray("Layout already imports FoundryKit styles directly")
442
+ if (content.includes('@import "tailwindcss"')) {
443
+ const updatedContent = content.replace(
444
+ '@import "tailwindcss"',
445
+ `/* Import FoundryKit's Tailwind CSS v4 styles */
446
+ @import "@foundrykit/tailwind/globals.css"`
262
447
  );
263
- return;
264
- }
265
- const importStatement = `import '@foundrykit/tailwind/globals.css';`;
266
- const importRegex = /^import\s+.*$/gm;
267
- const imports = layoutContent.match(importRegex);
268
- let updatedContent;
269
- if (imports && imports.length > 0) {
270
- const lastImport = imports[imports.length - 1];
271
- const lastImportIndex = layoutContent.lastIndexOf(lastImport);
272
- const insertPosition = lastImportIndex + lastImport.length;
273
- updatedContent = layoutContent.slice(0, insertPosition) + "\n" + importStatement + layoutContent.slice(insertPosition);
448
+ await fs.writeFile(globalsCssPath, updatedContent);
449
+ console.log(chalk.green("\u2705 Updated globals.css with FoundryKit styles"));
274
450
  } else {
275
- updatedContent = importStatement + "\n" + layoutContent;
451
+ const updatedContent = `/* Import FoundryKit's Tailwind CSS v4 styles */
452
+ @import "@foundrykit/tailwind/globals.css";
453
+
454
+ /* Your custom styles below */
455
+ ${content}`;
456
+ await fs.writeFile(globalsCssPath, updatedContent);
457
+ console.log(chalk.green("\u2705 Updated globals.css with FoundryKit styles"));
276
458
  }
277
- await fs.writeFile(layoutInfo.path, updatedContent);
278
- console.log(
279
- chalk.green("\u2705 Added direct import of FoundryKit styles to layout")
280
- );
281
459
  }
282
- async function findLayoutFile() {
283
- const layoutPaths = [
460
+ async function findLayoutFile(projectStructure) {
461
+ const possiblePaths = [];
462
+ if (projectStructure.appPath) {
463
+ const ext = projectStructure.isTypeScript ? ".tsx" : ".jsx";
464
+ possiblePaths.push(
465
+ path.join(projectStructure.appPath, `layout${ext}`),
466
+ path.join(projectStructure.appPath, `layout.js`)
467
+ );
468
+ }
469
+ possiblePaths.push(
284
470
  "app/layout.tsx",
285
471
  "app/layout.jsx",
286
472
  "app/layout.js",
287
473
  "src/app/layout.tsx",
288
474
  "src/app/layout.jsx",
289
475
  "src/app/layout.js"
290
- ];
291
- for (const p of layoutPaths) {
292
- const fullPath = path.join(process.cwd(), p);
476
+ );
477
+ for (const layoutPath of possiblePaths) {
478
+ const fullPath = path.join(process.cwd(), layoutPath);
293
479
  if (fs.existsSync(fullPath)) {
294
- return { path: fullPath };
480
+ return fullPath;
295
481
  }
296
482
  }
297
483
  return null;
298
484
  }
299
- async function updateLayoutImport(globalCssPath) {
300
- const layoutInfo = await findLayoutFile();
301
- if (!layoutInfo) {
485
+ async function updateLayoutImport(layoutPath, pathConfig) {
486
+ try {
487
+ let content = await fs.readFile(layoutPath, "utf-8");
488
+ const fromPath = path.dirname(layoutPath);
489
+ const toPath = path.join(process.cwd(), pathConfig.styles);
490
+ const relativePath = path.relative(fromPath, toPath).replace(/\\/g, "/");
491
+ const importPath = relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
492
+ const globalImportRegex = /import\s+['"]([^'"]*globals?\.css)['"]/;
493
+ const match = content.match(globalImportRegex);
494
+ if (match) {
495
+ content = content.replace(globalImportRegex, `import '${importPath}'`);
496
+ } else if (!content.includes(importPath)) {
497
+ const importStatement = `import '${importPath}';
498
+ `;
499
+ const importRegex = /^(import\s+.*?;\s*)+/m;
500
+ const importMatch = content.match(importRegex);
501
+ if (importMatch) {
502
+ const lastImportIndex = importMatch.index + importMatch[0].length;
503
+ content = content.slice(0, lastImportIndex) + importStatement + content.slice(lastImportIndex);
504
+ } else {
505
+ content = importStatement + content;
506
+ }
507
+ }
508
+ await fs.writeFile(layoutPath, content);
302
509
  console.log(
303
- chalk.yellow(
304
- "\u26A0\uFE0F No layout file found. Please import globals.css manually in your layout."
510
+ chalk.gray(
511
+ `Updated ${path.relative(process.cwd(), layoutPath)} with CSS import`
305
512
  )
306
513
  );
307
- return;
308
- }
309
- const layoutContent = await fs.readFile(layoutInfo.path, "utf-8");
310
- if (layoutContent.match(/import\s+['"][^'"]*globals\.css['"]/)) {
311
- console.log(chalk.gray("Layout already has a globals.css import"));
312
- return;
514
+ } catch (error) {
515
+ console.error(chalk.yellow("\u26A0\uFE0F Failed to update layout with CSS import"));
313
516
  }
314
- if (layoutContent.includes("@foundrykit/tailwind/globals.css")) {
315
- const layoutDir2 = path.dirname(layoutInfo.path);
316
- const cssRelativePath2 = path.relative(layoutDir2, globalCssPath).replace(/\\/g, "/");
317
- const importPath2 = cssRelativePath2.startsWith(".") ? cssRelativePath2 : `./${cssRelativePath2}`;
318
- const updatedContent2 = layoutContent.replace(
319
- /@foundrykit\/tailwind\/globals\.css/g,
320
- importPath2
321
- );
322
- await fs.writeFile(layoutInfo.path, updatedContent2);
323
- console.log(
324
- chalk.green("\u2705 Updated layout import to use local globals.css")
325
- );
326
- return;
517
+ }
518
+ async function addDirectImportToLayout(layoutPath, pathConfig) {
519
+ try {
520
+ let content = await fs.readFile(layoutPath, "utf-8");
521
+ const importStatement = `import '@foundrykit/tailwind/globals.css';
522
+ `;
523
+ if (!content.includes("@foundrykit/tailwind/globals.css")) {
524
+ const importRegex = /^(import\s+.*?;\s*)+/m;
525
+ const match = content.match(importRegex);
526
+ if (match) {
527
+ const lastImportIndex = match.index + match[0].length;
528
+ content = content.slice(0, lastImportIndex) + importStatement + content.slice(lastImportIndex);
529
+ } else {
530
+ content = importStatement + content;
531
+ }
532
+ await fs.writeFile(layoutPath, content);
533
+ console.log(
534
+ chalk.green("\u2705 Added direct import of FoundryKit styles to layout")
535
+ );
536
+ }
537
+ } catch (error) {
538
+ console.error(chalk.yellow("\u26A0\uFE0F Failed to add CSS import to layout"));
327
539
  }
328
- const layoutDir = path.dirname(layoutInfo.path);
329
- const cssRelativePath = path.relative(layoutDir, globalCssPath).replace(/\\/g, "/");
330
- const importPath = cssRelativePath.startsWith(".") ? cssRelativePath : `./${cssRelativePath}`;
331
- const importStatement = `import '${importPath}';`;
332
- const importRegex = /^import\s+.*$/gm;
333
- const imports = layoutContent.match(importRegex);
334
- let updatedContent;
335
- if (imports && imports.length > 0) {
336
- const lastImport = imports[imports.length - 1];
337
- const lastImportIndex = layoutContent.lastIndexOf(lastImport);
338
- const insertPosition = lastImportIndex + lastImport.length;
339
- updatedContent = layoutContent.slice(0, insertPosition) + "\n" + importStatement + layoutContent.slice(insertPosition);
340
- } else {
341
- updatedContent = importStatement + "\n" + layoutContent;
540
+ }
541
+ function resolveImportPath(fromFile, importPath) {
542
+ if (importPath.startsWith(".")) {
543
+ return path.join(path.dirname(fromFile), importPath);
544
+ } else if (importPath.startsWith("@/")) {
545
+ return path.join(process.cwd(), importPath.replace("@/", "src/"));
546
+ } else if (importPath.startsWith("/")) {
547
+ return path.join(process.cwd(), importPath);
342
548
  }
343
- await fs.writeFile(layoutInfo.path, updatedContent);
344
- console.log(chalk.green("\u2705 Layout file updated with globals.css import"));
549
+ return path.join(process.cwd(), importPath);
345
550
  }
346
- async function createDirectories() {
551
+ async function createDirectories(pathConfig) {
347
552
  console.log(chalk.blue("\u{1F4C1} Creating component directories..."));
348
553
  const dirs = [
349
- "components/primitives",
350
- "components/components",
351
- "components/blocks",
352
- "components/animations",
353
- "lib",
354
- "hooks"
554
+ pathConfig.primitives,
555
+ pathConfig.components,
556
+ pathConfig.blocks,
557
+ pathConfig.animations,
558
+ pathConfig.hooks,
559
+ pathConfig.lib
355
560
  ];
356
561
  for (const dir of dirs) {
357
- await fs.ensureDir(path.join(process.cwd(), dir));
562
+ const fullPath = path.join(process.cwd(), dir);
563
+ await fs.ensureDir(fullPath);
564
+ console.log(chalk.gray(`Created ${dir}`));
358
565
  }
359
566
  console.log(chalk.green("\u2705 Directories created"));
360
567
  }
361
- async function createExampleFiles() {
568
+ async function createExampleFiles(pathConfig, structure) {
362
569
  console.log(chalk.blue("\u{1F4DD} Creating example files..."));
363
- const utilsPath = path.join(process.cwd(), "lib/utils.ts");
570
+ const utilsPath = path.join(process.cwd(), pathConfig.utils);
364
571
  const utilsContent = `import { type ClassValue, clsx } from 'clsx';
365
572
  import { twMerge } from 'tailwind-merge';
366
573
 
@@ -368,16 +575,21 @@ export function cn(...inputs: ClassValue[]) {
368
575
  return twMerge(clsx(inputs));
369
576
  }
370
577
  `;
578
+ await fs.ensureDir(path.dirname(utilsPath));
371
579
  if (!fs.existsSync(utilsPath)) {
372
580
  await fs.writeFile(utilsPath, utilsContent);
373
- console.log(chalk.gray("Created lib/utils.ts"));
581
+ console.log(chalk.gray(`Created ${pathConfig.utils}`));
374
582
  }
375
- const providersPath = path.join(process.cwd(), "components/providers.tsx");
583
+ const providersPath = path.join(
584
+ process.cwd(),
585
+ path.dirname(pathConfig.components),
586
+ `theme-provider${structure.isTypeScript ? ".tsx" : ".jsx"}`
587
+ );
376
588
  const providersContent = `'use client';
377
589
 
378
590
  import { ThemeProvider } from 'next-themes';
379
591
 
380
- export function Providers({ children }: { children: React.ReactNode }) {
592
+ export function ThemeProvider({ children }: { children: React.ReactNode }) {
381
593
  return (
382
594
  <ThemeProvider attribute="class" defaultTheme="system" enableSystem>
383
595
  {children}
@@ -387,97 +599,63 @@ export function Providers({ children }: { children: React.ReactNode }) {
387
599
  `;
388
600
  if (!fs.existsSync(providersPath)) {
389
601
  await fs.writeFile(providersPath, providersContent);
390
- console.log(chalk.gray("Created components/providers.tsx"));
602
+ console.log(
603
+ chalk.gray(
604
+ `Created theme-provider.${structure.isTypeScript ? "tsx" : "jsx"}`
605
+ )
606
+ );
391
607
  }
392
608
  console.log(chalk.green("\u2705 Example files created"));
393
609
  }
394
610
  async function updatePackageScripts() {
395
- console.log(chalk.blue("\u{1F4CB} Updating package.json scripts..."));
611
+ console.log(chalk.blue("\u{1F4DD} Updating package.json scripts..."));
396
612
  const packageJsonPath = path.join(process.cwd(), "package.json");
397
613
  const packageJson2 = await fs.readJson(packageJsonPath);
398
- if (!packageJson2.scripts) {
399
- packageJson2.scripts = {};
400
- }
401
- if (!packageJson2.scripts["foundrykit"]) {
402
- packageJson2.scripts["foundrykit"] = "foundrykit";
403
- }
404
- if (!packageJson2.scripts["fk"]) {
405
- packageJson2.scripts["fk"] = "foundrykit";
406
- }
614
+ packageJson2.scripts = {
615
+ ...packageJson2.scripts,
616
+ "foundrykit:add": "pnpm dlx @foundrykit/cli add",
617
+ "foundrykit:update": "pnpm dlx @foundrykit/cli update"
618
+ };
407
619
  await fs.writeJson(packageJsonPath, packageJson2, { spaces: 2 });
408
620
  console.log(chalk.green("\u2705 Package scripts updated"));
409
621
  }
410
- async function createConfigFile(options) {
622
+ async function createConfigFile(options, structure, pathConfig) {
411
623
  console.log(chalk.blue("\u{1F4DD} Creating configuration file..."));
412
624
  const configPath = path.join(process.cwd(), "foundrykit.config.json");
413
625
  if (fs.existsSync(configPath) && !options.force) {
414
626
  console.log(chalk.yellow("\u26A0\uFE0F Configuration file already exists"));
415
627
  return;
416
628
  }
417
- const framework = await detectFramework();
418
- const isTypeScript = fs.existsSync(path.join(process.cwd(), "tsconfig.json"));
419
629
  const config = {
420
630
  version: "1.0.0",
421
631
  registry: "https://foundry.umiflow.com",
422
632
  paths: {
423
- utils: "./src/lib/utils.ts",
424
- primitives: "./src/components/primitives",
425
- components: "./src/components/components",
426
- blocks: "./src/components/blocks",
427
- hooks: "./src/hooks",
428
- animations: "./src/components/animations"
633
+ utils: pathConfig.utils,
634
+ primitives: pathConfig.primitives,
635
+ components: pathConfig.components,
636
+ blocks: pathConfig.blocks,
637
+ hooks: pathConfig.hooks,
638
+ animations: pathConfig.animations
429
639
  },
430
- packageManager: detectPackageManager(),
431
- typescript: isTypeScript,
432
- framework,
640
+ packageManager: structure.packageManager,
641
+ typescript: structure.isTypeScript,
642
+ framework: structure.framework,
433
643
  tailwind: {
434
- config: "./tailwind.config.js",
435
- css: "./src/styles/globals.css"
644
+ css: pathConfig.styles
436
645
  },
437
646
  aliases: {
438
- "@": "./src",
439
- "@/components": "./src/components",
440
- "@/lib": "./src/lib",
441
- "@/hooks": "./src/hooks"
647
+ "@": structure.baseDir.replace(/\/$/, "") || ".",
648
+ "@/components": path.dirname(pathConfig.components).replace(/\/$/, ""),
649
+ "@/lib": pathConfig.lib.replace(/\/$/, ""),
650
+ "@/hooks": pathConfig.hooks.replace(/\/$/, "")
442
651
  }
443
652
  };
444
- if (framework === "next") {
445
- const hasAppDir = fs.existsSync(path.join(process.cwd(), "app"));
446
- if (hasAppDir && !fs.existsSync(path.join(process.cwd(), "src/app"))) {
447
- config.paths = {
448
- utils: "./lib/utils.ts",
449
- primitives: "./components/primitives",
450
- components: "./components/components",
451
- blocks: "./components/blocks",
452
- hooks: "./hooks",
453
- animations: "./components/animations"
454
- };
455
- config.tailwind.css = "./app/globals.css";
456
- config.aliases = {
457
- "@": "./",
458
- "@/components": "./components",
459
- "@/lib": "./lib",
460
- "@/hooks": "./hooks"
461
- };
462
- }
463
- }
464
653
  await fs.writeJson(configPath, config, { spaces: 2 });
465
654
  console.log(chalk.green("\u2705 Created foundrykit.config.json"));
466
655
  }
467
- async function installUtils() {
468
- console.log(chalk.blue("\u{1F4E6} Installing utils..."));
469
- const configPath = path.join(process.cwd(), "foundrykit.config.json");
470
- if (!fs.existsSync(configPath)) {
471
- console.log(chalk.yellow("\u26A0\uFE0F Configuration file not found"));
472
- return;
473
- }
474
- const config = await fs.readJson(configPath);
475
- const utilsPath = path.join(process.cwd(), config.paths.utils);
476
- if (fs.existsSync(utilsPath)) {
477
- console.log(chalk.yellow("\u26A0\uFE0F Utils file already exists"));
478
- return;
479
- }
480
- await fs.ensureDir(path.dirname(utilsPath));
656
+ async function installUtils(pathConfig) {
657
+ console.log(chalk.blue("\u{1F4E6} Installing utility functions..."));
658
+ const utilsPath = path.join(process.cwd(), pathConfig.utils);
481
659
  const utilsContent = `import { type ClassValue, clsx } from 'clsx';
482
660
  import { twMerge } from 'tailwind-merge';
483
661
 
@@ -485,23 +663,14 @@ export function cn(...inputs: ClassValue[]) {
485
663
  return twMerge(clsx(inputs));
486
664
  }
487
665
  `;
488
- await fs.writeFile(utilsPath, utilsContent);
489
- console.log(chalk.green("\u2705 Created utils file"));
490
- }
491
- async function detectFramework() {
492
- const packageJsonPath = path.join(process.cwd(), "package.json");
493
- if (fs.existsSync(packageJsonPath)) {
494
- const packageJson2 = await fs.readJson(packageJsonPath);
495
- const deps = {
496
- ...packageJson2.dependencies,
497
- ...packageJson2.devDependencies
498
- };
499
- if (deps.next) return "next";
500
- if (deps.remix) return "remix";
501
- if (deps.vite) return "vite";
502
- if (deps.react) return "react";
666
+ const utilsDir = path.dirname(utilsPath);
667
+ await fs.ensureDir(utilsDir);
668
+ if (!fs.existsSync(utilsPath)) {
669
+ await fs.writeFile(utilsPath, utilsContent);
670
+ console.log(chalk.green("\u2705 Utility functions installed"));
671
+ } else {
672
+ console.log(chalk.gray("Utils file already exists, skipping..."));
503
673
  }
504
- return "react";
505
674
  }
506
675
 
507
676
  // src/commands/list.ts
@@ -830,9 +999,9 @@ import chalk4 from "chalk";
830
999
  import fs3 from "fs-extra";
831
1000
  import path3 from "path";
832
1001
  import inquirer2 from "inquirer";
833
- import { exec } from "child_process";
834
- import { promisify } from "util";
835
- var execAsync = promisify(exec);
1002
+ import { exec as exec2 } from "child_process";
1003
+ import { promisify as promisify2 } from "util";
1004
+ var execAsync = promisify2(exec2);
836
1005
  function addCommand() {
837
1006
  return new Command3("add").description("Add a component or block to your project").argument("[id]", "Component or block ID (optional for interactive mode)").option("--with-animation", "Include animation variants").option("-p, --path <path>", "Custom installation path").option("-o, --overwrite", "Overwrite existing files").action(async (id, options) => {
838
1007
  try {
@@ -984,11 +1153,16 @@ async function hasConflictingFiles(componentData, options) {
984
1153
  const config = await loadConfig();
985
1154
  const basePath = options.path || process.cwd();
986
1155
  for (const file of componentData.files) {
987
- const resolvedPath = resolveComponentPath(
988
- file.path,
989
- componentData.category,
990
- config
991
- );
1156
+ let resolvedPath;
1157
+ if (componentData.id === "utils" || file.path === "lib/utils.ts") {
1158
+ resolvedPath = config.paths.utils || "lib/utils.ts";
1159
+ } else {
1160
+ resolvedPath = resolveComponentPath(
1161
+ file.path,
1162
+ componentData.category,
1163
+ config
1164
+ );
1165
+ }
992
1166
  const filePath = path3.join(basePath, resolvedPath);
993
1167
  if (fs3.existsSync(filePath)) {
994
1168
  return true;
@@ -1001,13 +1175,24 @@ async function installComponentFiles(componentData, options) {
1001
1175
  const basePath = options.path || process.cwd();
1002
1176
  console.log(chalk4.blue("\u{1F4DD} Installing component files..."));
1003
1177
  for (const file of componentData.files) {
1004
- const resolvedPath = resolveComponentPath(
1005
- file.path,
1006
- componentData.category,
1007
- config
1008
- );
1178
+ let resolvedPath;
1179
+ if (componentData.id === "utils" || file.path === "lib/utils.ts") {
1180
+ resolvedPath = config.paths.utils || "lib/utils.ts";
1181
+ } else {
1182
+ resolvedPath = resolveComponentPath(
1183
+ file.path,
1184
+ componentData.category,
1185
+ config
1186
+ );
1187
+ }
1009
1188
  const filePath = path3.join(basePath, resolvedPath);
1010
1189
  await fs3.ensureDir(path3.dirname(filePath));
1190
+ if (!file.content || file.content.trim() === "") {
1191
+ console.log(
1192
+ chalk4.yellow(` \u26A0 Skipping ${resolvedPath} - no content available`)
1193
+ );
1194
+ continue;
1195
+ }
1011
1196
  await fs3.writeFile(filePath, file.content);
1012
1197
  console.log(chalk4.green(` \u2713 ${resolvedPath}`));
1013
1198
  }
@@ -1093,6 +1278,9 @@ async function loadConfig() {
1093
1278
  return fs3.readJson(configPath);
1094
1279
  }
1095
1280
  function resolveComponentPath(originalPath, category, config) {
1281
+ if (originalPath === "lib/utils.ts") {
1282
+ return config.paths.utils || "lib/utils.ts";
1283
+ }
1096
1284
  const categoryPath = config.paths[category] || config.paths.components;
1097
1285
  const fileName = path3.basename(originalPath);
1098
1286
  const componentName = path3.dirname(originalPath).split("/").pop() || "";