1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-04 18:37:45 +01:00

Formatting and lint fixes

This commit is contained in:
Kyle Spearrin 2020-05-26 09:34:44 -04:00
parent 723ff201f6
commit d6c5d0ec1c
3 changed files with 65 additions and 90 deletions

2
jslib

@ -1 +1 @@
Subproject commit 8438cafbd08c1c9b1440e0c5385e15e8fb5ac524 Subproject commit 2858724f4431038be190fc0b748efe287dd1bae6

View File

@ -1,55 +1,81 @@
import * as program from "commander"; import * as program from 'commander';
import { Response } from "jslib/cli/models/response";
import { MessageResponse } from "jslib/cli/models/response/messageResponse";
type Option = { import { Response } from 'jslib/cli/models/response';
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
interface IOption {
long: string; long: string;
short: string; short: string;
description: string; description: string;
}; }
type Command = { interface ICommand {
commands?: Command[]; commands?: ICommand[];
options?: Option[]; options?: IOption[];
_name: string; _name: string;
_description: string; _description: string;
}; }
const zshCompletion = (rootName: string, rootCommand: Command) => { const validShells = ['zsh'];
const renderCommandBlock = (name: string, command: Command): string => {
export class CompletionCommand {
async run(cmd: program.Command) {
const shell: typeof validShells[number] = cmd.shell;
if (!shell) {
return Response.badRequest('`shell` was not provided.');
}
if (!validShells.includes(shell)) {
return Response.badRequest('Unsupported shell.');
}
let content = '';
if (shell === 'zsh') {
content = this.zshCompletion('bw', cmd.parent).render();
}
const res = new MessageResponse(content, null);
return Response.success(res);
}
private zshCompletion(rootName: string, rootCommand: ICommand) {
return {
render: () => {
return [
`#compdef _${rootName} ${rootName}`,
'',
this.renderCommandBlock(rootName, rootCommand),
].join('\n');
},
};
}
private renderCommandBlock(name: string, command: ICommand): string {
const { commands = [], options = [] } = command; const { commands = [], options = [] } = command;
const hasOptions = options.length > 0; const hasOptions = options.length > 0;
const hasCommands = commands.length > 0; const hasCommands = commands.length > 0;
const _arguments = options const args = options
.map(({ long, short, description }) => { .map(({ long, short, description }) => {
const aliases = [short, long].filter(Boolean); const aliases = [short, long].filter(Boolean);
const opts = aliases.join(',');
const OPTS = aliases.join(","); const desc = `[${description.replace(`'`, `'"'"'`)}]`;
return aliases.length > 1 ? `'(${aliases.join(' ')})'{${opts}}'${desc}'` : `'${opts}${desc}'`;
const DESCRIPTION = `[${description.replace("'", `'"'"'`)}]`; }).concat(`'(-h --help)'{-h,--help}'[output usage information]'`,
return aliases.length > 1
? `'(${aliases.join(" ")})'{${OPTS}}'${DESCRIPTION}'`
: `'${OPTS}${DESCRIPTION}'`;
})
.concat(
`'(-h --help)'{-h,--help}'[output usage information]'`,
hasCommands ? '"1: :->cmnds"' : null, hasCommands ? '"1: :->cmnds"' : null,
'"*::arg:->args"' '"*::arg:->args"',
) ).filter(Boolean);
.filter(Boolean);
const commandBlockFunctionParts = []; const commandBlockFunctionParts = [];
if (hasCommands) { if (hasCommands) {
commandBlockFunctionParts.push("local -a commands"); commandBlockFunctionParts.push('local -a commands');
} }
if (hasOptions) { if (hasOptions) {
commandBlockFunctionParts.push( commandBlockFunctionParts.push(`_arguments -C \\\n ${args.join(` \\\n `)}`);
`_arguments -C \\\n ${_arguments.join(` \\\n `)}`
);
} }
if (hasCommands) { if (hasCommands) {
@ -57,79 +83,28 @@ const zshCompletion = (rootName: string, rootCommand: Command) => {
`case $state in `case $state in
cmnds) cmnds)
commands=( commands=(
${commands ${commands.map(({ _name, _description }) => `"${_name}:${_description}"`).join('\n ')}
.map(({ _name, _description }) => `"${_name}:${_description}"`)
.join("\n ")}
) )
_describe "command" commands _describe "command" commands
;; ;;
esac esac
case "$words[1]" in case "$words[1]" in
${commands ${commands.map(({ _name }) => [`${_name})`, `_${name}_${_name}`, ';;'].join('\n ')).join('\n ')}
.map(({ _name }) => esac`,
[`${_name})`, `_${name}_${_name}`, ";;"].join("\n ")
)
.join("\n ")}
esac`
); );
} }
const commandBlocParts = [ const commandBlocParts = [
`function _${name} {\n ${commandBlockFunctionParts.join( `function _${name} {\n ${commandBlockFunctionParts.join('\n\n ')}\n}`,
"\n\n "
)}\n}`,
]; ];
if (hasCommands) { if (hasCommands) {
commandBlocParts.push( commandBlocParts.push(
commands commands.map((c) => this.renderCommandBlock(`${name}_${c._name}`, c)).join('\n\n'),
.map((command) =>
renderCommandBlock(`${name}_${command._name}`, command)
)
.join("\n\n")
); );
} }
return commandBlocParts.join("\n\n"); return commandBlocParts.join('\n\n');
};
const render = () => {
return [
`#compdef _${rootName} ${rootName}`,
"",
renderCommandBlock(rootName, rootCommand),
].join("\n");
};
return {
render,
};
};
const validShells = ["zsh"];
export class CompletionCommand {
constructor() {}
async run(cmd: program.Command) {
const shell: typeof validShells[number] = cmd.shell;
if (!shell) {
return Response.badRequest("`shell` was not provided!");
}
if (!validShells.includes(shell)) {
return Response.badRequest(`Unsupported shell!`);
}
let content = "";
if (shell === "zsh") {
content = zshCompletion("bw", cmd.parent).render();
}
const res = new MessageResponse(content, null);
return Response.success(res);
} }
} }

View File

@ -669,7 +669,7 @@ export class Program extends BaseProgram {
.on('--help', () => { .on('--help', () => {
writeLn('\n Notes:'); writeLn('\n Notes:');
writeLn(''); writeLn('');
writeLn(' Valid shells are `zsh`.') writeLn(' Valid shells are `zsh`.');
writeLn(''); writeLn('');
writeLn(' Examples:'); writeLn(' Examples:');
writeLn(''); writeLn('');
@ -680,7 +680,7 @@ export class Program extends BaseProgram {
const command = new CompletionCommand(); const command = new CompletionCommand();
const response = await command.run(cmd); const response = await command.run(cmd);
this.processResponse(response); this.processResponse(response);
}) });
program program
.parse(process.argv); .parse(process.argv);