create-hokage-js-app
Version:
š„ Best CLI tool to create a MERN stack template. Quick, clean, and customizable.
115 lines (94 loc) ⢠5.68 kB
JavaScript
import path from 'path';
import { createSpinner } from 'nanospinner';
import { FolderManager } from "./utils/fs-funcs.js"
import { runCommand } from './utils/dependency.js';
import { info } from "../src/utils/chalk.js"
import chalk from 'chalk';
const fm = new FolderManager()
export class ProjectBuilder {
constructor(projectStyle, projectTemp, targetPath) {
this.targetPath = targetPath
this.projectStyle = projectStyle
this.projectTemp = projectTemp
this.templatePath = "";
this.clientPackages = "";
this.apiPackages = ""
}
async init() {
this.#setCssPathsAndPackages(this.projectTemp)
await this.#copyTemplate()
await this.installDependencies()
this.#tellAboutEnvs()
}
#setCssPathsAndPackages(projectType) {
switch (projectType) {
case '1':
this.templatePath = 'hassaammgl/create-hokage-js-app/templates/normalcss/js-template#main';
this.clientPackages = `npm i react react-dom zod axios react-router && npm i -D /js /react /react-dom /plugin-react-swc eslint eslint-plugin-react-hooks eslint-plugin-react-refresh globals vite`;
this.apiPackages = `npm install argon2 colors cookie-parser cors dotenv express express-async-handler joi jsonwebtoken mongoose morgan`;
break;
case '2':
this.templatePath = 'hassaammgl/create-hokage-js-app/templates/normalcss/ts-template#main';
this.clientPackages = `npm install react react-dom zod axios react-router && npm install --save-dev /js /react /react-dom /plugin-react eslint eslint-plugin-react-hooks eslint-plugin-react-refresh globals typescript typescript-eslint vite`;
this.apiPackages = `npm install argon2 colors cookie-parser cors dotenv express express-async-handler joi jsonwebtoken mongoose morgan && npm install --save-dev /argon2 /cookie-parser /cors /express /jsonwebtoken /morgan /node ts-node-dev tsx typescript`;
break;
case '3':
this.templatePath = 'hassaammgl/create-hokage-js-app/templates/normalcss/js-frontend-ts-backend#main';
this.clientPackages = `npm i react react-dom zod axios react-router && npm i -D /js /react /react-dom /plugin-react-swc eslint eslint-plugin-react-hooks eslint-plugin-react-refresh globals vite`;
this.apiPackages = `npm install argon2 colors cookie-parser cors dotenv express express-async-handler joi jsonwebtoken mongoose morgan && npm install --save-dev /argon2 /cookie-parser /cors /express /jsonwebtoken /morgan /node ts-node-dev tsx typescript`;
break;
case '4':
this.templatePath = 'hassaammgl/create-hokage-js-app/templates/normalcss/ts-frontend-js-backend#main';
this.clientPackages = `npm install react react-dom zod axios react-router && npm install --save-dev /js /react /react-dom /plugin-react eslint eslint-plugin-react-hooks eslint-plugin-react-refresh globals typescript typescript-eslint vite`;
this.apiPackages = `npm install argon2 colors cookie-parser cors dotenv express express-async-handler joi jsonwebtoken mongoose morgan`;
break;
default:
console.log('ā Invalid template project selected');
}
}
async #copyTemplate() {
const spinner = createSpinner(`Copying template...`).start();
try {
console.log(this.templatePath, this.targetPath);
await fm.copyFrom(this.templatePath, this.targetPath)
spinner.success({ text: `ā
Template copied to "${this.templatePath}"` });
} catch (error) {
spinner.error({ text: `ā Failed to copy template.` });
throw error;
}
}
async #tellAboutEnvs() {
const clientEnvPath = path.join(this.targetPath, "client", ".env");
const apiEnvPath = path.join(this.targetPath, "api", ".env");
console.log(chalk.green.bold("\nš¦ Project created successfully!"));
console.log(chalk.cyan("š ļø Before starting, please create environment files:"));
console.log(`\nā” ${chalk.yellow("Client env:")} ${clientEnvPath}`);
console.log(`ā” ${chalk.yellow("API env:")} ${apiEnvPath}`);
const apiExample = `PORT=5000
NODE_ENV=development
MONGO_URI=
JWT_SECRET=
JWT_REFRESH_SECRET=
FRONTEND_URL=http://localhost:5173
`;
const clientExample = `VITE_API_URL=http://localhost:5000
`;
console.log(chalk.magenta("\nExample API .env:"));
console.log(chalk.gray("-----------------"));
console.log(apiExample);
console.log(chalk.magenta("Example Client .env:"));
console.log(chalk.gray("--------------------"));
console.log(clientExample);
console.log(chalk.blue("\nš Once done, you're ready to run your dev servers. Happy coding!"));
}
async installDependencies() {
const clientPath = path.join(this.targetPath, 'client');
const apiPath = path.join(this.targetPath, 'api');
const spinner = createSpinner(`š¦ Installing Client packages in: ${clientPath}`).start()
await runCommand(clientPath, this.clientPackages);
spinner.stop()
const spinner1 = createSpinner(`š¦ Installing API packages in: ${apiPath}`).start()
await runCommand(apiPath, this.apiPackages);
spinner1.stop()
}
}