This repository was archived by the owner on Sep 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathCLI.ts
More file actions
192 lines (175 loc) · 4.99 KB
/
CLI.ts
File metadata and controls
192 lines (175 loc) · 4.99 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import * as path from 'path'
import { Command } from './Command'
import { Config } from './Config'
import { Output } from './Output'
import { RunOptions } from './types/common'
import Lock from './Plugin/Lock'
import { Dispatcher } from './Dispatcher/Dispatcher'
import { NotFound } from './NotFound'
import fs from './fs'
import { getCommandId } from './util'
import { StatusChecker } from './StatusChecker'
import * as Raven from 'raven'
Raven.config(
'https://337bd4ced421443282b2693709387a98:59e523cc610444919c3c38fb86bd430a@sentry.io/263237',
).install()
const debug = require('debug')('cli')
const handleEPIPE = err => {
if (err.code !== 'EPIPE') {
throw err
}
}
let out: Output
if (!global.testing) {
process.once('SIGINT', () => {
if (out) {
if (out.action.task) {
out.action.stop(out.color.red('ctrl-c'))
}
out.exit(1)
} else {
process.exit(1)
}
})
const handleErr = err => {
if (!out) {
throw err
}
out.error(err)
}
process.once('uncaughtException', handleErr)
process.once('unhandledRejection', handleErr)
process.stdout.on('error', handleEPIPE)
process.stderr.on('error', handleEPIPE)
}
process.env.CLI_ENGINE_VERSION = require('../package.json').version
export class CLI {
config: Config
cmd: Command
constructor({ config }: { config?: RunOptions } = {}) {
if (!config) {
config = {
mock: false,
}
}
const parentFilename = module.parent!.parent!
? module.parent!.parent!.filename
: module.parent!.filename
if (!config.initPath) {
config.initPath = parentFilename
}
if (!config.root) {
const findUp = require('find-up')
config.root = path.dirname(
findUp.sync('package.json', {
cwd: parentFilename,
}),
)
}
this.config = new Config(config)
}
async run() {
out = new Output(this.config)
this.config.setOutput(out)
if (this.cmdAskingForHelp) {
debug('command asking for help')
this.cmd = await this.Help.run(this.config)
} else {
const id = getCommandId(this.config.argv.slice(1))
debug('command id', id)
// if there is a subcommand, cut the first away so the Parser still works correctly
if (
this.config.argv[1] &&
this.config.argv[1].startsWith('-') &&
id !== 'help' &&
id !== 'init'
) {
this.config.argv = this.config.argv.slice(1)
}
const dispatcher = new Dispatcher(this.config)
let result = await dispatcher.findCommand(
id || this.config.defaultCommand || 'help',
)
// if nothing is found, try again with taking what is before :
if (!result.Command && id && id.includes(':')) {
result = await dispatcher.findCommand(id.split(':')[0])
}
const { plugin } = result
const foundCommand = result.Command
if (foundCommand) {
const lock = new Lock(out)
await lock.unread()
// TODO remove this
if (process.env.NOCK_WRITE_RESPONSE_CLI === 'true') {
debug('RECORDING')
require('nock').recorder.rec({
dont_print: true,
})
}
this.cmd = await foundCommand.run(this.config)
const checker = new StatusChecker(this.config, this.cmd.env)
checker.checkStatus(id, this.cmd.args, this.cmd.flags, this.cmd.argv)
if (process.env.NOCK_WRITE_RESPONSE_CLI === 'true') {
const requests = require('nock').recorder.play()
const requestsPath = path.join(process.cwd(), 'requests.js')
debug('WRITING', requestsPath)
fs.writeFileSync(requestsPath, requests.join('\n'))
}
} else {
const topic = await dispatcher.findTopic(id)
if (topic) {
await this.Help.run(this.config)
} else {
return new NotFound(out, this.config.argv).run()
}
}
}
if (
!(
this.config.argv.includes('logs') ||
this.config.argv.includes('logs:function') ||
(this.config.argv.includes('deploy') &&
(this.config.argv.includes('-w') ||
this.config.argv.includes('--watch')))
)
) {
const { timeout } = require('./util')
await timeout(this.flush(), 2000)
out.exit(0)
} else {
debug('not flushing')
}
}
flush(): Promise<{} | void> {
if (global.testing) {
return Promise.resolve()
}
const p = new Promise(resolve => process.stdout.once('drain', resolve))
process.stdout.write('')
return p
}
get cmdAskingForHelp(): boolean {
for (const arg of this.config.argv) {
if (['--help', '-h'].includes(arg)) {
return true
}
if (arg === '--') {
return false
}
}
return false
}
get Help() {
const { default: Help } = require('./commands/help')
return Help
}
}
export function run({ config }: { config?: RunOptions } = {}) {
if (!config) {
config = {
mock: false,
}
}
const cli = new CLI({ config })
return cli.run()
}