2020-11-11 18:16:18 +08:00
|
|
|
import Koa from 'koa';
|
2021-09-14 11:09:26 +08:00
|
|
|
import { Command, CommandOptions } from 'commander';
|
|
|
|
import Database, { DatabaseOptions, TableOptions } from '@nocobase/database';
|
|
|
|
import Resourcer, { ResourceOptions } from '@nocobase/resourcer';
|
|
|
|
import { PluginType, Plugin, PluginOptions } from './plugin';
|
2021-09-16 00:38:48 +08:00
|
|
|
import { registerActions } from '@nocobase/actions';
|
2021-10-18 12:49:37 +08:00
|
|
|
import {
|
|
|
|
createCli,
|
2021-11-08 19:32:59 +08:00
|
|
|
createI18n,
|
2021-10-18 12:49:37 +08:00
|
|
|
createDatabase,
|
|
|
|
createResourcer,
|
|
|
|
registerMiddlewares,
|
|
|
|
} from './helper';
|
2021-11-08 19:32:59 +08:00
|
|
|
import { i18n, InitOptions } from 'i18next';
|
2021-09-09 13:05:33 +08:00
|
|
|
|
|
|
|
export interface ResourcerOptions {
|
|
|
|
prefix?: string;
|
|
|
|
}
|
2020-11-12 11:48:27 +08:00
|
|
|
|
|
|
|
export interface ApplicationOptions {
|
2021-09-09 13:05:33 +08:00
|
|
|
database?: DatabaseOptions;
|
|
|
|
resourcer?: ResourcerOptions;
|
|
|
|
bodyParser?: any;
|
|
|
|
cors?: any;
|
|
|
|
dataWrapping?: boolean;
|
2021-10-18 12:49:37 +08:00
|
|
|
registerActions?: boolean;
|
2021-11-08 19:32:59 +08:00
|
|
|
i18n?: i18n | InitOptions;
|
2020-11-12 11:48:27 +08:00
|
|
|
}
|
2020-11-11 18:16:18 +08:00
|
|
|
|
2021-09-16 15:57:21 +08:00
|
|
|
interface DefaultState {
|
|
|
|
currentUser?: any;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DefaultContext {
|
|
|
|
db: Database;
|
|
|
|
resourcer: Resourcer;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface MiddlewareOptions {
|
|
|
|
name?: string;
|
|
|
|
resourceName?: string;
|
|
|
|
resourceNames?: string[];
|
|
|
|
insertBefore?: string;
|
|
|
|
insertAfter?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ActionsOptions {
|
|
|
|
resourceName?: string;
|
|
|
|
resourceNames?: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Application<
|
|
|
|
StateT = DefaultState,
|
2021-10-18 12:49:37 +08:00
|
|
|
ContextT = DefaultContext
|
2021-10-01 23:31:49 +08:00
|
|
|
> extends Koa {
|
2021-09-14 11:09:26 +08:00
|
|
|
public readonly db: Database;
|
2021-01-13 16:23:15 +08:00
|
|
|
|
|
|
|
public readonly resourcer: Resourcer;
|
2020-11-11 18:16:18 +08:00
|
|
|
|
2021-09-05 23:59:38 +08:00
|
|
|
public readonly cli: Command;
|
|
|
|
|
2021-11-08 19:32:59 +08:00
|
|
|
public readonly i18n: i18n;
|
|
|
|
|
2021-09-14 11:09:26 +08:00
|
|
|
protected plugins = new Map<string, Plugin>();
|
2020-11-12 11:48:27 +08:00
|
|
|
|
2021-01-13 16:23:15 +08:00
|
|
|
constructor(options: ApplicationOptions) {
|
|
|
|
super();
|
2021-09-09 13:05:33 +08:00
|
|
|
|
2021-10-18 12:49:37 +08:00
|
|
|
this.db = createDatabase(options);
|
|
|
|
this.resourcer = createResourcer(options);
|
|
|
|
this.cli = createCli(this, options);
|
2021-11-08 19:32:59 +08:00
|
|
|
this.i18n = createI18n(options);
|
2021-09-05 23:59:38 +08:00
|
|
|
|
2021-10-18 12:49:37 +08:00
|
|
|
registerMiddlewares(this, options);
|
|
|
|
if (options.registerActions !== false) {
|
|
|
|
registerActions(this);
|
2021-09-09 13:05:33 +08:00
|
|
|
}
|
2020-12-18 19:54:53 +08:00
|
|
|
}
|
|
|
|
|
2021-09-16 15:57:21 +08:00
|
|
|
use<NewStateT = {}, NewContextT = {}>(
|
|
|
|
middleware: Koa.Middleware<StateT & NewStateT, ContextT & NewContextT>,
|
|
|
|
options?: MiddlewareOptions,
|
2021-09-23 00:16:04 +08:00
|
|
|
) {
|
2021-09-16 15:57:21 +08:00
|
|
|
// @ts-ignore
|
|
|
|
return super.use(middleware);
|
|
|
|
}
|
|
|
|
|
2021-09-14 11:09:26 +08:00
|
|
|
collection(options: TableOptions) {
|
|
|
|
return this.db.table(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
resource(options: ResourceOptions) {
|
|
|
|
return this.resourcer.define(options);
|
|
|
|
}
|
|
|
|
|
2021-09-16 15:57:21 +08:00
|
|
|
actions(handlers: any, options?: ActionsOptions) {
|
2021-09-16 00:38:48 +08:00
|
|
|
return this.resourcer.registerActions(handlers);
|
|
|
|
}
|
|
|
|
|
2021-09-14 11:09:26 +08:00
|
|
|
command(nameAndArgs: string, opts?: CommandOptions) {
|
|
|
|
return this.cli.command(nameAndArgs, opts);
|
|
|
|
}
|
|
|
|
|
2021-10-18 12:49:37 +08:00
|
|
|
findCommand(name: string): Command {
|
|
|
|
return (this.cli as any)._findCommand(name);
|
|
|
|
}
|
|
|
|
|
2021-10-01 23:31:49 +08:00
|
|
|
plugin(options?: PluginType | PluginOptions, ext?: PluginOptions): Plugin {
|
2021-09-23 00:16:04 +08:00
|
|
|
if (typeof options === 'string') {
|
2021-10-01 23:31:49 +08:00
|
|
|
return this.plugin(require(options).default, ext);
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
|
|
|
let instance: Plugin;
|
2021-09-23 00:16:04 +08:00
|
|
|
if (typeof options === 'function') {
|
|
|
|
try {
|
2021-09-14 11:09:26 +08:00
|
|
|
// @ts-ignore
|
2021-09-23 00:16:04 +08:00
|
|
|
instance = new options({
|
|
|
|
name: options.name,
|
2021-10-01 23:31:49 +08:00
|
|
|
...ext,
|
2021-09-23 00:16:04 +08:00
|
|
|
app: this,
|
|
|
|
});
|
|
|
|
if (!(instance instanceof Plugin)) {
|
|
|
|
throw new Error('plugin must be instanceof Plugin');
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// console.log(err);
|
|
|
|
instance = new Plugin({
|
|
|
|
name: options.name,
|
2021-10-01 23:31:49 +08:00
|
|
|
...ext,
|
2021-09-23 00:16:04 +08:00
|
|
|
// @ts-ignore
|
|
|
|
load: options,
|
2021-09-14 11:09:26 +08:00
|
|
|
app: this,
|
|
|
|
});
|
|
|
|
}
|
2021-09-23 00:16:04 +08:00
|
|
|
} else if (typeof options === 'object') {
|
|
|
|
const plugin = options.plugin || Plugin;
|
|
|
|
instance = new plugin({
|
|
|
|
name: options.plugin ? plugin.name : undefined,
|
2021-09-14 11:09:26 +08:00
|
|
|
...options,
|
2021-10-01 23:31:49 +08:00
|
|
|
...ext,
|
2021-09-14 11:09:26 +08:00
|
|
|
app: this,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const name = instance.getName();
|
|
|
|
if (this.plugins.has(name)) {
|
|
|
|
throw new Error(`plugin name [${name}] is repeated`);
|
|
|
|
}
|
|
|
|
this.plugins.set(name, instance);
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
await this.emitAsync('plugins.beforeLoad');
|
|
|
|
for (const [name, plugin] of this.plugins) {
|
|
|
|
await this.emitAsync(`plugins.${name}.beforeLoad`);
|
|
|
|
await plugin.load();
|
|
|
|
await this.emitAsync(`plugins.${name}.afterLoad`);
|
|
|
|
}
|
|
|
|
await this.emitAsync('plugins.afterLoad');
|
|
|
|
}
|
|
|
|
|
2021-09-05 14:17:45 +08:00
|
|
|
async emitAsync(event: string | symbol, ...args: any[]): Promise<boolean> {
|
|
|
|
// @ts-ignore
|
|
|
|
const events = this._events;
|
|
|
|
let callbacks = events[event];
|
|
|
|
if (!callbacks) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// helper function to reuse as much code as possible
|
|
|
|
const run = (cb) => {
|
|
|
|
switch (args.length) {
|
|
|
|
// fast cases
|
|
|
|
case 0:
|
|
|
|
cb = cb.call(this);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
cb = cb.call(this, args[0]);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
cb = cb.call(this, args[0], args[1]);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
cb = cb.call(this, args[0], args[1], args[2]);
|
|
|
|
break;
|
|
|
|
// slower
|
|
|
|
default:
|
|
|
|
cb = cb.apply(this, args);
|
|
|
|
}
|
|
|
|
|
2021-09-09 13:05:33 +08:00
|
|
|
if (cb && (cb instanceof Promise || typeof cb.then === 'function')) {
|
2021-09-05 14:17:45 +08:00
|
|
|
return cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof callbacks === 'function') {
|
|
|
|
await run(callbacks);
|
|
|
|
} else if (typeof callbacks === 'object') {
|
|
|
|
callbacks = callbacks.slice().filter(Boolean);
|
|
|
|
await callbacks.reduce((prev, next) => {
|
|
|
|
return prev.then((res) => {
|
2021-09-09 13:05:33 +08:00
|
|
|
return run(next).then((result) =>
|
|
|
|
Promise.resolve(res.concat(result)),
|
|
|
|
);
|
2021-09-05 14:17:45 +08:00
|
|
|
});
|
|
|
|
}, Promise.resolve([]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-16 00:38:48 +08:00
|
|
|
async parse(argv = process.argv) {
|
2021-09-28 00:18:09 +08:00
|
|
|
await this.load();
|
2021-09-05 23:59:38 +08:00
|
|
|
return this.cli.parseAsync(argv);
|
2020-12-18 19:54:53 +08:00
|
|
|
}
|
|
|
|
|
2021-09-09 13:05:33 +08:00
|
|
|
async destroy() {
|
2021-09-14 11:09:26 +08:00
|
|
|
await this.db.close();
|
2021-09-09 13:05:33 +08:00
|
|
|
}
|
2020-11-11 18:16:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Application;
|