bitwarden-estensione-browser/apps/cli/src/commands/sync.command.ts

42 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-12-20 18:04:00 +01:00
import { SyncService } from "jslib-common/abstractions/sync.service";
import { Response } from "jslib-node/cli/models/response";
import { MessageResponse } from "jslib-node/cli/models/response/messageResponse";
import { StringResponse } from "jslib-node/cli/models/response/stringResponse";
2018-05-14 20:54:19 +02:00
2022-01-19 16:45:14 +01:00
import { CliUtils } from "src/utils";
2018-05-14 17:15:54 +02:00
export class SyncCommand {
2021-12-20 18:04:00 +01:00
constructor(private syncService: SyncService) {}
2018-05-14 17:15:54 +02:00
2022-01-19 16:45:14 +01:00
async run(cmdOptions: Record<string, any>): Promise<Response> {
const normalizedOptions = new Options(cmdOptions);
if (normalizedOptions.last) {
2021-12-20 18:04:00 +01:00
return await this.getLastSync();
2018-05-14 17:15:54 +02:00
}
2021-12-20 18:04:00 +01:00
try {
2022-03-03 18:24:41 +01:00
await this.syncService.fullSync(normalizedOptions.force, true);
2021-12-20 18:04:00 +01:00
const res = new MessageResponse("Syncing complete.", null);
return Response.success(res);
} catch (e) {
return Response.error("Syncing failed: " + e.toString());
}
2021-12-20 18:04:00 +01:00
}
private async getLastSync() {
const lastSyncDate = await this.syncService.getLastSync();
const res = new StringResponse(lastSyncDate == null ? null : lastSyncDate.toISOString());
return Response.success(res);
}
2018-05-14 17:15:54 +02:00
}
2022-01-19 16:45:14 +01:00
class Options {
last: boolean;
force: boolean;
constructor(passedOptions: Record<string, any>) {
this.last = CliUtils.convertBooleanOption(passedOptions?.last);
this.force = CliUtils.convertBooleanOption(passedOptions?.force);
2022-01-19 16:45:14 +01:00
}
}