/
index.ts
executable file
·106 lines (90 loc) · 3.38 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
import { flags } from '@salesforce/command';
import chalk from 'chalk';
import { cli } from 'cli-ux';
import * as fs from 'fs-extra';
import * as path from 'path';
import { targetUserNameCompletion } from '../../completions';
import { AutocompleteBase } from '../../base';
import { updateCache } from '../../cache';
import Create from './create';
export default class Index extends AutocompleteBase {
public static description = 'display autocomplete installation instructions';
public static args = [
{
name: 'shell',
description: 'shell type',
required: false
}
];
public static examples = [
'$ sfdx autocmplt',
'$ sfdx autocmplt bash',
'$ sfdx autocmplt zsh',
'$ sfdx autocmplt fish',
'$ sfdx autocmplt --refresh-cache'
];
protected static flagsConfig = {
'refresh-cache': flags.boolean({
description: 'refresh cache only (ignores displaying instructions)',
char: 'r'
}),
suppresswarnings: flags.boolean({
description: 'suppress warnings',
hidden: true
})
};
public async run() {
const shell = this.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(targetUserNameCompletion, 'targetusername');
cli.action.stop();
if (!this.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 fishNote = 'This assumes your Fish configuration is stored at ~/.config/fish/config.fish';
const note = shell === 'zsh' ? zshNote : shell === 'bash' ? bashNote : fishNote;
const tabStr = shell === 'bash' ? '<TAB><TAB>' : '<TAB>';
const addStr =
shell === 'fish'
? `Update your shell to load the new completions
${chalk.cyan('$ source ~/.config/fish/config.fish')}`
: `Add the autocomplete env var to your ${shell} profile and source it
${chalk.cyan(`$ printf "$(${bin} autocmplt:script ${shell})" >> ~/.${shell}rc; source ~/.${shell}rc`)}`;
this.log(`
${chalk.bold(`Setup Instructions for ${bin.toUpperCase()} CLI Autocomplete ---`)}
1) ${addStr}
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
Enjoy!
`);
}
if (this.flags.suppresswarnings) {
try {
const suppresswarningsfile = path.join(this.config.cacheDir, 'sfdx-autocmplt', 'suppresswarnings');
await fs.ensureFile(suppresswarningsfile);
await fs.writeJson(suppresswarningsfile, {
SuppressUpdateWarning: true
});
} catch (error) {
this.logger.error(error);
}
}
}
// tslint:disable-next-line: no-any
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);
}
}