Skip to content

Commit 42e4732

Browse files
committed
feat: don't empty directories with only .git folder
1 parent e25b9dc commit 42e4732

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,20 @@ function toValidPackageName(projectName) {
2727
.replace(/[^a-z0-9-~]+/g, '-')
2828
}
2929

30-
function canSafelyOverwrite(dir) {
31-
return !fs.existsSync(dir) || fs.readdirSync(dir).length === 0
30+
function canSkipEmptying(dir: string) {
31+
if (!fs.existsSync(dir)) {
32+
return true
33+
}
34+
35+
const files = fs.readdirSync(dir)
36+
if (files.length === 0) {
37+
return true
38+
}
39+
if (files.length === 1 && files[0] === '.git') {
40+
return true
41+
}
42+
43+
return false
3244
}
3345

3446
function emptyDir(dir) {
@@ -125,7 +137,7 @@ async function init() {
125137
},
126138
{
127139
name: 'shouldOverwrite',
128-
type: () => (canSafelyOverwrite(targetDir) || forceOverwrite ? null : 'confirm'),
140+
type: () => (canSkipEmptying(targetDir) || forceOverwrite ? null : 'confirm'),
129141
message: () => {
130142
const dirForPrompt =
131143
targetDir === '.' ? 'Current directory' : `Target directory "${targetDir}"`

0 commit comments

Comments
 (0)