From 61c628da4aaa2355a0d7c71d480fd5d5abcd358e Mon Sep 17 00:00:00 2001 From: purocean Date: Tue, 27 Apr 2021 17:42:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/useful/plugin/index.ts | 10 ++++++++++ src/server/constant.ts | 1 + src/server/init.ts | 18 +++++++++++++++++- src/server/main.ts | 21 ++++++++++++++++++++- 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/frontend/src/useful/plugin/index.ts b/frontend/src/useful/plugin/index.ts index 14e986c3..4852604d 100644 --- a/frontend/src/useful/plugin/index.ts +++ b/frontend/src/useful/plugin/index.ts @@ -1,6 +1,7 @@ import * as shortcut from '@/useful/shortcut' import { getLogger } from '@/useful/utils' import { useBus } from '@/useful/bus' +import { useToast } from '@/useful/toast' import buildInPlugins from './build-in' import store from '@/store' import * as tree from './tree' @@ -27,6 +28,9 @@ const ctx = { bus, store, shortcut, + ui: { + useToast, + }, registerHook: (type: HookType, fun: HookFun, once = false) => { if (Array.isArray(hooks[type])) { hooks[type] = [...hooks[type]!!, { fun, once }] @@ -73,6 +77,12 @@ export const init = () => { register(plugin) } }) + + ;(window as any).registerPlugin = register + + const script = window.document.createElement('script') + script.src = '/api/plugins' + window.document.body.appendChild(script) } export const triggerHook = ctx.triggerHook diff --git a/src/server/constant.ts b/src/server/constant.ts index 1123e599..1ea76a25 100644 --- a/src/server/constant.ts +++ b/src/server/constant.ts @@ -14,6 +14,7 @@ export const CONFIG_FILE = path.join(USER_DIR, 'config.json') export const STATIC_DIR = path.join(__dirname, '../../frontend/dist') export const HELP_DIR = path.join(__dirname, '../../help') export const ASSETS_DIR = path.join(__dirname, '../assets') +export const USER_PLUGIN_DIR = path.join(USER_DIR, './plugins') export const BIN_DIR = convertAppPath(path.join(__dirname, '../../bin')) export const RESOURCES_DIR = convertAppPath(path.join(__dirname, '../resources')) diff --git a/src/server/init.ts b/src/server/init.ts index 16dd052f..b10b7cdd 100644 --- a/src/server/init.ts +++ b/src/server/init.ts @@ -1,5 +1,6 @@ import * as fs from 'fs' -import { USER_DIR, TRASH_DIR, MAIN_REPO_DIR } from './constant' +import * as path from 'path' +import { USER_DIR, TRASH_DIR, MAIN_REPO_DIR, USER_PLUGIN_DIR } from './constant' const init = () => { if (!fs.existsSync(USER_DIR)) { @@ -11,6 +12,21 @@ const init = () => { if (!fs.existsSync(TRASH_DIR)) { fs.mkdirSync(TRASH_DIR) } + if (!fs.existsSync(USER_PLUGIN_DIR)) { + fs.mkdirSync(USER_PLUGIN_DIR) + fs.writeFileSync(path.join(USER_PLUGIN_DIR, 'plugin-example.js'), ` +window.registerPlugin({ + name: 'example-plugin', + register: ctx => { + console.log('example-plugin', 'register', ctx); + + // setTimeout(() => { + // ctx.ui.useToast().show('info', 'HELLO WORLD!'); + // }, 2000); + } +}); + `.trim()) + } } export default init diff --git a/src/server/main.ts b/src/server/main.ts index 30df393c..e12e4fef 100644 --- a/src/server/main.ts +++ b/src/server/main.ts @@ -6,7 +6,7 @@ import * as xStatic from 'koa-static' import * as mime from 'mime' import * as request from 'request' import * as pty from 'node-pty' -import { STATIC_DIR, HOME_DIR, HELP_DIR } from './constant' +import { STATIC_DIR, HOME_DIR, HELP_DIR, USER_PLUGIN_DIR } from './constant' import init from './init' import file from './file' import dataRepository from './repository' @@ -176,6 +176,24 @@ const readme = async (ctx: any, next: any) => { } } +const userPlugin = async (ctx: any, next: any) => { + if (ctx.path.startsWith('/api/plugins')) { + ctx.type = 'application/javascript; charset=utf-8'; + + let code = '' + fs.readdirSync(USER_PLUGIN_DIR, { withFileTypes: true }) + .filter(x => x.isFile && x.name.endsWith('.js')) + .forEach(x => { + code += `// ===== ${x.name} =====\n` + + fs.readFileSync(path.join(USER_PLUGIN_DIR, x.name)) + + '\n// ===== end =====\n\n' + }) + ctx.body = code + } else { + await next() + } +} + const wrapper = async (ctx: any, next: any, fun: any) => { try { await fun(ctx, next) @@ -207,6 +225,7 @@ const server = (port = 3000) => { app.use(async (ctx: any, next: any) => await wrapper(ctx, next, proxy)) app.use(async (ctx: any, next: any) => await wrapper(ctx, next, readme)) app.use(async (ctx: any, next: any) => await wrapper(ctx, next, markFile)) + app.use(async (ctx: any, next: any) => await wrapper(ctx, next, userPlugin)) app.use(async (ctx: any, next: any) => { if (ctx.path.startsWith('/static')) {