]> git.ipfire.org Git - thirdparty/vuejs/create-vue.git/commitdiff
feat: don't empty directories with only `.git` folder
authorHaoqun Jiang <haoqunjiang@gmail.com>
Tue, 12 Jul 2022 09:26:04 +0000 (17:26 +0800)
committerHaoqun Jiang <haoqunjiang@gmail.com>
Tue, 12 Jul 2022 09:30:01 +0000 (17:30 +0800)
index.ts

index a88828562b472d445b5fdcb91ba69d9e294a4c3d..5c1ffc4012030aad6804eeffe46530aca2fce9dd 100755 (executable)
--- 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}"`