joplin/CliClient/app/command-sync.js

75 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-07-10 20:03:46 +00:00
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { Setting } from 'lib/models/setting.js';
import { BaseItem } from 'lib/models/base-item.js';
import { vorpalUtils } from './vorpal-utils.js';
class Command extends BaseCommand {
2017-07-17 18:46:09 +00:00
constructor() {
super();
this.syncTarget_ = null;
}
2017-07-10 20:03:46 +00:00
usage() {
return 'sync';
}
description() {
return 'Synchronizes with remote storage.';
}
options() {
return [
2017-07-17 18:46:09 +00:00
['--target <target>', 'Sync to provided target (defaults to sync.target config value)'],
2017-07-10 20:03:46 +00:00
['--random-failures', 'For debugging purposes. Do not use.'],
];
}
async action(args) {
2017-07-17 18:46:09 +00:00
this.syncTarget_ = Setting.value('sync.target');
if (args.options.target) this.syncTarget_ = args.options.target;
let sync = await app().synchronizer(this.syncTarget_);
2017-07-14 19:06:01 +00:00
2017-07-13 18:09:47 +00:00
let options = {
onProgress: (report) => {
2017-07-14 19:06:01 +00:00
let lines = sync.reportToLines(report);
if (lines.length) vorpalUtils.redraw(lines.join(' '));
2017-07-13 18:09:47 +00:00
},
onMessage: (msg) => {
vorpalUtils.redrawDone();
this.log(msg);
},
randomFailures: args.options['random-failures'] === true,
};
2017-07-17 18:46:09 +00:00
this.log(_('Synchronization target: %s', this.syncTarget_));
2017-07-10 20:03:46 +00:00
2017-07-13 18:09:47 +00:00
if (!sync) throw new Error(_('Cannot initialize synchronizer.'));
2017-07-10 20:03:46 +00:00
2017-07-13 18:09:47 +00:00
this.log(_('Starting synchronization...'));
2017-07-10 20:03:46 +00:00
2017-07-13 18:09:47 +00:00
await sync.start(options);
vorpalUtils.redrawDone();
2017-07-15 22:47:11 +00:00
await app().refreshCurrentFolder();
2017-07-13 18:09:47 +00:00
this.log(_('Done.'));
2017-07-10 20:03:46 +00:00
}
async cancel() {
2017-07-17 18:46:09 +00:00
const target = this.syncTarget_ ? this.syncTarget_ : Setting.value('sync.target');
2017-07-10 20:03:46 +00:00
vorpalUtils.redrawDone();
this.log(_('Cancelling...'));
2017-07-17 18:46:09 +00:00
let sync = await app().synchronizer(target);
2017-07-10 20:03:46 +00:00
sync.cancel();
2017-07-17 18:46:09 +00:00
this.syncTarget_ = null;
2017-07-10 20:03:46 +00:00
}
}
module.exports = Command;