2022-11-03 07:56:27 +00:00
|
|
|
import { Plugin, PluginManager } from '@nocobase/server';
|
2022-03-19 11:28:53 +00:00
|
|
|
import send from 'koa-send';
|
|
|
|
import serve from 'koa-static';
|
2022-05-18 16:40:55 +00:00
|
|
|
import { isAbsolute, resolve } from 'path';
|
2022-02-11 10:13:14 +00:00
|
|
|
|
|
|
|
export class ClientPlugin extends Plugin {
|
|
|
|
async beforeLoad() {
|
2022-05-18 16:40:55 +00:00
|
|
|
// const cmd = this.app.findCommand('install');
|
|
|
|
// if (cmd) {
|
|
|
|
// cmd.option('--import-demo');
|
|
|
|
// }
|
2022-02-11 10:13:14 +00:00
|
|
|
this.app.on('afterInstall', async (app, options) => {
|
|
|
|
const [opts] = options?.cliArgs || [{}];
|
|
|
|
if (opts?.importDemo) {
|
2022-04-24 15:17:42 +00:00
|
|
|
//
|
2022-02-11 10:13:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2022-05-17 15:39:09 +00:00
|
|
|
this.app.acl.allow('app', 'getLang');
|
2022-06-17 02:25:59 +00:00
|
|
|
this.app.acl.allow('app', 'getInfo');
|
2022-09-18 06:10:01 +00:00
|
|
|
this.app.acl.allow('app', 'getPlugins');
|
2022-05-17 15:39:09 +00:00
|
|
|
this.app.acl.allow('plugins', 'getPinned', 'loggedIn');
|
2022-11-16 04:53:58 +00:00
|
|
|
const dialect = this.app.db.sequelize.getDialect();
|
2022-02-11 10:13:14 +00:00
|
|
|
this.app.resource({
|
|
|
|
name: 'app',
|
|
|
|
actions: {
|
2022-05-18 16:40:55 +00:00
|
|
|
async getInfo(ctx, next) {
|
|
|
|
const SystemSetting = ctx.db.getRepository('systemSettings');
|
|
|
|
const systemSetting = await SystemSetting.findOne();
|
2022-06-10 00:38:24 +00:00
|
|
|
const enabledLanguages: string[] = systemSetting.get('enabledLanguages') || [];
|
2022-05-18 16:40:55 +00:00
|
|
|
const currentUser = ctx.state.currentUser;
|
2022-07-14 12:57:26 +00:00
|
|
|
let lang = enabledLanguages?.[0] || process.env.APP_LANG || 'en-US';
|
2022-06-10 00:38:24 +00:00
|
|
|
if (enabledLanguages.includes(currentUser?.appLang)) {
|
|
|
|
lang = currentUser?.appLang;
|
|
|
|
}
|
2022-05-18 16:40:55 +00:00
|
|
|
ctx.body = {
|
2022-11-16 04:53:58 +00:00
|
|
|
database: {
|
|
|
|
dialect,
|
|
|
|
},
|
2022-06-17 02:25:59 +00:00
|
|
|
version: await ctx.app.version.get(),
|
2022-06-10 00:38:24 +00:00
|
|
|
lang,
|
2022-05-18 16:40:55 +00:00
|
|
|
};
|
|
|
|
await next();
|
|
|
|
},
|
2022-02-11 10:13:14 +00:00
|
|
|
async getLang(ctx, next) {
|
2022-03-21 13:37:35 +00:00
|
|
|
const SystemSetting = ctx.db.getRepository('systemSettings');
|
|
|
|
const systemSetting = await SystemSetting.findOne();
|
2022-06-10 00:38:24 +00:00
|
|
|
const enabledLanguages: string[] = systemSetting.get('enabledLanguages') || [];
|
2022-03-21 13:37:35 +00:00
|
|
|
const currentUser = ctx.state.currentUser;
|
2022-07-14 12:57:26 +00:00
|
|
|
let lang = enabledLanguages?.[0] || process.env.APP_LANG || 'en-US';
|
2022-06-10 00:38:24 +00:00
|
|
|
if (enabledLanguages.includes(currentUser?.appLang)) {
|
|
|
|
lang = currentUser?.appLang;
|
|
|
|
}
|
2022-02-11 10:13:14 +00:00
|
|
|
ctx.body = {
|
2022-06-10 00:38:24 +00:00
|
|
|
lang,
|
2022-02-11 10:13:14 +00:00
|
|
|
};
|
|
|
|
await next();
|
|
|
|
},
|
2022-09-18 06:10:01 +00:00
|
|
|
async getPlugins(ctx, next) {
|
|
|
|
const pm = ctx.db.getRepository('applicationPlugins');
|
|
|
|
const items = await pm.find({
|
|
|
|
filter: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
ctx.body = items
|
|
|
|
.filter((item) => {
|
|
|
|
try {
|
2022-11-03 07:56:27 +00:00
|
|
|
const packageName = PluginManager.getPackageName(item.name);
|
|
|
|
require.resolve(`${packageName}/client`);
|
2022-09-18 06:10:01 +00:00
|
|
|
return true;
|
|
|
|
} catch (error) {}
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
.map((item) => item.name);
|
|
|
|
await next();
|
|
|
|
},
|
2022-02-11 10:13:14 +00:00
|
|
|
},
|
|
|
|
});
|
2022-05-14 00:56:30 +00:00
|
|
|
this.app.resource({
|
|
|
|
name: 'plugins',
|
|
|
|
actions: {
|
|
|
|
// TODO: 临时
|
|
|
|
async getPinned(ctx, next) {
|
|
|
|
ctx.body = [
|
2022-09-18 06:10:01 +00:00
|
|
|
{ component: 'CollectionManagerShortcut' },
|
2022-05-14 00:56:30 +00:00
|
|
|
{ component: 'ACLShortcut' },
|
|
|
|
{ component: 'WorkflowShortcut' },
|
|
|
|
{ component: 'SchemaTemplateShortcut' },
|
|
|
|
{ component: 'SystemSettingsShortcut' },
|
|
|
|
{ component: 'FileStorageShortcut' },
|
|
|
|
];
|
|
|
|
await next();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2022-05-18 16:40:55 +00:00
|
|
|
let root = this.options.dist || `./packages/app/client/dist`;
|
|
|
|
if (!isAbsolute(root)) {
|
2022-03-19 11:28:53 +00:00
|
|
|
root = resolve(process.cwd(), root);
|
|
|
|
}
|
2022-09-02 03:44:22 +00:00
|
|
|
if (process.env.APP_ENV !== 'production' && root) {
|
2022-09-29 13:05:31 +00:00
|
|
|
this.app.middleware.nodes.unshift(async (ctx, next) => {
|
2022-09-02 03:44:22 +00:00
|
|
|
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 });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-02-11 10:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ClientPlugin;
|