1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-27 08:21:03 +02:00

Desktop: Fix missing plugin file error and missing setting key error in dev mode (#6827)

This commit is contained in:
Mayank Bondre 2022-09-12 20:38:06 +05:30 committed by GitHub
parent d07788607c
commit 66c9ee0a1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 7 deletions

View File

@ -1,7 +1,7 @@
import { installDefaultPlugins, getDefaultPluginsInstallState, setSettingsForDefaultPlugins, checkPreInstalledDefaultPlugins } from '@joplin/lib/services/plugins/defaultPlugins/defaultPluginsUtils';
import PluginRunner from '../../../app/services/plugins/PluginRunner';
import { pathExists } from 'fs-extra';
import { setupDatabaseAndSynchronizer, supportDir, switchClient } from '@joplin/lib/testing/test-utils';
import { checkThrow, setupDatabaseAndSynchronizer, supportDir, switchClient } from '@joplin/lib/testing/test-utils';
import PluginService, { defaultPluginSetting, DefaultPluginsInfo, PluginSettings } from '@joplin/lib/services/plugins/PluginService';
import Setting from '@joplin/lib/models/Setting';
@ -213,4 +213,57 @@ describe('defaultPluginsUtils', function() {
await service.destroy();
});
it('should not throw error on missing setting key', async () => {
const service = newPluginService();
const pluginScript = `
/* joplin-manifest:
{
"id": "io.github.jackgruber.backup",
"manifest_version": 1,
"app_min_version": "1.4",
"name": "JS Bundle test",
"version": "1.0.0"
}
*/
joplin.plugins.register({
onStart: async function() {
await joplin.settings.registerSettings({
path: {
value: "initial-path",
type: 2,
section: "backupSection",
public: true,
label: "Backup path",
},
})
},
});`;
const plugin = await service.loadPluginFromJsBundle('', pluginScript);
await service.runPlugin(plugin);
const defaultPluginsInfo: DefaultPluginsInfo = {
'io.github.jackgruber.backup': {
version: '1.0.2',
settings: {
'path': `${Setting.value('profileDir')}`,
'missing-key1': 'someValue',
},
},
'plugin.calebjohn.rich-markdown': {
version: '0.8.3',
settings: {
'missing-key2': 'someValue',
},
},
};
Setting.setValue('installedDefaultPlugins', ['']);
expect(checkThrow(() => setSettingsForDefaultPlugins(defaultPluginsInfo))).toBe(false);
expect(Setting.value('plugin-io.github.jackgruber.backup.path')).toBe(`${Setting.value('profileDir')}`);
await service.destroy();
});
});

View File

@ -278,8 +278,8 @@ class Application extends BaseApplication {
checkPreInstalledDefaultPlugins(defaultPluginsId, pluginSettings);
try {
const pluginsDir = path.join(bridge().buildDir(), 'defaultPlugins');
pluginSettings = await installDefaultPlugins(service, pluginsDir, defaultPluginsId, pluginSettings);
const defaultPluginsDir = path.join(bridge().buildDir(), 'defaultPlugins');
pluginSettings = await installDefaultPlugins(service, defaultPluginsDir, defaultPluginsId, pluginSettings);
if (await shim.fsDriver().exists(Setting.value('pluginDir'))) {
await service.loadAndRunPlugins(Setting.value('pluginDir'), pluginSettings);
}

View File

@ -3,9 +3,12 @@ import path = require('path');
import Setting from '../../../models/Setting';
import shim from '../../../shim';
import PluginService, { defaultPluginSetting, DefaultPluginsInfo, PluginSettings } from '../PluginService';
import Logger from '@joplin/lib/Logger';
import * as React from 'react';
const shared = require('@joplin/lib/components/shared/config-shared.js');
const logger = Logger.create('defaultPluginsUtils');
export function checkPreInstalledDefaultPlugins(defaultPluginsId: string[],pluginSettings: PluginSettings) {
const installedDefaultPlugins: Array<string> = Setting.value('installedDefaultPlugins');
for (const pluginId of defaultPluginsId) {
@ -15,8 +18,17 @@ export function checkPreInstalledDefaultPlugins(defaultPluginsId: string[],plugi
}
}
export async function installDefaultPlugins(service: PluginService, pluginsDir: string, defaultPluginsId: string[], pluginSettings: PluginSettings): Promise<PluginSettings> {
const defaultPluginsPaths = await shim.fsDriver().readDirStats(pluginsDir);
export async function installDefaultPlugins(service: PluginService, defaultPluginsDir: string, defaultPluginsId: string[], pluginSettings: PluginSettings): Promise<PluginSettings> {
if (!await shim.fsDriver().exists(defaultPluginsDir)) {
logger.info(`Could not find default plugins' directory: ${defaultPluginsDir} - skipping installation.`);
return pluginSettings;
}
const defaultPluginsPaths = await shim.fsDriver().readDirStats(defaultPluginsDir);
if (defaultPluginsPaths.length <= 0) {
logger.info(`Default plugins' directory is empty: ${defaultPluginsDir} - skipping installation.`);
return pluginSettings;
}
const installedPlugins = Setting.value('installedDefaultPlugins');
for (let pluginId of defaultPluginsPaths) {
@ -24,7 +36,7 @@ export async function installDefaultPlugins(service: PluginService, pluginsDir:
// if pluginId is present in 'installedDefaultPlugins' array or it doesn't have default plugin ID, then we won't install it again as default plugin
if (installedPlugins.includes(pluginId) || !defaultPluginsId.includes(pluginId)) continue;
const defaultPluginPath: string = path.join(pluginsDir, pluginId, 'plugin.jpl');
const defaultPluginPath: string = path.join(defaultPluginsDir, pluginId, 'plugin.jpl');
await service.installPlugin(defaultPluginPath, false);
pluginSettings = produce(pluginSettings, (draft: PluginSettings) => {
@ -41,7 +53,7 @@ export function setSettingsForDefaultPlugins(defaultPluginsInfo: DefaultPluginsI
for (const pluginId of Object.keys(defaultPluginsInfo)) {
if (!defaultPluginsInfo[pluginId].settings) continue;
for (const settingName of Object.keys(defaultPluginsInfo[pluginId].settings)) {
if (!installedDefaultPlugins.includes(pluginId)) {
if (!installedDefaultPlugins.includes(pluginId) && Setting.keyExists(`plugin-${pluginId}.${settingName}`)) {
Setting.setValue(`plugin-${pluginId}.${settingName}`, defaultPluginsInfo[pluginId].settings[settingName]);
}
}