1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-06 09:20:43 +01:00

centralize stdout on success

This commit is contained in:
Kyle Spearrin 2018-05-16 08:37:05 -04:00
parent 8fd74688b1
commit e7782b2082

View File

@ -172,48 +172,54 @@ export class Program {
} }
if (response.data != null) { if (response.data != null) {
let out: string = null;
if (response.data.object === 'string') { if (response.data.object === 'string') {
const data = (response.data as StringResponse).data; const data = (response.data as StringResponse).data;
if (data != null) { if (data != null) {
process.stdout.write(data); out = data;
} }
} else if (response.data.object === 'list') { } else if (response.data.object === 'list') {
this.printJson((response.data as ListResponse).data, cmd); out = this.getJson((response.data as ListResponse).data, cmd);
} else if (response.data.object === 'template') { } else if (response.data.object === 'template') {
this.printJson((response.data as TemplateResponse).template, cmd); out = this.getJson((response.data as TemplateResponse).template, cmd);
} else if (response.data.object === 'message') { } else if (response.data.object === 'message') {
this.printMessage(response); out = this.getMessage(response);
} else { } else {
this.printJson(response.data, cmd); out = this.getJson(response.data, cmd);
}
if (out != null) {
process.stdout.write(out);
} }
} }
process.exit(); process.exit();
} }
private printJson(obj: any, cmd: program.Command) { private getJson(obj: any, cmd: program.Command): string {
if (process.env.BW_PRETTY === 'true') { if (process.env.BW_PRETTY === 'true') {
process.stdout.write(JSON.stringify(obj, null, ' ')); return JSON.stringify(obj, null, ' ');
} else { } else {
process.stdout.write(JSON.stringify(obj)); return JSON.stringify(obj);
} }
} }
private printMessage(response: Response) { private getMessage(response: Response) {
const message = (response.data as MessageResponse); const message = (response.data as MessageResponse);
if (process.env.BW_RAW === 'true' && message.raw != null) { if (process.env.BW_RAW === 'true' && message.raw != null) {
process.stdout.write(message.raw); return message.raw;
return;
} }
let out: string = '';
if (message.title != null) { if (message.title != null) {
process.stdout.write(chalk.greenBright(message.title)); out = chalk.greenBright(message.title);
} }
if (message.message != null) { if (message.message != null) {
if (message.title != null) { if (message.title != null) {
process.stdout.write('\n'); out += '\n';
} }
process.stdout.write(message.message); out += message.message;
} }
return out.trim() === '' ? null : out;
} }
private async exitIfLocked() { private async exitIfLocked() {