|
1 | 1 | import fs from 'node:fs' |
2 | 2 | import { resolve } from 'node:path' |
3 | 3 | import { Command, InvalidArgumentError } from 'commander' |
4 | | -import { intro, log } from '@clack/prompts' |
| 4 | +import { cancel, confirm, intro, isCancel, log } from '@clack/prompts' |
5 | 5 | import chalk from 'chalk' |
6 | 6 | import semver from 'semver' |
7 | 7 |
|
@@ -70,6 +70,37 @@ export function cli({ |
70 | 70 |
|
71 | 71 | const program = new Command() |
72 | 72 |
|
| 73 | + async function confirmTargetDirectorySafety( |
| 74 | + targetDir: string, |
| 75 | + forced?: boolean, |
| 76 | + ) { |
| 77 | + if (forced) { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + if (!fs.existsSync(targetDir)) { |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + if (!fs.statSync(targetDir).isDirectory()) { |
| 86 | + throw new Error(`Target path exists and is not a directory: ${targetDir}`) |
| 87 | + } |
| 88 | + |
| 89 | + if (fs.readdirSync(targetDir).length === 0) { |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + const shouldContinue = await confirm({ |
| 94 | + message: `Target directory "${targetDir}" already exists and is not empty. Continue anyway?`, |
| 95 | + initialValue: false, |
| 96 | + }) |
| 97 | + |
| 98 | + if (isCancel(shouldContinue) || !shouldContinue) { |
| 99 | + cancel('Operation cancelled.') |
| 100 | + process.exit(0) |
| 101 | + } |
| 102 | + } |
| 103 | + |
73 | 104 | const availableFrameworks = getFrameworks().map((f) => f.name) |
74 | 105 |
|
75 | 106 | const toolchains = new Set<string>() |
@@ -251,6 +282,7 @@ export function cli({ |
251 | 282 | console.log(chalk.gray('├─') + ' ' + chalk.yellow('⟳') + ' installing packages...') |
252 | 283 | } |
253 | 284 | const silentEnvironment = createUIEnvironment(appName, true) |
| 285 | + await confirmTargetDirectorySafety(normalizedOpts.targetDir, options.force) |
254 | 286 | await createApp(silentEnvironment, normalizedOpts) |
255 | 287 | console.log(chalk.gray('└─') + ' ' + chalk.green('✓') + ` app created`) |
256 | 288 |
|
@@ -342,6 +374,7 @@ export function cli({ |
342 | 374 | finalOptions.targetDir = resolve(process.cwd(), finalOptions.projectName) |
343 | 375 | } |
344 | 376 |
|
| 377 | + await confirmTargetDirectorySafety(finalOptions.targetDir, options.force) |
345 | 378 | await createApp(environment, finalOptions) |
346 | 379 | } catch (error) { |
347 | 380 | log.error( |
|
0 commit comments