@@ -2,35 +2,46 @@ import { Group, User } from '@microsoft/microsoft-graph-types';
2
2
import { setTimeout } from 'timers/promises' ;
3
3
import fs from 'fs' ;
4
4
import path from 'path' ;
5
+ import { z } from 'zod' ;
5
6
import { Logger } from '../../../../cli/Logger.js' ;
6
- import GlobalOptions from '../../../../GlobalOptions .js' ;
7
+ import { globalOptionsZod } from '../../../../Command .js' ;
7
8
import request , { CliRequestOptions } from '../../../../request.js' ;
8
9
import { formatting } from '../../../../utils/formatting.js' ;
10
+ import { zod } from '../../../../utils/zod.js' ;
9
11
import GraphCommand from '../../../base/GraphCommand.js' ;
10
12
import commands from '../../commands.js' ;
11
13
12
- interface CommandArgs {
13
- options : Options ;
14
+ enum GroupVisibility {
15
+ Private = 'Private' ,
16
+ Public = 'Public' ,
17
+ HiddenMembership = 'HiddenMembership'
14
18
}
15
19
16
- interface Options extends GlobalOptions {
17
- displayName : string ;
18
- mailNickname : string ;
19
- description ?: string ;
20
- owners ?: string ;
21
- members ?: string ;
22
- visibility ?: string ;
23
- logoPath ?: string ;
24
- allowMembersToPost ?: boolean ;
25
- hideGroupInOutlook ?: boolean ;
26
- subscribeNewGroupMembers ?: boolean ;
27
- welcomeEmailDisabled ?: boolean ;
20
+ const options = globalOptionsZod
21
+ . extend ( {
22
+ displayName : zod . alias ( 'n' , z . string ( ) ) ,
23
+ mailNickname : zod . alias ( 'm' , z . string ( ) ) ,
24
+ description : zod . alias ( 'd' , z . string ( ) . optional ( ) ) ,
25
+ owners : z . string ( ) . optional ( ) ,
26
+ members : z . string ( ) . optional ( ) ,
27
+ visibility : zod . coercedEnum ( GroupVisibility ) . optional ( ) ,
28
+ logoPath : zod . alias ( 'l' , z . string ( ) . optional ( ) ) ,
29
+ allowMembersToPost : z . boolean ( ) . optional ( ) ,
30
+ hideGroupInOutlook : z . boolean ( ) . optional ( ) ,
31
+ subscribeNewGroupMembers : z . boolean ( ) . optional ( ) ,
32
+ welcomeEmailDisabled : z . boolean ( ) . optional ( )
33
+ } )
34
+ . strict ( ) ;
35
+
36
+ declare type Options = z . infer < typeof options > ;
37
+
38
+ interface CommandArgs {
39
+ options : Options ;
28
40
}
29
41
30
42
class EntraM365GroupAddCommand extends GraphCommand {
31
43
private static numRepeat : number = 15 ;
32
44
private pollingInterval : number = 500 ;
33
- private allowedVisibilities : string [ ] = [ 'Private' , 'Public' , 'HiddenMembership' ] ;
34
45
35
46
public get name ( ) : string {
36
47
return commands . M365GROUP_ADD ;
@@ -40,123 +51,57 @@ class EntraM365GroupAddCommand extends GraphCommand {
40
51
return 'Creates a Microsoft 365 Group' ;
41
52
}
42
53
43
- constructor ( ) {
44
- super ( ) ;
45
-
46
- this . #initTelemetry( ) ;
47
- this . #initOptions( ) ;
48
- this . #initTypes( ) ;
49
- this . #initValidators( ) ;
50
- }
51
-
52
- #initTelemetry( ) : void {
53
- this . telemetry . push ( ( args : CommandArgs ) => {
54
- Object . assign ( this . telemetryProperties , {
55
- description : typeof args . options . description !== 'undefined' ,
56
- owners : typeof args . options . owners !== 'undefined' ,
57
- members : typeof args . options . members !== 'undefined' ,
58
- logoPath : typeof args . options . logoPath !== 'undefined' ,
59
- visibility : typeof args . options . visibility !== 'undefined' ,
60
- allowMembersToPost : ! ! args . options . allowMembersToPost ,
61
- hideGroupInOutlook : ! ! args . options . hideGroupInOutlook ,
62
- subscribeNewGroupMembers : ! ! args . options . subscribeNewGroupMembers ,
63
- welcomeEmailDisabled : ! ! args . options . welcomeEmailDisabled
64
- } ) ;
65
- } ) ;
66
- }
67
-
68
- #initOptions( ) : void {
69
- this . options . unshift (
70
- {
71
- option : '-n, --displayName <displayName>'
72
- } ,
73
- {
74
- option : '-m, --mailNickname <mailNickname>'
75
- } ,
76
- {
77
- option : '-d, --description [description]'
78
- } ,
79
- {
80
- option : '--owners [owners]'
81
- } ,
82
- {
83
- option : '--members [members]'
84
- } ,
85
- {
86
- option : '--visibility [visibility]' ,
87
- autocomplete : this . allowedVisibilities
88
- } ,
89
- {
90
- option : '--allowMembersToPost [allowMembersToPost]' ,
91
- autocomplete : [ 'true' , 'false' ]
92
- } ,
93
- {
94
- option : '--hideGroupInOutlook [hideGroupInOutlook]' ,
95
- autocomplete : [ 'true' , 'false' ]
96
- } ,
97
- {
98
- option : '--subscribeNewGroupMembers [subscribeNewGroupMembers]' ,
99
- autocomplete : [ 'true' , 'false' ]
100
- } ,
101
- {
102
- option : '--welcomeEmailDisabled [welcomeEmailDisabled]' ,
103
- autocomplete : [ 'true' , 'false' ]
104
- } ,
105
- {
106
- option : '-l, --logoPath [logoPath]'
107
- }
108
- ) ;
109
- }
110
-
111
- #initTypes( ) : void {
112
- this . types . string . push ( 'displayName' , 'mailNickname' , 'description' , 'owners' , 'members' , 'visibility' , 'logoPath' ) ;
113
- this . types . boolean . push ( 'allowMembersToPost' , 'hideGroupInOutlook' , 'subscribeNewGroupMembers' , 'welcomeEmailDisabled' ) ;
54
+ public get schema ( ) : z . ZodTypeAny | undefined {
55
+ return options ;
114
56
}
115
57
116
- #initValidators ( ) : void {
117
- this . validators . push (
118
- async ( args : CommandArgs ) => {
119
- if ( args . options . owners ) {
120
- const owners : string [ ] = args . options . owners . split ( ',' ) . map ( o => o . trim ( ) ) ;
58
+ public getRefinedSchema ( schema : typeof options ) : z . ZodEffects < any > | undefined {
59
+ return schema
60
+ . refine ( options => {
61
+ if ( options . owners ) {
62
+ const owners : string [ ] = options . owners . split ( ',' ) . map ( o => o . trim ( ) ) ;
121
63
for ( let i = 0 ; i < owners . length ; i ++ ) {
122
64
if ( owners [ i ] . indexOf ( '@' ) < 0 ) {
123
- return ` ${ owners [ i ] } is not a valid userPrincipalName` ;
65
+ return false ;
124
66
}
125
67
}
126
68
}
127
-
128
- if ( args . options . members ) {
129
- const members : string [ ] = args . options . members . split ( ',' ) . map ( m => m . trim ( ) ) ;
69
+ return true ;
70
+ } , {
71
+ message : 'Invalid userPrincipalName for owners'
72
+ } )
73
+ . refine ( options => {
74
+ if ( options . members ) {
75
+ const members : string [ ] = options . members . split ( ',' ) . map ( m => m . trim ( ) ) ;
130
76
for ( let i = 0 ; i < members . length ; i ++ ) {
131
77
if ( members [ i ] . indexOf ( '@' ) < 0 ) {
132
- return ` ${ members [ i ] } is not a valid userPrincipalName` ;
78
+ return false ;
133
79
}
134
80
}
135
81
}
136
-
137
- if ( args . options . mailNickname . indexOf ( ' ' ) !== - 1 ) {
138
- return 'The option mailNickname cannot contain spaces.' ;
139
- }
140
-
141
- if ( args . options . logoPath ) {
142
- const fullPath : string = path . resolve ( args . options . logoPath ) ;
143
-
82
+ return true ;
83
+ } , {
84
+ message : 'Invalid userPrincipalName for members'
85
+ } )
86
+ . refine ( options => options . mailNickname . indexOf ( ' ' ) === - 1 , {
87
+ message : 'The option mailNickname cannot contain spaces.'
88
+ } )
89
+ . refine ( options => {
90
+ if ( options . logoPath ) {
91
+ const fullPath : string = path . resolve ( options . logoPath ) ;
92
+
144
93
if ( ! fs . existsSync ( fullPath ) ) {
145
- return `File ' ${ fullPath } ' not found` ;
94
+ return false ;
146
95
}
147
-
96
+
148
97
if ( fs . lstatSync ( fullPath ) . isDirectory ( ) ) {
149
- return `Path ' ${ fullPath } ' points to a directory` ;
98
+ return false ;
150
99
}
151
100
}
152
-
153
- if ( args . options . visibility && this . allowedVisibilities . map ( x => x . toLowerCase ( ) ) . indexOf ( args . options . visibility . toLowerCase ( ) ) === - 1 ) {
154
- return `${ args . options . visibility } is not a valid visibility. Allowed values are ${ this . allowedVisibilities . join ( ', ' ) } ` ;
155
- }
156
-
157
101
return true ;
158
- }
159
- ) ;
102
+ } , {
103
+ message : 'Invalid logoPath'
104
+ } ) ;
160
105
}
161
106
162
107
public async commandAction ( logger : Logger , args : CommandArgs ) : Promise < void > {
0 commit comments