Skip to content

Commit 40d05e8

Browse files
committed
refactor(cli): simplify command registration and improve import statements
1 parent 952e0a3 commit 40d05e8

File tree

4 files changed

+114
-118
lines changed

4 files changed

+114
-118
lines changed

.changeset/little-laws-shake.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"@codefast-ui/progress-circle": patch
3+
"@codefast-ui/checkbox-group": patch
4+
"@codefast-ui/input-number": patch
5+
"@codefast/typescript-config": patch
6+
"@codefast-ui/input": patch
7+
"@codefast/eslint-config": patch
8+
"@codefast/style-guide": patch
9+
"@codefast/hooks": patch
10+
"@codefast/cli": patch
11+
"@codefast/ui": patch
12+
---
13+
14+
refactor(cli): simplify command registration and improve import statements
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Command } from "commander";
1+
import { Command } from "commander";
22

33
import { checkExistingProject } from "@/commands/create-project/check-existing-project";
44
import {
@@ -14,87 +14,76 @@ import { checkEnvironment } from "@/commands/create-project/environment";
1414
import { createNextProject } from "@/commands/create-project/next-project";
1515
import { getErrorMessage, rl, runCommand } from "@/commands/create-project/utils";
1616

17-
/**
18-
* Sets up the "create-project" command in the provided CLI program. This command allows users
19-
* to create a new Next.js project with a recommended configuration.
20-
*
21-
* The "create-project" command includes features such as scaffolding a Next.js project, applying
22-
* TypeScript, TailwindCSS, ESLint, Prettier, Git hooks, and other tools, ensuring a streamlined
23-
* development experience.
24-
*
25-
* @param program - The CLI program instance in which the command will be registered.
26-
*/
27-
export function createProjectCommand(program: Command): void {
28-
program
29-
.command("create-project [name]")
30-
.description("Create a new Next.js project with recommended setup")
31-
.action(async (projectNameArg) => {
32-
try {
33-
// Check environment (PNPM, write permissions)
34-
checkEnvironment();
35-
36-
// Check if package.json exists and get the project name
37-
const { projectName, packageJsonExists } = await checkExistingProject(projectNameArg);
38-
39-
if (!packageJsonExists) {
40-
console.log(`\n🚀 Starting project creation for ${projectName}...\n`);
41-
createNextProject(projectName);
42-
process.chdir(projectName);
43-
console.log(`📂 Moved to ${projectName}`);
44-
}
45-
46-
// Update layout.tsx to use fontVariables
47-
updateLayoutFile(process.cwd());
48-
49-
// Update page.tsx to add JSX.Element return types
50-
updatePageFile(process.cwd());
51-
52-
// Edit the postcss.config.mjs file
53-
updatePostcssConfig(process.cwd());
54-
55-
// Install dependencies
56-
installDependencies();
57-
58-
// Remove unwanted packages
59-
cleanupPackages();
60-
61-
// Create configuration files
62-
createConfigFiles(process.cwd());
63-
64-
// Update next.config.ts with experimental configuration
65-
updateNextConfig(process.cwd());
66-
67-
// Update package.json
68-
updatePackageJson(process.cwd());
69-
70-
// Enable git hooks
71-
console.log(`\n🔄 Activating git hooks...`);
72-
runCommand("pnpm simple-git-hooks");
73-
74-
// Run Prettier to format the file
75-
console.log(`\n📝 Formatting files with Prettier...`);
76-
runCommand("pnpm format");
77-
78-
// Run ESLint
79-
console.log(`\n🔍 Running ESLint to check code style...`);
80-
runCommand("pnpm lint");
81-
82-
// Completion notice
83-
console.log(`\n✅ Project ${packageJsonExists ? "configured" : "created"} successfully!`);
84-
console.log(`- Project: ${projectName}`);
85-
console.log("- Next.js with TypeScript");
86-
console.log("- TailwindCSS and @codefast/ui");
87-
console.log("- ESLint, Prettier, Commitlint, and Git Hooks");
88-
console.log("- Lint-staged for pre-commit checks");
89-
console.log(`\n📁 Project directory: ${process.cwd()}`);
90-
console.log(`\n🚀 To start development:`);
91-
console.log(`cd ${projectName} && pnpm dev`);
92-
} catch (error) {
93-
console.error(`\n❌ An error occurred: ${getErrorMessage(error)}`);
94-
process.exit(1);
95-
} finally {
96-
// Close readline interface
97-
rl.close();
17+
export const createProjectCommand = new Command()
18+
.name("create-project [name]")
19+
.description("Create a new Next.js project with recommended setup")
20+
.action(async (projectNameArg) => {
21+
try {
22+
// Check environment (PNPM, write permissions)
23+
checkEnvironment();
24+
25+
// Check if package.json exists and get the project name
26+
const { projectName, packageJsonExists } = await checkExistingProject(projectNameArg);
27+
28+
if (!packageJsonExists) {
29+
console.log(`\n🚀 Starting project creation for ${projectName}...\n`);
30+
createNextProject(projectName);
31+
process.chdir(projectName);
32+
console.log(`📂 Moved to ${projectName}`);
9833
}
99-
});
100-
}
34+
35+
// Update layout.tsx to use fontVariables
36+
updateLayoutFile(process.cwd());
37+
38+
// Update page.tsx to add JSX.Element return types
39+
updatePageFile(process.cwd());
40+
41+
// Edit the postcss.config.mjs file
42+
updatePostcssConfig(process.cwd());
43+
44+
// Install dependencies
45+
installDependencies();
46+
47+
// Remove unwanted packages
48+
cleanupPackages();
49+
50+
// Create configuration files
51+
createConfigFiles(process.cwd());
52+
53+
// Update next.config.ts with experimental configuration
54+
updateNextConfig(process.cwd());
55+
56+
// Update package.json
57+
updatePackageJson(process.cwd());
58+
59+
// Enable git hooks
60+
console.log(`\n🔄 Activating git hooks...`);
61+
runCommand("pnpm simple-git-hooks");
62+
63+
// Run Prettier to format the file
64+
console.log(`\n📝 Formatting files with Prettier...`);
65+
runCommand("pnpm format");
66+
67+
// Run ESLint
68+
console.log(`\n🔍 Running ESLint to check code style...`);
69+
runCommand("pnpm lint");
70+
71+
// Completion notice
72+
console.log(`\n✅ Project ${packageJsonExists ? "configured" : "created"} successfully!`);
73+
console.log(`- Project: ${projectName}`);
74+
console.log("- Next.js with TypeScript");
75+
console.log("- TailwindCSS and @codefast/ui");
76+
console.log("- ESLint, Prettier, Commitlint, and Git Hooks");
77+
console.log("- Lint-staged for pre-commit checks");
78+
console.log(`\n📁 Project directory: ${process.cwd()}`);
79+
console.log(`\n🚀 To start development:`);
80+
console.log(`cd ${projectName} && pnpm dev`);
81+
} catch (error) {
82+
console.error(`\n❌ An error occurred: ${getErrorMessage(error)}`);
83+
process.exit(1);
84+
} finally {
85+
// Close readline interface
86+
rl.close();
87+
process.exit(0);
88+
}
89+
});
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
1-
import type { Command } from "commander";
1+
import { Command } from "commander";
22

33
import type { ProcessOptions } from "@/commands/update-exports/types";
44

55
import { processAllPackages } from "@/commands/update-exports/process-package";
66

7-
/**
8-
* Creates a commander command for updating exports
9-
* @param program - Commander program instance
10-
*/
11-
export function createUpdateExportsCommand(program: Command): void {
12-
// noinspection RequiredAttributes
13-
program
14-
.command("update-exports")
15-
.description("Updates exports for all packages")
16-
.option("-p, --package <package>", "Filter by package name")
17-
.option("-d, --dry-run", "Run without making changes")
18-
.option("-c, --config <path>", "Path to configuration file")
19-
.action(async (options) => {
20-
const processorOptions: ProcessOptions = {
21-
packageFilter: options.package,
22-
dryRun: Boolean(options.dryRun),
23-
configPath: options.config,
24-
};
7+
export const updateExportsCommand = new Command()
8+
.name("update-exports")
9+
.description("Updates exports for all packages")
10+
.option("-p, --package <package>", "Filter by package name")
11+
.option("-d, --dry-run", "Run without making changes")
12+
.option("-c, --config <path>", "Path to configuration file")
13+
.action(async (options) => {
14+
const processorOptions: ProcessOptions = {
15+
packageFilter: options.package,
16+
dryRun: Boolean(options.dryRun),
17+
configPath: options.config,
18+
};
2519

26-
try {
27-
await processAllPackages(processorOptions);
28-
} catch (error) {
29-
console.error("Error processing packages:", error);
30-
process.exit(1);
31-
}
32-
});
33-
}
20+
try {
21+
await processAllPackages(processorOptions);
22+
} catch (error) {
23+
console.error("Error processing packages:", error);
24+
process.exit(1);
25+
} finally {
26+
process.exit(0);
27+
}
28+
});

packages/cli/src/index.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { Command } from "commander";
44

55
import { createProjectCommand } from "@/commands/create-project/command";
6-
import { createUpdateExportsCommand } from "@/commands/update-exports";
6+
import { updateExportsCommand } from "@/commands/update-exports/command";
77
import { getPackageVersion } from "@/lib/package-info";
88

99
function main(): void {
@@ -12,12 +12,10 @@ function main(): void {
1212
program
1313
.name("codefast")
1414
.description("CodeFast CLI - A development toolkit for CodeFast.")
15-
.version(getPackageVersion(), "-v, --version", "display CLI version");
16-
17-
createUpdateExportsCommand(program);
18-
createProjectCommand(program);
19-
20-
program.parse(process.argv);
15+
.version(getPackageVersion(), "-v, --version", "display CLI version")
16+
.addCommand(updateExportsCommand)
17+
.addCommand(createProjectCommand)
18+
.parse(process.argv);
2119
}
2220

2321
main();

0 commit comments

Comments
 (0)