@@ -20,6 +20,7 @@ export interface Middleware {
20
20
relativePath : string ;
21
21
parentPath : string ;
22
22
global : boolean ;
23
+ command : string | null ;
23
24
}
24
25
25
26
export interface ParsedCommandData {
@@ -32,6 +33,8 @@ export interface CommandsRouterOptions {
32
33
}
33
34
34
35
const 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 ? $ / ;
35
38
const 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 ? $ / ;
36
39
const COMMAND_PATTERN = / ^ ( [ ^ + ( ) . ] [ ^ ( ) . ] * ) \. ( m | c ) ? ( j | t ) s x ? $ / ;
37
40
const CATEGORY_PATTERN = / ^ \( .+ \) $ / ;
@@ -62,7 +65,9 @@ export class CommandsRouter {
62
65
63
66
private isMiddleware ( name : string ) : boolean {
64
67
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 )
66
71
) ;
67
72
}
68
73
@@ -159,6 +164,9 @@ export class CommandsRouter {
159
164
relativePath : this . replaceEntrypoint ( path ) ,
160
165
parentPath : entry . parentPath ,
161
166
global : GLOBAL_MIDDLEWARE_PATTERN . test ( name ) ,
167
+ command : COMMAND_MIDDLEWARE_PATTERN . test ( name )
168
+ ? name . split ( '.' ) [ 0 ] || null
169
+ : null ,
162
170
} ;
163
171
164
172
this . middlewares . set ( middleware . id , middleware ) ;
@@ -170,9 +178,9 @@ export class CommandsRouter {
170
178
const commandPath = command . parentPath ;
171
179
const samePathMiddlewares = Array . from ( this . middlewares . values ( ) )
172
180
. 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 ;
176
184
} )
177
185
. map ( ( middleware ) => middleware . id ) ;
178
186
0 commit comments