Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ STARTER Which template to setup (required)
Options:
--dir, -d DIR Destination directory (default: ./node-app)
--project-name, -n NAME Google Cloud project name (default: directory basename)
--force, -f Overwrite existing destination directory if it's not empty
--help, -h Show this help message

Starters available:
Expand Down
22 changes: 21 additions & 1 deletion lib/Bootstrap.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/Bootstrap.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 20 additions & 3 deletions lib/Toolbelt.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/Toolbelt.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion src/Bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ export class Bootstrap {
default: 'node-app',
description: 'Google Cloud project name',
})
.option('force', {
type: 'boolean',
alias: 'f',
default: false,
description:
"Overwrite existing destination directory if it's not empty",
})
.version('1.0.0')
.help()

Expand Down Expand Up @@ -73,7 +80,19 @@ export class Bootstrap {
projectName: parsedArgs.projectName,
})
starter.setToolbelt(toolbelt)
toolbelt.mkdir(destination, { overwrite: true })

if (!toolbelt.isDirectoryEmpty(destination)) {
if (!parsedArgs.force) {
logger.info(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: Single log could be used

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to single log

`Directory '${destination}' already exists and is not empty. Use --force or -f flag to overwrite the existing directory.`
)
process.exit(1)
} else {
logger.info(`Overwriting existing directory '${destination}'`)
}
}

toolbelt.mkdir(destination, { overwrite: parsedArgs.force })
toolbelt.npm.init()
starter.install()
}
Expand Down
19 changes: 18 additions & 1 deletion src/Toolbelt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ export class Toolbelt {
public stringToPath(str: string) {
return path.normalize(str) as Path
}

public isDirectoryEmpty(dirpath: string): boolean {
const path = this.stringToPath(dirpath)

if (!fs.existsSync(path)) {
return true
}

const stat = fs.statSync(path)
if (!stat.isDirectory()) {
return true
}

const contents = fs.readdirSync(path)
return contents.length === 0
}

public mkdir(
dirpath: string,
option?: {
Expand All @@ -43,7 +60,7 @@ export class Toolbelt {
if (fs.existsSync(dirpath) && option?.overwrite) {
fs.rmSync(dirpath, { recursive: true })
}
fs.mkdirSync(dirpath)
fs.mkdirSync(dirpath, { recursive: true })
}
}
/**
Expand Down