-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathcreate.ts
249 lines (216 loc) · 6.8 KB
/
create.ts
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import {Command} from '@oclif/config'
import * as fs from 'fs-extra'
import * as path from 'path'
import {AutocompleteBase} from '../../base'
const debug = require('debug')('autocomplete:create')
export default class Create extends AutocompleteBase {
static hidden = true
static description = 'create autocomplete setup scripts and completion functions'
private _commands?: Command[]
async run() {
this.errorIfWindows()
// 1. ensure needed dirs
await this.ensureDirs()
// 2. save (generated) autocomplete files
await this.createFiles()
}
private async ensureDirs() {
// ensure autocomplete cache dir
await fs.ensureDir(this.autocompleteCacheDir)
// ensure autocomplete completions dir
await fs.ensureDir(this.completionsCacheDir)
}
private async createFiles() {
await fs.writeFile(this.bashSetupScriptPath, this.bashSetupScript)
await fs.writeFile(this.zshSetupScriptPath, this.zshSetupScript)
await fs.writeFile(this.bashCommandsListPath, this.bashCommandsList)
await fs.writeFile(this.zshCompletionSettersPath, this.zshCompletionSetters)
}
private get bashSetupScriptPath(): string {
// <cacheDir>/autocomplete/bash_setup
return path.join(this.autocompleteCacheDir, 'bash_setup')
}
private get bashCommandsListPath(): string {
// <cacheDir>/autocomplete/commands
return path.join(this.autocompleteCacheDir, 'commands')
}
private get zshSetupScriptPath(): string {
// <cacheDir>/autocomplete/zsh_setup
return path.join(this.autocompleteCacheDir, 'zsh_setup')
}
private get zshCompletionSettersPath(): string {
// <cacheDir>/autocomplete/commands_setters
return path.join(this.autocompleteCacheDir, 'commands_setters')
}
private get skipEllipsis(): boolean {
return process.env.HEROKU_AC_ZSH_SKIP_ELLIPSIS === '1'
}
private get commands(): Command[] {
if (this._commands) return this._commands
const plugins = this.config.plugins
const commands: Command[] = []
plugins.forEach(p => {
p.commands.forEach(c => {
if (c.hidden) return
try {
commands.push(c)
} catch (error) {
debug(`Error creating completions for command ${c.id}`)
debug(error.message)
this.writeLogFile(error.message)
}
})
})
this._commands = commands
return this._commands
}
private get bashCommandsList(): string {
return this.commands.map(c => {
try {
const publicFlags = this.genCmdPublicFlags(c).trim()
return `${c.id} ${publicFlags}`
} catch (error) {
debug(`Error creating bash completion for command ${c.id}, moving on...`)
debug(error.message)
this.writeLogFile(error.message)
return ''
}
}).join('\n')
}
private get zshCompletionSetters(): string {
const cmdsSetter = this.zshCommandsSetter
const flagSetters = this.zshCommandsFlagsSetters
return `${cmdsSetter}\n${flagSetters}`
}
private get zshCommandsSetter(): string {
const cmdsWithDescriptions = this.commands.map(c => {
try {
return this.genCmdWithDescription(c)
} catch (error) {
debug(`Error creating zsh autocomplete for command ${c.id}, moving on...`)
debug(error.message)
this.writeLogFile(error.message)
return ''
}
})
return this.genZshAllCmdsListSetter(cmdsWithDescriptions)
}
private get zshCommandsFlagsSetters(): string {
return this.commands.map(c => {
try {
return this.genZshCmdFlagsSetter(c)
} catch (error) {
debug(`Error creating zsh autocomplete for command ${c.id}, moving on...`)
debug(error.message)
this.writeLogFile(error.message)
return ''
}
}).join('\n')
}
private genCmdPublicFlags(Command: Command): string {
const Flags = Command.flags || {}
return Object.keys(Flags)
.filter(flag => !Flags[flag].hidden)
.map(flag => `--${flag}`)
.join(' ')
}
private genCmdWithDescription(Command: Command): string {
let description = ''
if (Command.description) {
const text = Command.description.split('\n')[0]
description = `:"${text}"`
}
return `"${Command.id.replace(/:/g, '\\:')}"${description}`
}
private genZshCmdFlagsSetter(Command: Command): string {
const id = Command.id
const flagscompletions = Object.keys(Command.flags || {})
.filter(flag => Command.flags && !Command.flags[flag].hidden)
.map(flag => {
const f = (Command.flags && Command.flags[flag]) || {description: ''}
const isBoolean = f.type === 'boolean'
const hasCompletion = 'completion' in f || this.findCompletion(id, flag, f.description)
const name = isBoolean ? flag : `${flag}=-`
let cachecompl = ''
if (hasCompletion) {
cachecompl = ': :_compadd_flag_options'
}
if (this.wantsLocalFiles(flag)) {
cachecompl = ': :_files'
}
const help = isBoolean ? '(switch) ' : (hasCompletion ? '(autocomplete) ' : '')
const completion = `--${name}[${help}${f.description}]${cachecompl}`
return `"${completion}"`
})
.join('\n')
if (flagscompletions) {
return `_set_${id.replace(/:/g, '_')}_flags () {
_flags=(
${flagscompletions}
)
}
`
}
return `# no flags for ${id}`
}
private genZshAllCmdsListSetter(cmdsWithDesc: Array<string>): string {
return `
_set_all_commands_list () {
_all_commands_list=(
${cmdsWithDesc.join('\n')}
)
}
`
}
private get envAnalyticsDir(): string {
return `HEROKU_AC_ANALYTICS_DIR=${path.join(
this.autocompleteCacheDir,
'completion_analytics',
)};`
}
private get envCommandsPath(): string {
return `HEROKU_AC_COMMANDS_PATH=${path.join(this.autocompleteCacheDir, 'commands')};`
}
private get bashSetupScript(): string {
return `${this.envAnalyticsDir}
${this.envCommandsPath}
HEROKU_AC_BASH_COMPFUNC_PATH=${path.join(
__dirname,
'..',
'..',
'..',
'autocomplete',
'bash',
'heroku.bash',
)} && test -f $HEROKU_AC_BASH_COMPFUNC_PATH && source $HEROKU_AC_BASH_COMPFUNC_PATH;
`
}
private get zshSetupScript(): string {
return `${this.skipEllipsis ? '' : this.completionDotsFunc}
${this.envAnalyticsDir}
${this.envCommandsPath}
HEROKU_AC_ZSH_SETTERS_PATH=\${HEROKU_AC_COMMANDS_PATH}_setters && test -f $HEROKU_AC_ZSH_SETTERS_PATH && source $HEROKU_AC_ZSH_SETTERS_PATH;
fpath=(
${path.join(__dirname, '..', '..', '..', 'autocomplete', 'zsh')}
$fpath
);
autoload -Uz compinit;
compinit;
`
}
private get completionDotsFunc(): string {
return `expand-or-complete-with-dots() {
echo -n "..."
zle expand-or-complete
zle redisplay
}
zle -N expand-or-complete-with-dots
bindkey "^I" expand-or-complete-with-dots`
}
private wantsLocalFiles(flag: string): boolean {
return [
'file',
'procfile',
].includes(flag)
}
}