-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathindex.ts
132 lines (97 loc) · 4.35 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
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
import {Args, Flags, ux} from '@oclif/core'
import {bold, cyan} from 'ansis'
import {EOL} from 'node:os'
import {AutocompleteBase} from '../../base.js'
import Create from './create.js'
export default class Index extends AutocompleteBase {
static args = {
shell: Args.string({
description: 'Shell type',
options: ['zsh', 'bash', 'powershell'],
required: false,
}),
}
static description = 'Display autocomplete installation instructions.'
static examples = [
'$ <%= config.bin %> autocomplete',
'$ <%= config.bin %> autocomplete bash',
'$ <%= config.bin %> autocomplete zsh',
'$ <%= config.bin %> autocomplete powershell',
'$ <%= config.bin %> autocomplete --refresh-cache',
]
static flags = {
'refresh-cache': Flags.boolean({char: 'r', description: 'Refresh cache (ignores displaying instructions)'}),
}
async run() {
const {args, flags} = await this.parse(Index)
const shell = args.shell ?? this.determineShell(this.config.shell)
if (shell === 'powershell' && this.config?.topicSeparator === ':') {
this.error(
`PowerShell completion is not supported in CLIs using colon as the topic separator.${EOL}See: https://oclif.io/docs/topic_separator`,
)
}
ux.action.start(`${bold('Building the autocomplete cache')}`)
await Create.run([], this.config)
ux.action.stop()
if (!flags['refresh-cache']) {
this.printShellInstructions(shell)
}
}
private printShellInstructions(shell: string): void {
const setupEnvVar = this.getSetupEnvVar(shell)
const tabStr = shell === 'bash' ? '<TAB><TAB>' : '<TAB>'
const scriptCommand = `${this.config.bin} autocomplete${this.config.topicSeparator}script ${shell}`
let instructions = `
Setup Instructions for ${this.config.bin.toUpperCase()} CLI Autocomplete ---
==============================================
`
switch (shell) {
case 'bash': {
instructions += `
1) Run this command in your terminal window:
${cyan(`printf "$(${scriptCommand})" >> ~/.bashrc; source ~/.bashrc`)}
The previous command adds the ${cyan(setupEnvVar)} environment variable to your Bash config file and then sources the file.
${bold('NOTE')}: If you’ve configured your terminal to start as a login shell, you may need to modify the command so it updates either the ~/.bash_profile or ~/.profile file. For example:
${cyan(`printf "$(${scriptCommand}) >> ~/.bash_profile; source ~/.bash_profile`)}
Or:
${cyan(`printf "$(${scriptCommand})" >> ~/.profile; source ~/.profile`)}
2) Start using autocomplete:
${cyan(`${this.config.bin} ${tabStr}`)} # Command completion
${cyan(`${this.config.bin} command --${tabStr}`)} # Flag completion
`
break
}
case 'zsh': {
instructions += `
1) Run this command in your terminal window:
${cyan(`printf "$(${scriptCommand})" >> ~/.zshrc; source ~/.zshrc`)}
The previous command adds the ${cyan(setupEnvVar)} environment variable to your zsh config file and then sources the file.
2) (Optional) Run this command to ensure that you have no permissions conflicts:
${cyan('compaudit -D')}
3) Start using autocomplete:
${cyan(`${this.config.bin} ${tabStr}`)} # Command completion
${cyan(`${this.config.bin} command --${tabStr}`)} # Flag completion
`
break
}
case 'powershell': {
instructions += `
1) Run these two cmdlets in your PowerShell window in the order shown:
${cyan(`New-Item -Type Directory -Path (Split-Path -Parent $PROFILE) -ErrorAction SilentlyContinue
Add-Content -Path $PROFILE -Value (Invoke-Expression -Command "${scriptCommand}"); .$PROFILE`)}
2) (Optional) If you want matching completions printed below the command line, run this cmdlet:
${cyan('Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete')}
3) Start using autocomplete:
${cyan(`${this.config.bin} ${tabStr}`)} # Command completion
${cyan(`${this.config.bin} command --${tabStr}`)} # Flag completion
`
break
}
}
instructions += `
Every time you enter ${tabStr}, the autocomplete feature displays a list of commands (or flags if you type --), along with their summaries. Enter a letter and then ${tabStr} again to narrow down the list until you end up with the complete command that you want to execute.
Enjoy!
`
this.log(instructions)
}
}