Skip to content

Commit

Permalink
Update index.js and project creation scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
utkarsh1311 committed Oct 23, 2024
1 parent 59573cf commit 80407d8
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 124 deletions.
82 changes: 47 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,61 @@ import banner from "./banner.js";
import createExpressProject from "./projects/createExpressProject.js";
import createReactProject from "./projects/createReactApp.js";

// Improved error handling
process.on("unhandledRejection", (err) => {
console.log(chalk.red("Error creating project 😢😢😢"));
console.error(chalk.red("Error creating project:"), err);
process.exit(1);
});

process.on("SIGINT", () => {
process.exit(1);
console.log(chalk.yellow("\nProcess terminated by user."));
process.exit(0);
});

console.log(chalk.yellowBright(banner));

console.log(chalk.bgRedBright(`Welcome to the make-project CLI`));
async function main() {
console.log(chalk.yellowBright(banner));
console.log(chalk.bgRedBright(`Welcome to the make-project CLI`));

const projectName = await input({
message: chalk.blue("Enter the project name ❓: "),
});
const projectName = await input({
message: chalk.blue("Enter the project name ❓: "),
validate: (input) => input.trim() !== "" || "Project name cannot be empty",
});

const projectType = await select({
message: "Select project type ⚙️",
choices: [
{
name: "React",
value: "react",
description:
" React is a JavaScript library for building user interfaces",
},
{
name: "Express",
value: "express",
description:
" Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.",
},
],
});
const projectType = await select({
message: "Select project type ⚙️",
choices: [
{
name: "React",
value: "react",
description: "React is a JavaScript library for building user interfaces",
},
{
name: "Express",
value: "express",
description: "Express is a minimal and flexible Node.js web application framework",
},
],
});

switch (projectType) {
case "react":
createReactProject(projectName);
break;
case "express":
createExpressProject(projectName);
break;
default:
console.log(chalk.red("Error creating project 😢😢😢"));
break;
try {
switch (projectType) {
case "react":
await createReactProject(projectName);
break;
case "express":
await createExpressProject(projectName);
break;
default:
throw new Error(`Unsupported project type: ${projectType}`);
}
console.log(chalk.green(`✅ Project "${projectName}" created successfully!`));
} catch (error) {
console.error(chalk.red(`Error creating project: ${error.message}`));
process.exit(1);
}
}

main().catch((err) => {
console.error(chalk.red("Unexpected error:"), err);
process.exit(1);
});
91 changes: 79 additions & 12 deletions projects/createExpressProject.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,95 @@
import chalk from "chalk";
import { execSync } from "child_process";
import path from "path";
import fs from "fs/promises";

const createExpressProject = async (projName) => {
const pathToProject = path.resolve(process.cwd(), projName);

try {
console.log(chalk.yellow("Creating Express app project...🚀🚀🚀"));
execSync(`mkdir ${projName} && cd ${projName} && npm init -y`);
console.log(chalk.yellow("Creating Express app project...🚀"));

// Create project directory
await fs.mkdir(pathToProject, { recursive: true });

// Initialize npm project
console.log(chalk.blue("Initializing npm project...📦"));
execSync(`npm init -y`, { cwd: pathToProject, stdio: 'inherit' });

console.log(chalk.red("Installing dependencies...🛠️🛠️🛠️"));
execSync(
`cd ${pathToProject} && npm install express morgan dotenv cors jsonwebtoken bcryptjs mongoose`
);
// Install dependencies
console.log(chalk.red("Installing dependencies...🛠️"));
const dependencies = [
"express",
"morgan",
"dotenv",
"cors",
"jsonwebtoken",
"bcryptjs",
"mongoose"
];
execSync(`npm install ${dependencies.join(" ")}`, { cwd: pathToProject, stdio: 'inherit' });

console.log(chalk.blue("Installing dev dependencies...✨✨✨"));
execSync(`cd ${pathToProject} && npm i -D nodemon `);
// Install dev dependencies
console.log(chalk.blue("Installing dev dependencies...✨"));
execSync(`npm install -D nodemon`, { cwd: pathToProject, stdio: 'inherit' });

console.log(chalk.bgGreenBright("Project created successfully 🎉🎉🎉"));
console.log(chalk.bgRedBright("Opening project in VSCode...🚀🚀🚀"));
execSync(`cd ${pathToProject} && code .`);
// Create basic project structure
await createProjectStructure(pathToProject);

console.log(chalk.green("Project created successfully 🎉"));
console.log(chalk.magenta("Opening project in VSCode..."));
execSync(`code ${pathToProject}`);

return true;
} catch (error) {
console.log(chalk.red("Error creating Express project 😢😢😢"));
console.error(chalk.red("Error creating Express project:"), error.message);
throw error;
}
};

async function createProjectStructure(projectPath) {
const dirs = ['src', 'src/routes', 'src/controllers', 'src/models', 'src/middleware'];
for (const dir of dirs) {
await fs.mkdir(path.join(projectPath, dir), { recursive: true });
}

// Create a basic server.js file
const serverContent = `
import express from 'express';
import dotenv from 'dotenv';
import cors from 'cors';
import morgan from 'morgan';
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(morgan('dev'));
app.use(express.json());
app.get('/', (req, res) => {
res.json({ message: 'Welcome to your Express API!' });
});
app.listen(PORT, () => {
console.log(\`Server running on port \${PORT}\`);
});
`;

await fs.writeFile(path.join(projectPath, 'src', 'server.js'), serverContent);

// Update package.json with start and dev scripts
const packageJsonPath = path.join(projectPath, 'package.json');
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf-8'));
packageJson.scripts = {
...packageJson.scripts,
start: "node src/server.js",
dev: "nodemon src/server.js"
};
packageJson.type = "module"; // Enable ES modules
await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2));
}

export default createExpressProject;
Loading

0 comments on commit 80407d8

Please sign in to comment.