Skip to content

Commit 8af9ba9

Browse files
committed
prefer nodefs to fs2 across changes
1 parent aff0f6c commit 8af9ba9

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55
import * as vscode from 'vscode'
6-
import * as fs from 'fs'
6+
import * as nodefs from 'fs'
77
import * as path from 'path'
88
import * as os from 'os'
99
import * as codeWhisperer from '../../client/codewhisperer'
@@ -44,7 +44,7 @@ import { AuthUtil } from '../../util/authUtil'
4444
import { createCodeWhispererChatStreamingClient } from '../../../shared/clients/codewhispererChatClient'
4545
import { downloadExportResultArchive } from '../../../shared/utilities/download'
4646
import { ExportIntent, TransformationDownloadArtifactType } from '@amzn/codewhisperer-streaming'
47-
import fs2 from '../../../shared/fs/fs'
47+
import fs from '../../../shared/fs/fs'
4848
import { ChatSessionManager } from '../../../amazonqGumby/chat/storages/chatSession'
4949
import { convertToTimeString, encodeHTML } from '../../../shared/utilities/textUtilities'
5050

@@ -109,7 +109,7 @@ export async function uploadArtifactToS3(
109109
) {
110110
throwIfCancelled()
111111
try {
112-
const uploadFileByteSize = (await fs.promises.stat(fileName)).size
112+
const uploadFileByteSize = (await nodefs.promises.stat(fileName)).size
113113
getLogger().info(
114114
`Uploading project artifact at %s with checksum %s using uploadId: %s and size %s kB`,
115115
fileName,
@@ -177,7 +177,7 @@ export async function stopJob(jobId: string) {
177177
}
178178

179179
export async function uploadPayload(payloadFileName: string, uploadContext?: UploadContext) {
180-
const buffer = fs.readFileSync(payloadFileName)
180+
const buffer = Buffer.from(await fs.readFile(payloadFileName))
181181
const sha256 = getSha256(buffer)
182182

183183
throwIfCancelled()
@@ -249,7 +249,7 @@ function isExcludedDependencyFile(path: string): boolean {
249249
* getFilesRecursively on the source code folder.
250250
*/
251251
function getFilesRecursively(dir: string, isDependenciesFolder: boolean): string[] {
252-
const entries = fs.readdirSync(dir, { withFileTypes: true })
252+
const entries = nodefs.readdirSync(dir, { withFileTypes: true })
253253
const files = entries.flatMap((entry) => {
254254
const res = path.resolve(dir, entry.name)
255255
// exclude 'target' directory from ZIP (except if zipping dependencies) due to issues in backend
@@ -301,22 +301,22 @@ export async function zipCode({ dependenciesFolder, humanInTheLoopFlag, modulePa
301301
const sourceFiles = getFilesRecursively(modulePath, false)
302302
let sourceFilesSize = 0
303303
for (const file of sourceFiles) {
304-
if (fs.statSync(file).isDirectory()) {
304+
if (nodefs.statSync(file).isDirectory()) {
305305
getLogger().info('CodeTransformation: Skipping directory, likely a symlink')
306306
continue
307307
}
308308
const relativePath = path.relative(modulePath, file)
309309
const paddedPath = path.join('sources', relativePath)
310310
zip.addLocalFile(file, path.dirname(paddedPath))
311-
sourceFilesSize += (await fs.promises.stat(file)).size
311+
sourceFilesSize += (await nodefs.promises.stat(file)).size
312312
}
313313
getLogger().info(`CodeTransformation: source code files size = ${sourceFilesSize}`)
314314
}
315315

316316
throwIfCancelled()
317317

318318
let dependencyFiles: string[] = []
319-
if (fs.existsSync(dependenciesFolder.path)) {
319+
if (await fs.exists(dependenciesFolder.path)) {
320320
dependencyFiles = getFilesRecursively(dependenciesFolder.path, true)
321321
}
322322

@@ -330,7 +330,7 @@ export async function zipCode({ dependenciesFolder, humanInTheLoopFlag, modulePa
330330
// const paddedPath = path.join(`dependencies/${dependenciesFolder.name}`, relativePath)
331331
const paddedPath = path.join(`dependencies/`, relativePath)
332332
zip.addLocalFile(file, path.dirname(paddedPath))
333-
dependencyFilesSize += (await fs.promises.stat(file)).size
333+
dependencyFilesSize += (await nodefs.promises.stat(file)).size
334334
}
335335
getLogger().info(`CodeTransformation: dependency files size = ${dependencyFilesSize}`)
336336
dependenciesCopied = true
@@ -353,19 +353,19 @@ export async function zipCode({ dependenciesFolder, humanInTheLoopFlag, modulePa
353353
}
354354

355355
tempFilePath = path.join(os.tmpdir(), 'zipped-code.zip')
356-
fs.writeFileSync(tempFilePath, zip.toBuffer())
357-
if (fs.existsSync(dependenciesFolder.path)) {
358-
fs.rmSync(dependenciesFolder.path, { recursive: true, force: true })
356+
await fs.writeFile(tempFilePath, zip.toBuffer())
357+
if (await fs.exists(dependenciesFolder.path)) {
358+
await fs.delete(dependenciesFolder.path, { recursive: true, force: true })
359359
}
360360
} catch (e: any) {
361361
throw Error('Failed to zip project')
362362
} finally {
363363
if (logFilePath) {
364-
fs.rmSync(logFilePath)
364+
await fs.delete(logFilePath)
365365
}
366366
}
367367

368-
const zipSize = (await fs.promises.stat(tempFilePath)).size
368+
const zipSize = (await nodefs.promises.stat(tempFilePath)).size
369369

370370
const exceedsLimit = zipSize > CodeWhispererConstants.uploadZipSizeLimitInBytes
371371

@@ -409,7 +409,7 @@ export async function startJob(uploadId: string) {
409409
}
410410

411411
export function getImageAsBase64(filePath: string) {
412-
const fileContents = fs.readFileSync(filePath, { encoding: 'base64' })
412+
const fileContents = nodefs.readFileSync(filePath, { encoding: 'base64' })
413413
return `data:image/svg+xml;base64,${fileContents}`
414414
}
415415

@@ -725,9 +725,9 @@ export async function downloadAndExtractResultArchive(
725725
pathToArchiveDir: string,
726726
downloadArtifactType: TransformationDownloadArtifactType
727727
) {
728-
const archivePathExists = await fs2.existsDir(pathToArchiveDir)
728+
const archivePathExists = await fs.existsDir(pathToArchiveDir)
729729
if (!archivePathExists) {
730-
await fs2.mkdir(pathToArchiveDir)
730+
await fs.mkdir(pathToArchiveDir)
731731
}
732732

733733
const pathToArchive = path.join(pathToArchiveDir, 'ExportResultsArchive.zip')

packages/core/src/shared/extensions/yaml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export async function activateYamlExtension(): Promise<YamlExtension | undefined
5353
// SLOW: This request happens on every keystroke! (5MB+ read from filesystem).
5454
// This is a design flaw in this registerContributor() API.
5555
// TODO: this function is non-async preventing us from using our fs module.
56-
return fs2.readFileSync(vscode.Uri.parse(uri).fsPath).toString()
56+
return nodefs.readFileSync(vscode.Uri.parse(uri).fsPath).toString()
5757
} catch (e) {
5858
getLogger().error(`YAML Extension: failed to read schema URI "${uri}": ${e}`)
5959
throw new Error(`${getIdeProperties().company} Toolkit could not parse JSON schema URI: ${uri}`)

packages/core/src/test/testUtil.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import globals from '../shared/extensionGlobals'
1414
import { waitUntil } from '../shared/utilities/timeoutUtils'
1515
import { MetricName, MetricShapes } from '../shared/telemetry/telemetry'
1616
import { keys, selectFrom } from '../shared/utilities/tsUtils'
17-
import fs2 from '../shared/fs/fs'
17+
import fs from '../shared/fs/fs'
1818
import { DeclaredCommand } from '../shared/vscode/commands2'
1919
import { mkdirSync, existsSync } from 'fs'
2020
import * as nodefs from 'fs/promises'
@@ -31,15 +31,15 @@ export async function toFile(o: any, filepath: string | vscode.Uri) {
3131
const file = typeof filepath === 'string' ? filepath : filepath.fsPath
3232
const text = o === undefined ? '' : o.toString()
3333
const dir = path.dirname(file)
34-
await fs2.mkdir(dir)
35-
await fs2.writeFile(file, text)
34+
await fs.mkdir(dir)
35+
await fs.writeFile(file, text)
3636
}
3737

3838
/**
3939
* Gets the contents of `filepath` as UTF-8 encoded string.
4040
*/
4141
export async function fromFile(filepath: string): Promise<string> {
42-
return fs2.readFileAsString(filepath)
42+
return await fs.readFileAsString(filepath)
4343
}
4444

4545
/** Gets the full path to the Toolkit source root on this machine. */
@@ -155,7 +155,7 @@ export async function createTestFile(fileName: string): Promise<vscode.Uri> {
155155
const tempFolder = await makeTemporaryToolkitFolder()
156156
testTempDirs.push(tempFolder) // ensures this is deleted at the end
157157
const tempFilePath = path.join(tempFolder, fileName)
158-
await fs2.writeFile(tempFilePath, '')
158+
await fs.writeFile(tempFilePath, '')
159159
return vscode.Uri.file(tempFilePath)
160160
}
161161

@@ -203,7 +203,7 @@ export async function createTestWorkspace(
203203

204204
do {
205205
const tempFilePath = path.join(workspace.uri.fsPath, `${fileNamePrefix}${n}${fileNameSuffix}`)
206-
await fs2.writeFile(tempFilePath, fileContent)
206+
await fs.writeFile(tempFilePath, fileContent)
207207
} while (--n > 0)
208208

209209
return workspace
@@ -234,7 +234,7 @@ export function assertEqualPaths(actual: string, expected: string, message?: str
234234
* Asserts that UTF-8 contents of `file` are equal to `expected`.
235235
*/
236236
export async function assertFileText(file: string, expected: string, message?: string | Error) {
237-
const actualContents = await fs2.readFileAsString(file)
237+
const actualContents = await fs.readFileAsString(file)
238238
assert.strictEqual(actualContents, expected, message)
239239
}
240240

@@ -248,11 +248,11 @@ export async function tickPromise<T>(promise: Promise<T>, clock: FakeTimers.Inst
248248
* Creates an executable file (including any parent directories) with the given contents.
249249
*/
250250
export async function createExecutableFile(filepath: string, contents: string): Promise<void> {
251-
await fs2.mkdir(path.dirname(filepath))
251+
await fs.mkdir(path.dirname(filepath))
252252
if (process.platform === 'win32') {
253-
await fs2.writeFile(filepath, `@echo OFF$\r\n${contents}\r\n`)
253+
await fs.writeFile(filepath, `@echo OFF$\r\n${contents}\r\n`)
254254
} else {
255-
await fs2.writeFile(filepath, `#!/bin/sh\n${contents}`)
255+
await fs.writeFile(filepath, `#!/bin/sh\n${contents}`)
256256
nodeFsSync.chmodSync(filepath, 0o744)
257257
}
258258
}

0 commit comments

Comments
 (0)