From 701e1160ea86b2ddd9f43121b447477caaf7aefa Mon Sep 17 00:00:00 2001 From: shadcn Date: Tue, 3 Sep 2024 21:47:15 +0400 Subject: [PATCH] feat(shadcn): add support for src dir (#4729) * feat(shadcn): add support for src dir * chore: add changesets --- .../{wet-forks-teach.md => flat-geese-tease.md} | 2 +- .changeset/forty-rabbits-wonder.md | 5 ----- packages/shadcn/src/commands/add.ts | 9 +++++++++ packages/shadcn/src/commands/init.ts | 6 ++++++ packages/shadcn/src/utils/create-project.ts | 13 +++++++++---- 5 files changed, 25 insertions(+), 10 deletions(-) rename .changeset/{wet-forks-teach.md => flat-geese-tease.md} (60%) delete mode 100644 .changeset/forty-rabbits-wonder.md diff --git a/.changeset/wet-forks-teach.md b/.changeset/flat-geese-tease.md similarity index 60% rename from .changeset/wet-forks-teach.md rename to .changeset/flat-geese-tease.md index 1597c71533..823749a3b2 100644 --- a/.changeset/wet-forks-teach.md +++ b/.changeset/flat-geese-tease.md @@ -2,4 +2,4 @@ "shadcn": patch --- -npx shadcn init +add --src-dir diff --git a/.changeset/forty-rabbits-wonder.md b/.changeset/forty-rabbits-wonder.md deleted file mode 100644 index 9db8773b39..0000000000 --- a/.changeset/forty-rabbits-wonder.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"shadcn": patch ---- - -fix theme values bug diff --git a/packages/shadcn/src/commands/add.ts b/packages/shadcn/src/commands/add.ts index 706e3da78b..04f96211d0 100644 --- a/packages/shadcn/src/commands/add.ts +++ b/packages/shadcn/src/commands/add.ts @@ -21,6 +21,7 @@ export const addOptionsSchema = z.object({ all: z.boolean(), path: z.string().optional(), silent: z.boolean(), + srcDir: z.boolean().optional(), }) export const add = new Command() @@ -40,6 +41,11 @@ export const add = new Command() .option("-a, --all", "add all available components", false) .option("-p, --path ", "the path to add the component to.") .option("-s, --silent", "mute output.", false) + .option( + "--src-dir", + "use the src directory when creating a new project.", + false + ) .action(async (components, opts) => { try { const options = addOptionsSchema.parse({ @@ -100,6 +106,7 @@ export const add = new Command() skipPreflight: false, silent: true, isNewProject: false, + srcDir: options.srcDir, }) } @@ -108,6 +115,7 @@ export const add = new Command() const { projectPath } = await createProject({ cwd: options.cwd, force: options.overwrite, + srcDir: options.srcDir, }) if (!projectPath) { logger.break() @@ -123,6 +131,7 @@ export const add = new Command() skipPreflight: true, silent: true, isNewProject: true, + srcDir: options.srcDir, }) shouldUpdateAppIndex = diff --git a/packages/shadcn/src/commands/init.ts b/packages/shadcn/src/commands/init.ts index bea0040756..a81a9831ad 100644 --- a/packages/shadcn/src/commands/init.ts +++ b/packages/shadcn/src/commands/init.ts @@ -32,6 +32,7 @@ export const initOptionsSchema = z.object({ force: z.boolean(), silent: z.boolean(), isNewProject: z.boolean(), + srcDir: z.boolean().optional(), }) export const init = new Command() @@ -50,6 +51,11 @@ export const init = new Command() process.cwd() ) .option("-s, --silent", "mute output.", false) + .option( + "--src-dir", + "use the src directory when creating a new project.", + false + ) .action(async (components, opts) => { try { const options = initOptionsSchema.parse({ diff --git a/packages/shadcn/src/utils/create-project.ts b/packages/shadcn/src/utils/create-project.ts index 92706e7845..1bbbdb6afa 100644 --- a/packages/shadcn/src/utils/create-project.ts +++ b/packages/shadcn/src/utils/create-project.ts @@ -10,15 +10,20 @@ import prompts from "prompts" import { z } from "zod" export async function createProject( - options: Pick, "cwd" | "force"> + options: Pick, "cwd" | "force" | "srcDir"> ) { + options = { + srcDir: false, + ...options, + } + if (!options.force) { const { proceed } = await prompts({ type: "confirm", name: "proceed", message: `The path ${highlighter.info( options.cwd - )} is empty. Would you like to start a new ${highlighter.info( + )} is does not contain a package.json file. Would you like to start a new ${highlighter.info( "Next.js" )} project?`, initial: true, @@ -81,7 +86,7 @@ export async function createProject( "--eslint", "--typescript", "--app", - "--no-src-dir", + options.srcDir ? "--src-dir" : "--no-src-dir", "--no-import-alias", `--use-${packageManager}`, ] @@ -97,7 +102,7 @@ export async function createProject( } catch (error) { logger.break() logger.error( - `Something went wront creating a new Next.js project. Please try again.` + `Something went wrong creating a new Next.js project. Please try again.` ) process.exit(1) }