2020-11-07 17:59:37 +02:00
|
|
|
import shim from '@joplin/lib/shim';
|
|
|
|
const { dirname } = require('@joplin/lib/path-utils');
|
|
|
|
const Setting = require('@joplin/lib/models/Setting').default;
|
2019-12-29 19:58:40 +02:00
|
|
|
const pluginAssets = require('./pluginAssets/index');
|
2020-11-08 03:08:33 +02:00
|
|
|
const KvStore = require('@joplin/lib/services/KvStore').default;
|
2019-12-29 19:58:40 +02:00
|
|
|
|
|
|
|
export default class PluginAssetsLoader {
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
static instance_: PluginAssetsLoader = null;
|
|
|
|
logger_: any = null;
|
2019-12-29 19:58:40 +02:00
|
|
|
|
|
|
|
static instance() {
|
|
|
|
if (PluginAssetsLoader.instance_) return PluginAssetsLoader.instance_;
|
|
|
|
PluginAssetsLoader.instance_ = new PluginAssetsLoader();
|
|
|
|
return PluginAssetsLoader.instance_;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
setLogger(logger: any) {
|
2019-12-29 19:58:40 +02:00
|
|
|
this.logger_ = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger() {
|
|
|
|
return this.logger_;
|
|
|
|
}
|
|
|
|
|
|
|
|
async importAssets() {
|
|
|
|
const destDir = `${Setting.value('resourceDir')}/pluginAssets`;
|
|
|
|
await shim.fsDriver().mkdir(destDir);
|
|
|
|
|
|
|
|
const hash = pluginAssets.hash;
|
|
|
|
if (hash === await KvStore.instance().value('PluginAssetsLoader.lastHash')) {
|
|
|
|
this.logger().info(`PluginAssetsLoader: Assets are up to date. Hash: ${hash}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.logger().info(`PluginAssetsLoader: Importing assets to ${destDir}`);
|
|
|
|
|
|
|
|
try {
|
2020-03-14 01:46:14 +02:00
|
|
|
for (const name in pluginAssets.files) {
|
2019-12-29 19:58:40 +02:00
|
|
|
const dataBase64 = pluginAssets.files[name].data;
|
|
|
|
const destPath = `${destDir}/${name}`;
|
|
|
|
await shim.fsDriver().mkdir(dirname(destPath));
|
|
|
|
await shim.fsDriver().unlink(destPath);
|
|
|
|
|
|
|
|
this.logger().info(`PluginAssetsLoader: Copying: ${name} => ${destPath}`);
|
|
|
|
await shim.fsDriver().writeFile(destPath, dataBase64);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.logger().error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
KvStore.instance().setValue('PluginAssetsLoader.lastHash', hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|