33 * SPDX-License-Identifier: Apache-2.0
44 */
55
6- /* eslint-disable no-restricted-imports */
76import * as proc from 'child_process'
8- import * as fs from 'fs-extra'
97import * as path from 'path'
8+ import { fs } from '../../src/shared'
109
1110const repoRoot = path . join ( process . cwd ( ) , '../../' ) // root/packages/toolkit -> root/
1211/**
@@ -33,17 +32,17 @@ async function generateServiceClients(serviceClientDefinitions: ServiceClientDef
3332}
3433
3534/** When cloning aws-sdk-js, we want to pull the version actually used in package-lock.json. */
36- function getJsSdkVersion ( ) : string {
37- const json = fs . readFileSync ( path . resolve ( repoRoot , 'package-lock.json' ) ) . toString ( )
35+ async function getJsSdkVersion ( ) : Promise < string > {
36+ const json = ( await fs . readFileBytes ( path . resolve ( repoRoot , 'package-lock.json' ) ) ) . toString ( )
3837 const packageLock = JSON . parse ( json )
3938
4039 return packageLock [ 'packages' ] [ 'node_modules/aws-sdk' ] [ 'version' ]
4140}
4241
4342async function cloneJsSdk ( dir : string ) : Promise < void > {
4443 // Output stderr while it clones so it doesn't look frozen
45- return new Promise < void > ( ( resolve , reject ) => {
46- const sdkversion = getJsSdkVersion ( )
44+ return new Promise < void > ( async ( resolve , reject ) => {
45+ const sdkversion = await getJsSdkVersion ( )
4746 if ( ! sdkversion ) {
4847 throw new Error ( 'failed to get sdk version from package-lock.json' )
4948 }
@@ -107,17 +106,17 @@ async function insertServiceClientsIntoJsSdk(
107106 jsSdkPath : string ,
108107 serviceClientDefinitions : ServiceClientDefinition [ ]
109108) : Promise < void > {
110- serviceClientDefinitions . forEach ( ( serviceClientDefinition ) => {
111- const apiVersion = getApiVersion ( serviceClientDefinition . serviceJsonPath )
109+ for ( const serviceClientDefinition of serviceClientDefinitions ) {
110+ const apiVersion = await getApiVersion ( serviceClientDefinition . serviceJsonPath )
112111
113112 // Copy the Service Json into the JS SDK for generation
114113 const jsSdkServiceJsonPath = path . join (
115114 jsSdkPath ,
116115 'apis' ,
117116 `${ serviceClientDefinition . serviceName . toLowerCase ( ) } -${ apiVersion } .normal.json`
118117 )
119- fs . copyFileSync ( serviceClientDefinition . serviceJsonPath , jsSdkServiceJsonPath )
120- } )
118+ await fs . copy ( serviceClientDefinition . serviceJsonPath , jsSdkServiceJsonPath )
119+ }
121120
122121 const apiMetadataPath = path . join ( jsSdkPath , 'apis' , 'metadata.json' )
123122 await patchServicesIntoApiMetadata (
@@ -132,8 +131,8 @@ interface ServiceJsonSchema {
132131 }
133132}
134133
135- function getApiVersion ( serviceJsonPath : string ) : string {
136- const json = fs . readFileSync ( serviceJsonPath ) . toString ( )
134+ async function getApiVersion ( serviceJsonPath : string ) : Promise < string > {
135+ const json = ( await fs . readFileBytes ( serviceJsonPath ) ) . toString ( )
137136 const serviceJson = JSON . parse ( json ) as ServiceJsonSchema
138137
139138 return serviceJson . metadata . apiVersion
@@ -149,14 +148,14 @@ interface ApiMetadata {
149148async function patchServicesIntoApiMetadata ( apiMetadataPath : string , serviceNames : string [ ] ) : Promise < void > {
150149 console . log ( `Patching services (${ serviceNames . join ( ', ' ) } ) into API Metadata...` )
151150
152- const apiMetadataJson = fs . readFileSync ( apiMetadataPath ) . toString ( )
151+ const apiMetadataJson = ( await fs . readFileBytes ( apiMetadataPath ) ) . toString ( )
153152 const apiMetadata = JSON . parse ( apiMetadataJson ) as ApiMetadata
154153
155154 serviceNames . forEach ( ( serviceName ) => {
156155 apiMetadata [ serviceName . toLowerCase ( ) ] = { name : serviceName }
157156 } )
158157
159- fs . writeFileSync ( apiMetadataPath , JSON . stringify ( apiMetadata , undefined , 4 ) )
158+ await fs . writeFile ( apiMetadataPath , JSON . stringify ( apiMetadata , undefined , 4 ) )
160159}
161160
162161/**
@@ -198,7 +197,7 @@ async function integrateServiceClient(repoPath: string, serviceJsonPath: string,
198197
199198 console . log ( `Integrating ${ typingsFilename } ...` )
200199
201- fs . copyFileSync ( sourceClientPath , destinationClientPath )
200+ await fs . copy ( sourceClientPath , destinationClientPath )
202201
203202 await sanitizeServiceClient ( destinationClientPath )
204203}
@@ -209,7 +208,7 @@ async function integrateServiceClient(repoPath: string, serviceJsonPath: string,
209208async function sanitizeServiceClient ( generatedClientPath : string ) : Promise < void > {
210209 console . log ( 'Altering Service Client to fit the codebase...' )
211210
212- let fileContents = fs . readFileSync ( generatedClientPath ) . toString ( )
211+ let fileContents = ( await fs . readFileBytes ( generatedClientPath ) ) . toString ( )
213212
214213 // Add a header stating the file is autogenerated
215214 fileContents = `
@@ -223,7 +222,7 @@ ${fileContents}
223222
224223 fileContents = fileContents . replace ( / ( i m p o r t .* f r o m .* ) \. \. ( .* ) / g, '$1aws-sdk$2' )
225224
226- fs . writeFileSync ( generatedClientPath , fileContents )
225+ await fs . writeFile ( generatedClientPath , fileContents )
227226}
228227
229228// ---------------------------------------------------------------------------------------------------------------------
0 commit comments