From 9372566bcfe6511eacd59f85f116fc479dcc06c6 Mon Sep 17 00:00:00 2001 From: Mvmo Date: Fri, 7 Jul 2023 07:03:52 +0200 Subject: [PATCH 1/3] feat: override dir --- cli/src/helpers/overrideDefaults.ts | 40 +++++++++++++++++++++++++++++ cli/src/helpers/scaffoldProject.ts | 3 +++ 2 files changed, 43 insertions(+) create mode 100644 cli/src/helpers/overrideDefaults.ts diff --git a/cli/src/helpers/overrideDefaults.ts b/cli/src/helpers/overrideDefaults.ts new file mode 100644 index 0000000000..9c3b0169fc --- /dev/null +++ b/cli/src/helpers/overrideDefaults.ts @@ -0,0 +1,40 @@ +import chalk from "chalk"; +import fs from "fs-extra"; +import { Ora } from "ora"; +import path from "path"; +import { env } from "process"; + +// this function first checks if one of the following env variables is set +// T3_CONFIG, XDG_CONFIG_HOME, HOME +// if so given path is joined with t3stack/ (unless it's the T3_CONFIG var, then it uses the directory directly) and override/ +// then the first path, which exists, is returned +const getOverridePath = (): string | undefined => { + return ["T3_CONFIG", "XDG_CONFIG_HOME", "HOME"] + .map((key) => [key, env[key]]) + .filter(([_, envValue]) => envValue !== undefined) + .map(([envKey, envValue]) => + path.join( + envValue as string, + envKey === "T3_CONFIG" ? "." : "t3stack", + "override" + ) + ) + .find(fs.existsSync); +}; + +export const overrideDefaults = (spinner: Ora, projectPath: string) => { + const overridePath = getOverridePath(); + if (!overridePath) return; + + spinner.info( + chalk.green( + `Found override directory - ${chalk.bgGreen(chalk.white(overridePath))}` + ) + ); + + fs.copySync(overridePath, projectPath, { overwrite: true }); + + spinner.info( + chalk.green("Successfully override files in just created project") + ); +}; diff --git a/cli/src/helpers/scaffoldProject.ts b/cli/src/helpers/scaffoldProject.ts index 466f2c457b..f6f6cf8d26 100644 --- a/cli/src/helpers/scaffoldProject.ts +++ b/cli/src/helpers/scaffoldProject.ts @@ -1,3 +1,4 @@ +import { overrideDefaults } from "./overrideDefaults.js"; import chalk from "chalk"; import fs from "fs-extra"; import inquirer from "inquirer"; @@ -103,6 +104,8 @@ export const scaffoldProject = async ({ const scaffoldedName = projectName === "." ? "App" : chalk.cyan.bold(projectName); + overrideDefaults(spinner, projectDir); + spinner.succeed( `${scaffoldedName} ${chalk.green("scaffolded successfully!")}\n` ); From 3f5a3605563ef675dc340cad75d8a0bc09164347 Mon Sep 17 00:00:00 2001 From: Mvmo Date: Fri, 7 Jul 2023 07:09:17 +0200 Subject: [PATCH 2/3] chore: add changeset --- .changeset/brave-dragons-sniff.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/brave-dragons-sniff.md diff --git a/.changeset/brave-dragons-sniff.md b/.changeset/brave-dragons-sniff.md new file mode 100644 index 0000000000..20d19ad445 --- /dev/null +++ b/.changeset/brave-dragons-sniff.md @@ -0,0 +1,5 @@ +--- +"create-t3-app": minor +--- + +Add the option to override default files From 5f9caa8cc6155c71bd09dbeb3442e22f39d3430f Mon Sep 17 00:00:00 2001 From: Mvmo Date: Fri, 7 Jul 2023 07:11:07 +0200 Subject: [PATCH 3/3] fix: code style - import type --- cli/src/helpers/overrideDefaults.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/helpers/overrideDefaults.ts b/cli/src/helpers/overrideDefaults.ts index 9c3b0169fc..07b971b768 100644 --- a/cli/src/helpers/overrideDefaults.ts +++ b/cli/src/helpers/overrideDefaults.ts @@ -1,6 +1,6 @@ import chalk from "chalk"; import fs from "fs-extra"; -import { Ora } from "ora"; +import { type Ora } from "ora"; import path from "path"; import { env } from "process";