Skip to content

Commit 7e9c637

Browse files
mashabekpavelsvagr
authored andcommitted
✨ Add --force option to overwrite existing destination directory in CLI
1 parent 144f284 commit 7e9c637

3 files changed

Lines changed: 44 additions & 4 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:

src/Bootstrap.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ export class Bootstrap {
4040
default: 'node-app',
4141
description: 'Google Cloud project name',
4242
})
43+
.option('force', {
44+
type: 'boolean',
45+
alias: 'f',
46+
default: false,
47+
description: 'Overwrite existing destination directory if it\'s not empty',
48+
})
4349
.version('1.0.0')
4450
.help()
4551

@@ -78,7 +84,18 @@ export class Bootstrap {
7884
projectName: parsedArgs.projectName,
7985
})
8086
starter.setToolbelt(toolbelt)
81-
toolbelt.mkdir(destination, { overwrite: true })
87+
88+
if (toolbelt.isDirectoryNonEmpty(destination)) {
89+
if (!parsedArgs.force) {
90+
logger.info(`Directory '${destination}' already exists and is not empty.`)
91+
logger.info('Use --force or -f flag to overwrite the existing directory.')
92+
process.exit(1)
93+
} else {
94+
logger.info(`Overwriting existing directory '${destination}'`)
95+
}
96+
}
97+
98+
toolbelt.mkdir(destination, { overwrite: parsedArgs.force })
8299
toolbelt.npm.init()
83100
starter.install()
84101
}

src/Toolbelt.ts

Lines changed: 25 additions & 3 deletions
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 isDirectoryNonEmpty(dirpath: string): boolean {
35+
const path = this.stringToPath(dirpath)
36+
37+
if (!fs.existsSync(path)) {
38+
return false
39+
}
40+
41+
const stat = fs.statSync(path)
42+
if (!stat.isDirectory()) {
43+
return false
44+
}
45+
46+
const contents = fs.readdirSync(path)
47+
return contents.length > 0
48+
}
49+
3350
public mkdir(
3451
dirpath: string,
3552
option?: {
@@ -40,10 +57,15 @@ export class Toolbelt {
4057
dirpath = this.stringToPath(dirpath)
4158
const rootPath = ['.', './']
4259
if (!rootPath.includes(dirpath)) {
43-
if (fs.existsSync(dirpath) && option?.overwrite) {
44-
fs.rmSync(dirpath, { recursive: true })
60+
if (fs.existsSync(dirpath)) {
61+
if (option?.overwrite) {
62+
fs.rmSync(dirpath, { recursive: true })
63+
fs.mkdirSync(dirpath, { recursive: true })
64+
}
65+
// If directory exists and we're not overwriting, do nothing
66+
} else {
67+
fs.mkdirSync(dirpath, { recursive: true })
4568
}
46-
fs.mkdirSync(dirpath)
4769
}
4870
}
4971
/**

0 commit comments

Comments
 (0)