forked from heroku/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegacy.js
121 lines (113 loc) · 3.27 KB
/
legacy.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// @flow
import {type Arg, type Flag} from 'cli-engine-config'
import {Command, flags as Flags} from 'cli-engine-heroku'
import vars from 'cli-engine-heroku/lib/vars'
export type LegacyContext = {
supportsColor: boolean
}
export type LegacyFlag = {
name: string,
description?: string,
char?: string,
hasValue?: boolean,
hidden?: boolean,
required?: boolean,
optional?: boolean,
parse?: any
}
export type LegacyCommand = {
topic: string,
command?: string,
aliases?: string[],
variableArgs?: boolean,
args: Arg[],
flags: LegacyFlag[],
description?: ?string,
help?: ?string,
usage?: ?string,
needsApp?: ?boolean,
needsAuth?: ?boolean,
needsOrg?: ?boolean,
hidden?: ?boolean,
default?: ?boolean,
run: (ctx: LegacyContext) => Promise<any>
}
export function convertFromV5 (c: LegacyCommand) {
class V5 extends Command {
static topic = c.topic
static command = c.command
static description = c.description
static hidden = !!c.hidden
static args = c.args || []
static flags = convertFlagsFromV5(c.flags)
static variableArgs = !!c.variableArgs
static help = c.help
static usage = c.usage
static aliases = c.aliases || []
run () {
let flags: any = this.flags
let args: (string[] | {[k: string]: string}) = this.argv
if (!c.variableArgs) {
// turn args into object v5 expects
args = {}
for (let i = 0; i < this.argv.length; i++) {
args[this.constructor.args[i].name] = this.argv[i]
}
}
const ctx = {
version: this.config.userAgent,
supportsColor: this.out.color.enabled,
auth: {},
debug: this.config.debug,
debugHeaders: this.config.debug > 1 || ['1', 'true'].includes(process.env.HEROKU_DEBUG_HEADERS),
flags,
args,
app: flags.app,
org: flags.org,
team: flags.team,
config: this.config,
apiUrl: vars.apiUrl,
herokuDir: this.config.cacheDir,
apiToken: this.heroku.auth,
apiHost: vars.apiHost,
gitHost: vars.gitHost,
httpGitHost: vars.httpGitHost,
cwd: process.cwd()
}
ctx.auth.password = ctx.apiToken
const ansi = require('ansi-escapes')
process.once('exit', () => {
if (process.stderr.isTTY) {
process.stderr.write(ansi.cursorShow)
}
})
return c.run(ctx)
}
}
if (c.needsApp || c.wantsApp) {
V5.flags.app = Flags.app({required: !!c.needsApp})
V5.flags.remote = Flags.remote()
}
if (c.needsOrg || c.wantsOrg) {
let opts = {required: !!c.needsOrg, hidden: false, description: 'organization to use'}
V5.flags.org = Flags.org(opts)
}
return V5
}
function convertFlagsFromV5 (flags: ?(LegacyFlag[] | {[name: string]: Flag})): {[name: string]: any} {
if (!flags) return {}
if (!Array.isArray(flags)) return flags
return flags.reduce((flags, flag) => {
let opts: Flag = {
char: (flag.char: any),
description: flag.description,
hidden: flag.hidden,
required: flag.required,
optional: flag.optional,
parse: flag.parse
}
Object.keys(opts).forEach(k => opts[k] === undefined && delete opts[k])
flags[flag.name] = flag.hasValue ? Flags.string(opts) : Flags.boolean((opts: any))
return flags
}, {})
}