2018-05-14 17:15:54 +02:00
|
|
|
import * as program from 'commander';
|
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { SyncService } from 'jslib-common/abstractions/sync.service';
|
2018-05-14 17:15:54 +02:00
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
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
|
|
|
|
2018-05-14 17:15:54 +02:00
|
|
|
export class SyncCommand {
|
2018-05-14 19:37:52 +02:00
|
|
|
constructor(private syncService: SyncService) { }
|
2018-05-14 17:15:54 +02:00
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
async run(options: program.OptionValues): Promise<Response> {
|
|
|
|
if (options.last || false) {
|
2018-05-16 19:53:12 +02:00
|
|
|
return await this.getLastSync();
|
|
|
|
}
|
|
|
|
|
2018-05-14 17:15:54 +02:00
|
|
|
try {
|
2021-02-03 18:44:33 +01:00
|
|
|
const result = await this.syncService.fullSync(options.force || false, true);
|
2018-05-16 19:53:12 +02:00
|
|
|
const res = new MessageResponse('Syncing complete.', null);
|
|
|
|
return Response.success(res);
|
2018-05-14 17:15:54 +02:00
|
|
|
} catch (e) {
|
2020-11-20 16:17:09 +01:00
|
|
|
return Response.error('Syncing failed: ' + e.toString());
|
2018-05-14 17:15:54 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-16 19:53:12 +02: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
|
|
|
}
|