mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
Make CLI more robust to missing commands, add new help flags
This commit is contained in:
parent
28c73bc62a
commit
e4ed7ff2be
8
cli.ts
8
cli.ts
|
|
@ -20,14 +20,6 @@ const args = process.argv;
|
||||||
const config = await new ConfigManager({}).getConfig();
|
const config = await new ConfigManager({}).getConfig();
|
||||||
|
|
||||||
const cliBuilder = new CliBuilder([
|
const cliBuilder = new CliBuilder([
|
||||||
new CliCommand(
|
|
||||||
["help"],
|
|
||||||
[],
|
|
||||||
() => {
|
|
||||||
cliBuilder.displayHelp();
|
|
||||||
},
|
|
||||||
"Shows help for the CLI"
|
|
||||||
),
|
|
||||||
new CliCommand<{
|
new CliCommand<{
|
||||||
username: string;
|
username: string;
|
||||||
password: string;
|
password: string;
|
||||||
|
|
|
||||||
|
|
@ -106,12 +106,28 @@ export class CliBuilder {
|
||||||
*/
|
*/
|
||||||
async processArgs(args: string[]) {
|
async processArgs(args: string[]) {
|
||||||
const revelantArgs = this.getRelevantArgs(args);
|
const revelantArgs = this.getRelevantArgs(args);
|
||||||
|
|
||||||
|
// Handle "-h", "--help" and "help" commands as special cases
|
||||||
|
if (revelantArgs.length === 1) {
|
||||||
|
if (["-h", "--help", "help"].includes(revelantArgs[0])) {
|
||||||
|
this.displayHelp();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Find revelant command
|
// Find revelant command
|
||||||
// Search for a command with as many categories matching args as possible
|
// Search for a command with as many categories matching args as possible
|
||||||
const matchingCommands = this.commands.filter(command =>
|
const matchingCommands = this.commands.filter(command =>
|
||||||
startsWithArray(revelantArgs, command.categories)
|
startsWithArray(revelantArgs, command.categories)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (matchingCommands.length === 0) {
|
||||||
|
console.log(
|
||||||
|
`Invalid command "${revelantArgs.join(" ")}". Please use the ${chalk.bold("help")} command to see a list of commands`
|
||||||
|
);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Get command with largest category size
|
// Get command with largest category size
|
||||||
const command = matchingCommands.reduce((prev, current) =>
|
const command = matchingCommands.reduce((prev, current) =>
|
||||||
prev.categories.length > current.categories.length ? prev : current
|
prev.categories.length > current.categories.length ? prev : current
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue