-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.factory.ts
More file actions
105 lines (98 loc) · 2.9 KB
/
program.factory.ts
File metadata and controls
105 lines (98 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* @file ProgramFactory
* @module nest-commander/ProgramFactory
*/
import {
cast,
defaults,
define,
fallback,
type Class
} from '@flex-development/tutils'
import type {
DynamicModule,
INestApplicationContext,
LogLevel
} from '@nestjs/common'
import type { NestApplicationContextOptions } from '@nestjs/common/interfaces/nest-application-context-options.interface'
import { NestFactory } from '@nestjs/core'
import CommandRunnerModule from './command-runner.module'
import type { ParseOptions } from './commander'
import type { CliApplicationContext } from './interfaces'
import { ProgramOptions } from './models'
import { CommandRunnerService } from './providers'
/**
* CLI program factory options.
*
* @see {@linkcode NestApplicationContextOptions}
* @see {@linkcode ProgramOptions}
*/
type ProgramFactoryOptions = NestApplicationContextOptions & ProgramOptions
/**
* CLI program factory.
*
* @class
*/
class ProgramFactory {
/**
* Create a CLI application context from a NestJS application context.
*
* @protected
* @static
*
* @param {INestApplicationContext} app - NestJS application context
* @return {CliApplicationContext} CLI application context
*/
protected static context(
app: INestApplicationContext
): CliApplicationContext {
define(app, 'run', {
/* c8 ignore next 7 */ value: async function run(
options: { close?: boolean } = {}
): Promise<void> {
await app.get(CommandRunnerService).run()
fallback(options.close, true) && (await app.close())
return void 0
}
})
define(app, 'runWith', {
/* c8 ignore next 9 */ value: async function runWith(
args: readonly string[] = [],
options: Partial<ParseOptions & { close?: boolean }> = {}
): Promise<void> {
options.from = fallback(options.from, 'user')
await app.get(CommandRunnerService).run(args, cast(options))
fallback(options.close, true) && (await app.close())
return void args
}
})
return cast<CliApplicationContext>(app)
}
/**
* Create a CLI application context.
*
* @see {@linkcode CliApplicationContext}
* @see {@linkcode DynamicModule}
* @see {@linkcode ProgramFactoryOptions}
*
* @public
* @static
* @async
*
* @param {Class<any> | DynamicModule} AppModule - Root module
* @param {ProgramFactoryOptions} [options={}] - Context options
* @return {Promise<CliApplicationContext>} CLI application context
*/
public static async create(
AppModule: Class<any> | DynamicModule,
options: ProgramFactoryOptions = {}
): Promise<CliApplicationContext> {
return this.context(
await NestFactory.createApplicationContext(
CommandRunnerModule.register(options, AppModule),
defaults(options, { logger: cast<LogLevel[]>(['error', 'warn']) })
)
)
}
}
export { ProgramFactory as default, type ProgramFactoryOptions }