From bad84c174e72946a79c1b17dcbafd4e35d3f8c44 Mon Sep 17 00:00:00 2001 From: Tajudeen Oyindamola Tajudeen <46614028+Pterjudin@users.noreply.github.com> Date: Thu, 19 Dec 2024 23:56:52 +0000 Subject: [PATCH] Update setup-project.js The refactor improves performance and maintainability by introducing centralized functions for repeated logic, such as execInProjectDir, and replacing synchronous file operations with asynchronous ones to prevent blocking. It enhances reliability with file existence checks (fs.pathExists) and robust error handling via try-catch. Cross-platform compatibility is ensured through consistent use of path.join, while clear and user-friendly logging with consola provides better execution feedback. Modularized, readable functions and standardized JSON formatting make the code easier to maintain and debug, resulting in a cleaner, more efficient, and user-friendly script. --- cli/setup-project.js | 81 +++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/cli/setup-project.js b/cli/setup-project.js index fbb2edba..b762c34a 100755 --- a/cli/setup-project.js +++ b/cli/setup-project.js @@ -3,19 +3,28 @@ const { consola } = require('consola'); const fs = require('fs-extra'); const path = require('path'); +// Execute shell command within a project directory +const execInProjectDir = async (projectName, command) => { + const projectPath = path.join(process.cwd(), projectName); + await execShellCommand(`cd ${projectPath} && ${command}`); +}; + +// Initialize Git repository const initGit = async (projectName) => { - await execShellCommand(`cd ${projectName} && git init && cd ..`); + await execInProjectDir(projectName, 'git init'); }; +// Install dependencies using pnpm const installDeps = async (projectName) => { - await runCommand(`cd ${projectName} && pnpm install`, { - loading: 'Installing project dependencies', + const projectPath = path.join(process.cwd(), projectName); + await runCommand(`cd ${projectPath} && pnpm install`, { + loading: 'Installing project dependencies', success: 'Dependencies installed', - error: 'Failed to install dependencies, Make sure you have pnpm installed', + error: 'Failed to install dependencies. Make sure you have pnpm installed.', }); }; -// remove unnecessary files, such us .git, ios, android, docs, cli, LICENSE +// Remove unnecessary files from the project directory const removeFiles = async (projectName) => { const FILES_TO_REMOVE = [ '.git', @@ -27,59 +36,63 @@ const removeFiles = async (projectName) => { 'LICENSE', ]; - FILES_TO_REMOVE.forEach((file) => { - fs.removeSync(path.join(process.cwd(), `${projectName}/${file}`)); - }); + const projectPath = path.join(process.cwd(), projectName); + for (const file of FILES_TO_REMOVE) { + const filePath = path.join(projectPath, file); + if (await fs.pathExists(filePath)) { + await fs.remove(filePath); + } + } }; -// Update package.json infos, name and set version to 0.0.1 + add initial version to osMetadata +// Update package.json details const updatePackageInfos = async (projectName) => { - const packageJsonPath = path.join( - process.cwd(), - `${projectName}/package.json` - ); - const packageJson = fs.readJsonSync(packageJsonPath); + const packageJsonPath = path.join(process.cwd(), `${projectName}/package.json`); + const packageJson = await fs.readJson(packageJsonPath); + packageJson.osMetadata = { initVersion: packageJson.version }; packageJson.version = '0.0.1'; - packageJson.name = projectName?.toLowerCase(); + packageJson.name = projectName.toLowerCase(); packageJson.repository = { type: 'git', url: 'git+https://github.com/user/repo-name.git', }; - fs.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 }); + + await fs.writeJson(packageJsonPath, packageJson, { spaces: 2 }); }; +// Update configuration files with project-specific values const updateProjectConfig = async (projectName) => { const configPath = path.join(process.cwd(), `${projectName}/env.js`); - const contents = fs.readFileSync(configPath, { - encoding: 'utf-8', - }); + const contents = await fs.readFile(configPath, 'utf-8'); + const replaced = contents .replace(/ObytesApp/gi, projectName) .replace(/com.obytes/gi, `com.${projectName.toLowerCase()}`) .replace(/obytes/gi, 'expo-owner'); - fs.writeFileSync(configPath, replaced, { spaces: 2 }); - const readmeFilePath = path.join( - process.cwd(), - `${projectName}/README-project.md` - ); - fs.renameSync( - readmeFilePath, - path.join(process.cwd(), `${projectName}/README.md`) - ); + await fs.writeFile(configPath, replaced); + + const readmeFilePath = path.join(process.cwd(), `${projectName}/README-project.md`); + const newReadmePath = path.join(process.cwd(), `${projectName}/README.md`); + + if (await fs.pathExists(readmeFilePath)) { + await fs.rename(readmeFilePath, newReadmePath); + } }; +// Main function to set up the project const setupProject = async (projectName) => { - consola.start(`Clean up and setup your project 🧹`); + consola.start(`Cleaning up and setting up your project 🧹`); + try { - removeFiles(projectName); + await removeFiles(projectName); await initGit(projectName); - updatePackageInfos(projectName); - updateProjectConfig(projectName); - consola.success(`Clean up and setup your project 🧹`); + await updatePackageInfos(projectName); + await updateProjectConfig(projectName); + consola.success(`Project setup completed successfully 🧹`); } catch (error) { - consola.error(`Failed to clean up project folder`, error); + consola.error(`Failed to set up the project:`, error); process.exit(1); } };