@foundrykit/cli 1.0.5 → 1.0.7

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/cli.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,262 @@ 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
+ "ui"
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
+ "@foundrykit/tailwind",
298
+ "@radix-ui/react-dialog",
299
+ "@radix-ui/react-dropdown-menu",
300
+ "@radix-ui/react-label",
301
+ "@radix-ui/react-popover",
302
+ "@radix-ui/react-select",
303
+ "@radix-ui/react-separator",
304
+ "@radix-ui/react-slot",
305
+ "@radix-ui/react-switch",
306
+ "@radix-ui/react-tabs",
307
+ "@radix-ui/react-tooltip"
308
+ ];
309
+ const devDependencies = ["tailwindcss", "autoprefixer", "postcss"];
96
310
  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")
311
+ const installCmd = packageManager === "npm" ? "install" : "add";
312
+ await execAsync2(
313
+ `${packageManager} ${installCmd} ${dependencies.join(" ")}`
105
314
  );
106
- console.log(chalk.gray("Please install them manually:"));
107
- console.log(
108
- chalk.cyan(` ${detectPackageManager()} add ${dependencies.join(" ")}`)
315
+ const devFlag = packageManager === "npm" ? "--save-dev" : "-D";
316
+ await execAsync2(
317
+ `${packageManager} ${installCmd} ${devFlag} ${devDependencies.join(" ")}`
109
318
  );
319
+ console.log(chalk.green("\u2705 Dependencies installed"));
320
+ } catch (error) {
321
+ console.error(chalk.yellow("\u26A0\uFE0F Failed to install some dependencies"));
322
+ console.error(chalk.gray("You may need to install them manually"));
110
323
  }
111
324
  }
112
325
  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";
326
+ if (fs.existsSync(path.join(process.cwd(), "pnpm-lock.yaml"))) return "pnpm";
327
+ if (fs.existsSync(path.join(process.cwd(), "yarn.lock"))) return "yarn";
328
+ if (fs.existsSync(path.join(process.cwd(), "bun.lockb"))) return "bun";
116
329
  return "npm";
117
330
  }
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");
331
+ async function setupTailwindV4(pathConfig, projectStructure, skipPrompts) {
332
+ console.log(chalk.blue("\u{1F3A8} Setting up Tailwind CSS v4..."));
333
+ const layoutPath = await findLayoutFile(projectStructure);
334
+ if (layoutPath) {
335
+ const layoutContent = await fs.readFile(layoutPath, "utf-8");
123
336
  if (layoutContent.includes("@foundrykit/tailwind/globals.css")) {
337
+ if (skipPrompts) {
338
+ console.log(
339
+ chalk.green(
340
+ "\u2705 Keeping existing import from @foundrykit/tailwind/globals.css"
341
+ )
342
+ );
343
+ return;
344
+ }
124
345
  const { importPreference } = await inquirer.prompt([
125
346
  {
126
347
  type: "list",
@@ -147,27 +368,18 @@ async function setupTailwindV4() {
147
368
  );
148
369
  return;
149
370
  }
150
- await createLocalGlobalsAndUpdateLayout(layoutInfo.path, layoutContent);
371
+ await createOrUpdateGlobalsCss(pathConfig, projectStructure);
372
+ await updateLayoutImport(layoutPath, pathConfig);
151
373
  return;
152
374
  }
153
375
  const globalsCssImportMatch = layoutContent.match(
154
- /import\s+['"]([^'"]*globals\.css)['"]/
376
+ /import\s+['"]([^'"]*globals?\.css)['"]/
155
377
  );
156
378
  if (globalsCssImportMatch && globalsCssImportMatch[1]) {
157
379
  const importPath = globalsCssImportMatch[1];
158
- const resolvedPath = importPath.startsWith(".") ? path.join(path.dirname(layoutInfo.path), importPath) : path.join(process.cwd(), importPath.replace(/^@\//, "app/"));
380
+ const resolvedPath = resolveImportPath(layoutPath, importPath);
159
381
  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);
382
+ await updateExistingGlobalsCss(resolvedPath);
171
383
  console.log(
172
384
  chalk.green("\u2705 Updated existing globals.css with FoundryKit styles")
173
385
  );
@@ -175,192 +387,188 @@ ${globalsCssContent}`;
175
387
  }
176
388
  }
177
389
  }
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
- ]);
390
+ let setupPreference = "local";
391
+ if (!skipPrompts) {
392
+ const answer = await inquirer.prompt([
393
+ {
394
+ type: "list",
395
+ name: "setupPreference",
396
+ message: "How would you like to import FoundryKit styles?",
397
+ choices: [
398
+ {
399
+ name: "Create/update a local globals.css file (recommended for customization)",
400
+ value: "local"
401
+ },
402
+ {
403
+ name: "Import directly from @foundrykit/tailwind/globals.css",
404
+ value: "direct"
405
+ }
406
+ ],
407
+ default: "local"
408
+ }
409
+ ]);
410
+ setupPreference = answer.setupPreference;
411
+ }
196
412
  if (setupPreference === "direct") {
197
- await addDirectImportToLayout();
413
+ await addDirectImportToLayout(layoutPath, pathConfig);
198
414
  } 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;
415
+ await createOrUpdateGlobalsCss(pathConfig, projectStructure);
416
+ if (layoutPath) {
417
+ await updateLayoutImport(layoutPath, pathConfig);
215
418
  }
216
419
  }
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")) {
420
+ console.log(chalk.green("\u2705 Tailwind CSS v4 configured"));
421
+ }
422
+ async function createOrUpdateGlobalsCss(pathConfig, projectStructure) {
423
+ const globalsCssPath = path.join(process.cwd(), pathConfig.styles);
424
+ if (fs.existsSync(globalsCssPath)) {
425
+ await updateExistingGlobalsCss(globalsCssPath);
426
+ } else {
223
427
  const cssContent = `/* Import FoundryKit's Tailwind CSS v4 styles */
224
428
  @import "@foundrykit/tailwind/globals.css";
225
429
 
226
430
  /* 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);
431
+ `;
432
+ await fs.ensureDir(path.dirname(globalsCssPath));
433
+ await fs.writeFile(globalsCssPath, cssContent);
434
+ console.log(chalk.green(`\u2705 Created ${pathConfig.styles}`));
245
435
  }
246
436
  }
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';`));
437
+ async function updateExistingGlobalsCss(globalsCssPath) {
438
+ const content = await fs.readFile(globalsCssPath, "utf-8");
439
+ if (content.includes("@foundrykit/tailwind/globals.css")) {
440
+ console.log(chalk.gray("globals.css already imports FoundryKit styles"));
256
441
  return;
257
442
  }
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")
443
+ if (content.includes('@import "tailwindcss"')) {
444
+ const updatedContent = content.replace(
445
+ '@import "tailwindcss"',
446
+ `/* Import FoundryKit's Tailwind CSS v4 styles */
447
+ @import "@foundrykit/tailwind/globals.css"`
262
448
  );
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);
449
+ await fs.writeFile(globalsCssPath, updatedContent);
450
+ console.log(chalk.green("\u2705 Updated globals.css with FoundryKit styles"));
274
451
  } else {
275
- updatedContent = importStatement + "\n" + layoutContent;
452
+ const updatedContent = `/* Import FoundryKit's Tailwind CSS v4 styles */
453
+ @import "@foundrykit/tailwind/globals.css";
454
+
455
+ /* Your custom styles below */
456
+ ${content}`;
457
+ await fs.writeFile(globalsCssPath, updatedContent);
458
+ console.log(chalk.green("\u2705 Updated globals.css with FoundryKit styles"));
276
459
  }
277
- await fs.writeFile(layoutInfo.path, updatedContent);
278
- console.log(
279
- chalk.green("\u2705 Added direct import of FoundryKit styles to layout")
280
- );
281
460
  }
282
- async function findLayoutFile() {
283
- const layoutPaths = [
461
+ async function findLayoutFile(projectStructure) {
462
+ const possiblePaths = [];
463
+ if (projectStructure.appPath) {
464
+ const ext = projectStructure.isTypeScript ? ".tsx" : ".jsx";
465
+ possiblePaths.push(
466
+ path.join(projectStructure.appPath, `layout${ext}`),
467
+ path.join(projectStructure.appPath, `layout.js`)
468
+ );
469
+ }
470
+ possiblePaths.push(
284
471
  "app/layout.tsx",
285
472
  "app/layout.jsx",
286
473
  "app/layout.js",
287
474
  "src/app/layout.tsx",
288
475
  "src/app/layout.jsx",
289
476
  "src/app/layout.js"
290
- ];
291
- for (const p of layoutPaths) {
292
- const fullPath = path.join(process.cwd(), p);
477
+ );
478
+ for (const layoutPath of possiblePaths) {
479
+ const fullPath = path.join(process.cwd(), layoutPath);
293
480
  if (fs.existsSync(fullPath)) {
294
- return { path: fullPath };
481
+ return fullPath;
295
482
  }
296
483
  }
297
484
  return null;
298
485
  }
299
- async function updateLayoutImport(globalCssPath) {
300
- const layoutInfo = await findLayoutFile();
301
- if (!layoutInfo) {
486
+ async function updateLayoutImport(layoutPath, pathConfig) {
487
+ try {
488
+ let content = await fs.readFile(layoutPath, "utf-8");
489
+ const fromPath = path.dirname(layoutPath);
490
+ const toPath = path.join(process.cwd(), pathConfig.styles);
491
+ const relativePath = path.relative(fromPath, toPath).replace(/\\/g, "/");
492
+ const importPath = relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
493
+ const globalImportRegex = /import\s+['"]([^'"]*globals?\.css)['"]/;
494
+ const match = content.match(globalImportRegex);
495
+ if (match) {
496
+ content = content.replace(globalImportRegex, `import '${importPath}'`);
497
+ } else if (!content.includes(importPath)) {
498
+ const importStatement = `import '${importPath}';
499
+ `;
500
+ const importRegex = /^(import\s+.*?;\s*)+/m;
501
+ const importMatch = content.match(importRegex);
502
+ if (importMatch) {
503
+ const lastImportIndex = importMatch.index + importMatch[0].length;
504
+ content = content.slice(0, lastImportIndex) + importStatement + content.slice(lastImportIndex);
505
+ } else {
506
+ content = importStatement + content;
507
+ }
508
+ }
509
+ await fs.writeFile(layoutPath, content);
302
510
  console.log(
303
- chalk.yellow(
304
- "\u26A0\uFE0F No layout file found. Please import globals.css manually in your layout."
511
+ chalk.gray(
512
+ `Updated ${path.relative(process.cwd(), layoutPath)} with CSS import`
305
513
  )
306
514
  );
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;
515
+ } catch (error) {
516
+ console.error(chalk.yellow("\u26A0\uFE0F Failed to update layout with CSS import"));
313
517
  }
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;
518
+ }
519
+ async function addDirectImportToLayout(layoutPath, pathConfig) {
520
+ try {
521
+ let content = await fs.readFile(layoutPath, "utf-8");
522
+ const importStatement = `import '@foundrykit/tailwind/globals.css';
523
+ `;
524
+ if (!content.includes("@foundrykit/tailwind/globals.css")) {
525
+ const importRegex = /^(import\s+.*?;\s*)+/m;
526
+ const match = content.match(importRegex);
527
+ if (match) {
528
+ const lastImportIndex = match.index + match[0].length;
529
+ content = content.slice(0, lastImportIndex) + importStatement + content.slice(lastImportIndex);
530
+ } else {
531
+ content = importStatement + content;
532
+ }
533
+ await fs.writeFile(layoutPath, content);
534
+ console.log(
535
+ chalk.green("\u2705 Added direct import of FoundryKit styles to layout")
536
+ );
537
+ }
538
+ } catch (error) {
539
+ console.error(chalk.yellow("\u26A0\uFE0F Failed to add CSS import to layout"));
327
540
  }
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;
541
+ }
542
+ function resolveImportPath(fromFile, importPath) {
543
+ if (importPath.startsWith(".")) {
544
+ return path.join(path.dirname(fromFile), importPath);
545
+ } else if (importPath.startsWith("@/")) {
546
+ return path.join(process.cwd(), importPath.replace("@/", "src/"));
547
+ } else if (importPath.startsWith("/")) {
548
+ return path.join(process.cwd(), importPath);
342
549
  }
343
- await fs.writeFile(layoutInfo.path, updatedContent);
344
- console.log(chalk.green("\u2705 Layout file updated with globals.css import"));
550
+ return path.join(process.cwd(), importPath);
345
551
  }
346
- async function createDirectories() {
552
+ async function createDirectories(pathConfig) {
347
553
  console.log(chalk.blue("\u{1F4C1} Creating component directories..."));
348
554
  const dirs = [
349
- "components/primitives",
350
- "components/components",
351
- "components/blocks",
352
- "components/animations",
353
- "lib",
354
- "hooks"
555
+ pathConfig.primitives,
556
+ pathConfig.components,
557
+ pathConfig.blocks,
558
+ pathConfig.animations,
559
+ pathConfig.hooks,
560
+ pathConfig.lib
355
561
  ];
356
562
  for (const dir of dirs) {
357
- await fs.ensureDir(path.join(process.cwd(), dir));
563
+ const fullPath = path.join(process.cwd(), dir);
564
+ await fs.ensureDir(fullPath);
565
+ console.log(chalk.gray(`Created ${dir}`));
358
566
  }
359
567
  console.log(chalk.green("\u2705 Directories created"));
360
568
  }
361
- async function createExampleFiles() {
569
+ async function createExampleFiles(pathConfig, structure) {
362
570
  console.log(chalk.blue("\u{1F4DD} Creating example files..."));
363
- const utilsPath = path.join(process.cwd(), "lib/utils.ts");
571
+ const utilsPath = path.join(process.cwd(), pathConfig.utils);
364
572
  const utilsContent = `import { type ClassValue, clsx } from 'clsx';
365
573
  import { twMerge } from 'tailwind-merge';
366
574
 
@@ -368,16 +576,21 @@ export function cn(...inputs: ClassValue[]) {
368
576
  return twMerge(clsx(inputs));
369
577
  }
370
578
  `;
579
+ await fs.ensureDir(path.dirname(utilsPath));
371
580
  if (!fs.existsSync(utilsPath)) {
372
581
  await fs.writeFile(utilsPath, utilsContent);
373
- console.log(chalk.gray("Created lib/utils.ts"));
582
+ console.log(chalk.gray(`Created ${pathConfig.utils}`));
374
583
  }
375
- const providersPath = path.join(process.cwd(), "components/providers.tsx");
584
+ const providersPath = path.join(
585
+ process.cwd(),
586
+ path.dirname(pathConfig.components),
587
+ `theme-provider${structure.isTypeScript ? ".tsx" : ".jsx"}`
588
+ );
376
589
  const providersContent = `'use client';
377
590
 
378
591
  import { ThemeProvider } from 'next-themes';
379
592
 
380
- export function Providers({ children }: { children: React.ReactNode }) {
593
+ export function ThemeProvider({ children }: { children: React.ReactNode }) {
381
594
  return (
382
595
  <ThemeProvider attribute="class" defaultTheme="system" enableSystem>
383
596
  {children}
@@ -387,97 +600,63 @@ export function Providers({ children }: { children: React.ReactNode }) {
387
600
  `;
388
601
  if (!fs.existsSync(providersPath)) {
389
602
  await fs.writeFile(providersPath, providersContent);
390
- console.log(chalk.gray("Created components/providers.tsx"));
603
+ console.log(
604
+ chalk.gray(
605
+ `Created theme-provider.${structure.isTypeScript ? "tsx" : "jsx"}`
606
+ )
607
+ );
391
608
  }
392
609
  console.log(chalk.green("\u2705 Example files created"));
393
610
  }
394
611
  async function updatePackageScripts() {
395
- console.log(chalk.blue("\u{1F4CB} Updating package.json scripts..."));
612
+ console.log(chalk.blue("\u{1F4DD} Updating package.json scripts..."));
396
613
  const packageJsonPath = path.join(process.cwd(), "package.json");
397
614
  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
- }
615
+ packageJson2.scripts = {
616
+ ...packageJson2.scripts,
617
+ "foundrykit:add": "pnpm dlx @foundrykit/cli add",
618
+ "foundrykit:update": "pnpm dlx @foundrykit/cli update"
619
+ };
407
620
  await fs.writeJson(packageJsonPath, packageJson2, { spaces: 2 });
408
621
  console.log(chalk.green("\u2705 Package scripts updated"));
409
622
  }
410
- async function createConfigFile(options) {
623
+ async function createConfigFile(options, structure, pathConfig) {
411
624
  console.log(chalk.blue("\u{1F4DD} Creating configuration file..."));
412
625
  const configPath = path.join(process.cwd(), "foundrykit.config.json");
413
626
  if (fs.existsSync(configPath) && !options.force) {
414
627
  console.log(chalk.yellow("\u26A0\uFE0F Configuration file already exists"));
415
628
  return;
416
629
  }
417
- const framework = await detectFramework();
418
- const isTypeScript = fs.existsSync(path.join(process.cwd(), "tsconfig.json"));
419
630
  const config = {
420
631
  version: "1.0.0",
421
632
  registry: "https://foundry.umiflow.com",
422
633
  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"
634
+ utils: pathConfig.utils,
635
+ primitives: pathConfig.primitives,
636
+ components: pathConfig.components,
637
+ blocks: pathConfig.blocks,
638
+ hooks: pathConfig.hooks,
639
+ animations: pathConfig.animations
429
640
  },
430
- packageManager: detectPackageManager(),
431
- typescript: isTypeScript,
432
- framework,
641
+ packageManager: structure.packageManager,
642
+ typescript: structure.isTypeScript,
643
+ framework: structure.framework,
433
644
  tailwind: {
434
- config: "./tailwind.config.js",
435
- css: "./src/styles/globals.css"
645
+ css: pathConfig.styles
436
646
  },
437
647
  aliases: {
438
- "@": "./src",
439
- "@/components": "./src/components",
440
- "@/lib": "./src/lib",
441
- "@/hooks": "./src/hooks"
648
+ "@": structure.baseDir.replace(/\/$/, "") || ".",
649
+ "@/components": path.dirname(pathConfig.components).replace(/\/$/, ""),
650
+ "@/lib": pathConfig.lib.replace(/\/$/, ""),
651
+ "@/hooks": pathConfig.hooks.replace(/\/$/, "")
442
652
  }
443
653
  };
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
654
  await fs.writeJson(configPath, config, { spaces: 2 });
465
655
  console.log(chalk.green("\u2705 Created foundrykit.config.json"));
466
656
  }
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));
657
+ async function installUtils(pathConfig) {
658
+ console.log(chalk.blue("\u{1F4E6} Installing utility functions..."));
659
+ const utilsPath = path.join(process.cwd(), pathConfig.utils);
481
660
  const utilsContent = `import { type ClassValue, clsx } from 'clsx';
482
661
  import { twMerge } from 'tailwind-merge';
483
662
 
@@ -485,23 +664,14 @@ export function cn(...inputs: ClassValue[]) {
485
664
  return twMerge(clsx(inputs));
486
665
  }
487
666
  `;
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";
667
+ const utilsDir = path.dirname(utilsPath);
668
+ await fs.ensureDir(utilsDir);
669
+ if (!fs.existsSync(utilsPath)) {
670
+ await fs.writeFile(utilsPath, utilsContent);
671
+ console.log(chalk.green("\u2705 Utility functions installed"));
672
+ } else {
673
+ console.log(chalk.gray("Utils file already exists, skipping..."));
503
674
  }
504
- return "react";
505
675
  }
506
676
 
507
677
  // src/commands/list.ts
@@ -830,9 +1000,9 @@ import chalk4 from "chalk";
830
1000
  import fs3 from "fs-extra";
831
1001
  import path3 from "path";
832
1002
  import inquirer2 from "inquirer";
833
- import { exec } from "child_process";
834
- import { promisify } from "util";
835
- var execAsync = promisify(exec);
1003
+ import { exec as exec2 } from "child_process";
1004
+ import { promisify as promisify2 } from "util";
1005
+ var execAsync = promisify2(exec2);
836
1006
  function addCommand() {
837
1007
  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
1008
  try {