Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: override defaults #1503

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brave-dragons-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-t3-app": minor
---

Add the option to override default files
40 changes: 40 additions & 0 deletions cli/src/helpers/overrideDefaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import chalk from "chalk";
import fs from "fs-extra";
import { type 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")
);
};
3 changes: 3 additions & 0 deletions cli/src/helpers/scaffoldProject.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { overrideDefaults } from "./overrideDefaults.js";
import chalk from "chalk";
import fs from "fs-extra";
import inquirer from "inquirer";
Expand Down Expand Up @@ -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`
);
Expand Down