This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
73 lines (57 loc) · 2.68 KB
/
index.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
import {flags} from '@heroku-cli/command'
import {AppCompletion, PipelineCompletion, SpaceCompletion, TeamCompletion} from '@heroku-cli/command/lib/completions'
import chalk from 'chalk'
import {cli} from 'cli-ux'
import * as path from 'path'
import {AutocompleteBase} from '../../base'
import {updateCache} from '../../cache'
import Create from './create'
export default class Index extends AutocompleteBase {
static description = 'display autocomplete installation instructions'
static args = [{name: 'shell', description: 'shell type', required: false}]
static flags = {
'refresh-cache': flags.boolean({description: 'refresh cache only (ignores displaying instructions)', char: 'r'}),
}
static examples = [
'$ heroku autocomplete',
'$ heroku autocomplete bash',
'$ heroku autocomplete zsh',
'$ heroku autocomplete --refresh-cache'
]
async run() {
const {args, flags} = this.parse(Index)
const shell = args.shell || this.config.shell
this.errorIfNotSupportedShell(shell)
cli.action.start(`${chalk.bold('Building the autocomplete cache')}`)
await Create.run([], this.config)
await this.updateCache(AppCompletion, 'app')
await this.updateCache(PipelineCompletion, 'pipeline')
await this.updateCache(SpaceCompletion, 'space')
await this.updateCache(TeamCompletion, 'team')
cli.action.stop()
if (!flags['refresh-cache']) {
const bin = this.config.bin
const bashNote = 'If your terminal starts as a login shell you may need to print the init script into ~/.bash_profile or ~/.profile.'
const zshNote = `After sourcing, you can run \`${chalk.cyan('$ compaudit -D')}\` to ensure no permissions conflicts are present`
const note = shell === 'zsh' ? zshNote : bashNote
const tabStr = shell === 'bash' ? '<TAB><TAB>' : '<TAB>'
this.log(`
${chalk.bold(`Setup Instructions for ${bin.toUpperCase()} CLI Autocomplete ---`)}
1) Add the autocomplete env var to your ${shell} profile and source it
${chalk.cyan(`$ printf "$(${bin} autocomplete:script ${shell})" >> ~/.${shell}rc; source ~/.${shell}rc`)}
NOTE: ${note}
2) Test it out, e.g.:
${chalk.cyan(`$ ${bin} ${tabStr}`)} # Command completion
${chalk.cyan(`$ ${bin} apps:info --${tabStr}`)} # Flag completion
${chalk.cyan(`$ ${bin} apps:info --app=${tabStr}`)} # Flag option completion
Visit the autocomplete Dev Center doc at https://devcenter.heroku.com/articles/heroku-cli-autocomplete
Enjoy!
`)
}
}
private async updateCache(completion: any, cacheKey: string) {
const cachePath = path.join(this.completionsCacheDir, cacheKey)
const options = await completion.options({config: this.config})
await updateCache(cachePath, options)
}
}