Refactor messageHandler and catch unknown commands

This commit is contained in:
Daniel Treviño
2018-11-01 12:05:33 +01:00
parent 8e2f2f53b8
commit c4e3965ff5
3 changed files with 36 additions and 9 deletions
+10
View File
@@ -1,5 +1,6 @@
#!/usr/bin/env node
const { printErrorAndExit } = require("./utils/messageHandler")
const packagejson = require("./package.json")
const updateNotifier = require("update-notifier")
const program = require("commander")
@@ -48,4 +49,13 @@ program
list()
})
// Error on unknown commands
program.on("command:*", () => {
const wrongCommands = program.args.join(" ")
printErrorAndExit(
`\nInvalid command: ${wrongCommands}\nSee --help for a list of available commands.\n`
)
})
program.parse(process.argv)
-9
View File
@@ -1,9 +0,0 @@
const chalk = require("chalk")
function printErrorAndExit(error) {
console.log(`${chalk.bold.red(error)}\n\n`)
process.exit(0)
}
module.exports = printErrorAndExit
+26
View File
@@ -0,0 +1,26 @@
const chalk = require("chalk")
function printMessage(message) {
console.log(message)
}
function printGreenMessage(message) {
console.log(`${chalk.green(message)}`)
}
function printError(error) {
console.log(`${chalk.bold.red(error)}`)
}
function printErrorAndExit(error) {
console.log(`${printError(error)}`)
process.exit(0)
}
module.exports = {
printMessage,
printErrorAndExit,
printError,
printGreenMessage
}