2017-11-03 02:09:34 +02:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const { JoplinDatabase } = require('lib/joplin-database.js');
|
|
|
|
const { DatabaseDriverNode } = require('lib/database-driver-node.js');
|
|
|
|
const { BaseModel } = require('lib/base-model.js');
|
|
|
|
const { Folder } = require('lib/models/folder.js');
|
|
|
|
const { Note } = require('lib/models/note.js');
|
|
|
|
const { Resource } = require('lib/models/resource.js');
|
|
|
|
const { Tag } = require('lib/models/tag.js');
|
|
|
|
const { NoteTag } = require('lib/models/note-tag.js');
|
|
|
|
const { Logger } = require('lib/logger.js');
|
|
|
|
const { Setting } = require('lib/models/setting.js');
|
|
|
|
const { BaseItem } = require('lib/models/base-item.js');
|
|
|
|
const { Synchronizer } = require('lib/synchronizer.js');
|
|
|
|
const { FileApi } = require('lib/file-api.js');
|
|
|
|
const { FileApiDriverMemory } = require('lib/file-api-driver-memory.js');
|
|
|
|
const { FileApiDriverLocal } = require('lib/file-api-driver-local.js');
|
2017-11-12 20:57:59 +02:00
|
|
|
const { FsDriverNode } = require('lib/fs-driver-node.js');
|
2017-11-03 02:09:34 +02:00
|
|
|
const { time } = require('lib/time-utils.js');
|
2017-11-24 20:37:40 +02:00
|
|
|
const SyncTargetRegistry = require('lib/SyncTargetRegistry.js');
|
2017-11-24 20:59:16 +02:00
|
|
|
const SyncTarget1 = require('lib/SyncTarget1.js');
|
|
|
|
const SyncTarget2 = require('lib/SyncTarget2.js');
|
2017-06-14 21:59:02 +02:00
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
let databases_ = [];
|
|
|
|
let synchronizers_ = [];
|
2017-06-14 21:59:02 +02:00
|
|
|
let fileApi_ = null;
|
2017-06-18 22:19:13 +02:00
|
|
|
let currentClient_ = 1;
|
2017-06-14 21:59:02 +02:00
|
|
|
|
2017-07-06 00:29:03 +02:00
|
|
|
const fsDriver = new FsDriverNode();
|
|
|
|
Logger.fsDriver_ = fsDriver;
|
|
|
|
Resource.fsDriver_ = fsDriver;
|
|
|
|
|
2017-07-06 21:48:17 +02:00
|
|
|
const logDir = __dirname + '/../tests/logs';
|
|
|
|
fs.mkdirpSync(logDir, 0o755);
|
|
|
|
|
2017-07-26 22:00:16 +02:00
|
|
|
const syncTargetId_ = Setting.SYNC_TARGET_MEMORY;
|
2017-07-23 16:11:44 +02:00
|
|
|
const syncDir = __dirname + '/../tests/sync';
|
|
|
|
|
2017-07-28 19:57:01 +02:00
|
|
|
const sleepTime = syncTargetId_ == Setting.SYNC_TARGET_FILESYSTEM ? 1001 : 400;
|
2017-07-24 20:58:11 +02:00
|
|
|
|
2017-06-25 17:17:40 +02:00
|
|
|
const logger = new Logger();
|
2017-07-06 21:48:17 +02:00
|
|
|
logger.addTarget('file', { path: logDir + '/log.txt' });
|
2017-06-25 17:17:40 +02:00
|
|
|
logger.setLevel(Logger.LEVEL_DEBUG);
|
|
|
|
|
2017-07-06 21:48:17 +02:00
|
|
|
BaseItem.loadClass('Note', Note);
|
|
|
|
BaseItem.loadClass('Folder', Folder);
|
|
|
|
BaseItem.loadClass('Resource', Resource);
|
|
|
|
BaseItem.loadClass('Tag', Tag);
|
|
|
|
BaseItem.loadClass('NoteTag', NoteTag);
|
|
|
|
|
2017-07-07 00:15:31 +02:00
|
|
|
Setting.setConstant('appId', 'net.cozic.joplin-cli');
|
|
|
|
Setting.setConstant('appType', 'cli');
|
|
|
|
|
2017-11-24 20:59:16 +02:00
|
|
|
SyncTargetRegistry.addClass(SyncTarget1);
|
|
|
|
SyncTargetRegistry.addClass(SyncTarget2);
|
|
|
|
|
2017-07-23 16:11:44 +02:00
|
|
|
function syncTargetId() {
|
2017-07-24 20:58:11 +02:00
|
|
|
return syncTargetId_;
|
2017-07-23 16:11:44 +02:00
|
|
|
}
|
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
function sleep(n) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve();
|
2017-06-19 21:18:22 +02:00
|
|
|
}, Math.round(n * 1000));
|
2017-06-18 22:19:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:29:19 +02:00
|
|
|
async function switchClient(id) {
|
2017-07-24 20:58:11 +02:00
|
|
|
await time.msleep(sleepTime); // Always leave a little time so that updated_time properties don't overlap
|
2017-07-03 20:29:19 +02:00
|
|
|
await Setting.saveAll();
|
2017-06-20 21:18:19 +02:00
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
currentClient_ = id;
|
|
|
|
BaseModel.db_ = databases_[id];
|
|
|
|
Folder.db_ = databases_[id];
|
|
|
|
Note.db_ = databases_[id];
|
|
|
|
BaseItem.db_ = databases_[id];
|
2017-06-20 21:18:19 +02:00
|
|
|
Setting.db_ = databases_[id];
|
|
|
|
|
|
|
|
return Setting.load();
|
2017-06-18 22:19:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearDatabase(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
|
|
|
|
let queries = [
|
|
|
|
'DELETE FROM notes',
|
|
|
|
'DELETE FROM folders',
|
2017-07-02 17:46:03 +02:00
|
|
|
'DELETE FROM resources',
|
|
|
|
'DELETE FROM tags',
|
|
|
|
'DELETE FROM note_tags',
|
2017-07-19 21:15:55 +02:00
|
|
|
|
|
|
|
'DELETE FROM deleted_items',
|
|
|
|
'DELETE FROM sync_items',
|
2017-06-18 22:19:13 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
return databases_[id].transactionExecBatch(queries);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupDatabase(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
|
|
|
|
if (databases_[id]) {
|
2017-06-19 20:58:49 +02:00
|
|
|
return clearDatabase(id).then(() => {
|
|
|
|
return Setting.load();
|
|
|
|
});
|
2017-06-14 21:59:02 +02:00
|
|
|
}
|
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
const filePath = __dirname + '/data/test-' + id + '.sqlite';
|
2017-06-14 21:59:02 +02:00
|
|
|
return fs.unlink(filePath).catch(() => {
|
|
|
|
// Don't care if the file doesn't exist
|
|
|
|
}).then(() => {
|
2017-07-06 21:48:17 +02:00
|
|
|
databases_[id] = new JoplinDatabase(new DatabaseDriverNode());
|
2017-10-21 20:07:56 +02:00
|
|
|
// databases_[id].setLogger(logger);
|
2017-11-12 20:57:59 +02:00
|
|
|
// console.info(filePath);
|
2017-06-18 22:19:13 +02:00
|
|
|
return databases_[id].open({ name: filePath }).then(() => {
|
|
|
|
BaseModel.db_ = databases_[id];
|
|
|
|
return setupDatabase(id);
|
2017-06-14 21:59:02 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
async function setupDatabaseAndSynchronizer(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
2017-06-18 01:49:52 +02:00
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
await setupDatabase(id);
|
|
|
|
|
|
|
|
if (!synchronizers_[id]) {
|
2017-11-24 20:37:40 +02:00
|
|
|
const SyncTargetClass = SyncTargetRegistry.classById(syncTargetId_);
|
|
|
|
const syncTarget = new SyncTargetClass(db(id));
|
|
|
|
syncTarget.setFileApi(fileApi());
|
|
|
|
syncTarget.setLogger(logger);
|
|
|
|
synchronizers_[id] = await syncTarget.synchronizer();
|
2017-06-18 01:49:52 +02:00
|
|
|
}
|
2017-06-18 22:19:13 +02:00
|
|
|
|
2017-07-24 20:58:11 +02:00
|
|
|
if (syncTargetId_ == Setting.SYNC_TARGET_FILESYSTEM) {
|
2017-07-23 16:11:44 +02:00
|
|
|
fs.removeSync(syncDir)
|
|
|
|
fs.mkdirpSync(syncDir, 0o755);
|
|
|
|
} else {
|
|
|
|
await fileApi().format();
|
|
|
|
}
|
2017-06-14 21:59:02 +02:00
|
|
|
}
|
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
function db(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
return databases_[id];
|
2017-06-14 21:59:02 +02:00
|
|
|
}
|
|
|
|
|
2017-06-18 22:19:13 +02:00
|
|
|
function synchronizer(id = null) {
|
|
|
|
if (id === null) id = currentClient_;
|
|
|
|
return synchronizers_[id];
|
2017-06-14 21:59:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function fileApi() {
|
2017-06-18 22:19:13 +02:00
|
|
|
if (fileApi_) return fileApi_;
|
|
|
|
|
2017-07-24 20:58:11 +02:00
|
|
|
if (syncTargetId_ == Setting.SYNC_TARGET_FILESYSTEM) {
|
2017-07-23 16:11:44 +02:00
|
|
|
fs.removeSync(syncDir)
|
|
|
|
fs.mkdirpSync(syncDir, 0o755);
|
|
|
|
fileApi_ = new FileApi(syncDir, new FileApiDriverLocal());
|
2017-07-26 22:00:16 +02:00
|
|
|
} else if (syncTargetId_ == Setting.SYNC_TARGET_MEMORY) {
|
2017-07-23 16:11:44 +02:00
|
|
|
fileApi_ = new FileApi('/root', new FileApiDriverMemory());
|
2017-07-24 20:58:11 +02:00
|
|
|
}
|
2017-07-26 22:00:16 +02:00
|
|
|
// } else if (syncTargetId == Setting.SYNC_TARGET_ONEDRIVE) {
|
|
|
|
// let auth = require('./onedrive-auth.json');
|
|
|
|
// if (!auth) {
|
|
|
|
// const oneDriveApiUtils = new OneDriveApiNodeUtils(oneDriveApi);
|
|
|
|
// auth = await oneDriveApiUtils.oauthDance();
|
|
|
|
// fs.writeFileSync('./onedrive-auth.json', JSON.stringify(auth));
|
|
|
|
// process.exit(1);
|
|
|
|
// } else {
|
|
|
|
// auth = JSON.parse(auth);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // const oneDriveApiUtils = new OneDriveApiNodeUtils(reg.oneDriveApi());
|
|
|
|
// // const auth = await oneDriveApiUtils.oauthDance(this);
|
|
|
|
// // Setting.setValue('sync.3.auth', auth ? JSON.stringify(auth) : null);
|
|
|
|
// // if (!auth) return;
|
|
|
|
// }
|
2017-07-24 20:58:11 +02:00
|
|
|
|
|
|
|
fileApi_.setLogger(logger);
|
|
|
|
fileApi_.setSyncTargetId(syncTargetId_);
|
|
|
|
return fileApi_;
|
2017-06-14 21:59:02 +02:00
|
|
|
}
|
|
|
|
|
2017-11-03 02:13:17 +02:00
|
|
|
module.exports = { setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId };
|