Skip to content
Merged
Changes from 2 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
36 changes: 31 additions & 5 deletions packages/create-vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const argv = minimist<{
}>(process.argv.slice(2), { string: ['_'] })
const cwd = process.cwd()

type OverwiteOption = {
value: string
display: string
}
type ColorFunc = (str: string | number) => string
type Framework = {
name: string
Expand All @@ -39,6 +43,21 @@ type FrameworkVariant = {
customCommand?: string
}

const OVERWRITEOPTIONS: OverwiteOption[] = [
{
value: 'yes',
display: 'Remove existing files and continue',
},
{
value: 'no',
display: 'Cancel operation',
},
{
value: 'ignore',
display: 'Ignore files and continue',
},
]

const FRAMEWORKS: Framework[] = [
{
name: 'vanilla',
Expand Down Expand Up @@ -267,17 +286,24 @@ async function init() {
},
{
type: () =>
!fs.existsSync(targetDir) || isEmpty(targetDir) ? null : 'confirm',
!fs.existsSync(targetDir) || isEmpty(targetDir) ? null : 'select',
name: 'overwrite',
message: () =>
(targetDir === '.'
? 'Current directory'
: `Target directory "${targetDir}"`) +
` is not empty. Remove existing files and continue?`,
` is not empty. Please choose how to proceed:`,
initial: 0,
choices: OVERWRITEOPTIONS.map((overwriteOption) => {
return {
title: overwriteOption.display,
value: overwriteOption.value,
}
}),
Comment thread
bluwy marked this conversation as resolved.
Outdated
},
{
type: (_, { overwrite }: { overwrite?: boolean }) => {
if (overwrite === false) {
type: (_, { overwrite }: { overwrite?: string }) => {
if (overwrite === 'no') {
throw new Error(red('✖') + ' Operation cancelled')
}
return null
Expand Down Expand Up @@ -342,7 +368,7 @@ async function init() {

const root = path.join(cwd, targetDir)

if (overwrite) {
if (overwrite === 'yes') {
emptyDir(root)
} else if (!fs.existsSync(root)) {
fs.mkdirSync(root, { recursive: true })
Expand Down