-
Notifications
You must be signed in to change notification settings - Fork 567
/
Copy pathgenerateServiceClient.ts
254 lines (212 loc) · 8.63 KB
/
generateServiceClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import * as proc from 'child_process' // eslint-disable-line no-restricted-imports
import * as nodefs from 'fs' // eslint-disable-line no-restricted-imports
import * as path from 'path'
const repoRoot = path.join(process.cwd(), '../../') // root/packages/toolkit -> root/
/**
* This script uses the AWS JS SDK to generate service clients where the client definition is contained within
* this repo. Client definitions are added at the bottom of this script.
*/
interface ServiceClientDefinition {
serviceName: string
serviceJsonPath: string
}
async function generateServiceClients(serviceClientDefinitions: ServiceClientDefinition[]): Promise<void> {
const tempJsSdkPath = path.join(repoRoot, 'node_modules', '.zzz-awssdk2')
console.log(`Temp JS SDK Repo location: ${tempJsSdkPath}`)
console.log('Service Clients to Generate: ', serviceClientDefinitions.map((x) => x.serviceName).join(', '))
await cloneJsSdk(tempJsSdkPath)
await insertServiceClientsIntoJsSdk(tempJsSdkPath, serviceClientDefinitions)
await runTypingsGenerator(tempJsSdkPath)
await integrateServiceClients(tempJsSdkPath, serviceClientDefinitions)
console.log('Done generating service client(s)')
}
/** When cloning aws-sdk-js, we want to pull the version actually used in package-lock.json. */
function getJsSdkVersion(): string {
const json = nodefs.readFileSync(path.resolve(repoRoot, 'package-lock.json')).toString()
const packageLock = JSON.parse(json)
return packageLock['packages']['node_modules/aws-sdk']['version']
}
async function cloneJsSdk(dir: string): Promise<void> {
// Output stderr while it clones so it doesn't look frozen
return new Promise<void>((resolve, reject) => {
const sdkversion = getJsSdkVersion()
if (!sdkversion) {
throw new Error('failed to get sdk version from package-lock.json')
}
const tag = `v${sdkversion}`
const gitHead = proc.spawnSync('git', ['-C', dir, 'rev-parse', 'HEAD'])
const alreadyCloned = gitHead.status !== undefined && gitHead.status === 0
const msg = `${alreadyCloned ? 'Updating' : 'Cloning'} AWS JS SDK...
tag: ${tag}
git: status=${gitHead.status} output=${gitHead.output.toString()}`
console.log(msg)
const gitArgs = alreadyCloned
? // Local repo exists already: just update it and checkout the tag.
// Fetch only the tag we need.
// git fetch origin tag v2.950.0 --no-tags
['-C', dir, 'fetch', '--quiet', 'origin', 'tag', tag, '--no-tags']
: // Local repo does not exist: clone it.
[
'-c',
'advice.detachedHead=false',
'clone',
'--quiet',
'-b',
tag,
'--depth',
'1',
'https://github.com/aws/aws-sdk-js.git',
dir,
]
const gitCmd = proc.execFile('git', gitArgs, { encoding: 'utf8' })
gitCmd.stderr?.on('data', (data: any) => {
console.log(data)
})
gitCmd.once('close', (code, signal) => {
gitCmd.stdout?.removeAllListeners()
// Only needed for the "update" case, but harmless for "clone".
const gitCheckout = proc.spawnSync('git', [
'-c',
'advice.detachedHead=false',
'-C',
dir,
'checkout',
'--force',
tag,
])
if (gitCheckout.status !== undefined && gitCheckout.status !== 0) {
console.log(`error: git: status=${gitCheckout.status} output=${gitCheckout.output.toString()}`)
}
resolve()
})
})
}
async function insertServiceClientsIntoJsSdk(
jsSdkPath: string,
serviceClientDefinitions: ServiceClientDefinition[]
): Promise<void> {
for (const serviceClientDefinition of serviceClientDefinitions) {
const apiVersion = getApiVersion(serviceClientDefinition.serviceJsonPath)
// Copy the Service Json into the JS SDK for generation
const jsSdkServiceJsonPath = path.join(
jsSdkPath,
'apis',
`${serviceClientDefinition.serviceName.toLowerCase()}-${apiVersion}.normal.json`
)
nodefs.copyFileSync(serviceClientDefinition.serviceJsonPath, jsSdkServiceJsonPath)
}
const apiMetadataPath = path.join(jsSdkPath, 'apis', 'metadata.json')
await patchServicesIntoApiMetadata(
apiMetadataPath,
serviceClientDefinitions.map((x) => x.serviceName)
)
}
interface ServiceJsonSchema {
metadata: {
apiVersion: string
}
}
function getApiVersion(serviceJsonPath: string): string {
const json = nodefs.readFileSync(serviceJsonPath).toString()
const serviceJson = JSON.parse(json) as ServiceJsonSchema
return serviceJson.metadata.apiVersion
}
interface ApiMetadata {
[key: string]: { name: string }
}
/**
* Updates the JS SDK's api metadata to contain the provided services
*/
async function patchServicesIntoApiMetadata(apiMetadataPath: string, serviceNames: string[]): Promise<void> {
console.log(`Patching services (${serviceNames.join(', ')}) into API Metadata...`)
const apiMetadataJson = nodefs.readFileSync(apiMetadataPath).toString()
const apiMetadata = JSON.parse(apiMetadataJson) as ApiMetadata
for (const serviceName of serviceNames) {
apiMetadata[serviceName.toLowerCase()] = { name: serviceName }
}
nodefs.writeFileSync(apiMetadataPath, JSON.stringify(apiMetadata, undefined, 4))
}
/**
* Generates service clients
*/
async function runTypingsGenerator(repoPath: string): Promise<void> {
console.log('Generating service client typings...')
const stdout = proc.execFileSync('node', ['scripts/typings-generator.js'], {
encoding: 'utf8',
cwd: repoPath,
})
console.log(stdout)
}
/**
* Copies the generated service clients into the repo
*/
async function integrateServiceClients(
repoPath: string,
serviceClientDefinitions: ServiceClientDefinition[]
): Promise<void> {
for (const serviceClientDefinition of serviceClientDefinitions) {
await integrateServiceClient(
repoPath,
serviceClientDefinition.serviceJsonPath,
serviceClientDefinition.serviceName
)
}
}
/**
* Copies the generated service client into the repo
*/
async function integrateServiceClient(repoPath: string, serviceJsonPath: string, serviceName: string): Promise<void> {
const typingsFilename = `${serviceName.toLowerCase()}.d.ts`
const sourceClientPath = path.join(repoPath, 'clients', typingsFilename)
const destinationClientPath = path.join(path.dirname(serviceJsonPath), typingsFilename)
console.log(`Integrating ${typingsFilename} ...`)
nodefs.copyFileSync(sourceClientPath, destinationClientPath)
await sanitizeServiceClient(destinationClientPath)
}
/**
* Patches the type file imports to be relative to the SDK module
*/
async function sanitizeServiceClient(generatedClientPath: string): Promise<void> {
console.log('Altering Service Client to fit the codebase...')
let fileContents = nodefs.readFileSync(generatedClientPath).toString()
// Add a header stating the file is autogenerated
fileContents = `
/**
* THIS FILE IS AUTOGENERATED BY 'generateServiceClient.ts'.
* DO NOT EDIT BY HAND.
*/
${fileContents}
`
fileContents = fileContents.replace(/(import .* from.*)\.\.(.*)/g, '$1aws-sdk$2')
nodefs.writeFileSync(generatedClientPath, fileContents)
}
// ---------------------------------------------------------------------------------------------------------------------
void (async () => {
const serviceClientDefinitions: ServiceClientDefinition[] = [
{
serviceJsonPath: 'src/shared/telemetry/service-2.json',
serviceName: 'ClientTelemetry',
},
{
serviceJsonPath: 'src/codewhisperer/client/service-2.json',
serviceName: 'CodeWhispererClient',
},
{
serviceJsonPath: 'src/codewhisperer/client/user-service-2.json',
serviceName: 'CodeWhispererUserClient',
},
{
serviceJsonPath: 'src/amazonqFeatureDev/client/codewhispererruntime-2022-11-11.json',
serviceName: 'FeatureDevProxyClient',
},
{
serviceJsonPath: 'src/shared/sagemaker/client/service-2.json',
serviceName: 'SageMakerClient',
},
]
await generateServiceClients(serviceClientDefinitions)
})()