2021-04-22 21:17:14 +02:00
|
|
|
import * as chalk from 'chalk';
|
2019-03-16 11:26:31 -04:00
|
|
|
|
|
|
|
|
import { Response } from './models/response';
|
|
|
|
|
import { ListResponse } from './models/response/listResponse';
|
|
|
|
|
import { MessageResponse } from './models/response/messageResponse';
|
|
|
|
|
import { StringResponse } from './models/response/stringResponse';
|
|
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { UserService } from 'jslib-common/abstractions/user.service';
|
2019-03-16 11:26:31 -04:00
|
|
|
|
|
|
|
|
export abstract class BaseProgram {
|
2019-06-04 21:03:15 -04:00
|
|
|
constructor(
|
|
|
|
|
private userService: UserService,
|
|
|
|
|
private writeLn: (s: string, finalLine: boolean, error: boolean) => void) { }
|
2019-03-16 11:26:31 -04:00
|
|
|
|
|
|
|
|
protected processResponse(response: Response, exitImmediately = false, dataProcessor: () => string = null) {
|
|
|
|
|
if (!response.success) {
|
|
|
|
|
if (process.env.BW_QUIET !== 'true') {
|
|
|
|
|
if (process.env.BW_RESPONSE === 'true') {
|
2020-12-11 10:44:57 -06:00
|
|
|
this.writeLn(this.getJson(response), true, false);
|
2019-03-16 11:26:31 -04:00
|
|
|
} else {
|
2019-06-04 21:03:15 -04:00
|
|
|
this.writeLn(chalk.redBright(response.message), true, true);
|
2019-03-16 11:26:31 -04:00
|
|
|
}
|
|
|
|
|
}
|
2021-05-26 07:39:40 +10:00
|
|
|
const exitCode = process.env.BW_CLEANEXIT ? 0 : 1;
|
2019-03-16 11:26:31 -04:00
|
|
|
if (exitImmediately) {
|
2021-05-26 07:39:40 +10:00
|
|
|
process.exit(exitCode);
|
2019-03-16 11:26:31 -04:00
|
|
|
} else {
|
2021-05-26 07:39:40 +10:00
|
|
|
process.exitCode = exitCode;
|
2019-03-16 11:26:31 -04:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (process.env.BW_RESPONSE === 'true') {
|
2019-06-04 21:03:15 -04:00
|
|
|
this.writeLn(this.getJson(response), true, false);
|
2019-03-16 11:26:31 -04:00
|
|
|
} else if (response.data != null) {
|
|
|
|
|
let out: string = dataProcessor != null ? dataProcessor() : null;
|
|
|
|
|
if (out == null) {
|
|
|
|
|
if (response.data.object === 'string') {
|
|
|
|
|
const data = (response.data as StringResponse).data;
|
|
|
|
|
if (data != null) {
|
|
|
|
|
out = data;
|
|
|
|
|
}
|
|
|
|
|
} else if (response.data.object === 'list') {
|
|
|
|
|
out = this.getJson((response.data as ListResponse).data);
|
|
|
|
|
} else if (response.data.object === 'message') {
|
|
|
|
|
out = this.getMessage(response);
|
|
|
|
|
} else {
|
|
|
|
|
out = this.getJson(response.data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (out != null && process.env.BW_QUIET !== 'true') {
|
2019-06-04 21:03:15 -04:00
|
|
|
this.writeLn(out, true, false);
|
2019-03-16 11:26:31 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (exitImmediately) {
|
|
|
|
|
process.exit(0);
|
|
|
|
|
} else {
|
|
|
|
|
process.exitCode = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected getJson(obj: any): string {
|
|
|
|
|
if (process.env.BW_PRETTY === 'true') {
|
|
|
|
|
return JSON.stringify(obj, null, ' ');
|
|
|
|
|
} else {
|
|
|
|
|
return JSON.stringify(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 13:51:48 -04:00
|
|
|
protected getMessage(response: Response): string {
|
2019-03-16 11:26:31 -04:00
|
|
|
const message = (response.data as MessageResponse);
|
2019-03-19 09:07:40 -04:00
|
|
|
if (process.env.BW_RAW === 'true') {
|
2019-03-16 11:26:31 -04:00
|
|
|
return message.raw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let out: string = '';
|
|
|
|
|
if (message.title != null) {
|
|
|
|
|
if (message.noColor) {
|
|
|
|
|
out = message.title;
|
|
|
|
|
} else {
|
|
|
|
|
out = chalk.greenBright(message.title);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (message.message != null) {
|
|
|
|
|
if (message.title != null) {
|
|
|
|
|
out += '\n';
|
|
|
|
|
}
|
|
|
|
|
out += message.message;
|
|
|
|
|
}
|
|
|
|
|
return out.trim() === '' ? null : out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async exitIfAuthed() {
|
|
|
|
|
const authed = await this.userService.isAuthenticated();
|
|
|
|
|
if (authed) {
|
|
|
|
|
const email = await this.userService.getEmail();
|
|
|
|
|
this.processResponse(Response.error('You are already logged in as ' + email + '.'), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async exitIfNotAuthed() {
|
|
|
|
|
const authed = await this.userService.isAuthenticated();
|
|
|
|
|
if (!authed) {
|
|
|
|
|
this.processResponse(Response.error('You are not logged in.'), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|