@codefast/cli 0.3.1-canary.11 → 0.3.1-canary.12

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
@@ -25,9 +25,38 @@ function runCommand(command) {
25
25
  stdio: "inherit"
26
26
  });
27
27
  }
28
- function writeConfigFile(filePath, content) {
29
- __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].writeFileSync(filePath, content);
30
- console.log(`šŸ“ Created ${filePath}`);
28
+ const FileUtils = {
29
+ exists (filePath) {
30
+ return __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].existsSync(filePath);
31
+ },
32
+ readFile (filePath) {
33
+ try {
34
+ return __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].readFileSync(filePath, "utf8");
35
+ } catch (error) {
36
+ throw new Error(`Failed to read file ${filePath}: ${getErrorMessage(error)}`);
37
+ }
38
+ },
39
+ writeFile (filePath, content) {
40
+ try {
41
+ __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].writeFileSync(filePath, content);
42
+ } catch (error) {
43
+ throw new Error(`Failed to write to file ${filePath}: ${getErrorMessage(error)}`);
44
+ }
45
+ },
46
+ writeConfigFile (filePath, content) {
47
+ __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].writeFileSync(filePath, content);
48
+ console.log(`šŸ“ Created ${filePath}`);
49
+ },
50
+ validatePath (filePath, filename) {
51
+ if (!this.exists(filePath)) {
52
+ console.warn(`āš ļø ${filename} not found, skipping update.`);
53
+ return false;
54
+ }
55
+ return true;
56
+ }
57
+ };
58
+ function getErrorMessage(error) {
59
+ return error instanceof Error ? error.message : "Unknown error occurred";
31
60
  }
32
61
  function validateProjectName(name) {
33
62
  if (!name || /^\s*$/.test(name)) return {
@@ -266,7 +295,6 @@ yarn-error.log*
266
295
  package-lock.json
267
296
  yarn.lock
268
297
  pnpm-lock.yaml
269
- src/app/globals.css
270
298
  `,
271
299
  description: ""
272
300
  }
@@ -292,6 +320,7 @@ export default config;
292
320
  {
293
321
  path: "eslint.config.mjs",
294
322
  content: `import { config } from "@codefast/eslint-config/next";
323
+
295
324
  /**
296
325
  * @see https://eslint.org/docs/latest/use/configure/configuration-files
297
326
  * @type {import('eslint').Linter.Config[]}
@@ -366,6 +395,7 @@ export const fontVariables = cn(fontGeistSans.variable, fontGeistMono.variable);
366
395
  :root {
367
396
  --font-sans: var(--font-geist-sans);
368
397
  --font-mono: var(--font-geist-mono);
398
+
369
399
  --input: var(--color-neutral-200);
370
400
  --border: var(--color-neutral-200);
371
401
  --ring: var(--color-neutral-400);
@@ -399,6 +429,7 @@ export const fontVariables = cn(fontGeistSans.variable, fontGeistMono.variable);
399
429
  --chart-4: var(--color-amber-600);
400
430
  --chart-5: var(--color-amber-500);
401
431
  }
432
+
402
433
  .dark {
403
434
  --input: var(--color-neutral-700);
404
435
  --border: --alpha(var(--color-neutral-700) / 50%);
@@ -439,87 +470,86 @@ export const fontVariables = cn(fontGeistSans.variable, fontGeistMono.variable);
439
470
  }
440
471
  ]
441
472
  };
473
+ function updateFileIfNeeded(filePath, filename, checkFn, updateFn, successMessage, alreadyUpdatedMessage) {
474
+ if (!FileUtils.validatePath(filePath, filename)) return;
475
+ const content = FileUtils.readFile(filePath);
476
+ if (checkFn(content)) return void console.log(`šŸ“ ${alreadyUpdatedMessage}`);
477
+ try {
478
+ const updatedContent = updateFn(content);
479
+ FileUtils.writeFile(filePath, updatedContent);
480
+ console.log(`šŸ“ ${successMessage}`);
481
+ } catch (error) {
482
+ console.warn(`āš ļø Failed to update ${filename}: ${getErrorMessage(error)}`);
483
+ }
484
+ }
442
485
  function updatePostcssConfig(projectDir) {
443
486
  const postcssConfigPath = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(projectDir, "postcss.config.mjs");
444
- if (!__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].existsSync(postcssConfigPath)) return void console.warn(`āš ļø postcss.config.mjs not found, skipping update.`);
445
- let content = __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].readFileSync(postcssConfigPath, "utf8");
446
487
  const typeComment = `/** @type {import('postcss-load-config').Config} */\n`;
447
- if (content.includes(typeComment)) console.log(`šŸ“ postcss.config.mjs already contains type comment, no changes made.`);
448
- else {
449
- content = content.replace("const config = {", `${typeComment}const config = {`);
450
- __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].writeFileSync(postcssConfigPath, content);
451
- console.log(`šŸ“ Updated postcss.config.mjs with type comment.`);
452
- }
488
+ updateFileIfNeeded(postcssConfigPath, "postcss.config.mjs", (content)=>content.includes(typeComment), (content)=>content.replace("const config = {", `${typeComment}const config = {`), "Updated postcss.config.mjs with type comment.", "postcss.config.mjs already contains type comment, no changes made.");
453
489
  }
454
490
  function updateLayoutFile(projectDir) {
455
491
  const layoutPath = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(projectDir, "src/app/layout.tsx");
456
- if (!__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].existsSync(layoutPath)) return void console.warn(`āš ļø src/app/layout.tsx not found, skipping update.`);
457
- let content = __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].readFileSync(layoutPath, "utf8");
458
- if (content.includes('<html className={cn(fontVariables, "antialiased")} lang="en">')) return void console.log(`šŸ“ src/app/layout.tsx already uses fontVariables in html tag, no changes made.`);
459
- const newImports = `import type { Metadata } from "next";
460
-
492
+ const htmlTarget = '<html className={cn(fontVariables, "antialiased")} lang="en">';
493
+ updateFileIfNeeded(layoutPath, "src/app/layout.tsx", (content)=>content.includes(htmlTarget), (content)=>{
494
+ const newImports = `import type { Metadata } from "next";
495
+ import type { JSX, ReactNode } from "react";
461
496
  import { cn } from "@codefast/ui";
462
-
463
497
  import { fontVariables } from "@/lib/fonts";
464
498
  import "@/app/globals.css";`;
465
- content = content.replaceAll(/import\s+{[^}]+}\s+from\s+["']next\/font\/google["'];\s*/g, "");
466
- content = content.replace(/const\s+geistSans\s*=[^;]+;\s*const\s+geistMono\s*=[^;]+;\s*/, "");
467
- content = content.replace(/import\s+type\s+{[^}]+}\s+from\s+["']next["'];\s*import\s+["']\.\/globals\.css["'];\s*/, newImports);
468
- const htmlRegex = /<html\s+lang="en"\s*>/;
469
- const newHtml = '<html className={cn(fontVariables, "antialiased")} lang="en">';
470
- content = content.replace(htmlRegex, newHtml);
471
- const bodyRegex = /<body\s+className={`[^`]*`}\s*>/;
472
- const newBody = "<body>";
473
- content = content.replace(bodyRegex, newBody);
474
- try {
475
- __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].writeFileSync(layoutPath, content);
476
- console.log(`šŸ“ Updated src/app/layout.tsx to use fontVariables in html tag and removed className from body`);
477
- } catch (error) {
478
- const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
479
- console.warn(`āš ļø Failed to update src/app/layout.tsx: ${errorMessage}`);
480
- }
499
+ let result = content;
500
+ result = result.replaceAll(/import\s+{[^}]+}\s+from\s+["']next\/font\/google["'];\s*/g, "");
501
+ result = result.replace(/const\s+geistSans\s*=[^;]+;\s*const\s+geistMono\s*=[^;]+;\s*/, "");
502
+ result = result.replace(/import\s+type\s+{[^}]+}\s+from\s+["']next["'];\s*import\s+["']\.\/globals\.css["'];\s*/, newImports);
503
+ result = result.replace(/export default function RootLayout\({\s*children,\s*}\s*:\s*Readonly<{\s*children:\s*React\.ReactNode;\s*}>\)\s*{/, "export default function RootLayout({ children }: Readonly<{ children: ReactNode }>): JSX.Element {");
504
+ result = result.replace(/<html\s+lang="en"\s*>/, htmlTarget);
505
+ result = result.replace(/<body\s+className={`[^`]*`}\s*>/, "<body>");
506
+ return result;
507
+ }, "Updated src/app/layout.tsx to use fontVariables in html tag, removed className from body, modified ReactNode import, and added return type to RootLayout", "src/app/layout.tsx already uses fontVariables in html tag, no changes made.");
481
508
  }
482
509
  function updatePackageJson(projectDir) {
483
510
  const packageJsonPath = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(projectDir, "package.json");
484
- const packageJson = JSON.parse(__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].readFileSync(packageJsonPath, "utf8"));
485
- packageJson.scripts = {
486
- ...packageJson.scripts,
487
- "deploy:preview": "vercel deploy --archive=tgz",
488
- "deploy:prod": "vercel deploy --archive=tgz --prod",
489
- format: "prettier --write --ignore-unknown .",
490
- "format:check": "prettier --check --ignore-unknown .",
491
- lint: "next lint --fix --max-warnings 0",
492
- "lint:check": "next lint --max-warnings 0",
493
- "package:install": "pnpm install",
494
- "package:outdated": "pnpm outdated",
495
- "package:upgrade": "pnpm update --latest",
496
- preview: "pnpm build && pnpm start"
497
- };
498
- packageJson["simple-git-hooks"] = {
499
- "pre-commit": "pnpm lint-staged",
500
- "commit-msg": "pnpm commitlint --edit"
501
- };
502
- __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
503
- console.log(`šŸ“ Updated package.json`);
511
+ if (!FileUtils.validatePath(packageJsonPath, "package.json")) return;
512
+ try {
513
+ const packageJson = JSON.parse(FileUtils.readFile(packageJsonPath));
514
+ packageJson.scripts = {
515
+ ...packageJson.scripts,
516
+ "deploy:preview": "vercel deploy --archive=tgz",
517
+ "deploy:prod": "vercel deploy --archive=tgz --prod",
518
+ format: "prettier --write --ignore-unknown .",
519
+ "format:check": "prettier --check --ignore-unknown .",
520
+ lint: "next lint --fix --max-warnings 0",
521
+ "lint:check": "next lint --max-warnings 0",
522
+ "package:install": "pnpm install",
523
+ "package:outdated": "pnpm outdated",
524
+ "package:upgrade": "pnpm update --latest",
525
+ preview: "pnpm build && pnpm start"
526
+ };
527
+ packageJson["simple-git-hooks"] = {
528
+ "pre-commit": "pnpm lint-staged",
529
+ "commit-msg": "pnpm commitlint --edit"
530
+ };
531
+ FileUtils.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
532
+ console.log(`šŸ“ Updated package.json`);
533
+ } catch (error) {
534
+ console.warn(`āš ļø Failed to update package.json: ${getErrorMessage(error)}`);
535
+ }
504
536
  }
505
537
  function updateNextConfig(projectDir) {
506
538
  const nextConfigPath = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(projectDir, "next.config.ts");
507
- if (!__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].existsSync(nextConfigPath)) return void console.warn(`āš ļø next.config.ts not found, skipping update.`);
508
- let content = __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].readFileSync(nextConfigPath, "utf8");
509
- if (content.includes('optimizePackageImports: ["@codefast/ui"]')) return void console.log(`šŸ“ next.config.ts already contains optimizePackageImports configuration, no changes made.`);
510
- const configPattern = /const nextConfig: NextConfig = \{/;
511
- const experimentalConfig = `const nextConfig: NextConfig = {
539
+ const optimizePackagesConfig = 'optimizePackageImports: ["@codefast/ui"]';
540
+ updateFileIfNeeded(nextConfigPath, "next.config.ts", (content)=>content.includes(optimizePackagesConfig), (content)=>content.replace(/const nextConfig: NextConfig = \{/, `const nextConfig: NextConfig = {
512
541
  experimental: {
513
542
  optimizePackageImports: ["@codefast/ui"],
514
- },`;
515
- content = content.replace(configPattern, experimentalConfig);
516
- try {
517
- __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].writeFileSync(nextConfigPath, content);
518
- console.log(`šŸ“ Updated next.config.ts with optimizePackageImports configuration.`);
519
- } catch (error) {
520
- const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
521
- console.warn(`āš ļø Failed to update next.config.ts: ${errorMessage}`);
522
- }
543
+ },`), "Updated next.config.ts with optimizePackageImports configuration.", "next.config.ts already contains optimizePackageImports configuration, no changes made.");
544
+ }
545
+ function updatePageFile(projectDir) {
546
+ const pagePath = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(projectDir, "src/app/page.tsx");
547
+ updateFileIfNeeded(pagePath, "src/app/page.tsx", (content)=>content.includes("): JSX.Element"), (content)=>{
548
+ let result = content;
549
+ if (!result.includes("import type { JSX }")) result = result.includes("import type {") ? result.replace(/import type {(?<types>[^}]+)} from "react";/, 'import type {$1, JSX } from "react";') : result.includes("import {") ? result.replace(/import {/, 'import type { JSX } from "react";\n\nimport {') : `import type { JSX } from "react";\n\n${result}`;
550
+ result = result.replace(/export default function Home\(\) {/, "export default function Home(): JSX.Element {");
551
+ return result;
552
+ }, "Updated src/app/page.tsx to add JSX.Element return type to Home component", "src/app/page.tsx already has JSX.Element return type, no changes made.");
523
553
  }
524
554
  function createConfigFiles(projectDir) {
525
555
  console.log(`\nšŸ“ Creating configuration files...`);
@@ -528,10 +558,10 @@ function createConfigFiles(projectDir) {
528
558
  for (const { path: filePath, content, description } of configs){
529
559
  const fullPath = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(projectDir, filePath);
530
560
  const dir = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].dirname(fullPath);
531
- if (!__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].existsSync(dir)) __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].mkdirSync(dir, {
561
+ if (!FileUtils.exists(dir)) __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].mkdirSync(dir, {
532
562
  recursive: true
533
563
  });
534
- writeConfigFile(fullPath, content);
564
+ FileUtils.writeConfigFile(fullPath, content);
535
565
  if (description) console.log(` ā„¹ļø ${filePath}: ${description}`);
536
566
  }
537
567
  }
@@ -550,8 +580,7 @@ function cleanupPackages() {
550
580
  try {
551
581
  packageJson = JSON.parse(__WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__.readFileSync(packageJsonPath, "utf8"));
552
582
  } catch (error) {
553
- const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
554
- console.warn(`āš ļø Error reading package.json: ${errorMessage}, skipping cleanup.`);
583
+ console.warn(`āš ļø Error reading package.json: ${getErrorMessage(error)}, skipping cleanup.`);
555
584
  return;
556
585
  }
557
586
  const dependencies = packageJson.dependencies ?? {};
@@ -587,6 +616,7 @@ function createProjectCommand(program) {
587
616
  console.log(`šŸ“‚ Moved to ${projectName}`);
588
617
  }
589
618
  updateLayoutFile(process.cwd());
619
+ updatePageFile(process.cwd());
590
620
  updatePostcssConfig(process.cwd());
591
621
  installDependencies();
592
622
  cleanupPackages();
@@ -597,6 +627,8 @@ function createProjectCommand(program) {
597
627
  runCommand("pnpm simple-git-hooks");
598
628
  console.log(`\nšŸ“ Formatting files with Prettier...`);
599
629
  runCommand("pnpm format");
630
+ console.log(`\nšŸ” Running ESLint to check code style...`);
631
+ runCommand("pnpm lint");
600
632
  console.log(`\nāœ… Project ${packageJsonExists ? "configured" : "created"} successfully!`);
601
633
  console.log(`- Project: ${projectName}`);
602
634
  console.log("- Next.js with TypeScript");
@@ -607,8 +639,7 @@ function createProjectCommand(program) {
607
639
  console.log(`\nšŸš€ To start development:`);
608
640
  console.log(`cd ${projectName} && pnpm dev`);
609
641
  } catch (error) {
610
- const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
611
- console.error(`\nāŒ An error occurred: ${errorMessage}`);
642
+ console.error(`\nāŒ An error occurred: ${getErrorMessage(error)}`);
612
643
  process.exit(1);
613
644
  } finally{
614
645
  rl.close();
@@ -743,7 +774,7 @@ const EXPORTS_PREVIEW_FILE = "exports-preview.json";
743
774
  const EXPORTS_ANALYSIS_FILE = "exports-analysis.json";
744
775
  const ENCODING = "utf8";
745
776
  const JSON_INDENT = 2;
746
- const FileUtils = {
777
+ const file_utils_FileUtils = {
747
778
  exists (filePath) {
748
779
  return __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].existsSync(filePath);
749
780
  },
@@ -781,19 +812,19 @@ const FileUtils = {
781
812
  }
782
813
  };
783
814
  function fileExists(filePath) {
784
- return FileUtils.exists(filePath);
815
+ return file_utils_FileUtils.exists(filePath);
785
816
  }
786
817
  function readPackageJson(filePath) {
787
- return FileUtils.readJsonFile(filePath);
818
+ return file_utils_FileUtils.readJsonFile(filePath);
788
819
  }
789
820
  function backupPackageJson(filePath) {
790
821
  try {
791
822
  const content = __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].readFileSync(filePath, ENCODING);
792
823
  const dir = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].dirname(filePath);
793
824
  const backupDir = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(dir, EXPORTS_ANALYSIS_DIR);
794
- FileUtils.ensureDirectoryExists(backupDir);
825
+ file_utils_FileUtils.ensureDirectoryExists(backupDir);
795
826
  const baseName = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].basename(filePath);
796
- const backupPath = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(backupDir, FileUtils.createBackupFilename(baseName));
827
+ const backupPath = __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(backupDir, file_utils_FileUtils.createBackupFilename(baseName));
797
828
  __WEBPACK_EXTERNAL_MODULE_node_fs_5ea92f0c__["default"].writeFileSync(backupPath, content, ENCODING);
798
829
  return true;
799
830
  } catch (error) {
@@ -802,11 +833,11 @@ function backupPackageJson(filePath) {
802
833
  }
803
834
  }
804
835
  function savePackageJson(filePath, content) {
805
- return FileUtils.writeJsonFile(filePath, content);
836
+ return file_utils_FileUtils.writeJsonFile(filePath, content);
806
837
  }
807
838
  function saveJsonToFile(packageDir, fileName, content) {
808
- const outputDir = FileUtils.getAnalysisDir(packageDir);
809
- FileUtils.writeJsonFile(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(outputDir, fileName), content);
839
+ const outputDir = file_utils_FileUtils.getAnalysisDir(packageDir);
840
+ file_utils_FileUtils.writeJsonFile(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].join(outputDir, fileName), content);
810
841
  }
811
842
  function saveExportsAnalysis(packageDir, analysis) {
812
843
  saveJsonToFile(packageDir, EXPORTS_ANALYSIS_FILE, analysis);
@@ -965,12 +996,6 @@ function main() {
965
996
  program.name("codefast").description("CodeFast CLI - A development toolkit for CodeFast.").version(getPackageVersion(), "-v, --version", "display CLI version");
966
997
  createUpdateExportsCommand(program);
967
998
  createProjectCommand(program);
968
- program.action(()=>{
969
- console.log('Use "codefast create-project" to create a new Next.js project.');
970
- console.log('Use "codefast update-exports" to update exports.');
971
- console.log('Run "codefast --help" to see all available commands.');
972
- program.help();
973
- });
974
999
  program.parse(process.argv);
975
1000
  }
976
1001
  main();