@@ -20,6 +20,7 @@ export interface Middleware {
2020 relativePath : string ;
2121 parentPath : string ;
2222 global : boolean ;
23+ command : string | null ;
2324}
2425
2526export interface ParsedCommandData {
@@ -32,6 +33,8 @@ export interface CommandsRouterOptions {
3233}
3334
3435const MIDDLEWARE_PATTERN = / ^ \+ m i d d l e w a r e \. ( m | c ) ? ( j | t ) s x ? $ / ;
36+ const COMMAND_MIDDLEWARE_PATTERN =
37+ / ^ \+ ( [ ^ + ( ) . ] [ ^ ( ) . ] * ) \. m i d d l e w a r e \. ( m | c ) ? ( j | t ) s x ? $ / ;
3538const GLOBAL_MIDDLEWARE_PATTERN = / ^ \+ g l o b a l - m i d d l e w a r e \. ( m | c ) ? ( j | t ) s x ? $ / ;
3639const COMMAND_PATTERN = / ^ ( [ ^ + ( ) . ] [ ^ ( ) . ] * ) \. ( m | c ) ? ( j | t ) s x ? $ / ;
3740const CATEGORY_PATTERN = / ^ \( .+ \) $ / ;
@@ -62,7 +65,9 @@ export class CommandsRouter {
6265
6366 private isMiddleware ( name : string ) : boolean {
6467 return (
65- MIDDLEWARE_PATTERN . test ( name ) || GLOBAL_MIDDLEWARE_PATTERN . test ( name )
68+ MIDDLEWARE_PATTERN . test ( name ) ||
69+ GLOBAL_MIDDLEWARE_PATTERN . test ( name ) ||
70+ COMMAND_MIDDLEWARE_PATTERN . test ( name )
6671 ) ;
6772 }
6873
@@ -159,6 +164,9 @@ export class CommandsRouter {
159164 relativePath : this . replaceEntrypoint ( path ) ,
160165 parentPath : entry . parentPath ,
161166 global : GLOBAL_MIDDLEWARE_PATTERN . test ( name ) ,
167+ command : COMMAND_MIDDLEWARE_PATTERN . test ( name )
168+ ? name . split ( '.' ) [ 0 ] || null
169+ : null ,
162170 } ;
163171
164172 this . middlewares . set ( middleware . id , middleware ) ;
@@ -170,9 +178,9 @@ export class CommandsRouter {
170178 const commandPath = command . parentPath ;
171179 const samePathMiddlewares = Array . from ( this . middlewares . values ( ) )
172180 . filter ( ( middleware ) => {
173- // if middleware's parent path is the same as command's parent path
174- // or if the middleware is global
175- return middleware . parentPath === commandPath || middleware . global ;
181+ if ( middleware . global ) return true ;
182+ if ( middleware . command ) return middleware . command === command . name ;
183+ return middleware . parentPath === commandPath ;
176184 } )
177185 . map ( ( middleware ) => middleware . id ) ;
178186
0 commit comments