This repository was archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathincomplete.ts
More file actions
66 lines (57 loc) · 2.23 KB
/
Copy pathincomplete.ts
File metadata and controls
66 lines (57 loc) · 2.23 KB
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
/*
* Copyright (c) 2022, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as os from 'os';
import { Hook, toConfiguredId, toStandardizedId, Interfaces, Command, loadHelpClass } from '@oclif/core';
import { Prompter } from '@salesforce/sf-plugins-core';
import { Lifecycle } from '@salesforce/core';
function buildChoices(
matches: Command.Loadable[],
config: Interfaces.Config
): Array<{ name: string; value: Command.Loadable }> {
const configuredIds = matches.map((p) => toConfiguredId(p.id, config));
const maxCommandLength = configuredIds.reduce((max, id) => Math.max(max, id.length), 0);
return matches.map((p, i) => {
const summary = p.summary ?? p.description?.split(os.EOL)[0] ?? '';
return {
name: `${configuredIds[i].padEnd(maxCommandLength + 5, ' ')}${summary}`,
value: p,
short: configuredIds[i],
};
});
}
async function determineCommand(config: Interfaces.Config, matches: Command.Loadable[]): Promise<string> {
if (matches.length === 1) return matches[0].id;
const prompter = new Prompter();
const choices = buildChoices(matches, config);
const { command } = await prompter.timedPrompt<{ command: Command.Loadable }>([
{
name: 'command',
type: 'list',
message: 'Which of these commands do you mean',
choices,
},
]);
return command.id;
}
const hook: Hook.CommandIncomplete = async function ({ config, matches, argv }) {
const command = await determineCommand(config, matches);
if (argv.includes('--help') || argv.includes('-h')) {
const Help = await loadHelpClass(config);
const help = new Help(config, config.pjson.helpOptions as Partial<Interfaces.HelpOptions>);
return help.showHelp([toStandardizedId(command, config), ...argv]);
}
if (matches.length === 1) {
await Lifecycle.getInstance().emitWarning(
`One command matches the partial command entered, running command:${os.EOL}${config.bin} ${toConfiguredId(
command,
config
)} ${argv.join(' ')}`
);
}
return config.runCommand(toStandardizedId(command, config), argv);
};
export default hook;