2022-11-03 07:56:27 +00:00
|
|
|
import { Plugin, PluginManager } from '@nocobase/server';
|
2023-04-03 08:45:53 +00:00
|
|
|
import fs from 'fs';
|
2022-03-19 11:28:53 +00:00
|
|
|
import send from 'koa-send';
|
|
|
|
import serve from 'koa-static';
|
2023-01-13 02:55:04 +00:00
|
|
|
import isEmpty from 'lodash/isEmpty';
|
2022-05-18 16:40:55 +00:00
|
|
|
import { isAbsolute, resolve } from 'path';
|
2023-01-13 02:55:04 +00:00
|
|
|
import { getAntdLocale } from './antd';
|
|
|
|
import { getCronLocale } from './cron';
|
|
|
|
import { getCronstrueLocale } from './cronstrue';
|
|
|
|
import { getMomentLocale } from './moment-locale';
|
|
|
|
import { getResourceLocale } from './resource';
|
2022-02-11 10:13:14 +00:00
|
|
|
|
2023-04-03 08:45:53 +00:00
|
|
|
async function getReadMe(name: string, locale: string) {
|
|
|
|
const packageName = PluginManager.getPackageName(name);
|
|
|
|
const dir = resolve(process.cwd(), 'node_modules', packageName);
|
2023-04-25 05:12:14 +00:00
|
|
|
const files = [resolve(dir, `README.${locale}.md`), resolve(dir, `README.md`)];
|
2023-04-12 04:24:09 +00:00
|
|
|
const file = files.find((file) => {
|
|
|
|
return fs.existsSync(file);
|
|
|
|
});
|
|
|
|
return file ? (await fs.promises.readFile(file)).toString() : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getTabs(name: string, locale: string) {
|
|
|
|
const packageName = PluginManager.getPackageName(name);
|
|
|
|
const dir = resolve(process.cwd(), 'node_modules', packageName);
|
2023-04-25 05:12:14 +00:00
|
|
|
const file = resolve(dir, 'docs', locale, 'tabs.json');
|
2023-04-12 04:24:09 +00:00
|
|
|
if (!fs.existsSync(file)) {
|
|
|
|
// TODO: compatible README, remove it in all plugin has tabs.json
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
title: 'README',
|
|
|
|
path: '__README__',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return JSON.parse((await fs.promises.readFile(file)).toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TabInfoParams {
|
|
|
|
filterByTk: string;
|
|
|
|
path: string;
|
|
|
|
locale: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getTabInfo({ filterByTk, path, locale }: TabInfoParams) {
|
|
|
|
const packageName = PluginManager.getPackageName(filterByTk);
|
|
|
|
const dir = resolve(process.cwd(), 'node_modules', packageName);
|
|
|
|
if (path === '__README__') {
|
|
|
|
return await getReadMe(filterByTk, locale);
|
2023-04-03 08:45:53 +00:00
|
|
|
}
|
2023-04-12 04:24:09 +00:00
|
|
|
const files = [
|
|
|
|
resolve(dir, 'docs', locale, `${path}.md`),
|
|
|
|
// default
|
|
|
|
resolve(dir, 'docs', 'en-US', `${path}.md`),
|
|
|
|
resolve(dir, 'docs', 'zh-CN', `${path}.md`),
|
|
|
|
];
|
|
|
|
const file = files.find((file) => {
|
|
|
|
return fs.existsSync(file);
|
|
|
|
});
|
|
|
|
return file ? (await fs.promises.readFile(file)).toString() : '';
|
2023-04-03 08:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getLang(ctx) {
|
|
|
|
const SystemSetting = ctx.db.getRepository('systemSettings');
|
|
|
|
const systemSetting = await SystemSetting.findOne();
|
|
|
|
const enabledLanguages: string[] = systemSetting.get('enabledLanguages') || [];
|
|
|
|
const currentUser = ctx.state.currentUser;
|
|
|
|
let lang = enabledLanguages?.[0] || process.env.APP_LANG || 'en-US';
|
|
|
|
if (enabledLanguages.includes(currentUser?.appLang)) {
|
|
|
|
lang = currentUser?.appLang;
|
|
|
|
}
|
|
|
|
if (ctx.request.query.locale) {
|
|
|
|
lang = ctx.request.query.locale;
|
|
|
|
}
|
|
|
|
return lang;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
2023-06-21 03:02:49 +00:00
|
|
|
|
|
|
|
this.db.on('systemSettings.beforeCreate', async (instance, { transaction }) => {
|
|
|
|
const uiSchemas = this.db.getRepository<any>('uiSchemas');
|
|
|
|
const schema = await uiSchemas.insert(
|
|
|
|
{
|
|
|
|
type: 'void',
|
|
|
|
'x-component': 'Menu',
|
|
|
|
'x-designer': 'Menu.Designer',
|
|
|
|
'x-initializer': 'MenuItemInitializers',
|
|
|
|
'x-component-props': {
|
|
|
|
mode: 'mix',
|
|
|
|
theme: 'dark',
|
|
|
|
// defaultSelectedUid: 'u8',
|
|
|
|
onSelect: '{{ onSelect }}',
|
|
|
|
sideMenuRefScopeKey: 'sideMenuRef',
|
|
|
|
},
|
|
|
|
properties: {},
|
|
|
|
},
|
|
|
|
{ transaction },
|
|
|
|
);
|
|
|
|
instance.set('options.adminSchemaUid', schema['x-uid']);
|
|
|
|
});
|
2022-02-11 10:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2023-06-21 03:02:49 +00:00
|
|
|
this.db.addMigrations({
|
|
|
|
namespace: 'client',
|
|
|
|
directory: resolve(__dirname, './migrations'),
|
|
|
|
context: {
|
|
|
|
plugin: this,
|
|
|
|
},
|
|
|
|
});
|
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');
|
2023-04-03 08:45:53 +00:00
|
|
|
this.app.acl.allow('plugins', '*', 'public');
|
2023-05-19 12:34:22 +00:00
|
|
|
this.app.acl.registerSnippet({
|
|
|
|
name: 'app',
|
2023-05-22 09:16:12 +00:00
|
|
|
actions: ['app:reboot', 'app:clearCache'],
|
2023-05-19 12:34:22 +00:00
|
|
|
});
|
2022-11-16 04:53:58 +00:00
|
|
|
const dialect = this.app.db.sequelize.getDialect();
|
2023-01-13 02:55:04 +00:00
|
|
|
const locales = require('./locale').default;
|
2023-05-19 12:34:22 +00:00
|
|
|
const restartMark = resolve(process.cwd(), 'storage', 'restart');
|
|
|
|
this.app.on('beforeStart', async () => {
|
|
|
|
if (fs.existsSync(restartMark)) {
|
|
|
|
fs.unlinkSync(restartMark);
|
|
|
|
}
|
|
|
|
});
|
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,
|
2023-03-19 15:40:11 +00:00
|
|
|
theme: currentUser?.systemSettings?.theme || systemSetting?.options?.theme || 'default',
|
2022-05-18 16:40:55 +00:00
|
|
|
};
|
|
|
|
await next();
|
|
|
|
},
|
2022-02-11 10:13:14 +00:00
|
|
|
async getLang(ctx, next) {
|
2023-04-03 08:45:53 +00:00
|
|
|
const lang = await getLang(ctx);
|
2023-01-13 02:55:04 +00:00
|
|
|
if (isEmpty(locales[lang])) {
|
|
|
|
locales[lang] = {};
|
|
|
|
}
|
|
|
|
if (isEmpty(locales[lang].resources)) {
|
|
|
|
locales[lang].resources = await getResourceLocale(lang, ctx.db);
|
|
|
|
}
|
|
|
|
if (isEmpty(locales[lang].antd)) {
|
|
|
|
locales[lang].antd = getAntdLocale(lang);
|
|
|
|
}
|
|
|
|
if (isEmpty(locales[lang].cronstrue)) {
|
|
|
|
locales[lang].cronstrue = getCronstrueLocale(lang);
|
|
|
|
}
|
|
|
|
if (isEmpty(locales[lang].cron)) {
|
|
|
|
locales[lang].cron = getCronLocale(lang);
|
|
|
|
}
|
2022-02-11 10:13:14 +00:00
|
|
|
ctx.body = {
|
2022-06-10 00:38:24 +00:00
|
|
|
lang,
|
2023-01-13 02:55:04 +00:00
|
|
|
moment: getMomentLocale(lang),
|
|
|
|
...locales[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();
|
|
|
|
},
|
2023-05-22 09:16:12 +00:00
|
|
|
async clearCache(ctx, next) {
|
|
|
|
await ctx.cache.reset();
|
2023-05-19 12:34:22 +00:00
|
|
|
await next();
|
|
|
|
},
|
|
|
|
reboot(ctx) {
|
|
|
|
const RESTART_CODE = 100;
|
|
|
|
process.on('exit', (code) => {
|
|
|
|
if (code === RESTART_CODE && process.env.APP_ENV === 'production') {
|
|
|
|
fs.writeFileSync(restartMark, '1');
|
|
|
|
console.log('Restart mark created.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ctx.app.on('afterStop', () => {
|
|
|
|
// Exit with code 100 will restart the process
|
|
|
|
process.exit(RESTART_CODE);
|
|
|
|
});
|
|
|
|
ctx.app.stop();
|
|
|
|
},
|
2022-02-11 10:13:14 +00:00
|
|
|
},
|
|
|
|
});
|
2022-05-14 00:56:30 +00:00
|
|
|
this.app.resource({
|
|
|
|
name: 'plugins',
|
|
|
|
actions: {
|
2023-04-03 08:45:53 +00:00
|
|
|
async getInfo(ctx, next) {
|
|
|
|
const lang = await getLang(ctx);
|
|
|
|
const { filterByTk } = ctx.action.params;
|
|
|
|
ctx.body = {
|
|
|
|
filterByTk,
|
|
|
|
readMe: await getReadMe(filterByTk, lang),
|
|
|
|
};
|
|
|
|
await next();
|
|
|
|
},
|
2023-04-12 04:24:09 +00:00
|
|
|
async getTabs(ctx, next) {
|
|
|
|
const lang = await getLang(ctx);
|
|
|
|
const { filterByTk } = ctx.action.params;
|
|
|
|
ctx.body = {
|
|
|
|
filterByTk,
|
|
|
|
tabs: await getTabs(filterByTk, lang),
|
|
|
|
};
|
|
|
|
await next();
|
|
|
|
},
|
|
|
|
async getTabInfo(ctx, next) {
|
|
|
|
const locale = await getLang(ctx);
|
|
|
|
const { filterByTk } = ctx.action.params;
|
|
|
|
ctx.body = {
|
|
|
|
filterByTk,
|
|
|
|
content: await getTabInfo({ ...(ctx.action.params as any), locale }),
|
|
|
|
};
|
|
|
|
await next();
|
|
|
|
},
|
2022-05-14 00:56:30 +00:00
|
|
|
},
|
|
|
|
});
|
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-12-02 06:23:07 +00:00
|
|
|
this.app.use(
|
|
|
|
async (ctx, 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 });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ tag: 'clientStatic', before: 'cors' },
|
|
|
|
);
|
2022-09-02 03:44:22 +00:00
|
|
|
}
|
2022-02-11 10:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ClientPlugin;
|