@@ -12,17 +12,19 @@ import {
1212 Provider ,
1313 runInContext
1414} from "@tsed/di" ;
15- import { $asyncEmit } from "@tsed/hooks" ;
15+ import { $asyncAlter , $asyncEmit } from "@tsed/hooks" ;
16+ import { pascalCase } from "change-case" ;
1617import { Argument , Command } from "commander" ;
1718import Inquirer from "inquirer" ;
18- // @ts -ignore
1919import inquirer_autocomplete_prompt from "inquirer-autocomplete-prompt" ;
2020import { v4 } from "uuid" ;
2121
2222import { CommandStoreKeys } from "../domains/CommandStoreKeys.js" ;
23+ import type { CommandData } from "../interfaces/CommandData.js" ;
2324import type { CommandMetadata } from "../interfaces/CommandMetadata.js" ;
2425import type { CommandArg , CommandOptions } from "../interfaces/CommandParameters.js" ;
2526import type { CommandProvider } from "../interfaces/CommandProvider.js" ;
27+ import type { Task } from "../interfaces/index.js" ;
2628import { PackageManagersModule } from "../packageManagers/index.js" ;
2729import { createSubTasks , createTasksRunner } from "../utils/createTasksRunner.js" ;
2830import { getCommandMetadata } from "../utils/getCommandMetadata.js" ;
@@ -63,7 +65,7 @@ export class CliService {
6365 * @param data
6466 * @param $ctx
6567 */
66- public runLifecycle ( cmdName : string , data : any = { } , $ctx : DIContext ) {
68+ public runLifecycle ( cmdName : string , data : CommandData = { } , $ctx : DIContext ) {
6769 return runInContext ( $ctx , async ( ) => {
6870 await $asyncEmit ( "$loadPackageJson" ) ;
6971
@@ -76,7 +78,7 @@ export class CliService {
7678 } ) ;
7779 }
7880
79- public async dispatch ( cmdName : string , data : any , $ctx : DIContext ) {
81+ public async dispatch ( cmdName : string , data : CommandData , $ctx : DIContext ) {
8082 try {
8183 $ctx . set ( "dispatchCmd" , cmdName ) ;
8284 $ctx . set ( "data" , data ) ;
@@ -113,27 +115,27 @@ export class CliService {
113115 /**
114116 * Run prompt for a given command
115117 * @param cmdName
116- * @param ctx Initial data
118+ * @param data Initial data
117119 */
118- public async beforePrompt ( cmdName : string , ctx : any = { } ) {
120+ public async beforePrompt ( cmdName : string , data : CommandData = { } ) {
119121 const provider = this . commands . get ( cmdName ) ;
120122 const instance = inject < CommandProvider > ( provider . useClass ) ! ;
121- const verbose = ctx . verbose ;
123+ const verbose = data . verbose ;
122124
123125 if ( instance . $beforePrompt ) {
124- ctx = await instance . $beforePrompt ( JSON . parse ( JSON . stringify ( ctx ) ) ) ;
125- ctx . verbose = verbose ;
126+ data = await instance . $beforePrompt ( JSON . parse ( JSON . stringify ( data ) ) ) ;
127+ data . verbose = verbose ;
126128 }
127129
128- return ctx ;
130+ return data ;
129131 }
130132
131133 /**
132134 * Run prompt for a given command
133135 * @param cmdName
134136 * @param ctx Initial data
135137 */
136- public async prompt ( cmdName : string , ctx : any = { } ) {
138+ public async prompt ( cmdName : string , ctx : CommandData = { } ) {
137139 const provider = this . commands . get ( cmdName ) ;
138140 const instance = inject < CommandProvider > ( provider . useClass ) ! ;
139141
@@ -159,7 +161,7 @@ export class CliService {
159161 * @param cmdName
160162 * @param data
161163 */
162- public async getTasks ( cmdName : string , data : any ) {
164+ public async getTasks ( cmdName : string , data : any ) : Promise < Task [ ] > {
163165 const $ctx = getContext ( ) ! ;
164166 const provider = this . commands . get ( cmdName ) ;
165167 const instance = inject < CommandProvider > ( provider . token ) ! ;
@@ -170,7 +172,11 @@ export class CliService {
170172 await instance . $beforeExec ( data ) ;
171173 }
172174
173- return [ ...( await instance . $exec ( data ) ) , ...( await this . hooks . emit ( CommandStoreKeys . EXEC_HOOKS , cmdName , data ) ) ] ;
175+ return [
176+ ...( await instance . $exec ( data ) ) ,
177+ ...( await this . hooks . emit ( CommandStoreKeys . EXEC_HOOKS , cmdName , data ) ) ,
178+ ...( await $asyncAlter ( `$alter${ pascalCase ( cmdName ) } Tasks` , [ ] , [ data ] ) )
179+ ] ;
174180 }
175181
176182 public async getPostInstallTasks ( cmdName : string , data : any ) {
@@ -182,6 +188,7 @@ export class CliService {
182188 return [
183189 ...( instance . $postInstall ? await instance . $postInstall ( data ) : [ ] ) ,
184190 ...( await this . hooks . emit ( CommandStoreKeys . POST_INSTALL_HOOKS , cmdName , data ) ) ,
191+ ...( await $asyncAlter ( `$alter${ pascalCase ( cmdName ) } PostInstallTasks` , [ ] as Task [ ] , [ data ] ) ) ,
185192 ...( instance . $afterPostInstall ? await instance . $afterPostInstall ( data ) : [ ] )
186193 ] ;
187194 }
@@ -203,7 +210,7 @@ export class CliService {
203210 ) ;
204211 const allOpts = mapCommanderOptions ( commandName , this . program . commands ) ;
205212
206- const data = {
213+ const data : CommandData = {
207214 ...allOpts ,
208215 verbose : ! ! this . program . opts ( ) . verbose ,
209216 ...mappedArgs ,
@@ -250,11 +257,13 @@ export class CliService {
250257 . forEach ( ( provider ) => this . build ( provider ) ) ;
251258 }
252259
253- private mapData ( cmdName : string , data : any , $ctx : DIContext ) {
260+ private mapData ( cmdName : string , data : CommandData , $ctx : DIContext ) {
254261 const provider = this . commands . get ( cmdName ) ;
255262 const instance = inject < CommandProvider > ( provider . useClass ) ! ;
256263 const verbose = data . verbose ;
257264
265+ data . commandName ||= cmdName ;
266+
258267 if ( instance . $mapContext ) {
259268 data = instance . $mapContext ( JSON . parse ( JSON . stringify ( data ) ) ) ;
260269 data . verbose = verbose ;
@@ -266,7 +275,7 @@ export class CliService {
266275 logger ( ) . level = "info" ;
267276 }
268277
269- data . bindLogger = $ctx . get ( "command" ) . bindLogger ;
278+ data . bindLogger = $ctx . get ( "command" ) ? .bindLogger ;
270279
271280 $ctx . set ( "data" , data ) ;
272281
0 commit comments