Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ module.exports = {
message:
'Avoid fs-extra, use shared/fs/fs.ts. Notify the Toolkit team if your required functionality is not available.',
},
{
name: 'fs',
message: 'Avoid fs and use shared/fs/fs.ts when possible.',
},
],
},
],
Expand Down
3 changes: 1 addition & 2 deletions packages/amazonq/scripts/build/copyFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

/* eslint-disable no-restricted-imports */
import * as fs from 'fs-extra'
import * as fs from 'fs-extra' // eslint-disable-line no-restricted-imports
import * as path from 'path'

// Moves all dependencies into `dist`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('DiffModel', function () {
return true
})

testDiffModel.parseDiff(getTestResourceFilePath('resources/files/addedFile.diff'), workspacePath)
await testDiffModel.parseDiff(getTestResourceFilePath('resources/files/addedFile.diff'), workspacePath)

assert.strictEqual(testDiffModel.changes.length, 1)
const change = testDiffModel.changes[0]
Expand All @@ -49,7 +49,7 @@ describe('DiffModel', function () {
'This guide walks you through using Gradle to build a simple Java project.'
)

testDiffModel.parseDiff(getTestResourceFilePath('resources/files/modifiedFile.diff'), workspacePath)
await testDiffModel.parseDiff(getTestResourceFilePath('resources/files/modifiedFile.diff'), workspacePath)

assert.strictEqual(testDiffModel.changes.length, 1)
const change = testDiffModel.changes[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import assert from 'assert'
import sinon from 'sinon'
import * as vscode from 'vscode'
import fs from 'fs'
import * as fs from 'fs' // eslint-disable-line no-restricted-imports

const mockCodeScanFindings = JSON.stringify([
{
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('securityScanHandler', function () {
let mockClient: Stub<DefaultCodeWhispererClient>
beforeEach(function () {
mockClient = stub(DefaultCodeWhispererClient)
sinon.stub(fs, 'existsSync').returns(true)
sinon.stub(fs, 'existsSync').resolves(true)
sinon.stub(fs, 'statSync').returns({ isFile: () => true } as fs.Stats)
})

Expand Down
3 changes: 1 addition & 2 deletions packages/core/scripts/build/copyFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

/* eslint-disable no-restricted-imports */
import * as fs from 'fs-extra'
import * as fs from 'fs-extra' // eslint-disable-line no-restricted-imports
import * as path from 'path'

// Moves all dependencies into `dist`
Expand Down
2 changes: 1 addition & 1 deletion packages/core/scripts/build/generateServiceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import * as proc from 'child_process'
import * as nodefs from 'fs'
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/
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/amazonq/lsp/lspController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as vscode from 'vscode'
import * as path from 'path'
import * as crypto from 'crypto'
import { createWriteStream } from 'fs'
import { createWriteStream } from 'fs' // eslint-disable-line no-restricted-imports
import { getLogger } from '../../shared/logger/logger'
import { CurrentWsFolders, collectFilesForIndex } from '../../shared/utilities/workspaceUtils'
import fetch from 'node-fetch'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* This class is responsible for responding to UI events by calling
* the Gumby extension.
*/
import nodefs from 'fs'
import path from 'path'
import * as vscode from 'vscode'
import { GumbyNamedMessages, Messenger } from './messenger/messenger'
Expand Down Expand Up @@ -56,6 +55,7 @@ import { getAuthType } from '../../../codewhisperer/service/transformByQ/transfo
import DependencyVersions from '../../models/dependencies'
import { getStringHash } from '../../../shared/utilities/textUtilities'
import { getVersionData } from '../../../codewhisperer/service/transformByQ/transformMavenHandler'
import { fs } from '../../../shared'

// These events can be interactions within the chat,
// or elsewhere in the IDE
Expand Down Expand Up @@ -488,7 +488,7 @@ export class GumbyController {
const session = this.sessionStorage.getSession()
switch (session.conversationState) {
case ConversationState.PROMPT_JAVA_HOME: {
const pathToJavaHome = extractPath(data.message)
const pathToJavaHome = await extractPath(data.message)

if (pathToJavaHome) {
await this.prepareProjectForSubmission({
Expand Down Expand Up @@ -562,7 +562,7 @@ export class GumbyController {
* @param text
* @returns the absolute path if path points to existing folder, otherwise undefined
*/
function extractPath(text: string): string | undefined {
async function extractPath(text: string): Promise<string | undefined> {
const resolvedPath = path.resolve(text.trim())
return nodefs.existsSync(resolvedPath) ? resolvedPath : undefined
return (await fs.exists(resolvedPath)) ? resolvedPath : undefined
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

import * as vscode from 'vscode'
import * as fs from 'fs'
import * as path from 'path'
import { getLogger, Logger } from '../../../shared/logger'
import { localize } from '../../../shared/utilities/vsCodeUtils'
Expand All @@ -15,7 +14,7 @@ import { AccessAnalyzer, SharedIniFileCredentials } from 'aws-sdk'
import { execFileSync } from 'child_process'
import { ToolkitError } from '../../../shared/errors'
import { makeTemporaryToolkitFolder, tryRemoveFolder } from '../../../shared/filesystemUtilities'
import { globals } from '../../../shared'
import { fs, globals } from '../../../shared'
import {
IamPolicyChecksConstants,
PolicyChecksCheckType,
Expand Down Expand Up @@ -331,7 +330,7 @@ export class IamPolicyChecksWebview extends VueWebview {
const document = IamPolicyChecksWebview.editedDocumentFileName
customPolicyCheckDiagnosticCollection.clear()
if (referenceDocument !== '') {
fs.writeFileSync(tempFilePath, referenceDocument)
await fs.writeFile(tempFilePath, referenceDocument)
} else {
this.onCustomPolicyCheckResponse.fire([
IamPolicyChecksConstants.MissingReferenceDocError,
Expand Down Expand Up @@ -807,8 +806,8 @@ export async function renderIamPolicyChecks(context: ExtContext): Promise<VueWeb

// Helper function to get document contents from a path
export async function _readCustomChecksFile(input: string): Promise<string> {
if (fs.existsSync(input)) {
return fs.readFileSync(input).toString()
if (await fs.exists(input)) {
return (await fs.readFileBytes(input)).toString()
} else {
try {
const [region, bucket, key] = parseS3Uri(input)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/awsService/s3/commands/uploadFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as path from 'path'
import * as mime from 'mime-types'
import * as vscode from 'vscode'
import { statSync } from 'fs'
import { statSync } from 'fs' // eslint-disable-line no-restricted-imports
import { S3 } from 'aws-sdk'
import { getLogger } from '../../../shared/logger'
import { S3Node } from '../explorer/s3Nodes'
Expand Down
15 changes: 7 additions & 8 deletions packages/core/src/codewhisperer/commands/startTransformByQ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

import * as vscode from 'vscode'
import * as fs from 'fs'
import * as os from 'os'
import path from 'path'
import { getLogger } from '../../shared/logger'
Expand Down Expand Up @@ -72,7 +71,7 @@ import DependencyVersions from '../../amazonqGumby/models/dependencies'
import { dependencyNoAvailableVersions } from '../../amazonqGumby/models/constants'
import { HumanInTheLoopManager } from '../service/transformByQ/humanInTheLoopManager'
import { setContext } from '../../shared/vscode/setContext'
import { makeTemporaryToolkitFolder } from '../../shared'
import { fs, makeTemporaryToolkitFolder } from '../../shared'
import globals from '../../shared/extensionGlobals'

function getFeedbackCommentData() {
Expand All @@ -95,7 +94,7 @@ export async function processTransformFormInput(
export async function setMaven() {
let mavenWrapperExecutableName = os.platform() === 'win32' ? 'mvnw.cmd' : 'mvnw'
const mavenWrapperExecutablePath = path.join(transformByQState.getProjectPath(), mavenWrapperExecutableName)
if (fs.existsSync(mavenWrapperExecutablePath)) {
if (await fs.exists(mavenWrapperExecutablePath)) {
if (mavenWrapperExecutableName === 'mvnw') {
mavenWrapperExecutableName = './mvnw' // add the './' for non-Windows
} else if (mavenWrapperExecutableName === 'mvnw.cmd') {
Expand Down Expand Up @@ -234,8 +233,8 @@ export async function parseBuildFile() {
const alias = path.basename(os.homedir())
absolutePaths.push(alias)
const buildFilePath = path.join(transformByQState.getProjectPath(), 'pom.xml')
if (fs.existsSync(buildFilePath)) {
const buildFileContents = fs.readFileSync(buildFilePath).toString().toLowerCase()
if (await fs.exists(buildFilePath)) {
const buildFileContents = (await fs.readFileBytes(buildFilePath)).toString().toLowerCase()
const detectedPaths = []
for (const absolutePath of absolutePaths) {
if (buildFileContents.includes(absolutePath)) {
Expand Down Expand Up @@ -599,7 +598,7 @@ export async function pollTransformationStatusUntilPlanReady(jobId: string) {
throw e
}

if (fs.existsSync(pathToLog) && !transformByQState.isCancelled()) {
if ((await fs.exists(pathToLog)) && !transformByQState.isCancelled()) {
throw new TransformationPreBuildError()
} else {
// not strictly needed to reset path here and above; doing it just to represent unavailable logs
Expand All @@ -620,7 +619,7 @@ export async function pollTransformationStatusUntilPlanReady(jobId: string) {

if (plan !== undefined) {
const planFilePath = path.join(transformByQState.getProjectPath(), 'transformation-plan.md')
fs.writeFileSync(planFilePath, plan)
await fs.writeFile(planFilePath, plan)
await vscode.commands.executeCommand('markdown.showPreview', vscode.Uri.file(planFilePath))
transformByQState.setPlanFilePath(planFilePath)
await setContext('gumby.isPlanAvailable', true)
Expand Down Expand Up @@ -752,7 +751,7 @@ export async function postTransformationJob() {
}

if (transformByQState.getPayloadFilePath() !== '') {
fs.rmSync(transformByQState.getPayloadFilePath(), { recursive: true, force: true }) // delete ZIP if it exists
await fs.delete(transformByQState.getPayloadFilePath(), { recursive: true, force: true }) // delete ZIP if it exists
}
}

Expand Down
13 changes: 7 additions & 6 deletions packages/core/src/codewhisperer/service/securityScanHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { sleep } from '../../shared/utilities/timeoutUtils'
import * as codewhispererClient from '../client/codewhisperer'
import * as CodeWhispererConstants from '../models/constants'
import { existsSync, statSync, readFileSync } from 'fs'
import { existsSync, statSync } from 'fs' // eslint-disable-line no-restricted-imports
import { RawCodeScanIssue } from '../models/model'
import * as crypto from 'crypto'
import path = require('path')
Expand All @@ -39,6 +39,7 @@ import {
UploadArtifactToS3Error,
} from '../models/errors'
import { getTelemetryReasonDesc } from '../../shared/errors'
import { fs } from '../../shared'

export async function listScanResults(
client: DefaultCodeWhispererClient,
Expand Down Expand Up @@ -220,7 +221,7 @@ export async function getPresignedUrlAndUpload(
throw new InvalidSourceZipError()
}
const srcReq: CreateUploadUrlRequest = {
contentMd5: getMd5(zipMetadata.zipFilePath),
contentMd5: await getMd5(zipMetadata.zipFilePath),
artifactType: 'SourceCode',
uploadIntent: getUploadIntent(scope),
uploadContext: {
Expand Down Expand Up @@ -251,9 +252,9 @@ function getUploadIntent(scope: CodeWhispererConstants.CodeAnalysisScope): Uploa
: CodeWhispererConstants.projectScanUploadIntent
}

function getMd5(fileName: string) {
async function getMd5(fileName: string) {
const hasher = crypto.createHash('md5')
hasher.update(readFileSync(fileName))
hasher.update(await fs.readFileBytes(fileName))
return hasher.digest('base64')
}

Expand Down Expand Up @@ -288,7 +289,7 @@ export async function uploadArtifactToS3(
const logger = getLoggerForScope(scope)
const encryptionContext = `{"uploadId":"${resp.uploadId}"}`
const headersObj: Record<string, string> = {
'Content-MD5': getMd5(fileName),
'Content-MD5': await getMd5(fileName),
'x-amz-server-side-encryption': 'aws:kms',
'Content-Type': 'application/zip',
'x-amz-server-side-encryption-context': Buffer.from(encryptionContext, 'utf8').toString('base64'),
Expand All @@ -300,7 +301,7 @@ export async function uploadArtifactToS3(

try {
const response = await request.fetch('PUT', resp.uploadUrl, {
body: readFileSync(fileName),
body: await fs.readFileBytes(fileName),
headers: resp?.requestHeaders ?? headersObj,
}).response
logger.debug(`StatusCode: ${response.status}, Text: ${response.statusText}`)
Expand Down
Loading
Loading