UNPKG

create-react-clean

Version:

A simply CLI who create a new project for React in clean architecture

95 lines (94 loc) โ€ข 3.55 kB
#!/usr/bin/env node import prompts from "prompts"; import { execa } from "execa"; import chalk from "chalk"; import path from "path"; import fs from "fs"; console.log(chalk.blueBright("\n๐Ÿงผ Welcome in ") + chalk.bold("create-react-clean") + chalk.blueBright(" !\n")); const askUserProjectName = async () => { const { projectName } = await prompts({ type: "text", name: "projectName", message: "What's the name of your project ?", validate: (name) => name ? true : "You must specify a name to create a project.", }); return projectName; }; const formateTemplate = (targetDir) => { const gitPath = path.join(targetDir, ".git"); try { if (fs.existsSync(gitPath)) fs.rmSync(gitPath, { recursive: true, force: true }); } catch { console.log(chalk.red("โŒ An error was append when formate the template.")); } return; }; const cloneTemplateAndCleanIt = async (projectName, targetDir) => { //Check if git was install if it's not we stop process try { await execa("git", ["--version"]); } catch { console.log(chalk.red("โŒ Git is not installed, install it before use command.")); process.exit(1); } // Try to clone template from gitlab if it doesnt work we stopped proccess try { await execa("git", [ "clone", "https://gitlab.com/maxime-gardier/react-clean-template.git", projectName, ], { //Allow to see actions and messages in user terminal stdio: "inherit", }); console.log(chalk.green("โœ… Template was cloned with success !")); } catch (err) { console.error(chalk.red("โŒ An error append when template was cloned ."), err); process.exit(1); } formateTemplate(targetDir); return; }; const manageDependencies = async (targetDir) => { console.log(chalk.gray("๐Ÿ“ฆ Installation of dependencies...")); //Try to install dependecies with pnpm , if it's not installed with npm and if npm doesnt work we let user make it manualy try { await execa("pnpm", ["install"], { cwd: targetDir, stdio: "inherit" }); console.log(chalk.green("โœ… Dependencies was install !")); } catch (err) { console.log(chalk.yellow("โš ๏ธ Pnpm wasn't install, we try with npm...")); try { await execa("npm", ["install"], { cwd: targetDir, stdio: "inherit" }); console.log(chalk.green("โœ… Dependencies was install !")); } catch (err2) { console.error(chalk.red("โŒ Installation of dependecies failed but you might be try it manualy ."), err2); } } }; const main = async () => { //Ask user and get his project name const projectName = await askUserProjectName(); //Build the path for create the project const targetDir = path.resolve(process.cwd(), projectName); // If dir already exist we stop the process , if it's not we continue if (fs.existsSync(targetDir)) { console.log(chalk.red(`โŒ Dir ${projectName} already exist, remove it or change the project name.`)); process.exit(1); } await cloneTemplateAndCleanIt(projectName, targetDir); await manageDependencies(targetDir); console.log(chalk.cyan(`\n๐ŸŽ‰ Project ${chalk.bold(projectName)} was successfuly created !`)); console.log(chalk.gray(`\n๐Ÿ“œ Next steps : 1. cd ${projectName} 2. pnpm dev (ou npm run dev) 3. Coding proprerly.`)); }; main();