-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·97 lines (81 loc) · 2.33 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env node
const path = require('path')
const yArgs = require('yargs')
const colors = require('colors')
const { Signale } = require('signale')
const signaleTypes = require('signale/types')
const _uniq = require('lodash/uniq')
const _values = require('lodash/values')
const getConfig = require('./lib/config')
const commands = require('./lib/commands')
const pkg = require('./package.json')
const updateNotifier = require('simple-update-notifier')
updateNotifier({ pkg })
const logLevels = _uniq(_values(signaleTypes).map(({ logLevel }) => logLevel))
const y = yArgs
.scriptName('httpmd')
.option('log-level', {
describe: 'Log level, increase to debug',
default: 'error',
choices: logLevels,
type: 'string',
alias: 'l',
global: true
})
.option('path', {
describe: 'Root directory',
default: process.cwd(),
demandOption: true,
type: 'string',
alias: 'p',
global: true,
normalize: true
})
.middleware((argv) => {
const { 'log-level': logLevel } = argv
const l = new Signale({
logLevel,
scope: 'cli',
types: {
...signaleTypes,
star: {
...signaleTypes.star,
logLevel: 'error'
}
}
})
l.info('started [%s]', logLevel)
argv.l = l
})
.middleware(async (argv) => {
const { l, path: rawPath } = argv
const cwd = process.cwd()
const basePath = rawPath[0] === '/' ? rawPath : path.join(cwd, rawPath)
const config = await getConfig({ basePath })
const { state } = config
const { md, configPath, template } = state
if (configPath) {
l.star('read config from %s', colors.bgGreen.black(configPath))
}
l.star('using template %s', colors.cyan(template.name))
md.pluginNames.forEach((name) => {
l.star('using md plugin %s', colors.yellow(name))
})
// eslint-why save config on args context
argv.config = config
// eslint-why save config on args context
argv.path = basePath
})
.example('$0 gen-config > .httpmdrc.json', 'Generate basic configuration')
.showHelpOnFail(false, 'Specify --help for available options')
.help()
.version()
.recommendCommands()
commands.forEach((def) => {
y.command(def)
})
// eslint-disable-next-line github/no-then
y.parse().catch((err) => {
// eslint-disable-next-line no-console
console.error(err.stack)
})