-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·82 lines (72 loc) · 2.71 KB
/
index.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
#!/usr/bin/env node
'use strict'
// Always colors for better debug
process.argv.push('--color=always')
// External dependencies
const { join, resolve } = require('path')
const __rootdirname = __dirname
const requireDir = require('require-dir')
const colors = require('chalk')
const shell = require('shelljs')
const extend = require('util')._extend
const asciify = require('asciify')
const http = require('http')
const url = require('url')
const fs = require('fs')
const net = require('net')
const yargs = require('yargs')
const glob = require('glob')
const querystring = require('querystring')
const HockeyApp = require('hockeyapp-api-wrapper')
const yaml = require('js-yaml')
const cucumberHtmlReporter = require('cucumber-html-reporter')
const gherkinLint = { run: function () { require('gherkin-lint') } }
const camelCase = require('camelcase')
// Show CLI info
if (yargs.argv._.length) {
const packageJson = require(join(__dirname, '/package.json'))
console.log(colors.blue(`[Patata]`), colors.yellow(`Version ${packageJson.version}. More info: ${packageJson.homepage}`))
}
// Switch CWD if specified from options
const cwd = resolve(yargs.argv.cwd || process.cwd())
process.chdir(cwd)
// External dependencies to pass to the commands
let dep = { yargs, join, resolve, console, colors, shell, process, __rootdirname, extend, url, asciify, http, net, fs, glob, querystring, HockeyApp, yaml, cucumberHtmlReporter, gherkinLint, camelCase }
// Internal dependencies
const inDepFns = requireDir(join(__rootdirname, 'lib', 'modules'), { camelcase: true, recurse: true })
// Internal dependencies as tree
function resolveInternalDependencies (src, dst) {
Object.keys(src).forEach(name => {
if (typeof src[name] === 'function') {
dst[camelCase(name)] = src[name](dep)
} else {
dst[camelCase(name)] = {}
resolveInternalDependencies(src[name], dst[camelCase(name)])
}
})
}
resolveInternalDependencies(inDepFns, dep)
// Load commands from folder and pass dependencies
const commandsFn = requireDir(join(__rootdirname, 'lib', 'commands'))
const commands = Object.keys(commandsFn).map((i) => commandsFn[i](dep))
// Change width of terminal messages
let terminalWidth = require('yargs').terminalWidth()
terminalWidth = terminalWidth > 100 ? 100 : terminalWidth
// Init CLI commands and options
commands.forEach(cmd => yargs.command(cmd.command, cmd.desc, cmd.builder, (argv) => {
try {
cmd.handler(argv)
} catch (ex) {
dep.log.exit(ex)
}
}))
yargs
.wrap(terminalWidth)
.options({ cwd: { desc: 'Change the current working directory', type: 'string' } })
.options({ verbose: { desc: 'Show all debug messages', type: 'boolean' } })
.demand(1)
.version()
.alias('version', 'v')
.help()
.alias('help', 'h')
.argv