Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 77ddc34

Browse files
authored
Merge pull request #345 from Taillis-Labs/cli-create-helper
feat(cli): Exit(1) if the directory contains files that could conflict
2 parents 2fa0322 + 42e423f commit 77ddc34

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

cli/helpers/is-folder-empty.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { green, blue } from 'https://deno.land/[email protected]/fmt/colors.ts'
2+
import { join } from "https://deno.land/std/path/mod.ts"
3+
4+
export default function (root: string, name: string): boolean {
5+
const validFiles = [
6+
'.DS_Store',
7+
'.git',
8+
'.gitattributes',
9+
'.gitignore',
10+
'.gitlab-ci.yml',
11+
'.hg',
12+
'.hgcheck',
13+
'.hgignore',
14+
'.idea',
15+
'.travis.yml',
16+
'LICENSE',
17+
'Thumbs.db',
18+
'docs',
19+
'mkdocs.yml',
20+
]
21+
22+
const conflicts = []
23+
24+
for (const { name: file } of Deno.readDirSync(root)) {
25+
// Support IntelliJ IDEA-based editors
26+
if (validFiles.includes(file) || /\.iml$/.test(file)) {
27+
conflicts.push(file)
28+
}
29+
}
30+
31+
if (conflicts.length > 0) {
32+
console.log(
33+
`The directory ${green(name)} contains files that could conflict:`
34+
)
35+
console.log()
36+
37+
for (const file of conflicts) {
38+
try {
39+
const stats = Deno.lstatSync(join(root, file))
40+
if (stats.isDirectory) {
41+
console.log(` ${blue(file)}/`)
42+
} else {
43+
console.log(` ${file}`)
44+
}
45+
} catch {
46+
console.log(` ${file}`)
47+
}
48+
}
49+
50+
console.log()
51+
console.log(
52+
'Either try using a new directory name, or remove the files listed above.'
53+
)
54+
console.log()
55+
56+
return false
57+
}
58+
59+
return true
60+
61+
}

cli/init.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import util from '../shared/util.ts'
88
import { defaultReactVersion } from '../shared/constants.ts'
99
import { VERSION } from '../version.ts'
1010
import { x_brotli, x_flate } from '../server/compress.ts'
11+
import isFolderEmpty from './helpers/is-folder-empty.ts';
1112

1213
export const helpMessage = `
1314
Usage:
@@ -28,6 +29,10 @@ export default async function (nameArg?: string) {
2829
return
2930
}
3031

32+
if (!isFolderEmpty(cwd, name)) {
33+
Deno.exit(1)
34+
}
35+
3136
const template = 'hello-world' // todo: add template select ui
3237
const vscode = await confirm('Add recommended workspace settings of VS Code?')
3338

0 commit comments

Comments
 (0)