11import { Collection , JSCodeshift , NewExpression } from "jscodeshift" ;
2- import { AWS_CREDENTIALS_MAP } from "../config" ;
2+ import { AWS_CREDENTIALS_MAP , AWS_TOKEN_MAP } from "../config" ;
33import { ImportType , addNamedModule } from "../modules" ;
44
55export interface ReplaceAwsCredentialsOptions {
66 v2GlobalName ?: string ;
77 importType : ImportType ;
88}
99
10- const PROVIDER_SWITCH_COMMENT = ` JS SDK v3 switched credential providers from classes to functions.` ;
11-
1210const getNewExpression = ( identifier : string , className : string ) =>
1311 ( {
1412 type : "NewExpression" ,
@@ -29,60 +27,65 @@ export const replaceAwsIdentity = (
2927) => {
3028 if ( ! v2GlobalName ) return ;
3129
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` ;
5736
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 (
6139 j . NewExpression ,
62- getNewExpression ( v2GlobalName , v2CredentialsName )
40+ getNewExpression ( v2GlobalName , identityProviderChain )
6341 ) ;
64-
65- if ( credsNewExpressions . size ( ) > 0 ) {
42+ if ( chainNewExpressions . size ( ) > 0 ) {
43+ const localName = "providerChain" ;
6644 addNamedModule ( j , source , {
6745 importType,
68- importedName : v3ProviderName ,
69- packageName : "@aws-sdk/credential-providers" ,
46+ localName,
47+ importedName : "chain" ,
48+ packageName : "@smithy/property-provider" ,
7049 } ) ;
71- credsNewExpressions . replaceWith ( ( { node } ) =>
50+ chainNewExpressions . replaceWith ( ( { node } ) =>
7251 j . callExpression . from ( {
73- callee : j . identifier ( v3ProviderName ) ,
52+ callee : j . identifier ( localName ) ,
7453 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 } ` ) ,
8257 ] ,
8358 arguments : node . arguments ,
8459 } )
8560 ) ;
8661 }
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+ }
8790 }
8891} ;
0 commit comments