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(cli): add 'no src directory' option #1941

Open
wants to merge 2 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/new-garlics-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-t3-app": minor
---

add 'no src directory' option during project creation on the cli
13 changes: 13 additions & 0 deletions cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface CliFlags {
noInstall: boolean;
default: boolean;
importAlias: string;
srcDir: boolean;

/** @internal Used in CI. */
CI: boolean;
Expand Down Expand Up @@ -52,6 +53,7 @@ const defaultOptions: CliResults = {
flags: {
noGit: false,
noInstall: false,
srcDir: false,
default: false,
CI: false,
tailwind: false,
Expand Down Expand Up @@ -91,6 +93,11 @@ export const runCli = async (): Promise<CliResults> => {
"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
Expand Down Expand Up @@ -253,6 +260,11 @@ export const runCli = async (): Promise<CliResults> => {
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?",
Expand Down Expand Up @@ -350,6 +362,7 @@ export const runCli = async (): Promise<CliResults> => {
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,
Expand Down
56 changes: 56 additions & 0 deletions cli/src/helpers/moveProjectSrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import path from "path";
import fs from "fs-extra";

import { type InstallerOptions } from "~/installers/index.js";

type MoveProjectSrcProps = Required<
Pick<InstallerOptions, "packages" | "projectDir" | "appRouter">
>;

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);
};
7 changes: 6 additions & 1 deletion cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -36,7 +37,7 @@ const main = async () => {
const {
appName,
packages,
flags: { noGit, noInstall, importAlias, appRouter },
flags: { noGit, noInstall, importAlias, appRouter, srcDir },
databaseProvider,
} = await runCli();

Expand Down Expand Up @@ -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);
Expand Down
Loading