Skip to content
New issue

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

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

Already on GitHub? # to your account

feat(shadcn): add support for src dir #4729

Merged
merged 2 commits into from
Sep 3, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"shadcn": patch
---

npx shadcn init
add --src-dir
5 changes: 0 additions & 5 deletions .changeset/forty-rabbits-wonder.md

This file was deleted.

9 changes: 9 additions & 0 deletions packages/shadcn/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -40,6 +41,11 @@ export const add = new Command()
.option("-a, --all", "add all available components", false)
.option("-p, --path <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({
Expand Down Expand Up @@ -100,6 +106,7 @@ export const add = new Command()
skipPreflight: false,
silent: true,
isNewProject: false,
srcDir: options.srcDir,
})
}

Expand All @@ -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()
Expand All @@ -123,6 +131,7 @@ export const add = new Command()
skipPreflight: true,
silent: true,
isNewProject: true,
srcDir: options.srcDir,
})

shouldUpdateAppIndex =
Expand Down
6 changes: 6 additions & 0 deletions packages/shadcn/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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({
Expand Down
13 changes: 9 additions & 4 deletions packages/shadcn/src/utils/create-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ import prompts from "prompts"
import { z } from "zod"

export async function createProject(
options: Pick<z.infer<typeof initOptionsSchema>, "cwd" | "force">
options: Pick<z.infer<typeof initOptionsSchema>, "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,
Expand Down Expand Up @@ -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}`,
]
Expand All @@ -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)
}
Expand Down
Loading