1- import { isEmpty , get , assign , keys , split , filter , join , includes } from 'lodash' ;
1+ import { isEmpty , get , assign , keys , split , filter , join , includes , replace } from 'lodash' ;
22import path from 'path' ;
33import {
44 IProjectConfig ,
@@ -11,6 +11,7 @@ import {
1111} from './interface' ;
1212import { makeUnderLine } from '../../libs' ;
1313import { logger } from '../../logger' ;
14+ import { COMMON_VARIABLE_TYPE_REG , SPECIALL_VARIABLE_TYPE_REG } from '../constant' ;
1415import chalk from 'chalk' ;
1516
1617export function humanWarning ( tips : string ) {
@@ -43,6 +44,21 @@ export function getCurrentPath(p: string = './', spath: string) {
4344 return p ? path . join ( dir , p ) : dir ;
4445}
4546
47+ function parseAction ( actionKey : string , method : string ) {
48+ const matchResult = replace ( actionKey , COMMON_VARIABLE_TYPE_REG , '$1' ) ;
49+ const funMatchResult = matchResult . match ( SPECIALL_VARIABLE_TYPE_REG ) ;
50+ if ( funMatchResult ) {
51+ const [ start , end ] = split ( funMatchResult [ 1 ] , '-' ) ;
52+ if ( end === 'regex' ) {
53+ const reg = new RegExp ( funMatchResult [ 2 ] ) ;
54+ return { action : start as IGlobalActionValue , success : reg . test ( method ) } ;
55+ }
56+ }
57+ const [ start , end ] = split ( actionKey , '-' ) ;
58+ const action = start as IGlobalActionValue ;
59+ return { action, success : end === method } ;
60+ }
61+
4662export function getActions ( configs : IProjectConfig , { method, spath } ) : IActionHook [ ] {
4763 function validate ( hook : IActionHook ) : IActionType {
4864 if ( 'run' in hook && ! ( 'component' in hook ) && ! ( 'plugin' in hook ) ) return 'run' ;
@@ -61,18 +77,17 @@ export function getActions(configs: IProjectConfig, { method, spath }): IActionH
6177 const keyList = keys ( actions ) ;
6278 for ( const actionKey of keyList ) {
6379 const hookList = actions [ actionKey ] ;
64- if ( isEmpty ( hookList ) ) continue ;
65- const [ start , end ] = split ( actionKey , '-' ) ;
66- const action = start as IGlobalActionValue ;
67- if ( end === method ) {
80+ if ( isEmpty ( hookList ) ) continue ;
81+ const { action, success } = parseAction ( actionKey , method ) ;
82+ if ( success ) {
6883 for ( const hookDetail of hookList ) {
6984 const type = validate ( hookDetail ) ;
7085 if ( type === 'run' ) {
7186 const obj = {
7287 type,
7388 value : hookDetail [ type ] ,
7489 path : getCurrentPath ( hookDetail . path , spath ) ,
75- pre : start === 'pre' ? true : false ,
90+ pre : action === 'pre' ? true : false ,
7691 action,
7792 } ;
7893 hooks . push ( obj ) ;
@@ -82,7 +97,7 @@ export function getActions(configs: IProjectConfig, { method, spath }): IActionH
8297 const obj = {
8398 type,
8499 value : hookDetail [ type ] ,
85- pre : start === 'pre' ? true : false ,
100+ pre : action === 'pre' ? true : false ,
86101 action,
87102 } ;
88103 hooks . push ( obj ) ;
@@ -92,7 +107,7 @@ export function getActions(configs: IProjectConfig, { method, spath }): IActionH
92107 const obj = {
93108 type,
94109 value : hookDetail [ type ] ,
95- pre : start === 'pre' ? true : false ,
110+ pre : action === 'pre' ? true : false ,
96111 action,
97112 args : hookDetail . args ,
98113 } ;
0 commit comments