File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -21,20 +21,44 @@ const program = new Command();
2121
2222program
2323 . usage ( '[options]' )
24+
2425 . option ( '--handler <path>' , 'Lambda@Edge handler script.' )
2526 . option ( '--port <number>' , 'HTTP server port number.' , 3000 )
27+
2628 . action ( function ( opts ) {
27- const script = process . cwd ( ) + '/' + opts . handler ;
29+ const errors = [ ] ;
30+
31+ try {
32+ const script = process . cwd ( ) + '/' + opts . handler ;
33+
34+ // Validate option values.
35+ if ( opts . handler && ! fs . existsSync ( script ) || ! opts . handler ) {
36+ errors . push ( " option '--handler <path>' allows path Lambda@Edge handler script" ) ;
37+ }
38+
39+ if ( opts . port && ! / ^ [ 0 - 9 ] { 2 , 5 } $ / . test ( opts . port ) || ! opts . port ) {
40+ errors . push ( " option '--port <number>' allows up to 5 numeric characters" ) ;
41+ }
42+
43+ if ( errors . length ) {
44+ console . error ( 'error: Invalid script arguments' ) ;
45+
46+ throw new Error ( errors . join ( '\n' ) ) ;
47+ }
2848
29- if ( fs . existsSync ( script ) ) {
3049 const { handler} = require ( script ) ;
3150
3251 if ( isValidFunc ( handler ) ) {
33- initServer ( handler , opts . port ) ;
34- } else {
35- throw new Error ( 'Invalid handler method' ) ;
52+ return initServer ( handler , opts . port ) ;
3653 }
37- } else {
54+
55+ throw new Error ( 'error: Invalid handler method' ) ;
56+
57+ } catch ( err ) {
58+ if ( err instanceof Error ) {
59+ console . error ( `${ err . message } \n` ) ;
60+ }
61+
3862 this . outputHelp ( ) ;
3963 }
4064 } ) ;
You can’t perform that action at this time.
0 commit comments