1
1
import { Collection , JSCodeshift , NewExpression } from "jscodeshift" ;
2
- import { AWS_CREDENTIALS_MAP } from "../config" ;
2
+ import { AWS_CREDENTIALS_MAP , AWS_TOKEN_MAP } from "../config" ;
3
3
import { ImportType , addNamedModule } from "../modules" ;
4
4
5
5
export interface ReplaceAwsCredentialsOptions {
6
6
v2GlobalName ?: string ;
7
7
importType : ImportType ;
8
8
}
9
9
10
- const PROVIDER_SWITCH_COMMENT = ` JS SDK v3 switched credential providers from classes to functions.` ;
11
-
12
10
const getNewExpression = ( identifier : string , className : string ) =>
13
11
( {
14
12
type : "NewExpression" ,
@@ -29,60 +27,65 @@ export const replaceAwsIdentity = (
29
27
) => {
30
28
if ( ! v2GlobalName ) return ;
31
29
32
- // ToDo: Add support for AWS.TokenProviderChain in future.
33
- const chainNewExpressions = source . find (
34
- j . NewExpression ,
35
- getNewExpression ( v2GlobalName , "CredentialProviderChain" )
36
- ) ;
37
- if ( chainNewExpressions . size ( ) > 0 ) {
38
- const localName = "providerChain" ;
39
- addNamedModule ( j , source , {
40
- importType,
41
- localName,
42
- importedName : "chain" ,
43
- packageName : "@smithy/property-provider" ,
44
- } ) ;
45
- chainNewExpressions . replaceWith ( ( { node } ) =>
46
- j . callExpression . from ( {
47
- callee : j . identifier ( localName ) ,
48
- comments : [
49
- j . commentLine ( PROVIDER_SWITCH_COMMENT ) ,
50
- j . commentLine ( " The CredentialProviderChain is now a chain of providers." ) ,
51
- j . commentLine ( " Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers" ) ,
52
- ] ,
53
- arguments : node . arguments ,
54
- } )
55
- ) ;
56
- }
30
+ for ( const [ identity , identityMap ] of Object . entries ( {
31
+ Credential : AWS_CREDENTIALS_MAP ,
32
+ Token : AWS_TOKEN_MAP ,
33
+ } ) ) {
34
+ const identitySwitchComment = ` JS SDK v3 switched ${ identity . toLowerCase ( ) } providers from classes to functions.` ;
35
+ const identityPackageName = `@aws-sdk/${ identity . toLowerCase ( ) } -providers` ;
57
36
58
- // ToDo: Add support for AWS.Token in future.
59
- for ( const [ v2CredentialsName , v3ProviderName ] of Object . entries ( AWS_CREDENTIALS_MAP ) ) {
60
- const credsNewExpressions = source . find (
37
+ const identityProviderChain = `${ identity } ProviderChain` ;
38
+ const chainNewExpressions = source . find (
61
39
j . NewExpression ,
62
- getNewExpression ( v2GlobalName , v2CredentialsName )
40
+ getNewExpression ( v2GlobalName , identityProviderChain )
63
41
) ;
64
-
65
- if ( credsNewExpressions . size ( ) > 0 ) {
42
+ if ( chainNewExpressions . size ( ) > 0 ) {
43
+ const localName = "providerChain" ;
66
44
addNamedModule ( j , source , {
67
45
importType,
68
- importedName : v3ProviderName ,
69
- packageName : "@aws-sdk/credential-providers" ,
46
+ localName,
47
+ importedName : "chain" ,
48
+ packageName : "@smithy/property-provider" ,
70
49
} ) ;
71
- credsNewExpressions . replaceWith ( ( { node } ) =>
50
+ chainNewExpressions . replaceWith ( ( { node } ) =>
72
51
j . callExpression . from ( {
73
- callee : j . identifier ( v3ProviderName ) ,
52
+ callee : j . identifier ( localName ) ,
74
53
comments : [
75
- j . commentLine ( PROVIDER_SWITCH_COMMENT ) ,
76
- j . commentLine (
77
- " This is the closest approximation from codemod of what your application needs."
78
- ) ,
79
- j . commentLine (
80
- " Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers"
81
- ) ,
54
+ j . commentLine ( identitySwitchComment ) ,
55
+ j . commentLine ( ` The ${ identityProviderChain } is now a chain of providers.` ) ,
56
+ j . commentLine ( ` Reference: https://www.npmjs.com/package/${ identityPackageName } ` ) ,
82
57
] ,
83
58
arguments : node . arguments ,
84
59
} )
85
60
) ;
86
61
}
62
+
63
+ for ( const [ v2IdentityName , v3ProviderName ] of Object . entries ( identityMap ) ) {
64
+ const credsNewExpressions = source . find (
65
+ j . NewExpression ,
66
+ getNewExpression ( v2GlobalName , v2IdentityName )
67
+ ) ;
68
+
69
+ if ( credsNewExpressions . size ( ) > 0 ) {
70
+ addNamedModule ( j , source , {
71
+ importType,
72
+ importedName : v3ProviderName ,
73
+ packageName : identityPackageName ,
74
+ } ) ;
75
+ credsNewExpressions . replaceWith ( ( { node } ) =>
76
+ j . callExpression . from ( {
77
+ callee : j . identifier ( v3ProviderName ) ,
78
+ comments : [
79
+ j . commentLine ( identitySwitchComment ) ,
80
+ j . commentLine (
81
+ " This is the closest approximation from codemod of what your application needs."
82
+ ) ,
83
+ j . commentLine ( ` Reference: https://www.npmjs.com/package/${ identityPackageName } ` ) ,
84
+ ] ,
85
+ arguments : node . arguments ,
86
+ } )
87
+ ) ;
88
+ }
89
+ }
87
90
}
88
91
} ;
0 commit comments