From 9df39056e2c9ed3ef8f3f3d54aafe094093a8931 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 8 Jul 2024 19:45:58 -0400 Subject: [PATCH 1/2] chore: add changeset --- .changeset/new-garlics-hammer.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/new-garlics-hammer.md diff --git a/.changeset/new-garlics-hammer.md b/.changeset/new-garlics-hammer.md new file mode 100644 index 0000000000..60e64a43d7 --- /dev/null +++ b/.changeset/new-garlics-hammer.md @@ -0,0 +1,5 @@ +--- +"create-t3-app": minor +--- + +add 'no src directory' option during project creation on the cli From 916b533a1c87aeb417c8019fc014e98bb9c1e1be Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 8 Jul 2024 19:47:17 -0400 Subject: [PATCH 2/2] feat(cli): add 'no src directory' option --- cli/src/cli/index.ts | 13 +++++++ cli/src/helpers/moveProjectSrc.ts | 56 +++++++++++++++++++++++++++++++ cli/src/index.ts | 7 +++- 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 cli/src/helpers/moveProjectSrc.ts diff --git a/cli/src/cli/index.ts b/cli/src/cli/index.ts index ce5ef6ca50..b43694d46e 100644 --- a/cli/src/cli/index.ts +++ b/cli/src/cli/index.ts @@ -20,6 +20,7 @@ interface CliFlags { noInstall: boolean; default: boolean; importAlias: string; + srcDir: boolean; /** @internal Used in CI. */ CI: boolean; @@ -52,6 +53,7 @@ const defaultOptions: CliResults = { flags: { noGit: false, noInstall: false, + srcDir: false, default: false, CI: false, tailwind: false, @@ -91,6 +93,11 @@ export const runCli = async (): Promise => { "Bypass the CLI and use all default options to bootstrap a new t3-app", false ) + .option( + "--srcDir", + "Explicitly tell the CLI to create a 'src' directory in the project", + false + ) /** START CI-FLAGS */ /** * @experimental Used for CI E2E tests. If any of the following option-flags are provided, we @@ -253,6 +260,11 @@ export const runCli = async (): Promise => { message: "Will you be using Tailwind CSS for styling?", }); }, + srcDir: () => { + return p.confirm({ + message: "Would you like to use 'src' directory?", + }); + }, trpc: () => { return p.confirm({ message: "Would you like to use tRPC?", @@ -350,6 +362,7 @@ export const runCli = async (): Promise => { flags: { ...cliResults.flags, appRouter: project.appRouter ?? cliResults.flags.appRouter, + srcDir: project.srcDir ?? cliResults.flags.srcDir, noGit: !project.git || cliResults.flags.noGit, noInstall: !project.install || cliResults.flags.noInstall, importAlias: project.importAlias ?? cliResults.flags.importAlias, diff --git a/cli/src/helpers/moveProjectSrc.ts b/cli/src/helpers/moveProjectSrc.ts new file mode 100644 index 0000000000..ddc41f24d7 --- /dev/null +++ b/cli/src/helpers/moveProjectSrc.ts @@ -0,0 +1,56 @@ +import path from "path"; +import fs from "fs-extra"; + +import { type InstallerOptions } from "~/installers/index.js"; + +type MoveProjectSrcProps = Required< + Pick +>; + +export const moveProjectSrc = async ({ + projectDir, + packages, +}: MoveProjectSrcProps) => { + const srcDir = path.join(projectDir, "src"); + + const usingTw = packages.tailwind.inUse; + const usingDrizzle = packages.drizzle.inUse; + + if (usingDrizzle) { + const drizzleConfigFile = path.join(projectDir, "drizzle.config.ts"); + fs.writeFileSync( + drizzleConfigFile, + fs + .readFileSync(drizzleConfigFile, "utf8") + .replace("./src/server/db/schema.ts", "./server/db/schema.ts") + ); + } + + if (usingTw) { + const tailwindConfigFile = path.join(projectDir, "tailwind.config.ts"); + fs.writeFileSync( + tailwindConfigFile, + fs + .readFileSync(tailwindConfigFile, "utf8") + .replace("./src/**/*.tsx", "./**/*.tsx") + ); + } + + const tsconfigFile = path.join(projectDir, "tsconfig.json"); + const nextconfigFile = path.join(projectDir, "next.config.js"); + fs.writeFileSync( + tsconfigFile, + fs.readFileSync(tsconfigFile, "utf8").replace("./src/*", "./*") + ); + fs.writeFileSync( + nextconfigFile, + fs.readFileSync(nextconfigFile, "utf8").replace("./src/env.js", "./env.js") + ); + + await fs.ensureDir(srcDir); + + await fs.copy(srcDir, projectDir); + + await fs.emptyDir(srcDir); + await fs.rmdir(srcDir); +}; diff --git a/cli/src/index.ts b/cli/src/index.ts index 228192ef3a..96cf688986 100644 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -15,6 +15,7 @@ import { logger } from "~/utils/logger.js"; import { parseNameAndPath } from "~/utils/parseNameAndPath.js"; import { renderTitle } from "~/utils/renderTitle.js"; import { installDependencies } from "./helpers/installDependencies.js"; +import { moveProjectSrc } from "./helpers/moveProjectSrc.js"; import { getVersion } from "./utils/getT3Version.js"; import { getNpmVersion, @@ -36,7 +37,7 @@ const main = async () => { const { appName, packages, - flags: { noGit, noInstall, importAlias, appRouter }, + flags: { noGit, noInstall, importAlias, appRouter, srcDir }, databaseProvider, } = await runCli(); @@ -74,6 +75,10 @@ const main = async () => { spaces: 2, }); + if (!srcDir) { + await moveProjectSrc({ projectDir, packages: usePackages, appRouter }); + } + // update import alias in any generated files if not using the default if (importAlias !== "~/") { setImportAlias(projectDir, importAlias);