Skip to content

Commit 1480e80

Browse files
authored
Merge pull request #39 from AckeeCZ/feat/34-safe-ouput-dir-overwrite
Feat/34 safe ouput dir overwrite
2 parents c99f609 + b522871 commit 1480e80

7 files changed

Lines changed: 82 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ STARTER Which template to setup (required)
2525
Options:
2626
--dir, -d DIR Destination directory (default: ./node-app)
2727
--project-name, -n NAME Google Cloud project name (default: directory basename)
28+
--force, -f Overwrite existing destination directory if it's not empty
2829
--help, -h Show this help message
2930
3031
Starters available:

lib/Bootstrap.js

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Bootstrap.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Toolbelt.js

Lines changed: 20 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Toolbelt.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Bootstrap.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ export class Bootstrap {
3535
default: 'node-app',
3636
description: 'Google Cloud project name',
3737
})
38+
.option('force', {
39+
type: 'boolean',
40+
alias: 'f',
41+
default: false,
42+
description:
43+
"Overwrite existing destination directory if it's not empty",
44+
})
3845
.version('1.0.0')
3946
.help()
4047

@@ -73,7 +80,19 @@ export class Bootstrap {
7380
projectName: parsedArgs.projectName,
7481
})
7582
starter.setToolbelt(toolbelt)
76-
toolbelt.mkdir(destination, { overwrite: true })
83+
84+
if (!toolbelt.isDirectoryEmpty(destination)) {
85+
if (!parsedArgs.force) {
86+
logger.info(
87+
`Directory '${destination}' already exists and is not empty. Use --force or -f flag to overwrite the existing directory.`
88+
)
89+
process.exit(1)
90+
} else {
91+
logger.info(`Overwriting existing directory '${destination}'`)
92+
}
93+
}
94+
95+
toolbelt.mkdir(destination, { overwrite: parsedArgs.force })
7796
toolbelt.npm.init()
7897
starter.install()
7998
}

src/Toolbelt.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ export class Toolbelt {
3030
public stringToPath(str: string) {
3131
return path.normalize(str) as Path
3232
}
33+
34+
public isDirectoryEmpty(dirpath: string): boolean {
35+
const path = this.stringToPath(dirpath)
36+
37+
if (!fs.existsSync(path)) {
38+
return true
39+
}
40+
41+
const stat = fs.statSync(path)
42+
if (!stat.isDirectory()) {
43+
return true
44+
}
45+
46+
const contents = fs.readdirSync(path)
47+
return contents.length === 0
48+
}
49+
3350
public mkdir(
3451
dirpath: string,
3552
option?: {
@@ -43,7 +60,7 @@ export class Toolbelt {
4360
if (fs.existsSync(dirpath) && option?.overwrite) {
4461
fs.rmSync(dirpath, { recursive: true })
4562
}
46-
fs.mkdirSync(dirpath)
63+
fs.mkdirSync(dirpath, { recursive: true })
4764
}
4865
}
4966
/**

0 commit comments

Comments
 (0)