1- import * as _ from "lodash" ;
2- import logger from "./logger" ;
3- import { ConfigHostEntry } from "./config" ;
4- import config from "./config" ;
5- import { failedExit } from "./cli-utils" ;
1+ import * as _ from 'lodash' ;
2+ import logger from './logger' ;
3+ import config , { ConfigHostEntry } from './config' ;
4+
5+ import { failedExit } from './cli-utils' ;
6+
7+ // eslint-disable-next-line @typescript-eslint/no-var-requires
8+ require ( 'dotenv' ) . config ( ) ;
69
710const apiEnvCommandLineOptions : Record < string , any > = {
811 host : {
9- type : " string" ,
10- description : " Host/port - will override --env" ,
12+ type : ' string' ,
13+ description : ' Host/port - will override --env' ,
1114 } ,
1215 protocol : {
13- choices : [ " http" , " https" ] ,
16+ choices : [ ' http' , ' https' ] ,
1417 description :
15- " What protocol to use (if not specified in url), defaults to http for local, https otherwise" ,
18+ ' What protocol to use (if not specified in url), defaults to http for local, https otherwise' ,
1619 } ,
1720 key : {
18- type : " string" ,
19- description : ` Authorization key, if not specified will try to find one in the env, in .env or, in local mode, directly in mongo` ,
21+ type : ' string' ,
22+ description : ' Authorization key, if not specified will try to find one in the env, in .env or, in local mode, directly in mongo' ,
2023 } ,
2124} ;
2225
26+ /**
27+ *
28+ */
2329export function getApiEnvCommandLineOptions ( ) : Record < string , any > {
2430 if ( config . keyTypes ) {
2531 apiEnvCommandLineOptions . key_type = {
2632 choices : config . keyTypes ,
2733 default : config . keyTypes [ 0 ] ,
28- description : " authorization key type to use" ,
34+ description : ' authorization key type to use' ,
2935 } ;
3036 }
3137
3238 if ( config . hosts ) {
3339 apiEnvCommandLineOptions . env = {
3440 choices : [ ..._ . keys ( config . hosts ) , ..._ . flatMap ( config . hosts , ( v ) => v . aliases || [ ] ) ] ,
35- description : ` api host to talk to` ,
41+ description : ' api host to talk to' ,
3642 } ;
3743
3844 _ . forEach ( config . hosts , ( hostEntry : ConfigHostEntry , key : string ) => {
@@ -53,44 +59,55 @@ export interface ApiEnv {
5359 keyType ?: string ;
5460}
5561
56- type KeyParams = Pick < ApiEnv , " keyEnv" | " keyType" > ;
62+ type KeyParams = Pick < ApiEnv , ' keyEnv' | ' keyType' > ;
5763
64+ /**
65+ * @param root0
66+ * @param root0.keyEnv
67+ * @param root0.keyType
68+ */
5869function findKey ( { keyEnv, keyType } : KeyParams ) : string {
59- require ( "dotenv" ) . config ( ) ;
60-
61- const env_variable_name = [ keyEnv , keyType , "API_KEY" ]
70+ const envVariableName = [ keyEnv , keyType , 'API_KEY' ]
6271 . filter ( ( s ) => ! _ . isEmpty ( s ) )
63- . join ( "_" )
72+ . join ( '_' )
6473 . toUpperCase ( ) ;
65- logger . info ( `Looking for key in env ${ env_variable_name } ` ) ;
66- const key = process . env [ env_variable_name ] ;
74+ logger . info ( `Looking for key in env ${ envVariableName } ` ) ;
75+ const key = process . env [ envVariableName ] ;
6776 if ( ! key ) {
68- failedExit ( `No key found for ${ env_variable_name } in .env and --key not specified` ) ;
77+ failedExit ( `No key found for ${ envVariableName } in .env and --key not specified` ) ;
6978 }
7079 return key ;
7180}
7281
73- export function fixApiEnvKey ( apiEnv : Partial < ApiEnv > ) {
74- apiEnv . key =
75- apiEnv . key ||
76- findKey ( { keyEnv : apiEnv . keyEnv || "" , keyType : apiEnv . keyType || config . keyTypes ?. [ 0 ] } ) ;
82+ /**
83+ * @param apiEnv
84+ */
85+ export function fixApiEnvKey ( apiEnv : Partial < ApiEnv > ) : void {
86+ // eslint-disable-next-line no-param-reassign
87+ apiEnv . key = apiEnv . key
88+ || findKey ( { keyEnv : apiEnv . keyEnv || '' , keyType : apiEnv . keyType || config . keyTypes ?. [ 0 ] } ) ;
7789}
7890
91+ /**
92+ * @param argv
93+ */
7994export function argvToApiEnv ( argv : any ) : ApiEnv {
8095 let apiEnv : Partial < ApiEnv > = _ . clone ( argv ) ;
8196
8297 let aliasedHostEntry : ConfigHostEntry ;
8398 _ . forEach ( config . hosts , ( hostEntry : ConfigHostEntry , key : string ) => {
8499 if ( argv [ key ] ) {
85100 if ( aliasedHostEntry ) {
86- throw new Error ( `Can only specify one of ${ _ . keys ( config . hosts ) . join ( "," ) } ` ) ;
101+ throw new Error ( `Can only specify one of ${ _ . keys ( config . hosts ) . join ( ',' ) } ` ) ;
87102 }
88103
89104 if ( hostEntry . takesArg ) {
90105 const toReplace = key . toUpperCase ( ) ;
106+ // eslint-disable-next-line no-param-reassign
91107 hostEntry . host = hostEntry . host . replace ( toReplace , argv [ key ] ) ;
92108 }
93109
110+ // eslint-disable-next-line no-param-reassign
94111 hostEntry . keyEnv = hostEntry . keyEnv || key ;
95112
96113 aliasedHostEntry = hostEntry ;
@@ -104,10 +121,10 @@ export function argvToApiEnv(argv: any): ApiEnv {
104121 } ;
105122 }
106123
107- if ( apiEnv . host . startsWith ( " http" ) ) {
124+ if ( apiEnv . host . startsWith ( ' http' ) ) {
108125 const url = new URL ( apiEnv . host ) ;
109126 apiEnv . host = url . host ;
110- apiEnv . protocol = url . protocol . replace ( ":" , "" ) ;
127+ apiEnv . protocol = url . protocol . replace ( ':' , '' ) ;
111128 }
112129
113130 fixApiEnvKey ( apiEnv ) ;
0 commit comments