Skip to content

Commit 916b533

Browse files
committed
feat(cli): add 'no src directory' option
1 parent 9df3905 commit 916b533

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

cli/src/cli/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ interface CliFlags {
2020
noInstall: boolean;
2121
default: boolean;
2222
importAlias: string;
23+
srcDir: boolean;
2324

2425
/** @internal Used in CI. */
2526
CI: boolean;
@@ -52,6 +53,7 @@ const defaultOptions: CliResults = {
5253
flags: {
5354
noGit: false,
5455
noInstall: false,
56+
srcDir: false,
5557
default: false,
5658
CI: false,
5759
tailwind: false,
@@ -91,6 +93,11 @@ export const runCli = async (): Promise<CliResults> => {
9193
"Bypass the CLI and use all default options to bootstrap a new t3-app",
9294
false
9395
)
96+
.option(
97+
"--srcDir",
98+
"Explicitly tell the CLI to create a 'src' directory in the project",
99+
false
100+
)
94101
/** START CI-FLAGS */
95102
/**
96103
* @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<CliResults> => {
253260
message: "Will you be using Tailwind CSS for styling?",
254261
});
255262
},
263+
srcDir: () => {
264+
return p.confirm({
265+
message: "Would you like to use 'src' directory?",
266+
});
267+
},
256268
trpc: () => {
257269
return p.confirm({
258270
message: "Would you like to use tRPC?",
@@ -350,6 +362,7 @@ export const runCli = async (): Promise<CliResults> => {
350362
flags: {
351363
...cliResults.flags,
352364
appRouter: project.appRouter ?? cliResults.flags.appRouter,
365+
srcDir: project.srcDir ?? cliResults.flags.srcDir,
353366
noGit: !project.git || cliResults.flags.noGit,
354367
noInstall: !project.install || cliResults.flags.noInstall,
355368
importAlias: project.importAlias ?? cliResults.flags.importAlias,

cli/src/helpers/moveProjectSrc.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import path from "path";
2+
import fs from "fs-extra";
3+
4+
import { type InstallerOptions } from "~/installers/index.js";
5+
6+
type MoveProjectSrcProps = Required<
7+
Pick<InstallerOptions, "packages" | "projectDir" | "appRouter">
8+
>;
9+
10+
export const moveProjectSrc = async ({
11+
projectDir,
12+
packages,
13+
}: MoveProjectSrcProps) => {
14+
const srcDir = path.join(projectDir, "src");
15+
16+
const usingTw = packages.tailwind.inUse;
17+
const usingDrizzle = packages.drizzle.inUse;
18+
19+
if (usingDrizzle) {
20+
const drizzleConfigFile = path.join(projectDir, "drizzle.config.ts");
21+
fs.writeFileSync(
22+
drizzleConfigFile,
23+
fs
24+
.readFileSync(drizzleConfigFile, "utf8")
25+
.replace("./src/server/db/schema.ts", "./server/db/schema.ts")
26+
);
27+
}
28+
29+
if (usingTw) {
30+
const tailwindConfigFile = path.join(projectDir, "tailwind.config.ts");
31+
fs.writeFileSync(
32+
tailwindConfigFile,
33+
fs
34+
.readFileSync(tailwindConfigFile, "utf8")
35+
.replace("./src/**/*.tsx", "./**/*.tsx")
36+
);
37+
}
38+
39+
const tsconfigFile = path.join(projectDir, "tsconfig.json");
40+
const nextconfigFile = path.join(projectDir, "next.config.js");
41+
fs.writeFileSync(
42+
tsconfigFile,
43+
fs.readFileSync(tsconfigFile, "utf8").replace("./src/*", "./*")
44+
);
45+
fs.writeFileSync(
46+
nextconfigFile,
47+
fs.readFileSync(nextconfigFile, "utf8").replace("./src/env.js", "./env.js")
48+
);
49+
50+
await fs.ensureDir(srcDir);
51+
52+
await fs.copy(srcDir, projectDir);
53+
54+
await fs.emptyDir(srcDir);
55+
await fs.rmdir(srcDir);
56+
};

cli/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { logger } from "~/utils/logger.js";
1515
import { parseNameAndPath } from "~/utils/parseNameAndPath.js";
1616
import { renderTitle } from "~/utils/renderTitle.js";
1717
import { installDependencies } from "./helpers/installDependencies.js";
18+
import { moveProjectSrc } from "./helpers/moveProjectSrc.js";
1819
import { getVersion } from "./utils/getT3Version.js";
1920
import {
2021
getNpmVersion,
@@ -36,7 +37,7 @@ const main = async () => {
3637
const {
3738
appName,
3839
packages,
39-
flags: { noGit, noInstall, importAlias, appRouter },
40+
flags: { noGit, noInstall, importAlias, appRouter, srcDir },
4041
databaseProvider,
4142
} = await runCli();
4243

@@ -74,6 +75,10 @@ const main = async () => {
7475
spaces: 2,
7576
});
7677

78+
if (!srcDir) {
79+
await moveProjectSrc({ projectDir, packages: usePackages, appRouter });
80+
}
81+
7782
// update import alias in any generated files if not using the default
7883
if (importAlias !== "~/") {
7984
setImportAlias(projectDir, importAlias);

0 commit comments

Comments
 (0)