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 ( ) ;
6
9
7
10
const apiEnvCommandLineOptions : Record < string , any > = {
8
11
host : {
9
- type : " string" ,
10
- description : " Host/port - will override --env" ,
12
+ type : ' string' ,
13
+ description : ' Host/port - will override --env' ,
11
14
} ,
12
15
protocol : {
13
- choices : [ " http" , " https" ] ,
16
+ choices : [ ' http' , ' https' ] ,
14
17
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' ,
16
19
} ,
17
20
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' ,
20
23
} ,
21
24
} ;
22
25
26
+ /**
27
+ *
28
+ */
23
29
export function getApiEnvCommandLineOptions ( ) : Record < string , any > {
24
30
if ( config . keyTypes ) {
25
31
apiEnvCommandLineOptions . key_type = {
26
32
choices : config . keyTypes ,
27
33
default : config . keyTypes [ 0 ] ,
28
- description : " authorization key type to use" ,
34
+ description : ' authorization key type to use' ,
29
35
} ;
30
36
}
31
37
32
38
if ( config . hosts ) {
33
39
apiEnvCommandLineOptions . env = {
34
40
choices : [ ..._ . keys ( config . hosts ) , ..._ . flatMap ( config . hosts , ( v ) => v . aliases || [ ] ) ] ,
35
- description : ` api host to talk to` ,
41
+ description : ' api host to talk to' ,
36
42
} ;
37
43
38
44
_ . forEach ( config . hosts , ( hostEntry : ConfigHostEntry , key : string ) => {
@@ -53,44 +59,55 @@ export interface ApiEnv {
53
59
keyType ?: string ;
54
60
}
55
61
56
- type KeyParams = Pick < ApiEnv , " keyEnv" | " keyType" > ;
62
+ type KeyParams = Pick < ApiEnv , ' keyEnv' | ' keyType' > ;
57
63
64
+ /**
65
+ * @param root0
66
+ * @param root0.keyEnv
67
+ * @param root0.keyType
68
+ */
58
69
function 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' ]
62
71
. filter ( ( s ) => ! _ . isEmpty ( s ) )
63
- . join ( "_" )
72
+ . join ( '_' )
64
73
. 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 ] ;
67
76
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` ) ;
69
78
}
70
79
return key ;
71
80
}
72
81
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 ] } ) ;
77
89
}
78
90
91
+ /**
92
+ * @param argv
93
+ */
79
94
export function argvToApiEnv ( argv : any ) : ApiEnv {
80
95
let apiEnv : Partial < ApiEnv > = _ . clone ( argv ) ;
81
96
82
97
let aliasedHostEntry : ConfigHostEntry ;
83
98
_ . forEach ( config . hosts , ( hostEntry : ConfigHostEntry , key : string ) => {
84
99
if ( argv [ key ] ) {
85
100
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 ( ',' ) } ` ) ;
87
102
}
88
103
89
104
if ( hostEntry . takesArg ) {
90
105
const toReplace = key . toUpperCase ( ) ;
106
+ // eslint-disable-next-line no-param-reassign
91
107
hostEntry . host = hostEntry . host . replace ( toReplace , argv [ key ] ) ;
92
108
}
93
109
110
+ // eslint-disable-next-line no-param-reassign
94
111
hostEntry . keyEnv = hostEntry . keyEnv || key ;
95
112
96
113
aliasedHostEntry = hostEntry ;
@@ -104,10 +121,10 @@ export function argvToApiEnv(argv: any): ApiEnv {
104
121
} ;
105
122
}
106
123
107
- if ( apiEnv . host . startsWith ( " http" ) ) {
124
+ if ( apiEnv . host . startsWith ( ' http' ) ) {
108
125
const url = new URL ( apiEnv . host ) ;
109
126
apiEnv . host = url . host ;
110
- apiEnv . protocol = url . protocol . replace ( ":" , "" ) ;
127
+ apiEnv . protocol = url . protocol . replace ( ':' , '' ) ;
111
128
}
112
129
113
130
fixApiEnvKey ( apiEnv ) ;
0 commit comments