1
+ import { type BaseGeneratorConstructor , type GeneratorMeta } from '@yeoman/types' ;
1
2
import YeomanCommand , { addEnvironmentOptions } from './util/command.js' ;
2
3
import { createEnv } from './index.js' ;
3
4
5
+ export type CommandPreparation = {
6
+ resolved ?: string ;
7
+ command ?: YeomanCommand ;
8
+ generator ?: BaseGeneratorConstructor ;
9
+ namespace ?: string ;
10
+ } ;
11
+
4
12
/**
5
13
* Prepare a commander instance for cli support.
6
14
*
7
15
* @param {Command } command - Command to be prepared
8
16
* @param generatorPath - Generator to create Command
9
17
* @return {Command } return command
10
18
*/
11
- export const prepareGeneratorCommand = async ( command : YeomanCommand , generatorPath : string , namespace ?: string ) => {
19
+ export const prepareGeneratorCommand = async ( {
20
+ command = addEnvironmentOptions ( new YeomanCommand ( ) ) ,
21
+ resolved,
22
+ generator,
23
+ namespace,
24
+ } : CommandPreparation ) => {
12
25
const env = createEnv ( ) ;
13
- const meta = env . register ( generatorPath , { namespace } ) ;
26
+ let meta : GeneratorMeta ;
27
+ if ( generator && namespace ) {
28
+ meta = env . register ( generator , { namespace, resolved } ) ;
29
+ } else if ( resolved ) {
30
+ meta = env . register ( resolved , { namespace } ) ;
31
+ } else {
32
+ throw new Error ( `A generator with namespace or a generator path is required` ) ;
33
+ }
34
+
14
35
command . env = env ;
15
36
command . registerGenerator ( await meta . instantiateHelp ( ) ) ;
16
37
command . action ( async function ( this : YeomanCommand ) {
@@ -32,7 +53,8 @@ export const prepareGeneratorCommand = async (command: YeomanCommand, generatorP
32
53
* @param generatorPaht - Generator to create Command
33
54
* @return Return a Command instance
34
55
*/
35
- export const prepareCommand = async ( generatorPath : string , command = new YeomanCommand ( ) ) => {
36
- command = addEnvironmentOptions ( command ) ;
37
- return prepareGeneratorCommand ( command , generatorPath ) ;
56
+ export const prepareCommand = async ( options : CommandPreparation ) => {
57
+ options . command = options . command ?? new YeomanCommand ( ) ;
58
+ addEnvironmentOptions ( options . command ) ;
59
+ return prepareGeneratorCommand ( options ) ;
38
60
} ;
0 commit comments