2022-02-11 15:59:03 +00:00
|
|
|
import { skip } from '@nocobase/acl';
|
2022-02-11 10:13:14 +00:00
|
|
|
import { Plugin } from '@nocobase/server';
|
2022-03-19 11:28:53 +00:00
|
|
|
import send from 'koa-send';
|
|
|
|
import serve from 'koa-static';
|
|
|
|
import { resolve } from 'path';
|
2022-02-11 10:13:14 +00:00
|
|
|
|
|
|
|
export class ClientPlugin extends Plugin {
|
|
|
|
async beforeLoad() {
|
|
|
|
const cmd = this.app.findCommand('install');
|
|
|
|
if (cmd) {
|
|
|
|
cmd.option('--import-demo');
|
|
|
|
cmd.option('--lang [lang]');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.app.on('afterInstall', async (app, options) => {
|
|
|
|
const [opts] = options?.cliArgs || [{}];
|
|
|
|
if (opts?.importDemo) {
|
|
|
|
}
|
|
|
|
if (opts?.lang) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2022-02-11 15:59:03 +00:00
|
|
|
this.app.acl.use(
|
|
|
|
skip({
|
|
|
|
resourceName: 'app',
|
|
|
|
actionName: 'getLang',
|
|
|
|
}),
|
|
|
|
);
|
2022-02-11 10:13:14 +00:00
|
|
|
this.app.resource({
|
|
|
|
name: 'app',
|
|
|
|
actions: {
|
|
|
|
async getLang(ctx, next) {
|
|
|
|
ctx.body = {
|
|
|
|
lang: 'zh-CN',
|
|
|
|
};
|
|
|
|
await next();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2022-03-19 11:28:53 +00: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 10:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ClientPlugin;
|