nocobase/packages/plugins/client/src/server.ts
SemmyWong d831a9b889
feat: plugin export (#479)
* feat: init export plugin

* feat: add client export

* fix: fix the word spell

* feat: export plugin done

* feat: init export plugin

* feat: add client export

* fix: fix the word spell

* feat: export plugin done

* ci: change plugin-export version

* refactor: renders add ctx params

* fix: fix select and multipleSelect export

* fix: array convert string

* refactor: move SchemaInitializerPluginProvider

* fix: build error

* fix: change umijs config

* fix: update SchemaInitializerPluginProvider

* fix: import server

* fix: fix some bug

* fix: fix some bug

* refactor: export plugin refactor

* refactor: create all export fields by default

* fix: fix export plugin bug

* fix(plugin-collection-manager): uiSchema toJSON

* fix: update yarn.lock

* fix: fix init fields bug

* refactor: enum params pass by client

* fix: fix export table header title

* refactor: refactor dataIndex

* fix: fix dataIndex maybe complex object

* fix: add checkboxGroup in export plugin

* fix: add checkbox and i18n

* feat: improve code

Co-authored-by: chenos <chenlinxh@gmail.com>
2022-06-14 15:01:53 +08:00

103 lines
3.2 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 enabledLanguages: string[] = systemSetting.get('enabledLanguages') || [];
const currentUser = ctx.state.currentUser;
let lang = systemSetting?.appLang || process.env.APP_LANG || 'en-US';
if (enabledLanguages.includes(currentUser?.appLang)) {
lang = currentUser?.appLang;
}
ctx.body = {
version: this.app.getVersion(),
lang,
};
await next();
},
async getLang(ctx, next) {
const SystemSetting = ctx.db.getRepository('systemSettings');
const systemSetting = await SystemSetting.findOne();
const enabledLanguages: string[] = systemSetting.get('enabledLanguages') || [];
const currentUser = ctx.state.currentUser;
let lang = systemSetting?.appLang || process.env.APP_LANG || 'en-US';
if (enabledLanguages.includes(currentUser?.appLang)) {
lang = currentUser?.appLang;
}
ctx.body = {
lang,
};
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;