From 319ae1edfa59882fa9ba640dfeca351c376bcd5a Mon Sep 17 00:00:00 2001 From: Nazar Kornienko Date: Mon, 28 Oct 2024 03:47:19 +0100 Subject: [PATCH] make files cleaner and rm some comments --- src/prompts/01-justInstallRelivator.ts | 68 ++---------------- src/prompts/02-buildOwnRelivator.ts | 70 ++----------------- src/prompts/03-installAnyGitRepo.ts | 69 ++---------------- src/prompts/04-askProjectDetails.ts | 13 ++-- src/prompts/08-askGitInitialization.ts | 1 - src/prompts/09-askInstallDependencies.ts | 6 +- src/prompts/10-askSummaryConfirmation.ts | 1 - .../11-askInternationalizationSetup.ts | 1 - .../12-askToResolveProjectConflicts.ts | 1 - src/prompts/13-askCheckAndDownloadFiles.ts | 5 +- src/prompts/14-showCongratulationMenu.ts | 7 +- src/prompts/utils/choosePackageManager.ts | 5 +- src/settings.ts | 1 + 13 files changed, 35 insertions(+), 213 deletions(-) diff --git a/src/prompts/01-justInstallRelivator.ts b/src/prompts/01-justInstallRelivator.ts index 213777b..bd2f6e3 100644 --- a/src/prompts/01-justInstallRelivator.ts +++ b/src/prompts/01-justInstallRelivator.ts @@ -1,70 +1,10 @@ -import { consola } from "consola"; -import path from "pathe"; - -import { askAppName } from "~/prompts/05-askAppName"; -import { askUserName } from "~/prompts/06-askUserName"; -import { askAppDomain } from "~/prompts/07-askAppDomain"; -import { askGitInitialization } from "~/prompts/08-askGitInitialization"; -import { askInstallDependencies } from "~/prompts/09-askInstallDependencies"; -import { askSummaryConfirmation } from "~/prompts/10-askSummaryConfirmation"; -import { askInternationalizationSetup } from "~/prompts/11-askInternationalizationSetup"; -import { askCheckAndDownloadFiles } from "~/prompts/13-askCheckAndDownloadFiles"; -import { showCongratulationMenu } from "~/prompts/14-showCongratulationMenu"; -import { downloadI18nFiles } from "~/prompts/utils/downloadI18nFiles"; -import { getCurrentWorkingDirectory } from "~/prompts/utils/fs"; -import { handleStringReplacements } from "~/prompts/utils/handleStringReplacements"; -import { downloadGitRepo } from "~/prompts/utils/downloadGitRepo"; -import { moveAppToLocale } from "~/prompts/utils/moveAppToLocale"; -import { isDev, REPO_SHORT_URLS } from "~/settings"; +import { askProjectDetails } from "~/prompts/04-askProjectDetails"; +import { REPO_SHORT_URLS } from "~/settings"; export async function justInstallRelivator() { - consola.info( - "Let's create a brand-new web app using the Relivator Next.js template. After that, you can customize everything however you like.", - ); - const template = REPO_SHORT_URLS.relivatorGithubLink; - const appName = await askAppName(); - const username = await askUserName(); - const domain = await askAppDomain(); - const git = await askGitInitialization(); - const deps = await askInstallDependencies("justInstallRelivator"); - - const confirmed = await askSummaryConfirmation( - template, - appName, - username, - domain, - git, - deps, - ); - - if (!confirmed) { - consola.info("Project creation process was canceled."); - return; - } - - await downloadGitRepo(appName, template, deps, git); - - const cwd = getCurrentWorkingDirectory(); - const targetDir = isDev - ? path.join(cwd, "..", appName) - : path.join(cwd, appName); - - await handleStringReplacements( - targetDir, - template, - appName, - username, - domain, - ); - - const shouldAddI18n = await askInternationalizationSetup(); - if (shouldAddI18n) { - await moveAppToLocale(targetDir); - await downloadI18nFiles(targetDir, isDev); - } + const message = `Let's create your brand-new web app using the ${template} starter! After that, you can customize everything however you like.`; - await askCheckAndDownloadFiles(targetDir, appName); - await showCongratulationMenu(targetDir, deps, template, targetDir); + await askProjectDetails(template, message, "justInstallRelivator", false); } diff --git a/src/prompts/02-buildOwnRelivator.ts b/src/prompts/02-buildOwnRelivator.ts index 363714a..d34208a 100644 --- a/src/prompts/02-buildOwnRelivator.ts +++ b/src/prompts/02-buildOwnRelivator.ts @@ -1,70 +1,10 @@ -import { consola } from "consola"; -import path from "pathe"; - -import { askAppName } from "~/prompts/05-askAppName"; -import { askUserName } from "~/prompts/06-askUserName"; -import { askAppDomain } from "~/prompts/07-askAppDomain"; -import { askGitInitialization } from "~/prompts/08-askGitInitialization"; -import { askInstallDependencies } from "~/prompts/09-askInstallDependencies"; -import { askSummaryConfirmation } from "~/prompts/10-askSummaryConfirmation"; -import { askInternationalizationSetup } from "~/prompts/11-askInternationalizationSetup"; -import { askCheckAndDownloadFiles } from "~/prompts/13-askCheckAndDownloadFiles"; -import { showCongratulationMenu } from "~/prompts/14-showCongratulationMenu"; -import { downloadI18nFiles } from "~/prompts/utils/downloadI18nFiles"; -import { getCurrentWorkingDirectory } from "~/prompts/utils/fs"; -import { handleStringReplacements } from "~/prompts/utils/handleStringReplacements"; -import { downloadGitRepo } from "~/prompts/utils/downloadGitRepo"; -import { moveAppToLocale } from "~/prompts/utils/moveAppToLocale"; -import { isDev } from "~/settings"; +import { askProjectDetails } from "~/prompts/04-askProjectDetails"; +import { REPO_SHORT_URLS } from "~/settings"; export async function buildOwnRelivator() { - consola.info( - "Let's build your own Relivator from scratch! We'll use the blefnk/versator-nextjs-template as a starting point.", - ); - - const template = "blefnk/versator-nextjs-template"; - const appName = await askAppName(); - const githubUser = await askUserName(); - const website = await askAppDomain(); - const git = await askGitInitialization(); - const deps = await askInstallDependencies("buildOwnRelivator"); - - const confirmed = await askSummaryConfirmation( - template, - appName, - githubUser, - website, - git, - deps, - ); - - if (!confirmed) { - consola.info("Project creation process was canceled."); - return; - } - - await downloadGitRepo(appName, template, deps, git); - - const cwd = getCurrentWorkingDirectory(); - const targetDir = isDev - ? path.join(cwd, "..", appName) - : path.join(cwd, appName); - - await handleStringReplacements( - targetDir, - template, - appName, - githubUser, - website, - ); - - const enableI18n = await askInternationalizationSetup(); + const template = REPO_SHORT_URLS.versatorGithubLink; - if (enableI18n) { - await moveAppToLocale(targetDir); - await downloadI18nFiles(targetDir, isDev); - } + const message = `Let's build your own Relivator from scratch! We'll use the ${template} as a starting point.`; - await askCheckAndDownloadFiles(targetDir, appName); - await showCongratulationMenu(targetDir, deps, template, targetDir); + await askProjectDetails(template, message, "buildOwnRelivator", true); } diff --git a/src/prompts/03-installAnyGitRepo.ts b/src/prompts/03-installAnyGitRepo.ts index 7889e5a..92ec4cd 100644 --- a/src/prompts/03-installAnyGitRepo.ts +++ b/src/prompts/03-installAnyGitRepo.ts @@ -1,17 +1,8 @@ import { consola } from "consola"; -import path from "pathe"; -import { askAppName } from "~/prompts/05-askAppName"; -import { askGitInitialization } from "~/prompts/08-askGitInitialization"; -import { askInstallDependencies } from "~/prompts/09-askInstallDependencies"; -import { askSummaryConfirmation } from "~/prompts/10-askSummaryConfirmation"; -import { choosePackageManager } from "~/prompts/utils/choosePackageManager"; -import { getCurrentWorkingDirectory } from "~/prompts/utils/fs"; -import { downloadGitRepo } from "~/prompts/utils/downloadGitRepo"; +import { askProjectDetails } from "~/prompts/04-askProjectDetails"; import { validate } from "~/prompts/utils/validate"; -import { isDev, REPO_SHORT_URLS } from "~/settings"; +import { REPO_SHORT_URLS } from "~/settings"; import { justInstallRelivator } from "~/prompts/01-justInstallRelivator"; -import { askUserName } from "~/prompts/06-askUserName"; -import { askAppDomain } from "~/prompts/07-askAppDomain"; export async function installAnyGitRepo() { consola.info( @@ -46,13 +37,13 @@ export async function installAnyGitRepo() { "blefnk/create-t3-app", "blefnk/create-next-app", "blefnk/astro-starlight-template", + REPO_SHORT_URLS.versatorGithubLink, "reliverse/template-browser-extension", ], type: "select", }, ); validate(reliverseTemplate, "string", "Template selection canceled."); - repoToInstall = reliverseTemplate; } else if ( projectCategory === @@ -63,10 +54,8 @@ export async function installAnyGitRepo() { "shadcn-ui/taxonomy", "onwidget/astrowind", ]; - const randomDefaultLink = defaultLinks[Math.floor(Math.random() * defaultLinks.length)]; - const customLink = await consola.prompt( "Enter the GitHub repository link:", { @@ -76,7 +65,6 @@ export async function installAnyGitRepo() { }, ); validate(customLink, "string", "Custom template providing canceled."); - repoToInstall = customLink; } else if ( projectCategory === @@ -91,10 +79,8 @@ export async function installAnyGitRepo() { "blefnk/knip", "47ng/nuqs", ]; - const randomDefaultLink = defaultLinks[Math.floor(Math.random() * defaultLinks.length)]; - const customLink = await consola.prompt( "Enter the GitHub repository link:", { @@ -103,7 +89,6 @@ export async function installAnyGitRepo() { type: "text", }, ); - validate(customLink, "string", "Custom template providing canceled."); repoToInstall = customLink; } else { @@ -118,50 +103,10 @@ export async function installAnyGitRepo() { return justInstallRelivator(); } - const projectName = await askAppName(); - const username = await askUserName(); - const domain = await askAppDomain(); - const gitOption = await askGitInitialization(); - const installDeps = await askInstallDependencies("installAnyGitRepo"); - - const confirmed = await askSummaryConfirmation( + await askProjectDetails( repoToInstall, - projectName, - username, - domain, - gitOption, - installDeps, + `Setting up the repository: ${repoToInstall}...`, + "installAnyGitRepo", + false, ); - - if (!confirmed) { - consola.info("Project setup was canceled."); - return; - } - - const cwd = getCurrentWorkingDirectory(); - - const targetDir = isDev - ? path.join(cwd, "..", projectName) - : path.join(cwd, projectName); - - await downloadGitRepo(projectName, repoToInstall, installDeps, gitOption); - - if (installDeps) { - const pkgManager = await choosePackageManager(cwd); - consola.info(`Using ${pkgManager} to install dependencies...`); - - try { - consola.success("Dependencies installed successfully."); - } catch (error) { - consola.error("Failed to install dependencies:", error); - } - } else { - const pkgManager = await choosePackageManager(cwd); - consola.info( - `👉 To install manually, run: cd ${targetDir} && ${pkgManager} i`, - ); - } - - consola.success(`Repository from ${repoToInstall} installed successfully.`); - consola.info(`👉 If you have VSCode installed, run: code ${targetDir}\n`); } diff --git a/src/prompts/04-askProjectDetails.ts b/src/prompts/04-askProjectDetails.ts index d4bf712..4d404fa 100644 --- a/src/prompts/04-askProjectDetails.ts +++ b/src/prompts/04-askProjectDetails.ts @@ -19,7 +19,8 @@ import { isDev } from "~/settings"; export async function askProjectDetails( template: string, message: string, - mode: "justInstallRelivator" | "buildOwnRelivator", + mode: "buildOwnRelivator" | "installAnyGitRepo" | "justInstallRelivator", + allowI18nPrompt: boolean, ) { consola.info(message); @@ -58,10 +59,12 @@ export async function askProjectDetails( domain, ); - const enableI18n = await askInternationalizationSetup(); - if (enableI18n) { - await moveAppToLocale(targetDir); - await downloadI18nFiles(targetDir, isDev); + if (allowI18nPrompt) { + const i18nShouldBeEnabled = await askInternationalizationSetup(); + if (i18nShouldBeEnabled) { + await moveAppToLocale(targetDir); + await downloadI18nFiles(targetDir, isDev); + } } await askCheckAndDownloadFiles(targetDir, appName); diff --git a/src/prompts/08-askGitInitialization.ts b/src/prompts/08-askGitInitialization.ts index 5b00c25..338187f 100644 --- a/src/prompts/08-askGitInitialization.ts +++ b/src/prompts/08-askGitInitialization.ts @@ -1,6 +1,5 @@ import { consola } from "consola"; -// Prompt user about initializing Git or keeping the .git folder export async function askGitInitialization(): Promise { const gitOption = await consola.prompt( "Do you want to initialize a Git repository, keep the existing .git folder, or do nothing?", diff --git a/src/prompts/09-askInstallDependencies.ts b/src/prompts/09-askInstallDependencies.ts index 9130fef..cb9387b 100644 --- a/src/prompts/09-askInstallDependencies.ts +++ b/src/prompts/09-askInstallDependencies.ts @@ -1,4 +1,5 @@ import { consola } from "consola"; +import { choosePackageManager } from "~/prompts/utils/choosePackageManager"; import { validate } from "~/prompts/utils/validate"; @@ -6,8 +7,11 @@ export async function askInstallDependencies( mode: "buildOwnRelivator" | "installAnyGitRepo" | "justInstallRelivator", ): Promise { if (mode === "installAnyGitRepo") { + const cwd = process.cwd(); + const pkgManager = await choosePackageManager(cwd); + consola.info( - "In `installAnyGitRepo` mode, dependencies may not be installed automatically. Run `bun i` manually if needed.", + `In installAnyGitRepo mode, dependencies may not be installed automatically. If something, after project is created, you can run the following command to manually install deps: ${pkgManager} i`, ); } diff --git a/src/prompts/10-askSummaryConfirmation.ts b/src/prompts/10-askSummaryConfirmation.ts index 0b5898c..e4949be 100644 --- a/src/prompts/10-askSummaryConfirmation.ts +++ b/src/prompts/10-askSummaryConfirmation.ts @@ -2,7 +2,6 @@ import { consola } from "consola"; import { validate } from "~/prompts/utils/validate"; -// Prompt user for confirmation before proceeding with installation export async function askSummaryConfirmation( template: string, projectName: string, diff --git a/src/prompts/11-askInternationalizationSetup.ts b/src/prompts/11-askInternationalizationSetup.ts index 5207fd8..acc017b 100644 --- a/src/prompts/11-askInternationalizationSetup.ts +++ b/src/prompts/11-askInternationalizationSetup.ts @@ -2,7 +2,6 @@ import { confirm } from "@inquirer/prompts"; import { validate } from "~/prompts/utils/validate"; -// Prompt to ask the user if they want i18n support export async function askInternationalizationSetup(): Promise { const useI18n = await confirm({ default: true, diff --git a/src/prompts/12-askToResolveProjectConflicts.ts b/src/prompts/12-askToResolveProjectConflicts.ts index 1889f30..a7fae2b 100644 --- a/src/prompts/12-askToResolveProjectConflicts.ts +++ b/src/prompts/12-askToResolveProjectConflicts.ts @@ -56,7 +56,6 @@ const handleFileConflicts = async ({ continue; // Skip to the next file } - // Default or custom prompt message for manual handling const message = customMessage || `Do you want to remove or rename the ${fileDescription} file by adding .txt?`; diff --git a/src/prompts/13-askCheckAndDownloadFiles.ts b/src/prompts/13-askCheckAndDownloadFiles.ts index a660dce..dbcfcea 100644 --- a/src/prompts/13-askCheckAndDownloadFiles.ts +++ b/src/prompts/13-askCheckAndDownloadFiles.ts @@ -8,7 +8,6 @@ import { checkFileExists } from "~/prompts/utils/fileUtils"; import { getCurrentWorkingDirectory } from "~/prompts/utils/fs"; import { DEBUG, FILE_PATHS, fileCategories } from "~/settings"; -// Function to check for required files and allow the user to download them export const askCheckAndDownloadFiles = async ( targetDir: string, projectName: string, @@ -35,7 +34,7 @@ export const askCheckAndDownloadFiles = async ( } // Handle project files conflicts - await resolveProjectConflicts(targetDir); // 12-resolveProjectConflicts.ts + await resolveProjectConflicts(targetDir); // If there are missing files, prompt the user to download them if (missingFiles.length > 0) { @@ -58,7 +57,7 @@ export const askCheckAndDownloadFiles = async ( (category) => fileCategories[category as keyof typeof fileCategories] || [], ) - .filter(Boolean); // Ensure there are no undefined values + .filter(Boolean); const cwd = getCurrentWorkingDirectory(); const tempCloneRepo = "https://github.com/blefnk/relivator"; diff --git a/src/prompts/14-showCongratulationMenu.ts b/src/prompts/14-showCongratulationMenu.ts index e4bba4a..9d17d94 100644 --- a/src/prompts/14-showCongratulationMenu.ts +++ b/src/prompts/14-showCongratulationMenu.ts @@ -23,20 +23,19 @@ export async function showCongratulationMenu( consola.info(`- Open the project: cd ${targetDir}`); if (!deps) { - consola.info("- Install dependencies manually: npx nypm i"); + consola.info(`- Install dependencies manually: ${pkgManager} i`); } consola.info(`- Apply linting and formatting: ${pkgManager} appts`); consola.info(`- Run the project: ${pkgManager} dev`); consola.info(""); consola.success(`🎉 ${source} was successfully installed to ${dir}.`); - consola.info(`👉 If you have VSCode installed, run: code ${targetDir}`); + consola.info(`- If you have VSCode installed, run: code ${targetDir}`); console.info(""); const vscodeInstalled = isVSCodeInstalled(); - // Checkbox prompt for multiple actions const nextActions = await checkbox({ choices: [ { name: "Close Reliverse CLI", checked: true, value: "close" }, @@ -58,7 +57,6 @@ export async function showCongratulationMenu( message: "What would you like to do next?", }); - // Execute selected actions for (const action of nextActions) { if (action === "docs") { consola.info("Opening Reliverse Documentation..."); @@ -95,7 +93,6 @@ export async function showCongratulationMenu( } } - // CLI will always close after all actions are executed consola.success( "👋 Closing the CLI... Thanks for using Reliverse! See you next time!\n", ); diff --git a/src/prompts/utils/choosePackageManager.ts b/src/prompts/utils/choosePackageManager.ts index 2e61408..129573b 100644 --- a/src/prompts/utils/choosePackageManager.ts +++ b/src/prompts/utils/choosePackageManager.ts @@ -1,15 +1,12 @@ -// choosePackageManager.ts - import type { PackageManagerName } from "nypm"; import { consola } from "consola"; import { detectPackageManager } from "nypm"; -// Function to select the package manager export async function choosePackageManager( cwd: string, ): Promise { - const detectedPkgManager = (await detectPackageManager(cwd))?.name || "pnpm"; // Fallback to pnpm if detection fails + const detectedPkgManager = (await detectPackageManager(cwd))?.name || "pnpm"; let pkgManager: PackageManagerName = detectedPkgManager as PackageManagerName; diff --git a/src/settings.ts b/src/settings.ts index 86cb5b9..35c3be3 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -35,6 +35,7 @@ export const REPO_FULL_URLS = { export const REPO_SHORT_URLS = { relivatorGithubLink: "blefnk/relivator-nextjs-template", + versatorGithubLink: "blefnk/versator-nextjs-template", }; // Path settings for important files and directories