Skip to content

Commit

Permalink
make files cleaner and rm some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
blefnk committed Oct 28, 2024
1 parent a048af0 commit 319ae1e
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 213 deletions.
68 changes: 4 additions & 64 deletions src/prompts/01-justInstallRelivator.ts
Original file line number Diff line number Diff line change
@@ -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);
}
70 changes: 5 additions & 65 deletions src/prompts/02-buildOwnRelivator.ts
Original file line number Diff line number Diff line change
@@ -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);
}
69 changes: 7 additions & 62 deletions src/prompts/03-installAnyGitRepo.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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 ===
Expand All @@ -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:",
{
Expand All @@ -76,7 +65,6 @@ export async function installAnyGitRepo() {
},
);
validate(customLink, "string", "Custom template providing canceled.");

repoToInstall = customLink;
} else if (
projectCategory ===
Expand All @@ -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:",
{
Expand All @@ -103,7 +89,6 @@ export async function installAnyGitRepo() {
type: "text",
},
);

validate(customLink, "string", "Custom template providing canceled.");
repoToInstall = customLink;
} else {
Expand All @@ -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`);
}
13 changes: 8 additions & 5 deletions src/prompts/04-askProjectDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/prompts/08-askGitInitialization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { consola } from "consola";

// Prompt user about initializing Git or keeping the .git folder
export async function askGitInitialization(): Promise<string> {
const gitOption = await consola.prompt(
"Do you want to initialize a Git repository, keep the existing .git folder, or do nothing?",
Expand Down
6 changes: 5 additions & 1 deletion src/prompts/09-askInstallDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { consola } from "consola";
import { choosePackageManager } from "~/prompts/utils/choosePackageManager";

import { validate } from "~/prompts/utils/validate";

export async function askInstallDependencies(
mode: "buildOwnRelivator" | "installAnyGitRepo" | "justInstallRelivator",
): Promise<boolean> {
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`,
);
}

Expand Down
1 change: 0 additions & 1 deletion src/prompts/10-askSummaryConfirmation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/prompts/11-askInternationalizationSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
const useI18n = await confirm({
default: true,
Expand Down
1 change: 0 additions & 1 deletion src/prompts/12-askToResolveProjectConflicts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?`;
Expand Down
5 changes: 2 additions & 3 deletions src/prompts/13-askCheckAndDownloadFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand All @@ -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";
Expand Down
Loading

0 comments on commit 319ae1e

Please sign in to comment.