1
1
import chalk from 'chalk' ;
2
-
3
- import { InputCustomOptions , InputQuestion , ListQuestion } from 'inquirer' ;
2
+ import { InputCustomOptions } from 'inquirer' ;
4
3
5
4
import type { InputSetting , RuleEntry , Result , ResultPart } from './types' ;
6
5
@@ -30,11 +29,7 @@ export default function getPrompt(
30
29
type : ResultPart ,
31
30
rules : RuleEntry [ ] = [ ] ,
32
31
settings : InputSetting = { }
33
- ) :
34
- | InputQuestion < Result >
35
- | ListQuestion < Result >
36
- | InputCustomOptions < Result >
37
- | null {
32
+ ) : InputCustomOptions < Result > | null {
38
33
const emptyRule = rules . filter ( getHasName ( 'empty' ) ) . find ( ruleIsActive ) ;
39
34
40
35
const mustBeEmpty = emptyRule ? ruleIsApplicable ( emptyRule ) : false ;
@@ -55,23 +50,53 @@ export default function getPrompt(
55
50
56
51
const enumRule = rules . filter ( getHasName ( 'enum' ) ) . find ( enumRuleIsActive ) ;
57
52
53
+ const tabCompletion = enumRule
54
+ ? enumRule [ 1 ] [ 2 ] . map ( ( enumerable ) => {
55
+ const enumSettings = ( settings . enumerables || { } ) [ enumerable ] || { } ;
56
+ return {
57
+ value : forceLeadingBlankFn ( forceCaseFn ( enumerable ) ) ,
58
+ description : enumSettings . description || '' ,
59
+ } ;
60
+ } )
61
+ : [ ] ;
62
+
63
+ const maxLength = ( res : Result ) => {
64
+ let remainingHeaderLength = Infinity ;
65
+ if ( settings . header && settings . header . length ) {
66
+ const header = format ( {
67
+ type : res . type ,
68
+ scope : res . scope ,
69
+ subject : res . subject ,
70
+ } ) ;
71
+ remainingHeaderLength = settings . header . length - header . length ;
72
+ }
73
+ return Math . min ( inputMaxLength , remainingHeaderLength ) ;
74
+ } ;
75
+
58
76
return {
59
77
type : 'input-custom' ,
60
78
name : type ,
61
79
message : `${ type } :` ,
62
- validate ( ) : boolean | string {
80
+ validate ( input , answers ) {
81
+ if ( input . length > maxLength ( answers || { } ) ) {
82
+ return 'Input contains too many characters!' ;
83
+ }
84
+ if ( required && input . trim ( ) . length === 0 ) {
85
+ // Show help if enum is defined and input may not be empty
86
+ return `⚠ ${ chalk . bold ( type ) } may not be empty.` ;
87
+ }
88
+
89
+ const tabValues = tabCompletion . map ( ( item ) => item . value ) ;
90
+ if (
91
+ input . length > 0 &&
92
+ tabValues . length > 0 &&
93
+ ! tabValues . includes ( input )
94
+ ) {
95
+ return `⚠ ${ chalk . bold ( type ) } must be one of ${ tabValues . join ( ', ' ) } .` ;
96
+ }
63
97
return true ;
64
98
} ,
65
- tabCompletion : enumRule
66
- ? enumRule [ 1 ] [ 2 ] . map ( ( enumerable ) => {
67
- const enumSettings = ( settings . enumerables || { } ) [ enumerable ] || { } ;
68
- return {
69
- value : forceLeadingBlankFn ( forceCaseFn ( enumerable ) ) ,
70
- description : enumSettings . description || '' ,
71
- } ;
72
- } )
73
- : [ ] ,
74
- required,
99
+ tabCompletion,
75
100
log ( answers ?: Result ) {
76
101
let prefix =
77
102
`${ chalk . white ( 'Please enter a' ) } ${ chalk . bold ( type ) } : ${ meta ( {
@@ -90,18 +115,7 @@ export default function getPrompt(
90
115
}
91
116
return prefix + EOL ;
92
117
} ,
93
- maxLength ( res : Result ) {
94
- let remainingHeaderLength = Infinity ;
95
- if ( settings . header && settings . header . length ) {
96
- const header = format ( {
97
- type : res . type ,
98
- scope : res . scope ,
99
- subject : res . subject ,
100
- } ) ;
101
- remainingHeaderLength = settings . header . length - header . length ;
102
- }
103
- return Math . min ( inputMaxLength , remainingHeaderLength ) ;
104
- } ,
118
+ maxLength,
105
119
transformer ( value : string ) {
106
120
return forceCaseFn ( value ) ;
107
121
} ,
0 commit comments