1515'use strict' ;
1616
1717const yargs = require ( 'yargs' ) ;
18+ const camelcase = require ( 'camelcase' ) ;
1819
1920const MIN_MSG = 'You need at least one command.' ;
2021
@@ -26,6 +27,37 @@ if (process.env.NODE_OPTIONS) {
2627 . join ( ' ' ) ;
2728}
2829
30+ function envAwareStrict ( args , aliases ) {
31+ const specialKeys = [ '$0' , '--' , '_' ] ;
32+ const illegalEnv = [ 'saveConfig' , 'add' , 'default' ] ;
33+
34+ const hlxEnv = { } ;
35+ Object
36+ . keys ( process . env )
37+ . filter ( key => key . startsWith ( 'HLX_' ) )
38+ . forEach ( ( key ) => {
39+ hlxEnv [ camelcase ( key . substring ( 4 ) ) ] = key ;
40+ } ) ;
41+
42+ illegalEnv . forEach ( ( key ) => {
43+ if ( key in hlxEnv ) {
44+ throw new Error ( `${ hlxEnv [ key ] } is not allowed in environment.` ) ;
45+ }
46+ } ) ;
47+
48+ const unknown = [ ] ;
49+ Object . keys ( args ) . forEach ( ( key ) => {
50+ if ( specialKeys . indexOf ( key ) === - 1 && ! ( key in hlxEnv ) && ! ( key in aliases ) ) {
51+ unknown . push ( key ) ;
52+ }
53+ } ) ;
54+
55+ if ( unknown . length > 0 ) {
56+ return unknown . length === 1 ? `Unknown argument: ${ unknown [ 0 ] } ` : `Unknown arguments: ${ unknown . join ( ', ' ) } ` ;
57+ }
58+ return true ;
59+ }
60+
2961/**
3062 * Adds the default logging options.
3163 * @param argv Yargs
@@ -61,10 +93,10 @@ class CLI {
6193 auth : require ( './auth.js' ) ( ) ,
6294 } ;
6395 this . _failFn = ( message , err , argv ) => {
64- const msg = err ? err . message : message ;
96+ const msg = err && err . message ? err . message : message ;
6597 console . error ( msg ) ;
6698 if ( msg === MIN_MSG || / .* U n k n o w n a r g u m e n t .* / . test ( msg ) || / .* N o t e n o u g h n o n - o p t i o n a r g u m e n t s : .* / . test ( msg ) ) {
67- console . error ( '\nUsage: %s' , argv . help ( ) ) ;
99+ console . error ( '\n %s' , argv . help ( ) ) ;
68100 }
69101 process . exit ( 1 ) ;
70102 } ;
@@ -84,16 +116,28 @@ class CLI {
84116 const argv = yargs ( ) ;
85117 Object . values ( this . _commands ) . forEach ( cmd => argv . command ( cmd ) ) ;
86118
87- return logArgs ( argv )
119+ const ret = logArgs ( argv )
88120 . scriptName ( 'hlx' )
121+ . usage ( 'Usage: $0 <command> [options]' )
122+ . parserConfiguration ( { 'camel-case-expansion' : false } )
123+ . env ( 'HLX' )
124+ . check ( envAwareStrict )
125+ . showHelpOnFail ( true )
89126 . fail ( this . _failFn )
90127 . exitProcess ( args . indexOf ( '--get-yargs-completions' ) > - 1 )
91- . strict ( )
92128 . demandCommand ( 1 , MIN_MSG )
93129 . epilogue ( 'for more information, find our manual at https://github.com/adobe/helix-cli' )
94130 . help ( )
95131 . completion ( )
96132 . parse ( args ) ;
133+
134+ // hack to check if command is valid in non-strict mode
135+ const cmd = ret . _ [ 0 ] ;
136+ if ( cmd && ! ( cmd in this . _commands ) ) {
137+ console . error ( 'Unknown command: %s\n' , cmd ) ;
138+ argv . showHelp ( ) ;
139+ process . exit ( 1 ) ;
140+ }
97141 }
98142}
99143
0 commit comments