Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(cli): allow initializing non-empty directory #15272

Merged
merged 3 commits into from
Dec 12, 2023
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,
}
}),
bluwy marked this conversation as resolved.
Show resolved Hide resolved
},
{
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