From: Haoqun Jiang Date: Tue, 12 Jul 2022 09:26:04 +0000 (+0800) Subject: feat: don't empty directories with only `.git` folder X-Git-Tag: v3.2.3~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b62cf5696f58b5873c40bbb977b649a0cd716234;p=thirdparty%2Fvuejs%2Fcreate-vue.git feat: don't empty directories with only `.git` folder --- diff --git a/index.ts b/index.ts index a8882856..5c1ffc40 100755 --- a/index.ts +++ b/index.ts @@ -27,8 +27,20 @@ function toValidPackageName(projectName) { .replace(/[^a-z0-9-~]+/g, '-') } -function canSafelyOverwrite(dir) { - return !fs.existsSync(dir) || fs.readdirSync(dir).length === 0 +function canSkipEmptying(dir: string) { + if (!fs.existsSync(dir)) { + return true + } + + const files = fs.readdirSync(dir) + if (files.length === 0) { + return true + } + if (files.length === 1 && files[0] === '.git') { + return true + } + + return false } function emptyDir(dir) { @@ -125,7 +137,7 @@ async function init() { }, { name: 'shouldOverwrite', - type: () => (canSafelyOverwrite(targetDir) || forceOverwrite ? null : 'confirm'), + type: () => (canSkipEmptying(targetDir) || forceOverwrite ? null : 'confirm'), message: () => { const dirForPrompt = targetDir === '.' ? 'Current directory' : `Target directory "${targetDir}"`