1
1
const fs = require ( 'fs' ) ;
2
-
3
- const getValues = ( path = '.env' ) =>
4
- fs
5
- . readFileSync ( path , { encoding : 'utf-8' } )
6
- . trim ( )
7
- . split ( '\n' )
8
- . map ( line => line . split ( / = ( .* ) / ) )
9
- . reduce ( ( acc , [ key , value ] ) => {
10
- acc [ key ] = value ;
11
- return acc ;
12
- } , { } ) ;
2
+ const { fromPairs, get, map, pipe, split, trim} = require ( 'lodash/fp' ) ;
3
+
4
+ const fromCallback = fun => ( ...args ) =>
5
+ new Promise ( ( resolve , reject ) =>
6
+ fun ( ...args , ( err , data ) => ( err ? reject ( err ) : resolve ( data ) ) )
7
+ ) ;
8
+ const readFile = fromCallback ( fs . readFile ) ;
9
+
10
+ const getValues = ( path = '.ssm' ) => {
11
+ const values = ( async ( ) => {
12
+ try {
13
+ return pipe (
14
+ trim ,
15
+ split ( '\n' ) ,
16
+ map ( split ( / = ( .* ) / ) ) ,
17
+ fromPairs
18
+ ) ( await readFile ( path , { encoding : 'utf-8' } ) ) ;
19
+ } catch ( err ) {
20
+ return null ;
21
+ }
22
+ } ) ( ) ;
23
+ return key => values . then ( get ( key ) ) ;
24
+ } ;
13
25
14
26
class ServerlessOfflineSSMProvider {
15
27
constructor ( serverless ) {
@@ -24,17 +36,17 @@ class ServerlessOfflineSSMProvider {
24
36
if ( service !== 'SSM' || method !== 'getParameter' )
25
37
return request ( service , method , params , options ) ;
26
38
27
- return request ( service , method , params , options ) . catch ( error => {
39
+ return request ( service , method , params , options ) . catch ( async error => {
28
40
const { Name} = params ;
29
- const Value = this . values [ Name ] ;
41
+ const Value = await this . values ( Name ) ;
30
42
31
- if ( ! Value ) return Promise . reject ( error ) ;
43
+ if ( ! Value ) throw error ;
32
44
33
- return Promise . resolve ( {
45
+ return {
34
46
Parameter : {
35
47
Value
36
48
}
37
- } ) ;
49
+ } ;
38
50
} ) ;
39
51
} ;
40
52
0 commit comments