Skip to content
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 packages/cta-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ Remove your node_modules directory and package lock file and re-install.`,
'--add-on-config <config>',
'JSON string with add-on configuration options',
)
.option(
'-f, --force',
'force project creation even if the target directory is not empty',
false,
)

program.action(async (projectName: string, options: CliOptions) => {
if (options.listAddOns) {
Expand Down
23 changes: 21 additions & 2 deletions packages/cta-cli/src/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { intro } from '@clack/prompts'
import fs from 'node:fs'
import { cancel, confirm, intro, isCancel } from '@clack/prompts'

import {
finalizeAddOns,
Expand All @@ -12,8 +13,8 @@ import {
getProjectName,
promptForAddOnOptions,
selectAddOns,
selectGit,
selectDeployment,
selectGit,
Comment on lines -15 to +17
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed ESLint's sort-imports warning introduced in c2ef696.

selectPackageManager,
selectRouterType,
selectTailwind,
Expand Down Expand Up @@ -42,6 +43,7 @@ export async function promptForCreateOptions(

options.framework = getFrameworkById(cliOptions.framework || 'react-cra')!

// Validate project name
if (cliOptions.projectName) {
const { valid, error } = validateProjectName(cliOptions.projectName)
if (!valid) {
Expand All @@ -53,6 +55,23 @@ export async function promptForCreateOptions(
options.projectName = await getProjectName()
}

// Check if target directory is empty
if (
!cliOptions.force &&
fs.existsSync(options.projectName) &&
fs.readdirSync(options.projectName).length > 0
) {
const shouldContinue = await confirm({
message: `Target directory ${options.projectName} is not empty. Do you want to continue?`,
initialValue: true,
})

if (isCancel(shouldContinue) || !shouldContinue) {
cancel('Operation cancelled.')
process.exit(0)
}
}

// Router type selection
if (forcedMode) {
options.mode = forcedMode
Expand Down
1 change: 1 addition & 0 deletions packages/cta-cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export interface CliOptions {
interactive?: boolean
ui?: boolean
addOnConfig?: string
force?: boolean
}
1 change: 0 additions & 1 deletion packages/cta-cli/src/ui-prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { validateProjectName } from './utils.js'
import type { AddOn, PackageManager } from '@tanstack/cta-engine'

import type { Framework } from '@tanstack/cta-engine/dist/types/types.js'
import { InitialData } from '../../cta-ui/src/types'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s not part of the main change, but I removed an unused import.


export async function getProjectName(): Promise<string> {
const value = await text({
Expand Down