Skip to content

Commit

Permalink
Plugins: Fixed import API
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Sep 4, 2021
1 parent 973121a commit 736bbbd
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
5 changes: 4 additions & 1 deletion .eslintignore
Expand Up @@ -64,7 +64,7 @@ packages/tools/PortableAppsLauncher
packages/fork-*
plugin_types/
readme/
commands/index.ts
**/commands/index.ts

# AUTO-GENERATED - EXCLUDED TYPESCRIPT BUILD
packages/app-cli/app/LinkSelector.d.ts
Expand Down Expand Up @@ -886,6 +886,9 @@ packages/lib/JoplinServerApi.js.map
packages/lib/Logger.d.ts
packages/lib/Logger.js
packages/lib/Logger.js.map
packages/lib/ObjectUtils.d.ts
packages/lib/ObjectUtils.js
packages/lib/ObjectUtils.js.map
packages/lib/PoorManIntervals.d.ts
packages/lib/PoorManIntervals.js
packages/lib/PoorManIntervals.js.map
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -871,6 +871,9 @@ packages/lib/JoplinServerApi.js.map
packages/lib/Logger.d.ts
packages/lib/Logger.js
packages/lib/Logger.js.map
packages/lib/ObjectUtils.d.ts
packages/lib/ObjectUtils.js
packages/lib/ObjectUtils.js.map
packages/lib/PoorManIntervals.d.ts
packages/lib/PoorManIntervals.js
packages/lib/PoorManIntervals.js.map
Expand Down
39 changes: 26 additions & 13 deletions packages/lib/ObjectUtils.js → packages/lib/ObjectUtils.ts
@@ -1,6 +1,4 @@
const ObjectUtils = {};

ObjectUtils.sortByValue = function(object) {
export function sortByValue(object: any) {
const temp = [];
for (const k in object) {
if (!object.hasOwnProperty(k)) continue;
Expand All @@ -19,16 +17,16 @@ ObjectUtils.sortByValue = function(object) {
return v1 < v2 ? -1 : +1;
});

const output = {};
const output: any = {};
for (let i = 0; i < temp.length; i++) {
const item = temp[i];
output[item.key] = item.value;
}

return output;
};
}

ObjectUtils.fieldsEqual = function(o1, o2) {
export function fieldsEqual(o1: any, o2: any) {
if ((!o1 || !o2) && o1 !== o2) return false;

for (const k in o1) {
Expand All @@ -42,22 +40,37 @@ ObjectUtils.fieldsEqual = function(o1, o2) {
if (c1.length !== c2.length) return false;

return true;
};
}

ObjectUtils.convertValuesToFunctions = function(o) {
const output = {};
export function convertValuesToFunctions(o: any) {
const output: any = {};
for (const n in o) {
if (!o.hasOwnProperty(n)) continue;
output[n] = () => {
return typeof o[n] === 'function' ? o[n]() : o[n];
};
}
return output;
};
}

ObjectUtils.isEmpty = function(o) {
export function isEmpty(o: any) {
if (!o) return true;
return Object.keys(o).length === 0 && o.constructor === Object;
};
}

// export function isStringifiable(o:any):boolean {
// if (o === null || o === undefined) return true;

// if (Array.isArray(o)) {
// for (const e of o) {
// if (!isStringifiable(e)) return false;
// }
// return true;
// }

// if (typeof o === 'object') {

// }

module.exports = ObjectUtils;
// return true;
// }
11 changes: 6 additions & 5 deletions packages/lib/services/interop/InteropService_Importer_Base.ts
Expand Up @@ -5,27 +5,28 @@ import { ImportExportResult } from './types';
import Setting from '../../models/Setting';

export default class InteropService_Importer_Base {

private metadata_: any = null;
protected sourcePath_: string = '';
protected options_: any = {};

setMetadata(md: any) {
public setMetadata(md: any) {
this.metadata_ = md;
}

metadata() {
public metadata() {
return this.metadata_;
}

async init(sourcePath: string, options: any) {
public async init(sourcePath: string, options: any) {
this.sourcePath_ = sourcePath;
this.options_ = options;
}

// @ts-ignore
async exec(result: ImportExportResult): Promise<ImportExportResult> {}
public async exec(result: ImportExportResult): Promise<ImportExportResult> {}

async temporaryDirectory_(createIt: boolean) {
protected async temporaryDirectory_(createIt: boolean) {
const md5 = require('md5');
const tempDir = `${Setting.value('tempDir')}/${md5(Math.random() + Date.now())}`;
if (createIt) await require('fs-extra').mkdirp(tempDir);
Expand Down
18 changes: 15 additions & 3 deletions packages/lib/services/interop/InteropService_Importer_Custom.ts
Expand Up @@ -5,15 +5,27 @@ export default class InteropService_Importer_Custom extends InteropService_Impor

private module_: Module = null;

constructor(handler: Module) {
public constructor(handler: Module) {
super();
this.module_ = handler;
}

async exec(result: ImportExportResult): Promise<ImportExportResult> {
public async exec(result: ImportExportResult): Promise<ImportExportResult> {
// When passing the options to the plugin, we strip off any function
// because they won't serialized over ipc.

const processedOptions: any = {};

if (this.options_) {
for (const [k, v] of Object.entries(this.options_)) {
if (typeof v === 'function') continue;
processedOptions[k] = v;
}
}

return this.module_.onExec({
sourcePath: this.sourcePath_,
options: this.options_,
options: processedOptions,
warnings: result.warnings,
});
}
Expand Down

0 comments on commit 736bbbd

Please sign in to comment.