pluginManager.ts (083800902850659f068b818a69c95f117a577235) pluginManager.ts (4d9d3c4c191facbdbac0344e2ec8f2057c33f673)
1import {
2 copyFile,
3 exists,
4 readDir,
5 readFile,
6 unlink,
7 writeFile,
8} from 'react-native-fs';

--- 8 unchanged lines hidden (view full) ---

17import MediaMeta from './mediaMeta';
18import {nanoid} from 'nanoid';
19import {errorLog, trace} from '../utils/log';
20import Cache from './cache';
21import {isSameMediaItem, resetMediaItem} from '@/utils/mediaItem';
22import {internalSerialzeKey, internalSymbolKey} from '@/constants/commonConst';
23import Download from './download';
24import delay from '@/utils/delay';
1import {
2 copyFile,
3 exists,
4 readDir,
5 readFile,
6 unlink,
7 writeFile,
8} from 'react-native-fs';

--- 8 unchanged lines hidden (view full) ---

17import MediaMeta from './mediaMeta';
18import {nanoid} from 'nanoid';
19import {errorLog, trace} from '../utils/log';
20import Cache from './cache';
21import {isSameMediaItem, resetMediaItem} from '@/utils/mediaItem';
22import {internalSerialzeKey, internalSymbolKey} from '@/constants/commonConst';
23import Download from './download';
24import delay from '@/utils/delay';
25import * as cheerio from 'cheerio';
25
26axios.defaults.timeout = 1500;
27
28const sha256 = CryptoJs.SHA256;
29
30enum PluginStateCode {
31 /** 版本不匹配 */
32 VersionNotMatch = 'VERSION NOT MATCH',

--- 26 unchanged lines hidden (view full) ---

59 // eslint-disable-next-line no-new-func
60 _instance = Function(`
61 'use strict';
62 try {
63 return ${funcCode};
64 } catch(e) {
65 return null;
66 }
26
27axios.defaults.timeout = 1500;
28
29const sha256 = CryptoJs.SHA256;
30
31enum PluginStateCode {
32 /** 版本不匹配 */
33 VersionNotMatch = 'VERSION NOT MATCH',

--- 26 unchanged lines hidden (view full) ---

60 // eslint-disable-next-line no-new-func
61 _instance = Function(`
62 'use strict';
63 try {
64 return ${funcCode};
65 } catch(e) {
66 return null;
67 }
67 `)()({CryptoJs, axios, dayjs});
68 `)()({CryptoJs, axios, dayjs, cheerio});
68 this.checkValid(_instance);
69 } catch (e: any) {
70 this.state = 'error';
71 this.stateCode = PluginStateCode.CannotParse;
72 if (e?.stateCode) {
73 this.stateCode = e.stateCode;
74 }
75 errorLog(`${pluginPath}插件无法解析 `, {

--- 329 unchanged lines hidden (view full) ---

405 const result =
406 (await this.plugin.instance?.importMusicSheet?.(urlLike)) ?? [];
407 result.forEach(_ => resetMediaItem(_, this.plugin.name));
408 return result;
409 } catch {
410 return [];
411 }
412 }
69 this.checkValid(_instance);
70 } catch (e: any) {
71 this.state = 'error';
72 this.stateCode = PluginStateCode.CannotParse;
73 if (e?.stateCode) {
74 this.stateCode = e.stateCode;
75 }
76 errorLog(`${pluginPath}插件无法解析 `, {

--- 329 unchanged lines hidden (view full) ---

406 const result =
407 (await this.plugin.instance?.importMusicSheet?.(urlLike)) ?? [];
408 result.forEach(_ => resetMediaItem(_, this.plugin.name));
409 return result;
410 } catch {
411 return [];
412 }
413 }
414 /** 导入单曲 */
415 async importMusicItem(urlLike: string): Promise<IMusic.IMusicItem | null> {
416 try {
417 const result = await this.plugin.instance?.importMusicItem?.(
418 urlLike,
419 );
420 if (!result) {
421 throw new Error();
422 }
423 resetMediaItem(result, this.plugin.name);
424 return result;
425 } catch {
426 return null;
427 }
428 }
413}
414
415let plugins: Array<Plugin> = [];
416const pluginStateMapper = new StateMapper(() => plugins);
417
418async function setup() {
419 const _plugins: Array<Plugin> = [];
420 try {

--- 25 unchanged lines hidden (view full) ---

446 );
447 errorLog('插件初始化失败', e?.message);
448 throw e;
449 }
450}
451
452// 安装插件
453async function installPlugin(pluginPath: string) {
429}
430
431let plugins: Array<Plugin> = [];
432const pluginStateMapper = new StateMapper(() => plugins);
433
434async function setup() {
435 const _plugins: Array<Plugin> = [];
436 try {

--- 25 unchanged lines hidden (view full) ---

462 );
463 errorLog('插件初始化失败', e?.message);
464 throw e;
465 }
466}
467
468// 安装插件
469async function installPlugin(pluginPath: string) {
454 if (pluginPath.endsWith('.js') && (await exists(pluginPath))) {
470 let checkPath = decodeURIComponent(pluginPath);
471 if (pluginPath.endsWith('.js') && (await exists(checkPath))) {
455 const funcCode = await readFile(pluginPath, 'utf8');
456 const plugin = new Plugin(funcCode, pluginPath);
457 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash);
458 if (_pluginIndex !== -1) {
472 const funcCode = await readFile(pluginPath, 'utf8');
473 const plugin = new Plugin(funcCode, pluginPath);
474 const _pluginIndex = plugins.findIndex(p => p.hash === plugin.hash);
475 if (_pluginIndex !== -1) {
459 return;
476 throw new Error('插件已安装');
460 }
461 if (plugin.hash !== '') {
462 const fn = nanoid();
463 const _pluginPath = `${pathConst.pluginPath}${fn}.js`;
464 await copyFile(pluginPath, _pluginPath);
465 plugin.path = _pluginPath;
466 plugins = plugins.concat(plugin);
477 }
478 if (plugin.hash !== '') {
479 const fn = nanoid();
480 const _pluginPath = `${pathConst.pluginPath}${fn}.js`;
481 await copyFile(pluginPath, _pluginPath);
482 plugin.path = _pluginPath;
483 plugins = plugins.concat(plugin);
484 console.log(plugins, 'xxxx');
467 pluginStateMapper.notify();
485 pluginStateMapper.notify();
486 return;
468 }
487 }
488 throw new Error('插件无法解析');
469 }
489 }
490 throw new Error('插件不存在');
470}
471
472/** 卸载插件 */
473async function uninstallPlugin(hash: string) {
474 const targetIndex = plugins.findIndex(_ => _.hash === hash);
475 if (targetIndex !== -1) {
476 try {
477 await unlink(plugins[targetIndex].path);

--- 34 unchanged lines hidden ---
491}
492
493/** 卸载插件 */
494async function uninstallPlugin(hash: string) {
495 const targetIndex = plugins.findIndex(_ => _.hash === hash);
496 if (targetIndex !== -1) {
497 try {
498 await unlink(plugins[targetIndex].path);

--- 34 unchanged lines hidden ---