tachybase_todo/packages/plugins/client/src/plugin.ts

85 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-02-11 23:59:03 +08:00
import { skip } from '@nocobase/acl';
2022-02-11 18:13:14 +08:00
import { Plugin } from '@nocobase/server';
2022-03-19 19:28:53 +08:00
import send from 'koa-send';
import serve from 'koa-static';
import { resolve } from 'path';
2022-02-11 18:13:14 +08:00
export class ClientPlugin extends Plugin {
async beforeLoad() {
const cmd = this.app.findCommand('install');
if (cmd) {
cmd.option('--import-demo');
}
this.app.on('afterInstall', async (app, options) => {
const [opts] = options?.cliArgs || [{}];
if (opts?.importDemo) {
2022-04-24 23:17:42 +08:00
//
2022-02-11 18:13:14 +08:00
}
});
}
async load() {
2022-02-11 23:59:03 +08:00
this.app.acl.use(
skip({
resourceName: 'app',
actionName: 'getLang',
}),
);
2022-02-11 18:13:14 +08:00
this.app.resource({
name: 'app',
actions: {
async getLang(ctx, next) {
2022-03-21 21:37:35 +08:00
const SystemSetting = ctx.db.getRepository('systemSettings');
const systemSetting = await SystemSetting.findOne();
const currentUser = ctx.state.currentUser;
2022-02-11 18:13:14 +08:00
ctx.body = {
2022-03-21 21:37:35 +08:00
lang: currentUser?.appLang || systemSetting?.appLang || process.env.APP_LANG || 'en-US',
2022-02-11 18:13:14 +08:00
};
await next();
},
},
});
2022-05-14 08:56:30 +08:00
this.app.resource({
name: 'plugins',
actions: {
// TODO: 临时
async getPinned(ctx, next) {
ctx.body = [
{ component: 'DesignableSwitch', pin: true },
{ component: 'CollectionManagerShortcut', pin: true },
{ component: 'ACLShortcut' },
{ component: 'WorkflowShortcut' },
{ component: 'SchemaTemplateShortcut' },
{ component: 'SystemSettingsShortcut' },
{ component: 'FileStorageShortcut' },
];
await next();
},
},
});
2022-03-19 19:28:53 +08:00
let root = this.options.dist;
if (root && !root.startsWith('/')) {
root = resolve(process.cwd(), root);
}
this.app.middleware.unshift(async (ctx, next) => {
if (process.env.NOCOBASE_ENV === 'production') {
return next();
}
if (!root) {
return next();
}
await serve(root)(ctx, next);
// console.log('koa-send', root, ctx.status);
if (ctx.status == 404) {
return send(ctx, 'index.html', { root });
}
});
2022-02-11 18:13:14 +08:00
}
getName(): string {
return this.getPackageName(__dirname);
}
2022-02-11 18:13:14 +08:00
}
export default ClientPlugin;