You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-11-26 22:41:17 +02:00
49
packages/lib/testing/plugins/createTestPlugin.ts
Normal file
49
packages/lib/testing/plugins/createTestPlugin.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { join } from 'path';
|
||||
import { PluginManifest } from '../../services/plugins/utils/types';
|
||||
import Setting from '../../models/Setting';
|
||||
import { writeFile } from 'fs-extra';
|
||||
import { defaultPluginSetting } from '../../services/plugins/PluginService';
|
||||
|
||||
|
||||
const setPluginEnabled = (id: string, enabled: boolean) => {
|
||||
const newPluginStates = {
|
||||
...Setting.value('plugins.states'),
|
||||
[id]: {
|
||||
...defaultPluginSetting(),
|
||||
enabled,
|
||||
},
|
||||
};
|
||||
Setting.setValue('plugins.states', newPluginStates);
|
||||
};
|
||||
|
||||
interface Options {
|
||||
onStart?: string;
|
||||
enabled?: boolean;
|
||||
}
|
||||
|
||||
const createTestPlugin = async (manifest: PluginManifest, { onStart = '', enabled = true }: Options = {}) => {
|
||||
const pluginSource = `
|
||||
/* joplin-manifest:
|
||||
${JSON.stringify(manifest)}
|
||||
*/
|
||||
|
||||
joplin.plugins.register({
|
||||
onStart: async function() {
|
||||
${onStart}
|
||||
},
|
||||
});
|
||||
`;
|
||||
const pluginPath = join(Setting.value('pluginDir'), `${manifest.id}.js`);
|
||||
await writeFile(pluginPath, pluginSource, 'utf-8');
|
||||
|
||||
setPluginEnabled(manifest.id, enabled);
|
||||
|
||||
return {
|
||||
manifest,
|
||||
setEnabled: (enabled: boolean) => {
|
||||
setPluginEnabled(manifest.id, enabled);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export default createTestPlugin;
|
||||
Reference in New Issue
Block a user