Skip to content

Commit 0953292

Browse files
committed
feat: Scaffold up command line application
Now to make it actually do things.
1 parent 17a6752 commit 0953292

4 files changed

Lines changed: 62 additions & 0 deletions

File tree

lib/actions/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
init: require('./init')
3+
}

lib/actions/init/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function initAction (blueprintName, options) {
2+
console.log("Yes lads")
3+
}
4+
5+
module.exports = initAction

lib/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
const program = require('commander')
2+
const chalk = require('chalk')
3+
const actions = require('./actions')
4+
5+
require('../lib/util/enhanceErrorMessages')
26

37
function tymlyCli (argv) {
48
program
59
.version(require('../package').version)
610
.usage('<command> [options]')
711

12+
program
13+
.command('init [blueprint-name]')
14+
.description('create a new Tymly blueprint')
15+
.action((cmd, options) => {
16+
actions.init(cmd, options)
17+
})
18+
19+
program
20+
.arguments('<command>')
21+
.action((cmd) => {
22+
program.outputHelp()
23+
console.log(`${chalk.red('Unknown command')} ${chalk.yellow(cmd)}.`)
24+
console.log()
25+
})
26+
27+
program.on('--help', () => {
28+
console.log()
29+
console.log(`Run ${chalk.cyan(`tymly <command> --help`)} for detailed usage of given command.`)
30+
console.log()
31+
})
32+
833
program.parse(argv)
934

1035
if (!argv.slice(2).length) {

lib/util/enhanceErrorMessages.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const program = require('commander')
2+
const chalk = require('chalk')
3+
4+
function enhanceErrorMessages (methodName, log) {
5+
program.Command.prototype[methodName] = function (...args) {
6+
if (methodName === 'unknownOption' && this._allowUnknownOption) {
7+
return
8+
}
9+
this.outputHelp()
10+
console.log(chalk.red(log(...args)))
11+
console.log()
12+
}
13+
}
14+
15+
enhanceErrorMessages('missingArgument', argName => {
16+
return `\nMissing required argument ${chalk.yellow(`<${argName}>`)}.`
17+
})
18+
19+
enhanceErrorMessages('unknownOption', optionName => {
20+
return `\nUnknown option ${chalk.yellow(optionName)}.`
21+
})
22+
23+
enhanceErrorMessages('optionMissingArgument', (option, flag) => {
24+
return `Missing required argument for option ${chalk.yellow(option.flags)}` + (
25+
flag ? `, got ${chalk.yellow(flag)}` : ``
26+
)
27+
})
28+
29+
module.exports = enhanceErrorMessages

0 commit comments

Comments
 (0)