Skip to content

Commit 6fea980

Browse files
browser: remove fs-extra usage in CodeWhisperer logic (#4442)
* Remove fs-extra usage in CodeWhisperer logic * minor fixes from nikolas Signed-off-by: nkomonen <[email protected]> --------- Signed-off-by: nkomonen <[email protected]> Co-authored-by: Andrew Yu <[email protected]>
1 parent 66cc926 commit 6fea980

File tree

17 files changed

+92
-62
lines changed

17 files changed

+92
-62
lines changed

docs/browser.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ to help us visualize the imports and determine which module is importing a certa
8080

8181
### How to use
8282

83-
1. Install the `graphiz` cli, this provides the `dot` cli command
84-
- Mac: `brew install graphiz`
83+
1. Install the `graphviz` cli, this provides the `dot` cli command
84+
- Mac: `brew install graphviz`
8585
- Others: [See documentation](https://www.graphviz.org/download/)
8686
2. Run `npx depcruise {RELATIVE_PATH_TO_FILE} --output-type dot | dot -T svg > dependency-graph.svg`
8787
- For example, `npx depcruise src/srcShared/fs.ts --output-type dot | dot -T svg > dependency-graph.svg` generates the following which shows `fs-extra` is imported by `fileSystemUtilities.ts`:

packages/toolkit/src/codewhisperer/commands/startSecurityScan.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export async function startSecurityScan(
141141
getLogger().error('Failed to upload code artifacts', error)
142142
throw error
143143
} finally {
144-
dependencyGraph.removeTmpFiles(truncation)
144+
await dependencyGraph.removeTmpFiles(truncation)
145145
codeScanTelemetryEntry.artifactsUploadDuration = performance.now() - uploadStartTime
146146
}
147147

packages/toolkit/src/codewhisperer/util/dependencyGraph/cloudformationDependencyGraph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class cloudformationDependencyGraph extends DependencyGraph {
5959
}
6060
await sleep(1000)
6161
const truncDirPath = this.getTruncDirPath(uri)
62-
this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
62+
await this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
6363
const zipFilePath = this.zipDir(truncDirPath, CodeWhispererConstants.codeScanZipExt)
6464
const zipFileSize = statSync(zipFilePath).size
6565
return {

packages/toolkit/src/codewhisperer/util/dependencyGraph/csharpDependencyGraph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class CsharpDependencyGraph extends DependencyGraph {
171171
}
172172
await sleep(1000)
173173
const truncDirPath = this.getTruncDirPath(uri)
174-
this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
174+
await this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
175175
const zipFilePath = this.zipDir(truncDirPath, CodeWhispererConstants.codeScanZipExt)
176176
const zipFileSize = statSync(zipFilePath).size
177177
return {

packages/toolkit/src/codewhisperer/util/dependencyGraph/dependencyGraph.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import * as fs from 'fs-extra'
6+
import { fsCommon } from '../../../srcShared/fs'
77
import * as vscode from 'vscode'
88
import admZip from 'adm-zip'
9-
import { existsSync, statSync } from 'fs'
109
import { asyncCallWithTimeout } from '../commonUtil'
1110
import * as path from 'path'
1211
import { tempDirPath } from '../../../shared/filesystemUtilities'
@@ -137,12 +136,12 @@ export abstract class DependencyGraph {
137136
return paths
138137
}
139138

140-
protected copyFileToTmp(uri: vscode.Uri, destDir: string) {
139+
protected async copyFileToTmp(uri: vscode.Uri, destDir: string) {
141140
const projectName = this.getProjectName(uri)
142141
if (projectName) {
143142
const pos = uri.path.indexOf(projectName)
144143
const dest = path.join(destDir, uri.path.substring(pos))
145-
fs.copySync(uri.fsPath, dest)
144+
await fsCommon.copy(uri.fsPath, dest)
146145
}
147146
}
148147

@@ -153,18 +152,6 @@ export abstract class DependencyGraph {
153152
return dir + extension
154153
}
155154

156-
protected removeDir(dir: string) {
157-
if (existsSync(dir)) {
158-
fs.removeSync(dir)
159-
}
160-
}
161-
162-
protected removeZip(zipFilePath: string) {
163-
if (existsSync(zipFilePath)) {
164-
fs.unlinkSync(zipFilePath)
165-
}
166-
}
167-
168155
protected getTruncDirPath(uri: vscode.Uri) {
169156
if (this._truncDir === '') {
170157
this._truncDir = path.join(
@@ -175,20 +162,22 @@ export abstract class DependencyGraph {
175162
return this._truncDir
176163
}
177164

178-
protected getFilesTotalSize(files: string[]) {
179-
return files.map(file => statSync(file)).reduce((accumulator, { size }) => accumulator + size, 0)
165+
protected async getFilesTotalSize(files: string[]) {
166+
const statsPromises = files.map(file => fsCommon.stat(file))
167+
const stats = await Promise.all(statsPromises)
168+
return stats.reduce((accumulator, stat) => accumulator + stat.size, 0)
180169
}
181170

182-
protected copyFilesToTmpDir(files: Set<string> | string[], dir: string) {
183-
files.forEach(filePath => {
184-
this.copyFileToTmp(vscode.Uri.file(filePath), dir)
185-
})
171+
protected async copyFilesToTmpDir(files: Set<string> | string[], dir: string) {
172+
for (const filePath of files) {
173+
await this.copyFileToTmp(vscode.Uri.file(filePath), dir)
174+
}
186175
}
187176

188-
public removeTmpFiles(truncation: Truncation) {
177+
public async removeTmpFiles(truncation: Truncation) {
189178
getLogger().verbose(`Cleaning up temporary files...`)
190-
this.removeZip(truncation.zipFilePath)
191-
this.removeDir(truncation.rootDir)
179+
await fsCommon.delete(truncation.zipFilePath)
180+
await fsCommon.delete(truncation.rootDir)
192181
getLogger().verbose(`Complete cleaning up temporary files.`)
193182
}
194183

packages/toolkit/src/codewhisperer/util/dependencyGraph/goDependencyGraph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class GoDependencyGraph extends DependencyGraph {
6969
await sleep(1000)
7070
getLogger().verbose(`CodeWhisperer: Picked source files: [${[...this._pickedSourceFiles].join(', ')}]`)
7171
const truncDirPath = this.getTruncDirPath(uri)
72-
this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
72+
await this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
7373
const zipFilePath = this.zipDir(truncDirPath, CodeWhispererConstants.codeScanZipExt)
7474
const zipFileSize = statSync(zipFilePath).size
7575
return {

packages/toolkit/src/codewhisperer/util/dependencyGraph/javaDependencyGraph.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,10 @@ export class JavaDependencyGraph extends DependencyGraph {
342342
const buildFiles: string[] = this.generateBuildFilePaths()
343343
const truncDirPath = this.getTruncDirPath(uri)
344344
getLogger().debug(`Picked source files: [${[...this._pickedSourceFiles].join(', ')}]`)
345-
this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
345+
await this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
346346
getLogger().debug(`Picked build artifacts: [${buildFiles}]`)
347-
this.copyFilesToTmpDir(buildFiles, truncDirPath)
348-
const totalBuildSize = this.getFilesTotalSize(Array.from(buildFiles.values()))
347+
await this.copyFilesToTmpDir(buildFiles, truncDirPath)
348+
const totalBuildSize = await this.getFilesTotalSize(Array.from(buildFiles.values()))
349349
const zipFilePath = this.zipDir(truncDirPath, CodeWhispererConstants.codeScanZipExt)
350350
const zipFileSize = statSync(zipFilePath).size
351351
return {

packages/toolkit/src/codewhisperer/util/dependencyGraph/javascriptDependencyGraph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export class JavascriptDependencyGraph extends DependencyGraph {
187187
}
188188
await sleep(1000)
189189
const truncDirPath = this.getTruncDirPath(uri)
190-
this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
190+
await this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
191191
const zipFilePath = this.zipDir(truncDirPath, CodeWhispererConstants.codeScanZipExt)
192192
const zipFileSize = statSync(zipFilePath).size
193193
return {

packages/toolkit/src/codewhisperer/util/dependencyGraph/pythonDependencyGraph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export class PythonDependencyGraph extends DependencyGraph {
185185
}
186186
await sleep(1000)
187187
const truncDirPath = this.getTruncDirPath(uri)
188-
this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
188+
await this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
189189
const zipFilePath = this.zipDir(truncDirPath, CodeWhispererConstants.codeScanZipExt)
190190
const zipFileSize = statSync(zipFilePath).size
191191
return {

packages/toolkit/src/codewhisperer/util/dependencyGraph/rubyDependencyGraph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export class RubyDependencyGraph extends DependencyGraph {
196196
}
197197
await sleep(1000)
198198
const truncDirPath = this.getTruncDirPath(uri)
199-
this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
199+
await this.copyFilesToTmpDir(this._pickedSourceFiles, truncDirPath)
200200
const zipFilePath = this.zipDir(truncDirPath, CodeWhispererConstants.codeScanZipExt)
201201
const zipFileSize = statSync(zipFilePath).size
202202
return {

0 commit comments

Comments
 (0)