tachybase_todo/packages/plugins/client/src/plugin.ts
ChengLei Shao 5df11c58c7
fix: 204 no content response (#378)
* fix: 204 no content response

* Update data-wrapping.ts

* Update plugin.ts

Co-authored-by: chenos <chenlinxh@gmail.com>
2022-05-22 14:48:50 +08:00

93 lines
2.8 KiB
TypeScript

import { Plugin } from '@nocobase/server';
import send from 'koa-send';
import serve from 'koa-static';
import { isAbsolute, resolve } from 'path';
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) {
//
}
});
}
async load() {
this.app.acl.allow('app', 'getLang');
this.app.acl.allow('plugins', 'getPinned', 'loggedIn');
this.app.resource({
name: 'app',
actions: {
async getInfo(ctx, next) {
const SystemSetting = ctx.db.getRepository('systemSettings');
const systemSetting = await SystemSetting.findOne();
const currentUser = ctx.state.currentUser;
ctx.body = {
version: this.app.getVersion(),
lang: currentUser?.appLang || systemSetting?.appLang || process.env.APP_LANG || 'en-US',
};
await next();
},
async getLang(ctx, next) {
const SystemSetting = ctx.db.getRepository('systemSettings');
const systemSetting = await SystemSetting.findOne();
const currentUser = ctx.state.currentUser;
ctx.body = {
lang: currentUser?.appLang || systemSetting?.appLang || process.env.APP_LANG || 'en-US',
};
await next();
},
},
});
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();
},
},
});
let root = this.options.dist || `./packages/app/client/dist`;
if (!isAbsolute(root)) {
root = resolve(process.cwd(), root);
}
this.app.middleware.unshift(async (ctx, next) => {
if (process.env.APP_ENV === 'production') {
return next();
}
if (!root) {
return next();
}
if (ctx.path.startsWith(this.app.resourcer.options.prefix)) {
return next();
}
await serve(root)(ctx, next);
// console.log('koa-send', root, ctx.status);
if (ctx.status == 404) {
return send(ctx, 'index.html', { root });
}
});
}
getName(): string {
return this.getPackageName(__dirname);
}
}
export default ClientPlugin;