add yargs
This commit is contained in:
parent
b30f9bf7d1
commit
1ca32341fa
@ -16,7 +16,7 @@ import { AppInterceptor } from '../providers/app.interceptor';
|
|||||||
import { AppPipe } from '../providers/app.pipe';
|
import { AppPipe } from '../providers/app.pipe';
|
||||||
import { App, AppConfig, CreateOptions } from '../types';
|
import { App, AppConfig, CreateOptions } from '../types';
|
||||||
|
|
||||||
import { CreateModule } from './utils';
|
import { createCommands, CreateModule } from './utils';
|
||||||
|
|
||||||
export const app: App = { configure: new Configure(), commands: [] };
|
export const app: App = { configure: new Configure(), commands: [] };
|
||||||
|
|
||||||
@ -34,6 +34,7 @@ export const createApp = (options: CreateOptions) => async (): Promise<App> => {
|
|||||||
app.container = await builder({ configure: app.configure, BootModule });
|
app.container = await builder({ configure: app.configure, BootModule });
|
||||||
|
|
||||||
useContainer(app.container.select(BootModule), { fallbackOnErrors: true });
|
useContainer(app.container.select(BootModule), { fallbackOnErrors: true });
|
||||||
|
app.commands = await createCommands(options.commands, app as Required<App>);
|
||||||
return app;
|
return app;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -3,7 +3,9 @@ import chalk from 'chalk';
|
|||||||
import deepmerge from 'deepmerge';
|
import deepmerge from 'deepmerge';
|
||||||
import { isNil } from 'lodash';
|
import { isNil } from 'lodash';
|
||||||
|
|
||||||
import { PanicOption } from '../types';
|
import { Arguments, CommandModule } from 'yargs';
|
||||||
|
|
||||||
|
import { App, CommandCollection, PanicOption } from '../types';
|
||||||
|
|
||||||
export function toBoolean(value?: string | boolean): boolean {
|
export function toBoolean(value?: string | boolean): boolean {
|
||||||
if (isNil(value)) {
|
if (isNil(value)) {
|
||||||
@ -14,7 +16,7 @@ export function toBoolean(value?: string | boolean): boolean {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return JSON.parse(value.toLowerCase());
|
return JSON.parse(value.toLowerCase());
|
||||||
} catch (error) {
|
} catch {
|
||||||
return value as unknown as boolean;
|
return value as unknown as boolean;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -41,7 +43,7 @@ export function isAsyncFunction<T, P extends Array<any>>(
|
|||||||
callback: (...args: P) => T | Promise<T>,
|
callback: (...args: P) => T | Promise<T>,
|
||||||
): callback is (...args: P) => Promise<T> {
|
): callback is (...args: P) => Promise<T> {
|
||||||
const AsyncFunction = (async () => {}).constructor;
|
const AsyncFunction = (async () => {}).constructor;
|
||||||
return callback instanceof AsyncFunction === true;
|
return callback instanceof AsyncFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CreateModule(
|
export function CreateModule(
|
||||||
@ -82,3 +84,21 @@ export async function panic(option: PanicOption | string) {
|
|||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function createCommands(
|
||||||
|
factory: () => CommandCollection,
|
||||||
|
app: Required<App>,
|
||||||
|
): Promise<CommandModule<any, any>[]> {
|
||||||
|
const collection: CommandCollection = [...factory()];
|
||||||
|
const commands = await Promise.all(collection.map(async (command) => command(app)));
|
||||||
|
return commands.map((command) => ({
|
||||||
|
...command,
|
||||||
|
handler: async (args: Arguments<RecordAny>) => {
|
||||||
|
await app.container.close();
|
||||||
|
await command.handler(args);
|
||||||
|
if (command.instant) {
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
@ -34,6 +34,8 @@ export interface CreateOptions {
|
|||||||
|
|
||||||
storage: ConfigStorageOption;
|
storage: ConfigStorageOption;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
commands: () => CommandCollection;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ContainerBuilder {
|
export interface ContainerBuilder {
|
||||||
|
@ -17,6 +17,7 @@ import { RestfulModule } from './modules/restful/restful.module';
|
|||||||
import { ApiConfig } from './modules/restful/types';
|
import { ApiConfig } from './modules/restful/types';
|
||||||
|
|
||||||
export const createOptions: CreateOptions = {
|
export const createOptions: CreateOptions = {
|
||||||
|
commands: () => [],
|
||||||
config: { factories: configs as any, storage: { enable: true } },
|
config: { factories: configs as any, storage: { enable: true } },
|
||||||
modules: async (configure) => [
|
modules: async (configure) => [
|
||||||
DatabaseModule.forRoot(configure),
|
DatabaseModule.forRoot(configure),
|
||||||
|
Loading…
Reference in New Issue
Block a user