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';
|
2017-07-17 22:43:29 +00:00
|
|
|
import { Synchronizer } from 'lib/synchronizer.js';
|
2017-07-17 19:37:59 +00:00
|
|
|
const locker = require('proper-lockfile');
|
|
|
|
const fs = require('fs-extra');
|
2017-07-10 20:03:46 +00:00
|
|
|
|
|
|
|
class Command extends BaseCommand {
|
|
|
|
|
2017-07-17 18:46:09 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.syncTarget_ = null;
|
2017-07-17 19:37:59 +00:00
|
|
|
this.releaseLockFn_ = null;
|
2017-07-17 18:46:09 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 20:03:46 +00:00
|
|
|
usage() {
|
|
|
|
return 'sync';
|
|
|
|
}
|
|
|
|
|
|
|
|
description() {
|
2017-07-18 18:21:03 +00:00
|
|
|
return _('Synchronizes with remote storage.');
|
2017-07-10 20:03:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
options() {
|
|
|
|
return [
|
2017-07-18 18:21:03 +00:00
|
|
|
['--target <target>', _('Sync to provided target (defaults to sync.target config value)')],
|
2017-07-18 18:49:47 +00:00
|
|
|
['--random-failures', 'For debugging purposes. Do not use.'],
|
2017-07-10 20:03:46 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-07-17 19:37:59 +00:00
|
|
|
static lockFile(filePath) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
locker.lock(filePath, (error, release) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(release);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static isLocked(filePath) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
locker.check(filePath, (error, isLocked) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(isLocked);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-10 20:03:46 +00:00
|
|
|
async action(args) {
|
2017-07-17 19:37:59 +00:00
|
|
|
this.releaseLockFn_ = null;
|
2017-07-17 18:46:09 +00:00
|
|
|
|
2017-07-17 19:37:59 +00:00
|
|
|
const lockFilePath = Setting.value('tempDir') + '/synclock';
|
|
|
|
if (!await fs.pathExists(lockFilePath)) await fs.writeFile(lockFilePath, 'synclock');
|
2017-07-14 19:06:01 +00:00
|
|
|
|
2017-07-17 19:37:59 +00:00
|
|
|
if (await Command.isLocked(lockFilePath)) throw new Error(_('Synchronisation is already in progress.'));
|
2017-07-13 18:09:47 +00:00
|
|
|
|
2017-07-17 19:37:59 +00:00
|
|
|
this.releaseLockFn_ = await Command.lockFile(lockFilePath);
|
2017-07-10 20:03:46 +00:00
|
|
|
|
2017-07-17 19:37:59 +00:00
|
|
|
try {
|
|
|
|
this.syncTarget_ = Setting.value('sync.target');
|
|
|
|
if (args.options.target) this.syncTarget_ = args.options.target;
|
2017-07-10 20:03:46 +00:00
|
|
|
|
2017-07-17 19:37:59 +00:00
|
|
|
let sync = await app().synchronizer(this.syncTarget_);
|
2017-07-10 20:03:46 +00:00
|
|
|
|
2017-07-17 19:37:59 +00:00
|
|
|
let options = {
|
|
|
|
onProgress: (report) => {
|
2017-07-17 22:43:29 +00:00
|
|
|
let lines = Synchronizer.reportToLines(report);
|
2017-07-17 19:37:59 +00:00
|
|
|
if (lines.length) vorpalUtils.redraw(lines.join(' '));
|
|
|
|
},
|
|
|
|
onMessage: (msg) => {
|
|
|
|
vorpalUtils.redrawDone();
|
|
|
|
this.log(msg);
|
|
|
|
},
|
|
|
|
randomFailures: args.options['random-failures'] === true,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.log(_('Synchronization target: %s', this.syncTarget_));
|
|
|
|
|
|
|
|
if (!sync) throw new Error(_('Cannot initialize synchronizer.'));
|
|
|
|
|
|
|
|
this.log(_('Starting synchronization...'));
|
|
|
|
|
|
|
|
await sync.start(options);
|
|
|
|
vorpalUtils.redrawDone();
|
|
|
|
|
|
|
|
await app().refreshCurrentFolder();
|
2017-07-15 22:47:11 +00:00
|
|
|
|
2017-07-17 19:37:59 +00:00
|
|
|
this.log(_('Done.'));
|
|
|
|
} catch (error) {
|
|
|
|
this.releaseLockFn_();
|
|
|
|
this.releaseLockFn_ = null;
|
|
|
|
throw error;
|
|
|
|
}
|
2017-07-15 22:47:11 +00:00
|
|
|
|
2017-07-17 19:37:59 +00:00
|
|
|
this.releaseLockFn_();
|
|
|
|
this.releaseLockFn_ = null;
|
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;
|