From 67a2d6bcf6575052bd10b49eb07f3151e9b5c003 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 8 Oct 2024 12:09:07 -0400 Subject: [PATCH 1/5] initial work --- .../service/securityScanHandler.test.ts | 4 +- .../chat/controller/controller.ts | 8 +- .../accessanalyzer/vue/iamPolicyChecks.ts | 9 +-- .../commands/startTransformByQ.ts | 15 ++-- .../service/securityScanHandler.ts | 13 ++-- .../transformByQ/transformApiHandler.ts | 36 ++++----- .../transformByQ/transformFileHandler.ts | 7 +- .../transformationResultsViewProvider.ts | 75 ++++++++++--------- .../dynamicResources/awsResourceManager.ts | 5 +- .../commands/downloadSchemaItemCode.ts | 19 ++--- .../lambda/vue/remoteInvoke/invokeLambda.ts | 6 +- .../shared/cloudformation/cloudformation.ts | 5 +- packages/core/src/shared/extensions/yaml.ts | 4 +- .../core/src/shared/fs/templateRegistry.ts | 2 +- packages/core/src/shared/icons.ts | 18 ++--- .../resourcefetcher/httpResourceFetcher.ts | 6 +- .../awsSamDebugConfigurationValidator.ts | 22 +++--- packages/core/src/shared/sshConfig.ts | 8 +- .../src/shared/utilities/streamUtilities.ts | 8 +- .../src/shared/utilities/textUtilities.ts | 11 +-- ...etStateMachineDefinitionFromCfnTemplate.ts | 9 ++- .../test/awsService/ec2/sshKeyPair.test.ts | 4 +- .../core/src/test/codewhisperer/testUtil.ts | 6 +- .../sso/ssoAccessTokenProvider.test.ts | 4 +- .../awsResourceManager.test.ts | 3 +- .../commands/downloadSchemaItemCode.test.ts | 8 +- packages/core/src/test/shared/fs/fs.test.ts | 3 +- packages/core/src/test/shared/icons.test.ts | 40 +++++----- .../shared/logger/sharedFileTransport.test.ts | 3 +- .../test/shared/logger/toolkitLogger.test.ts | 6 +- .../src/test/shared/vscode/runCommand.test.ts | 8 +- packages/core/src/test/testUtil.ts | 5 +- packages/core/src/testLint/gitSecrets.test.ts | 40 +++++----- 33 files changed, 208 insertions(+), 212 deletions(-) diff --git a/packages/amazonq/test/unit/codewhisperer/service/securityScanHandler.test.ts b/packages/amazonq/test/unit/codewhisperer/service/securityScanHandler.test.ts index c88a9001a87..d72d422fd0d 100644 --- a/packages/amazonq/test/unit/codewhisperer/service/securityScanHandler.test.ts +++ b/packages/amazonq/test/unit/codewhisperer/service/securityScanHandler.test.ts @@ -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' const mockCodeScanFindings = JSON.stringify([ { @@ -72,7 +72,7 @@ describe('securityScanHandler', function () { let mockClient: Stub 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) }) diff --git a/packages/core/src/amazonqGumby/chat/controller/controller.ts b/packages/core/src/amazonqGumby/chat/controller/controller.ts index eea6313e449..65331aa7766 100644 --- a/packages/core/src/amazonqGumby/chat/controller/controller.ts +++ b/packages/core/src/amazonqGumby/chat/controller/controller.ts @@ -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' @@ -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 @@ -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({ @@ -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 { const resolvedPath = path.resolve(text.trim()) - return nodefs.existsSync(resolvedPath) ? resolvedPath : undefined + return (await fs.exists(resolvedPath)) ? resolvedPath : undefined } diff --git a/packages/core/src/awsService/accessanalyzer/vue/iamPolicyChecks.ts b/packages/core/src/awsService/accessanalyzer/vue/iamPolicyChecks.ts index 3af08e76cea..4bca437f553 100644 --- a/packages/core/src/awsService/accessanalyzer/vue/iamPolicyChecks.ts +++ b/packages/core/src/awsService/accessanalyzer/vue/iamPolicyChecks.ts @@ -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' @@ -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, @@ -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, @@ -807,8 +806,8 @@ export async function renderIamPolicyChecks(context: ExtContext): Promise { - 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) diff --git a/packages/core/src/codewhisperer/commands/startTransformByQ.ts b/packages/core/src/codewhisperer/commands/startTransformByQ.ts index 4d3c667f235..7f263901fe4 100644 --- a/packages/core/src/codewhisperer/commands/startTransformByQ.ts +++ b/packages/core/src/codewhisperer/commands/startTransformByQ.ts @@ -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' @@ -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() { @@ -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') { @@ -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)) { @@ -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 @@ -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) @@ -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 } } diff --git a/packages/core/src/codewhisperer/service/securityScanHandler.ts b/packages/core/src/codewhisperer/service/securityScanHandler.ts index e746ad6e108..50db7cc4ada 100644 --- a/packages/core/src/codewhisperer/service/securityScanHandler.ts +++ b/packages/core/src/codewhisperer/service/securityScanHandler.ts @@ -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' import { RawCodeScanIssue } from '../models/model' import * as crypto from 'crypto' import path = require('path') @@ -39,6 +39,7 @@ import { UploadArtifactToS3Error, } from '../models/errors' import { getTelemetryReasonDesc } from '../../shared/errors' +import { fs } from '../../shared' export async function listScanResults( client: DefaultCodeWhispererClient, @@ -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: { @@ -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') } @@ -288,7 +289,7 @@ export async function uploadArtifactToS3( const logger = getLoggerForScope(scope) const encryptionContext = `{"uploadId":"${resp.uploadId}"}` const headersObj: Record = { - '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'), @@ -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}`) diff --git a/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts b/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts index b10418bd3af..6d837ebe5c8 100644 --- a/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts +++ b/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ import * as vscode from 'vscode' -import * as nodefs from 'fs' import * as path from 'path' import * as os from 'os' import * as codeWhisperer from '../../client/codewhisperer' @@ -47,6 +46,7 @@ import { ExportIntent, TransformationDownloadArtifactType } from '@amzn/codewhis import fs from '../../../shared/fs/fs' import { ChatSessionManager } from '../../../amazonqGumby/chat/storages/chatSession' import { convertToTimeString, encodeHTML } from '../../../shared/utilities/textUtilities' +import { readdirSync } from 'fs' export function getSha256(buffer: Buffer) { const hasher = crypto.createHash('sha256') @@ -109,7 +109,7 @@ export async function uploadArtifactToS3( ) { throwIfCancelled() try { - const uploadFileByteSize = (await nodefs.promises.stat(fileName)).size + const uploadFileByteSize = (await fs.stat(fileName)).size getLogger().info( `Uploading project artifact at %s with checksum %s using uploadId: %s and size %s kB`, fileName, @@ -249,7 +249,7 @@ function isExcludedDependencyFile(path: string): boolean { * getFilesRecursively on the source code folder. */ function getFilesRecursively(dir: string, isDependenciesFolder: boolean): string[] { - const entries = nodefs.readdirSync(dir, { withFileTypes: true }) + const entries = readdirSync(dir, { withFileTypes: true }) const files = entries.flatMap((entry) => { const res = path.resolve(dir, entry.name) // exclude 'target' directory from ZIP (except if zipping dependencies) due to issues in backend @@ -301,14 +301,14 @@ export async function zipCode({ dependenciesFolder, humanInTheLoopFlag, modulePa const sourceFiles = getFilesRecursively(modulePath, false) let sourceFilesSize = 0 for (const file of sourceFiles) { - if (nodefs.statSync(file).isDirectory()) { + if (await fs.existsDir(file)) { getLogger().info('CodeTransformation: Skipping directory, likely a symlink') continue } const relativePath = path.relative(modulePath, file) const paddedPath = path.join('sources', relativePath) zip.addLocalFile(file, path.dirname(paddedPath)) - sourceFilesSize += (await nodefs.promises.stat(file)).size + sourceFilesSize += (await fs.stat(file)).size } getLogger().info(`CodeTransformation: source code files size = ${sourceFilesSize}`) } @@ -330,7 +330,7 @@ export async function zipCode({ dependenciesFolder, humanInTheLoopFlag, modulePa // const paddedPath = path.join(`dependencies/${dependenciesFolder.name}`, relativePath) const paddedPath = path.join(`dependencies/`, relativePath) zip.addLocalFile(file, path.dirname(paddedPath)) - dependencyFilesSize += (await nodefs.promises.stat(file)).size + dependencyFilesSize += (await fs.stat(file)).size } getLogger().info(`CodeTransformation: dependency files size = ${dependencyFilesSize}`) dependenciesCopied = true @@ -365,7 +365,7 @@ export async function zipCode({ dependenciesFolder, humanInTheLoopFlag, modulePa } } - const zipSize = (await nodefs.promises.stat(tempFilePath)).size + const zipSize = (await fs.stat(tempFilePath)).size const exceedsLimit = zipSize > CodeWhispererConstants.uploadZipSizeLimitInBytes @@ -408,8 +408,8 @@ export async function startJob(uploadId: string) { } } -export function getImageAsBase64(filePath: string) { - const fileContents = nodefs.readFileSync(filePath, { encoding: 'base64' }) +export async function getImageAsBase64(filePath: string) { + const fileContents = Buffer.from(await fs.readFileBytes(filePath)).toString('base64') return `data:image/svg+xml;base64,${fileContents}` } @@ -418,7 +418,7 @@ export function getImageAsBase64(filePath: string) { * ex. getIcon('transform-file') returns the 'transform-file-light.svg' icon if user has a light theme enabled, * otherwise 'transform-file-dark.svg' is returned. */ -export function getTransformationIcon(name: string) { +export async function getTransformationIcon(name: string) { let iconPath = '' switch (name) { case 'linesOfCode': @@ -448,7 +448,7 @@ export function getTransformationIcon(name: string) { } else { iconPath += '-dark.svg' } - return getImageAsBase64(globals.context.asAbsolutePath(path.join('resources/icons/aws/amazonq', iconPath))) + return await getImageAsBase64(globals.context.asAbsolutePath(path.join('resources/icons/aws/amazonq', iconPath))) } export function getFormattedString(s: string) { @@ -495,17 +495,17 @@ export function getTableMapping(stepZeroProgressUpdates: ProgressUpdates) { return map } -export function getJobStatisticsHtml(jobStatistics: any) { +export async function getJobStatisticsHtml(jobStatistics: any) { let htmlString = '' if (jobStatistics.length === 0) { return htmlString } htmlString += `
` - jobStatistics.forEach((stat: { name: string; value: string }) => { - htmlString += `

${getFormattedString(stat.name)}: ${stat.value}

` - }) + } htmlString += `
` return htmlString } @@ -533,9 +533,9 @@ export async function getTransformationPlan(jobId: string) { const jobStatistics = JSON.parse(tableMapping['0']).rows // ID of '0' reserved for job statistics table // get logo directly since we only use one logo regardless of color theme - const logoIcon = getTransformationIcon('transformLogo') + const logoIcon = await getTransformationIcon('transformLogo') - const arrowIcon = getTransformationIcon('upArrow') + const arrowIcon = await getTransformationIcon('upArrow') let plan = `\n\n

${CodeWhispererConstants.planTitle}


` const authType = await getAuthType() @@ -547,7 +547,7 @@ export async function getTransformationPlan(jobId: string) { } plan += `

${ CodeWhispererConstants.planIntroductionMessage - }

${getJobStatisticsHtml(jobStatistics)}
` + }

${await getJobStatisticsHtml(jobStatistics)}` plan += `

${CodeWhispererConstants.planHeaderMessage}

${CodeWhispererConstants.planDisclaimerMessage} Read more.

` response.transformationPlan.transformationSteps.slice(1).forEach((step) => { plan += `

${step.name}

Scroll to top

${step.description}

` diff --git a/packages/core/src/codewhisperer/service/transformByQ/transformFileHandler.ts b/packages/core/src/codewhisperer/service/transformByQ/transformFileHandler.ts index d7040a11cd6..17df83fd67c 100644 --- a/packages/core/src/codewhisperer/service/transformByQ/transformFileHandler.ts +++ b/packages/core/src/codewhisperer/service/transformByQ/transformFileHandler.ts @@ -8,7 +8,6 @@ import * as path from 'path' import * as os from 'os' import xml2js = require('xml2js') import * as CodeWhispererConstants from '../../models/constants' -import { existsSync, writeFileSync } from 'fs' import { BuildSystem, FolderInfo, transformByQState } from '../../models/model' import { IManifestFile } from '../../../amazonqFeatureDev/models' import fs from '../../../shared/fs/fs' @@ -25,13 +24,13 @@ export function getDependenciesFolderInfo(): FolderInfo { export async function writeLogs() { const logFilePath = path.join(os.tmpdir(), 'build-logs.txt') - writeFileSync(logFilePath, transformByQState.getErrorLog()) + await fs.writeFile(logFilePath, transformByQState.getErrorLog()) return logFilePath } export async function checkBuildSystem(projectPath: string) { const mavenBuildFilePath = path.join(projectPath, 'pom.xml') - if (existsSync(mavenBuildFilePath)) { + if (await fs.exists(mavenBuildFilePath)) { return BuildSystem.Maven } return BuildSystem.Unknown @@ -55,7 +54,7 @@ export async function createPomCopy( export async function replacePomVersion(pomFileVirtualFileReference: vscode.Uri, version: string, delimiter: string) { const pomFileText = await fs.readFileText(pomFileVirtualFileReference.fsPath) const pomFileTextWithNewVersion = pomFileText.replace(delimiter, version) - writeFileSync(pomFileVirtualFileReference.fsPath, pomFileTextWithNewVersion) + await fs.writeFile(pomFileVirtualFileReference.fsPath, pomFileTextWithNewVersion) } export async function getJsonValuesFromManifestFile( diff --git a/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts b/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts index 2f31027fecc..d8842c9f840 100644 --- a/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts +++ b/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts @@ -5,7 +5,6 @@ import AdmZip from 'adm-zip' import os from 'os' -import fs from 'fs' import { parsePatch, applyPatches, ParsedDiff } from 'diff' import path from 'path' import vscode from 'vscode' @@ -20,17 +19,19 @@ import * as CodeWhispererConstants from '../../models/constants' import { createCodeWhispererChatStreamingClient } from '../../../shared/clients/codewhispererChatClient' import { ChatSessionManager } from '../../../amazonqGumby/chat/storages/chatSession' import { setContext } from '../../../shared/vscode/setContext' +import { fs } from '../../../shared' +import { existsSync, readFileSync, rmSync, writeFileSync } from 'fs' export abstract class ProposedChangeNode { abstract readonly resourcePath: string abstract generateCommand(): vscode.Command abstract generateDescription(): string - abstract saveFile(): void + abstract saveFile(): Promise - public saveChange(): void { + public async saveChange(): Promise { try { - this.saveFile() + await this.saveFile() } catch (err) { //to do: file system-related error handling if (err instanceof Error) { @@ -66,8 +67,8 @@ export class ModifiedChangeNode extends ProposedChangeNode { return 'M' } - override saveFile(): void { - fs.copyFileSync(this.tmpChangedPath, this.originalPath) + override async saveFile(): Promise { + await fs.copy(this.tmpChangedPath, this.originalPath) } } @@ -96,13 +97,13 @@ export class AddedChangeNode extends ProposedChangeNode { return 'A' } - override saveFile(): void { + override async saveFile(): Promise { // create parent directory before copying files (ex. for the summary/ and assets/ folders) const parentDir = path.dirname(this.pathToWorkspaceFile) - if (!fs.existsSync(parentDir)) { - fs.mkdirSync(parentDir, { recursive: true }) + if (!(await fs.exists(parentDir))) { + await fs.mkdir(parentDir) } - fs.copyFileSync(this.pathToTmpFile, this.pathToWorkspaceFile) + await fs.copy(this.pathToTmpFile, this.pathToWorkspaceFile) } } @@ -121,19 +122,19 @@ export class DiffModel { * @param changedFiles List of files that were changed * @returns Path to the folder containing the copied files */ - public copyProject(pathToWorkspace: string, changedFiles: ParsedDiff[]) { + public async copyProject(pathToWorkspace: string, changedFiles: ParsedDiff[]) { const pathToTmpSrcDir = path.join(os.tmpdir(), `project-copy-${Date.now()}`) - fs.mkdirSync(pathToTmpSrcDir) - changedFiles.forEach((file) => { - const pathToTmpFile = path.join(pathToTmpSrcDir, file.oldFileName!.substring(2)) + await fs.mkdir(pathToTmpSrcDir) + for (const file of changedFiles) { + const pathToTmpFile = path.join(pathToTmpSrcDir, file.newFileName!.substring(2)) // use mkdirsSync to create parent directories in pathToTmpFile too - fs.mkdirSync(path.dirname(pathToTmpFile), { recursive: true }) + await fs.mkdir(path.dirname(pathToTmpFile)) const pathToOldFile = path.join(pathToWorkspace, file.oldFileName!.substring(2)) // pathToOldFile will not exist for new files such as summary.md - if (fs.existsSync(pathToOldFile)) { - fs.copyFileSync(pathToOldFile, pathToTmpFile) + if (await fs.exists(pathToOldFile)) { + await fs.copy(pathToOldFile, pathToTmpFile) } - }) + } return pathToTmpSrcDir } @@ -142,23 +143,23 @@ export class DiffModel { * @param pathToWorkspace Path to the project that was transformed * @returns List of nodes containing the paths of files that were modified, added, or removed */ - public parseDiff(pathToDiff: string, pathToWorkspace: string): ProposedChangeNode[] { - const diffContents = fs.readFileSync(pathToDiff, 'utf8') + public async parseDiff(pathToDiff: string, pathToWorkspace: string): Promise { + const diffContents = await fs.readFileText(pathToDiff) const changedFiles = parsePatch(diffContents) // path to the directory containing copy of the changed files in the transformed project - const pathToTmpSrcDir = this.copyProject(pathToWorkspace, changedFiles) + const pathToTmpSrcDir = await this.copyProject(pathToWorkspace, changedFiles) transformByQState.setProjectCopyFilePath(pathToTmpSrcDir) applyPatches(changedFiles, { loadFile: function (fileObj, callback) { // load original contents of file const filePath = path.join(pathToWorkspace, fileObj.oldFileName!.substring(2)) - if (!fs.existsSync(filePath)) { + if (existsSync(filePath)) { // must be a new file (ex. summary.md), so pass empty string as original contents and do not pass error callback(undefined, '') } else { // must be a modified file (most common), so pass original contents - const fileContents = fs.readFileSync(filePath, 'utf-8') + const fileContents = readFileSync(filePath, 'utf-8') callback(undefined, fileContents) } }, @@ -166,7 +167,7 @@ export class DiffModel { patched: function (fileObj, content, callback) { const filePath = path.join(pathToTmpSrcDir, fileObj.newFileName!.substring(2)) // write changed contents to the copy of the original file (or create a new file) - fs.writeFileSync(filePath, content) + writeFileSync(filePath, content) callback(undefined) }, complete: function (err) { @@ -185,8 +186,8 @@ export class DiffModel { const originalPath = path.join(pathToWorkspace, file.oldFileName!.substring(2)) const tmpChangedPath = path.join(pathToTmpSrcDir, file.newFileName!.substring(2)) - const originalFileExists = fs.existsSync(originalPath) - const changedFileExists = fs.existsSync(tmpChangedPath) + const originalFileExists = existsSync(originalPath) + const changedFileExists = existsSync(tmpChangedPath) if (originalFileExists && changedFileExists) { return new ModifiedChangeNode(originalPath, tmpChangedPath) @@ -207,10 +208,10 @@ export class DiffModel { return this.changes[0] } - public saveChanges() { - this.changes.forEach((file) => { - file.saveChange() - }) + public async saveChanges() { + for (const file of this.changes) { + await file.saveChange() + } this.clearChanges() } @@ -271,11 +272,11 @@ export class ProposedTransformationExplorer { await setContext('gumby.reviewState', TransformByQReviewStatus.NotStarted) // delete result archive after changes cleared; summary is under ResultArchiveFilePath - if (fs.existsSync(transformByQState.getResultArchiveFilePath())) { - fs.rmSync(transformByQState.getResultArchiveFilePath(), { recursive: true, force: true }) + if (existsSync(transformByQState.getResultArchiveFilePath())) { + rmSync(transformByQState.getResultArchiveFilePath(), { recursive: true, force: true }) } - if (fs.existsSync(transformByQState.getProjectCopyFilePath())) { - fs.rmSync(transformByQState.getProjectCopyFilePath(), { recursive: true, force: true }) + if (existsSync(transformByQState.getProjectCopyFilePath())) { + rmSync(transformByQState.getProjectCopyFilePath(), { recursive: true, force: true }) } diffModel.clearChanges() @@ -341,7 +342,7 @@ export class ProposedTransformationExplorer { ) // Update downloaded artifact size - exportResultsArchiveSize = (await fs.promises.stat(pathToArchive)).size + exportResultsArchiveSize = (await fs.stat(pathToArchive)).size telemetry.record({ codeTransformTotalByteSize: exportResultsArchiveSize }) }) @@ -372,7 +373,7 @@ export class ProposedTransformationExplorer { pathContainingArchive = path.dirname(pathToArchive) const zip = new AdmZip(pathToArchive) zip.extractAllTo(pathContainingArchive) - diffModel.parseDiff( + await diffModel.parseDiff( path.join(pathContainingArchive, ExportResultArchiveStructure.PathToDiffPatch), transformByQState.getProjectPath() ) @@ -405,7 +406,7 @@ export class ProposedTransformationExplorer { }) vscode.commands.registerCommand('aws.amazonq.transformationHub.reviewChanges.acceptChanges', async () => { - diffModel.saveChanges() + await diffModel.saveChanges() telemetry.ui_click.emit({ elementId: 'transformationHub_acceptChanges' }) void vscode.window.showInformationMessage(CodeWhispererConstants.changesAppliedNotification) transformByQState.getChatControllers()?.transformationFinished.fire({ diff --git a/packages/core/src/dynamicResources/awsResourceManager.ts b/packages/core/src/dynamicResources/awsResourceManager.ts index 6c09423be64..690d45a5fe2 100644 --- a/packages/core/src/dynamicResources/awsResourceManager.ts +++ b/packages/core/src/dynamicResources/awsResourceManager.ts @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { writeFileSync } from 'fs' import * as path from 'path' import * as vscode from 'vscode' import { CloudFormationClient } from '../shared/clients/cloudFormationClient' @@ -163,7 +162,7 @@ export class AwsResourceManager { `.${normalizedTypeName}.awsResource.json` ) const fullPath = path.join(this.folder!, filename) - writeFileSync(fullPath, contents) + await fs.writeFile(fullPath, contents) return vscode.Uri.file(fullPath) } @@ -184,7 +183,7 @@ export class AwsResourceManager { this.extensionContext.globalStorageUri.fsPath, `${normalizedTypeName}.schema.json` ) - writeFileSync(schemaFile, JSON.stringify(JSON.parse(type.Schema), undefined, 2)) + await fs.writeFile(schemaFile, JSON.stringify(JSON.parse(type.Schema), undefined, 2)) location = vscode.Uri.file(schemaFile) const typeSchema = JSON.parse(await readFileAsString(schemaFile)) as TypeSchema this.schemas.set(typeName, { diff --git a/packages/core/src/eventSchemas/commands/downloadSchemaItemCode.ts b/packages/core/src/eventSchemas/commands/downloadSchemaItemCode.ts index 5077b21c909..31a40def224 100644 --- a/packages/core/src/eventSchemas/commands/downloadSchemaItemCode.ts +++ b/packages/core/src/eventSchemas/commands/downloadSchemaItemCode.ts @@ -6,7 +6,6 @@ import * as nls from 'vscode-nls' const localize = nls.loadMessageBundle() import { Schemas } from 'aws-sdk' -import fs = require('fs') import path = require('path') import * as vscode from 'vscode' import { SchemaClient } from '../../shared/clients/schemaClient' @@ -26,6 +25,8 @@ import { import admZip from 'adm-zip' import globals from '../../shared/extensionGlobals' import { telemetry } from '../../shared/telemetry/telemetry' +import { closeSync, openSync, writeSync } from 'fs' +import { fs } from '../../shared' enum CodeGenerationStatus { CREATE_COMPLETE = 'CREATE_COMPLETE', @@ -296,12 +297,12 @@ export class CodeExtractor { //write binary data into a temp zip file in a temp directory const zipContentsBinary = new Uint8Array(zipContents) - const fd = fs.openSync(codeZipFile, 'w') - fs.writeSync(fd, zipContentsBinary, 0, zipContentsBinary.byteLength, 0) - fs.closeSync(fd) + const fd = openSync(codeZipFile, 'w') + writeSync(fd, zipContentsBinary, 0, zipContentsBinary.byteLength, 0) + closeSync(fd) let overwriteFiles: boolean = false - const collisionExist = this.checkFileCollisions(codeZipFile, destinationDirectory) + const collisionExist = await this.checkFileCollisions(codeZipFile, destinationDirectory) if (collisionExist) { overwriteFiles = await this.confirmOverwriteCollisions() @@ -323,21 +324,21 @@ export class CodeExtractor { } // Check if downloaded code hierarchy has collisions with the destination directory and display them in output channel - public checkFileCollisions(codeZipFile: string, destinationDirectory: string): boolean { + public async checkFileCollisions(codeZipFile: string, destinationDirectory: string): Promise { const zip = new admZip(codeZipFile) const zipEntries = zip.getEntries() const detectedCollisions: string[] = [] - zipEntries.forEach(function (zipEntry) { + for (const zipEntry of zipEntries) { if (zipEntry.isDirectory) { // Ignore directories because those can/will merged } else { const intendedDestinationPath = path.join(destinationDirectory, '/', zipEntry.entryName) - if (fs.existsSync(intendedDestinationPath)) { + if (await fs.exists(intendedDestinationPath)) { detectedCollisions.push(intendedDestinationPath) } } - }) + } if (detectedCollisions.length > 0) { this.writeToOutputChannel(detectedCollisions) diff --git a/packages/core/src/lambda/vue/remoteInvoke/invokeLambda.ts b/packages/core/src/lambda/vue/remoteInvoke/invokeLambda.ts index 852537800f8..306abbf1c39 100644 --- a/packages/core/src/lambda/vue/remoteInvoke/invokeLambda.ts +++ b/packages/core/src/lambda/vue/remoteInvoke/invokeLambda.ts @@ -4,7 +4,6 @@ */ import { _Blob } from 'aws-sdk/clients/lambda' -import { readFileSync } from 'fs' import * as _ from 'lodash' import * as vscode from 'vscode' import { DefaultLambdaClient, LambdaClient } from '../../../shared/clients/lambdaClient' @@ -20,6 +19,7 @@ import * as nls from 'vscode-nls' import { VueWebview } from '../../../webviews/main' import { telemetry } from '../../../shared/telemetry/telemetry' import { Result } from '../../../shared/telemetry/telemetry' +import { fs } from '../../../shared' const localize = nls.loadMessageBundle() @@ -99,14 +99,14 @@ export class RemoteInvokeWebview extends VueWebview { } try { - const fileContent = readFileSync(fileLocations[0].fsPath, { encoding: 'utf8' }) + const fileContent = fs.readFileText(fileLocations[0].fsPath) return { sample: fileContent, selectedFile: fileLocations[0].path, } } catch (e) { - getLogger().error('readFileSync: Failed to read file at path %O', fileLocations[0].fsPath, e) + getLogger().error('readFileText: Failed to read file at path %O', fileLocations[0].fsPath, e) void vscode.window.showErrorMessage((e as Error).message) } } diff --git a/packages/core/src/shared/cloudformation/cloudformation.ts b/packages/core/src/shared/cloudformation/cloudformation.ts index f8548a4b929..3713727b21e 100644 --- a/packages/core/src/shared/cloudformation/cloudformation.ts +++ b/packages/core/src/shared/cloudformation/cloudformation.ts @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { promises as nodefs } from 'fs' import * as vscode from 'vscode' import { schema } from 'yaml-cfn' import * as yaml from 'js-yaml' @@ -461,7 +460,7 @@ export async function tryLoad( export async function save(template: Template, filename: string): Promise { const templateAsYaml: string = yaml.dump(template) - await nodefs.writeFile(filename, templateAsYaml, 'utf8') + await fs.writeFile(filename, templateAsYaml) } export function validateTemplate(template: Template): void { @@ -880,7 +879,7 @@ export async function createStarterTemplateFile(isSam?: boolean): Promise defaultUri: wsFolder && wsFolder[0] ? wsFolder[0].uri : undefined, }) if (loc) { - await nodefs.writeFile(loc.fsPath, content) + await fs.writeFile(loc.fsPath, content) await vscode.commands.executeCommand('vscode.open', loc) } } diff --git a/packages/core/src/shared/extensions/yaml.ts b/packages/core/src/shared/extensions/yaml.ts index a285554df88..ad9593b7369 100644 --- a/packages/core/src/shared/extensions/yaml.ts +++ b/packages/core/src/shared/extensions/yaml.ts @@ -9,7 +9,7 @@ import { getLogger } from '../logger/logger' import { getIdeProperties } from '../extensionUtilities' import { activateExtension } from '../utilities/vsCodeUtils' import { AWS_SCHEME } from '../constants' -import * as nodefs from 'fs' +import { readFileSync } from 'fs' // sourced from https://github.com/redhat-developer/vscode-yaml/blob/3d82d61ea63d3e3a9848fe6b432f8f1f452c1bec/src/schema-extension-api.ts // removed everything that is not currently being used @@ -53,7 +53,7 @@ export async function activateYamlExtension(): Promise { public name: string = 'CloudFormationTemplateRegistry' diff --git a/packages/core/src/shared/icons.ts b/packages/core/src/shared/icons.ts index 2d1813d9138..01502350d11 100644 --- a/packages/core/src/shared/icons.ts +++ b/packages/core/src/shared/icons.ts @@ -6,12 +6,12 @@ import globals, { isWeb } from './extensionGlobals' import type * as packageJson from '../../package.json' -import * as fs from 'fs' import * as path from 'path' import { Uri, ThemeIcon, ThemeColor } from 'vscode' import { isCloud9 } from './extensionUtilities' import { memoize } from './utilities/functionUtils' import { getLogger } from './logger/logger' +import fs from './fs/fs' // Animation: // https://code.visualstudio.com/api/references/icons-in-labels#animation @@ -87,17 +87,17 @@ export function addColor(icon: IconPath, color: string | ThemeColor): IconPath { return new Icon(icon.id, icon.source, typeof color === 'string' ? new ThemeColor(color) : color) } -function resolveIconId( +async function resolveIconId( id: IconId, shouldUseCloud9 = isCloud9(), iconsPath = globals.context.asAbsolutePath(path.join('resources', 'icons')) -): IconPath { +): Promise { const [namespace, ...rest] = id.split('-') const name = rest.join('-') // This 'override' logic is to support legacy use-cases, though ideally we wouldn't need it at all - const cloud9Override = shouldUseCloud9 ? resolvePathsSync(path.join(iconsPath, 'cloud9'), id) : undefined - const override = cloud9Override ?? resolvePathsSync(path.join(iconsPath, namespace), name) + const cloud9Override = shouldUseCloud9 ? await resolvePathsSync(path.join(iconsPath, 'cloud9'), id) : undefined + const override = cloud9Override ?? (await resolvePathsSync(path.join(iconsPath, namespace), name)) if (override) { getLogger().verbose(`icons: using override for "${id}"`) return override @@ -105,7 +105,7 @@ function resolveIconId( // TODO: remove when they support codicons + the contribution point if (shouldUseCloud9) { - const generated = resolvePathsSync(path.join(iconsPath, 'cloud9', 'generated'), id) + const generated = await resolvePathsSync(path.join(iconsPath, 'cloud9', 'generated'), id) if (generated) { return generated @@ -121,16 +121,16 @@ function resolveIconId( return new Icon(namespace === 'vscode' ? name : id, source) } -function resolvePathsSync( +async function resolvePathsSync( rootDir: string, target: string -): { light: Uri; dark: Uri; toString: () => string } | undefined { +): Promise<{ light: Uri; dark: Uri; toString: () => string } | undefined> { const filename = `${target}.svg` const darkPath = path.join(rootDir, 'dark', filename) const lightPath = path.join(rootDir, 'light', filename) try { - if (!isWeb() && fs.existsSync(darkPath) && fs.existsSync(lightPath)) { + if (!isWeb() && (await fs.exists(darkPath)) && (await fs.exists(lightPath))) { return { dark: Uri.file(darkPath), light: Uri.file(lightPath), toString: () => filename } } } catch (error) { diff --git a/packages/core/src/shared/resourcefetcher/httpResourceFetcher.ts b/packages/core/src/shared/resourcefetcher/httpResourceFetcher.ts index 19c4c8b04d4..ddda8e8c43c 100644 --- a/packages/core/src/shared/resourcefetcher/httpResourceFetcher.ts +++ b/packages/core/src/shared/resourcefetcher/httpResourceFetcher.ts @@ -2,11 +2,10 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - -import * as fs from 'fs' import * as http from 'http' import * as https from 'https' import * as stream from 'stream' +import * as fs from 'fs' import got, { Response, RequestError, CancelError } from 'got' import urlToOptions from 'got/dist/source/core/utils/url-to-options' import Request from 'got/dist/source/core' @@ -16,6 +15,7 @@ import { ResourceFetcher } from './resourcefetcher' import { Timeout, CancellationError, CancelEvent } from '../utilities/timeoutUtils' import { isCloud9 } from '../extensionUtilities' import { Headers } from 'got/dist/source/core' +import { createWriteStream } from 'fs' // XXX: patched Got module for compatability with older VS Code versions (e.g. Cloud9) // `got` has also deprecated `urlToOptions` @@ -149,7 +149,7 @@ export class HttpResourceFetcher implements ResourceFetcher { private pipeGetRequest(pipeLocation: string, timeout?: Timeout): FetcherResult { const requester = isCloud9() ? patchedGot : got const requestStream = requester.stream(this.url, { headers: this.buildRequestHeaders() }) - const fsStream = fs.createWriteStream(pipeLocation) + const fsStream = createWriteStream(pipeLocation) const done = new Promise((resolve, reject) => { const pipe = stream.pipeline(requestStream, fsStream, (err) => { diff --git a/packages/core/src/shared/sam/debugger/awsSamDebugConfigurationValidator.ts b/packages/core/src/shared/sam/debugger/awsSamDebugConfigurationValidator.ts index 6545db60497..c3fbd66643a 100644 --- a/packages/core/src/shared/sam/debugger/awsSamDebugConfigurationValidator.ts +++ b/packages/core/src/shared/sam/debugger/awsSamDebugConfigurationValidator.ts @@ -4,7 +4,6 @@ */ import * as vscode from 'vscode' -import * as fs from 'fs' import * as path from 'path' import { samImageLambdaRuntimes, samZipLambdaRuntimes } from '../../../lambda/models/samLambdaRuntime' import * as CloudFormation from '../../cloudformation/cloudformation' @@ -20,6 +19,7 @@ import { } from './awsSamDebugConfiguration' import { tryGetAbsolutePath } from '../../utilities/workspaceUtils' import { CloudFormationTemplateRegistry } from '../../fs/templateRegistry' +import fs from '../../fs/fs' export interface ValidationResult { isValid: boolean @@ -43,7 +43,7 @@ export class DefaultAwsSamDebugConfigurationValidator implements AwsSamDebugConf ): Promise { let rv: ValidationResult = { isValid: false, message: undefined } if (resolveVars) { - config = doTraverseAndReplace(config, this.workspaceFolder?.uri.fsPath ?? '') + config = await doTraverseAndReplace(config, this.workspaceFolder?.uri.fsPath ?? '') } if (!config.request) { rv.message = localize( @@ -93,7 +93,7 @@ export class DefaultAwsSamDebugConfigurationValidator implements AwsSamDebugConf } if (rv.isValid) { - rv = this.validateLambda(config) + rv = await this.validateLambda(config) } if (!rv.isValid && !rv.message) { @@ -277,10 +277,10 @@ export class DefaultAwsSamDebugConfigurationValidator implements AwsSamDebugConf return { isValid: true } } - private validateLambda(config: AwsSamDebuggerConfiguration): ValidationResult { + private async validateLambda(config: AwsSamDebuggerConfiguration): Promise { if (config.lambda?.payload?.path) { const fullpath = tryGetAbsolutePath(this.workspaceFolder, config.lambda?.payload?.path) - if (!fs.existsSync(fullpath)) { + if (!(await fs.exists(fullpath))) { return { isValid: false, message: localize( @@ -303,14 +303,14 @@ export class DefaultAwsSamDebugConfigurationValidator implements AwsSamDebugConf * @param config * @returns resolved config */ -export function resolveWorkspaceFolderVariable( +export async function resolveWorkspaceFolderVariable( folder: vscode.WorkspaceFolder | undefined, config: AwsSamDebuggerConfiguration -): AwsSamDebuggerConfiguration { - return doTraverseAndReplace(config, folder?.uri.fsPath) +): Promise { + return await doTraverseAndReplace(config, folder?.uri.fsPath) } -function doTraverseAndReplace(object: { [key: string]: any }, fspath: string | undefined): any { +async function doTraverseAndReplace(object: { [key: string]: any }, fspath: string | undefined): Promise { const wsfRegex = /^(.*)(\$\{workspaceFolder\})(.*)$/g if (!vscode.workspace.workspaceFolders && !fspath) { throw new Error('No workspace folders available; cannot resolve workspaceFolder variable.') @@ -324,7 +324,7 @@ function doTraverseAndReplace(object: { [key: string]: any }, fspath: string | u if (result) { if (!fspath) { for (const wsf of vscode.workspace.workspaceFolders!) { - if (fs.existsSync(path.join(result[1], wsf.uri.fsPath, result[3]))) { + if (await fs.exists(path.join(result[1], wsf.uri.fsPath, result[3]))) { fspath = wsf.uri.fsPath break } @@ -336,7 +336,7 @@ function doTraverseAndReplace(object: { [key: string]: any }, fspath: string | u final[key] = path.join(result[1], fspath, result[3]) } } else if (typeof val === 'object') { - final[key] = doTraverseAndReplace(val, fspath) + final[key] = await doTraverseAndReplace(val, fspath) } } diff --git a/packages/core/src/shared/sshConfig.ts b/packages/core/src/shared/sshConfig.ts index 06a93ee450b..b611f449a7a 100644 --- a/packages/core/src/shared/sshConfig.ts +++ b/packages/core/src/shared/sshConfig.ts @@ -16,7 +16,6 @@ import { CancellationError } from './utilities/timeoutUtils' import { getSshConfigPath } from './extensions/ssh' import globals from './extensionGlobals' import { fileExists, readFileAsString } from './filesystemUtilities' -import { chmodSync } from 'fs' import fs from './fs/fs' const localize = nls.loadMessageBundle() @@ -112,15 +111,14 @@ export class SshConfig { try { const parentsDir = path.dirname(sshConfigPath) const grandParentsDir = path.dirname(parentsDir) - // TODO: replace w/ fs.chmod once stub is merged. await fs.mkdir(grandParentsDir) - chmodSync(grandParentsDir, 0o755) + await fs.chmod(grandParentsDir, 0o755) await fs.mkdir(parentsDir) - chmodSync(parentsDir, 0o700) + await fs.chmod(parentsDir, 0o700) await fs.appendFile(sshConfigPath, section) - chmodSync(sshConfigPath, 0o600) + await fs.chmod(sshConfigPath, 0o600) } catch (e) { const message = localize( 'AWS.sshConfig.error.writeFail', diff --git a/packages/core/src/shared/utilities/streamUtilities.ts b/packages/core/src/shared/utilities/streamUtilities.ts index ef9c91d0752..159cf0e983f 100644 --- a/packages/core/src/shared/utilities/streamUtilities.ts +++ b/packages/core/src/shared/utilities/streamUtilities.ts @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import * as fs from 'fs' +import { createReadStream as createRStream, createWriteStream as createWStream } from 'fs' import { Readable, Writable, pipeline } from 'stream' import * as vscode from 'vscode' @@ -15,11 +15,11 @@ export interface FileStreams { export class DefaultFileStreams implements FileStreams { public createReadStream(uri: vscode.Uri): Readable { - return fs.createReadStream(uri.fsPath) + return createRStream(uri.fsPath) } public createWriteStream(uri: vscode.Uri): Writable { - return fs.createWriteStream(uri.fsPath) + return createWStream(uri.fsPath) } } @@ -27,7 +27,7 @@ export function streamToFile(stream: Readable, target: vscode.Uri): Promise { + close(fd, (err) => { if (err) { throw err } diff --git a/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts b/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts index d357cfe413e..e550aa6d5d2 100644 --- a/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts +++ b/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import * as fs from 'fs' +import { fs } from '../../../shared' import { getLogger, Logger } from '../../../shared/logger' /** @@ -12,10 +12,13 @@ import { getLogger, Logger } from '../../../shared/logger' * * @returns the escaped ASL Json definition string of the state machine construct */ -export function getStateMachineDefinitionFromCfnTemplate(uniqueIdentifier: string, templatePath: string): string { +export async function getStateMachineDefinitionFromCfnTemplate( + uniqueIdentifier: string, + templatePath: string +): Promise { const logger: Logger = getLogger() try { - const data = fs.readFileSync(templatePath, 'utf8') + const data = await fs.readFileText(templatePath) const jsonObj = JSON.parse(data) const resources = jsonObj?.Resources if (!resources) { diff --git a/packages/core/src/test/awsService/ec2/sshKeyPair.test.ts b/packages/core/src/test/awsService/ec2/sshKeyPair.test.ts index 58e9dc82c36..574450d20f9 100644 --- a/packages/core/src/test/awsService/ec2/sshKeyPair.test.ts +++ b/packages/core/src/test/awsService/ec2/sshKeyPair.test.ts @@ -4,7 +4,6 @@ */ import * as vscode from 'vscode' import assert from 'assert' -import nodefs from 'fs' import * as sinon from 'sinon' import * as path from 'path' import * as os from 'os' @@ -13,6 +12,7 @@ import { createTestWorkspaceFolder, installFakeClock } from '../../testUtil' import { InstalledClock } from '@sinonjs/fake-timers' import { ChildProcess } from '../../../shared/utilities/processUtils' import { fs, globals } from '../../../shared' +import { statSync } from 'fs' describe('SshKeyUtility', async function () { let temporaryDirectory: string @@ -54,7 +54,7 @@ describe('SshKeyUtility', async function () { it('sets permission of the file to read/write owner', async function () { if (!globals.isWeb && os.platform() !== 'win32') { - const result = nodefs.statSync(keyPair.getPrivateKeyPath()) + const result = statSync(keyPair.getPrivateKeyPath()) assert.strictEqual(result.mode & 0o777, 0o600) } }) diff --git a/packages/core/src/test/codewhisperer/testUtil.ts b/packages/core/src/test/codewhisperer/testUtil.ts index 217837d33ed..d2164c40545 100644 --- a/packages/core/src/test/codewhisperer/testUtil.ts +++ b/packages/core/src/test/codewhisperer/testUtil.ts @@ -17,7 +17,7 @@ import { getLogger } from '../../shared/logger' import { CodeWhispererCodeCoverageTracker } from '../../codewhisperer/tracker/codewhispererCodeCoverageTracker' import globals from '../../shared/extensionGlobals' import { session } from '../../codewhisperer/util/codeWhispererSession' -import fs from 'fs' +import { Dirent } from 'fs' import { DefaultAWSClientBuilder, ServiceOptions } from '../../shared/awsClientBuilder' import { FakeAwsContext } from '../utilities/fakeAwsContext' import { Service } from 'aws-sdk' @@ -201,8 +201,8 @@ export function createCodeActionContext(): vscode.CodeActionContext { } } -export function createMockDirentFile(fileName: string): fs.Dirent { - const dirent = new fs.Dirent() +export function createMockDirentFile(fileName: string): Dirent { + const dirent = new Dirent() dirent.isFile = () => true dirent.name = fileName return dirent diff --git a/packages/core/src/test/credentials/sso/ssoAccessTokenProvider.test.ts b/packages/core/src/test/credentials/sso/ssoAccessTokenProvider.test.ts index 51c508134af..f71d71ba2cc 100644 --- a/packages/core/src/test/credentials/sso/ssoAccessTokenProvider.test.ts +++ b/packages/core/src/test/credentials/sso/ssoAccessTokenProvider.test.ts @@ -24,9 +24,9 @@ import { getOpenExternalStub } from '../../globalSetup.test' import { getTestWindow } from '../../shared/vscode/window' import { SeverityLevel } from '../../shared/vscode/message' import { ToolkitError } from '../../../shared/errors' -import * as fs from 'fs' import * as path from 'path' import { Stub, stub } from '../../utilities/stubber' +import { fs } from '../../../shared' const hourInMs = 3600000 @@ -73,7 +73,7 @@ describe('SsoAccessTokenProvider', function () { async function makeTemporaryTokenCacheFolder() { const root = await makeTemporaryToolkitFolder() const cacheDir = path.join(root, '.aws', 'sso', 'cache') - fs.mkdirSync(cacheDir, { recursive: true }) + await fs.mkdir(cacheDir) return cacheDir } diff --git a/packages/core/src/test/dynamicResources/awsResourceManager.test.ts b/packages/core/src/test/dynamicResources/awsResourceManager.test.ts index 54311a3aca2..a7229d41fb2 100644 --- a/packages/core/src/test/dynamicResources/awsResourceManager.test.ts +++ b/packages/core/src/test/dynamicResources/awsResourceManager.test.ts @@ -16,7 +16,6 @@ import { CloudControlClient, DefaultCloudControlClient } from '../../shared/clie import { CloudFormationClient, DefaultCloudFormationClient } from '../../shared/clients/cloudFormationClient' import { makeTemporaryToolkitFolder, readFileAsString } from '../../shared/filesystemUtilities' import { FakeExtensionContext } from '../fakeExtensionContext' -import { existsSync } from 'fs' import { ResourceTypeMetadata } from '../../dynamicResources/model/resources' import globals from '../../shared/extensionGlobals' import { Stub, stub } from '../utilities/stubber' @@ -189,7 +188,7 @@ describe('ResourceManager', function () { const [mapping] = registerMappingSpy.args[registerMappingSpy.args.length - 1] const expectedSchemaLocation = path.join(tempFolder, 'sometype.schema.json') - assert.ok(existsSync(expectedSchemaLocation)) + assert.ok(await fs.exists(expectedSchemaLocation)) assert.strictEqual(mapping.type, 'json') testutil.assertEqualPaths(mapping.uri.fsPath, editor.document.uri.fsPath) const schema = mapping.schema as vscode.Uri diff --git a/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts b/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts index d93fce0cd28..ff62c2572b8 100644 --- a/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts +++ b/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts @@ -25,7 +25,7 @@ import admZip from 'adm-zip' import { makeTemporaryToolkitFolder } from '../../../shared/filesystemUtilities' import { DefaultSchemaClient } from '../../../shared/clients/schemaClient' import { fs } from '../../../shared' -import * as nodefs from 'fs' +import { closeSync, openSync, writeSync } from 'fs' describe('CodeDownloader', function () { let tempFolder: string @@ -682,9 +682,9 @@ describe('CodeExtractor', function () { const zip = new admZip() zip.addFile(fileName, Buffer.from(fileContent)) const buffer = zip.toBuffer() - const fd = nodefs.openSync(zipFileName, 'w') - nodefs.writeSync(fd, buffer, 0, buffer.byteLength, 0) - nodefs.closeSync(fd) + const fd = openSync(zipFileName, 'w') + writeSync(fd, buffer, 0, buffer.byteLength, 0) + closeSync(fd) return zip } diff --git a/packages/core/src/test/shared/fs/fs.test.ts b/packages/core/src/test/shared/fs/fs.test.ts index fefec8f0484..528a3bc0bca 100644 --- a/packages/core/src/test/shared/fs/fs.test.ts +++ b/packages/core/src/test/shared/fs/fs.test.ts @@ -9,7 +9,6 @@ import * as path from 'path' import * as utils from 'util' import { existsSync, mkdirSync, promises as nodefs, readFileSync } from 'fs' import { stat } from 'fs/promises' -import nodeFs from 'fs' import fs, { FileSystem } from '../../../shared/fs/fs' import * as os from 'os' import { isMinVscode, isWin } from '../../../shared/vscode/env' @@ -106,7 +105,7 @@ describe('FileSystem', function () { sandbox.stub(fs, 'rename').throws(new Error('Test Error Message VSC')) } if (throws.node) { - sandbox.stub(nodeFs.promises, 'rename').throws(new Error('Test Error Message Node')) + sandbox.stub(nodefs, 'rename').throws(new Error('Test Error Message Node')) } const filePath = testFolder.pathFrom('myFileName') diff --git a/packages/core/src/test/shared/icons.test.ts b/packages/core/src/test/shared/icons.test.ts index 3fd1feaf373..c76ccfcb93c 100644 --- a/packages/core/src/test/shared/icons.test.ts +++ b/packages/core/src/test/shared/icons.test.ts @@ -11,46 +11,46 @@ import { makeTemporaryToolkitFolder, tryRemoveFolder } from '../../shared/filesy import { fs } from '../../shared' describe('getIcon', function () { - it('returns a ThemeIcon for `vscode` codicons', function () { - const icon = getIcon('vscode-gear', false) + it('returns a ThemeIcon for `vscode` codicons', async function () { + const icon = await getIcon('vscode-gear', false) assert.ok(icon instanceof ThemeIcon) assert.strictEqual(icon.id, 'gear') }) - it('returns a ThemeIcon for `aws` icons', function () { - const icon = getIcon('aws-cdk-logo', false) + it('returns a ThemeIcon for `aws` icons', async function () { + const icon = await getIcon('aws-cdk-logo', false) assert.ok(icon instanceof ThemeIcon) assert.strictEqual(icon.id, 'aws-cdk-logo') }) - it('returns icon URIs for non-codicon icons', function () { - const icon = getIcon('vscode-help', false) + it('returns icon URIs for non-codicon icons', async function () { + const icon = await getIcon('vscode-help', false) assert.ok(!(icon instanceof ThemeIcon)) assert.ok(icon.dark.path.endsWith('/resources/icons/vscode/dark/help.svg')) assert.ok(icon.light.path.endsWith('/resources/icons/vscode/light/help.svg')) }) - it('can use specific icons for Cloud9', function () { - const icon = getIcon('vscode-help', true) + it('can use specific icons for Cloud9', async function () { + const icon = await getIcon('vscode-help', true) assert.ok(!(icon instanceof ThemeIcon)) assert.ok(icon.dark.path.endsWith('/resources/icons/cloud9/dark/vscode-help.svg')) assert.ok(icon.light.path.endsWith('/resources/icons/cloud9/light/vscode-help.svg')) }) - it('can use generated icons for Cloud9', function () { - const icon = getIcon('aws-cdk-logo', true) + it('can use generated icons for Cloud9', async function () { + const icon = await getIcon('aws-cdk-logo', true) assert.ok(!(icon instanceof ThemeIcon)) assert.ok(icon.dark.path.endsWith('/resources/icons/cloud9/generated/dark/aws-cdk-logo.svg')) assert.ok(icon.light.path.endsWith('/resources/icons/cloud9/generated/light/aws-cdk-logo.svg')) }) - it('can use codicons for Cloud9', function () { - const icon = getIcon('vscode-gear', true) + it('can use codicons for Cloud9', async function () { + const icon = await getIcon('vscode-gear', true) assert.ok(!(icon instanceof ThemeIcon)) assert.ok(icon.dark.path.endsWith('/resources/icons/cloud9/generated/dark/vscode-gear.svg')) @@ -72,7 +72,7 @@ describe('getIcon', function () { await fs.writeFile(p, '') } - const icon = getIcon('aws-cdk-logo', false, tempDir) + const icon = await getIcon('aws-cdk-logo', false, tempDir) assert.ok(!(icon instanceof ThemeIcon)) assert.strictEqual(icon.dark.fsPath, Uri.file(paths[1]).fsPath) @@ -90,7 +90,7 @@ describe('getIcon', function () { await fs.mkdir(path.dirname(logoPath)) await fs.writeFile(logoPath, '') - const icon = getIcon('aws-cdk-logo', false, tempDir) + const icon = await getIcon('aws-cdk-logo', false, tempDir) assert.ok(icon instanceof ThemeIcon) assert.strictEqual(icon.source?.fsPath, Uri.file(logoPath).fsPath) @@ -101,18 +101,18 @@ describe('getIcon', function () { }) describe('codicon', function () { - it('inserts icon ids', function () { - const result = codicon`my icon: ${getIcon('vscode-gear')}` + it('inserts icon ids', async function () { + const result = codicon`my icon: ${await getIcon('vscode-gear')}` assert.strictEqual(result, 'my icon: $(gear)') }) - it('skips adding icons if no icon font is available', function () { - const result = codicon`my icon: ${getIcon('vscode-help')}` + it('skips adding icons if no icon font is available', async function () { + const result = codicon`my icon: ${await getIcon('vscode-help')}` assert.strictEqual(result, 'my icon:') }) - it('trims the resulting string', function () { - const result = codicon` some text ${getIcon('vscode-help')} ` + it('trims the resulting string', async function () { + const result = codicon` some text ${await getIcon('vscode-help')} ` assert.strictEqual(result, 'some text') }) }) diff --git a/packages/core/src/test/shared/logger/sharedFileTransport.test.ts b/packages/core/src/test/shared/logger/sharedFileTransport.test.ts index 92b646890db..8759b7758a4 100644 --- a/packages/core/src/test/shared/logger/sharedFileTransport.test.ts +++ b/packages/core/src/test/shared/logger/sharedFileTransport.test.ts @@ -10,7 +10,6 @@ import fs from '../../../shared/fs/fs' import { stub, SinonStub } from 'sinon' import { MESSAGE } from '../../../shared/logger/consoleLogTransport' import { createTestFile } from '../../testUtil' -import { readFileSync } from 'fs' import { sleep } from '../../../shared/utilities/timeoutUtils' describe('SharedFileTransport', function () { @@ -47,7 +46,7 @@ describe('SharedFileTransport', function () { await lastLog // wait for the last log to be written to the file // assert all logs were written to file - const actualText = readFileSync(logFile.fsPath, 'utf8') + const actualText = await fs.readFileText(logFile.fsPath) assert.strictEqual(actualText, 'a1\na2\na3\na4\na5\na6\n') assert.strictEqual(nextFunc.callCount, 6) }) diff --git a/packages/core/src/test/shared/logger/toolkitLogger.test.ts b/packages/core/src/test/shared/logger/toolkitLogger.test.ts index 89bafa728ca..d70dbb3436c 100644 --- a/packages/core/src/test/shared/logger/toolkitLogger.test.ts +++ b/packages/core/src/test/shared/logger/toolkitLogger.test.ts @@ -4,13 +4,13 @@ */ import assert from 'assert' -import * as fs from 'fs' import * as filesystemUtilities from '../../../shared/filesystemUtilities' import * as vscode from 'vscode' import { ToolkitLogger } from '../../../shared/logger/toolkitLogger' import { MockOutputChannel } from '../../mockOutputChannel' import { sleep, waitUntil } from '../../../shared/utilities/timeoutUtils' import { ToolkitError } from '../../../shared/errors' +import { fs } from '../../../shared' /** * Disposes the logger then waits for the write streams to flush. The `expected` and `unexpected` arrays just look @@ -30,10 +30,10 @@ async function checkFile( setTimeout(() => reject(new Error('Timed out waiting for log message')), 10_000) // Wait for file to exist - while (!fs.existsSync(logPath.fsPath)) { + while (!(await fs.exists(logPath.fsPath))) { await sleep(200) } - const contents = fs.readFileSync(logPath.fsPath) + const contents = Buffer.from(await fs.readFileBytes(logPath.fsPath)) // Error if unexpected messages are in the log file const foundUnexpected = unexpected diff --git a/packages/core/src/test/shared/vscode/runCommand.test.ts b/packages/core/src/test/shared/vscode/runCommand.test.ts index 52a7bccb69a..651585194e5 100644 --- a/packages/core/src/test/shared/vscode/runCommand.test.ts +++ b/packages/core/src/test/shared/vscode/runCommand.test.ts @@ -6,7 +6,6 @@ import globals from '../../../shared/extensionGlobals' import os from 'os' -import { promises as fsPromises } from 'fs' import fs from '../../../shared/fs/fs' import * as sinon from 'sinon' import assert from 'assert' @@ -80,7 +79,8 @@ describe('runCommand', function () { before(async function () { tempFolder = await makeTemporaryToolkitFolder() unwritableFile = path.join(tempFolder, 'unwritableFile') - await fsPromises.writeFile(unwritableFile, 'foo', { mode: 0o400 }) + await fs.writeFile(unwritableFile, 'foo') + await fs.chmod(unwritableFile, 0o400) }) after(async function () { @@ -126,7 +126,7 @@ describe('runCommand', function () { it('nodejs ISDIR', async function () { await runAndWaitForMessage(/EISDIR: illegal operation on a directory/, async () => { // Try to write to the current directory. 💩 - const err = await fsPromises.writeFile('.', 'foo').catch((e) => e) + const err = await fs.writeFile('.', 'foo').catch((e) => e) const err2 = new Error('generic error') ;(err2 as any).cause = err throw err2 @@ -165,7 +165,7 @@ describe('runCommand', function () { await Promise.all([ viewLogsDialog.then((dialog) => dialog.close()), testCommand.execute(async () => { - const err = await fsPromises.writeFile(unwritableFile, 'bar').catch((e) => e) + const err = await fs.writeFile(unwritableFile, 'bar').catch((e) => e) throw fakeErrorChain(err, new Error('error 3')) }), ]) diff --git a/packages/core/src/test/testUtil.ts b/packages/core/src/test/testUtil.ts index c09fbfe6ccc..1772b67853a 100644 --- a/packages/core/src/test/testUtil.ts +++ b/packages/core/src/test/testUtil.ts @@ -15,7 +15,6 @@ import { MetricName, MetricShapes } from '../shared/telemetry/telemetry' import { keys, selectFrom } from '../shared/utilities/tsUtils' import fs from '../shared/fs/fs' import { DeclaredCommand } from '../shared/vscode/commands2' -import { mkdirSync, existsSync } from 'fs' import { randomBytes } from 'crypto' import request from '../shared/request' import { stub } from 'sinon' @@ -109,8 +108,8 @@ export class TestFolder { async mkdir(relativeDirPath?: string): Promise { relativeDirPath ??= randomBytes(4).toString('hex') const absolutePath = this.pathFrom(relativeDirPath) - mkdirSync(absolutePath, { recursive: true }) - assert(existsSync(absolutePath)) + await fs.mkdir(absolutePath) + assert(await fs.exists(absolutePath)) return absolutePath } diff --git a/packages/core/src/testLint/gitSecrets.test.ts b/packages/core/src/testLint/gitSecrets.test.ts index 4719245134d..a9049e6aa64 100644 --- a/packages/core/src/testLint/gitSecrets.test.ts +++ b/packages/core/src/testLint/gitSecrets.test.ts @@ -8,8 +8,8 @@ import { describe } from 'mocha' import assert from 'assert' import * as path from 'path' import { platform } from 'os' -import { existsSync, mkdirSync, unlinkSync, writeFileSync } from 'fs' import { runCmd } from './testUtils' +import { fs } from '../shared' /** * NOTES: @@ -40,22 +40,22 @@ describe('git-secrets', function () { }) } - function setupTestFixturesDir(toolkitProjectDir: string) { + async function setupTestFixturesDir(toolkitProjectDir: string) { const testFixturesPath = path.join(toolkitProjectDir, 'src', 'testFixtures', 'bin') - mkdirSync(testFixturesPath, { recursive: true }) + await fs.mkdir(testFixturesPath) return testFixturesPath } - function setupAccessKeyFile(testFixturesPath: string) { + async function setupAccessKeyFile(testFixturesPath: string) { const accessKeyFilePath = path.join(testFixturesPath, 'fileWithAccessKey.ts') - deleteFileIfExists(accessKeyFilePath) + await deleteFileIfExists(accessKeyFilePath) return accessKeyFilePath } - function setupGitSecretsExecutable(testFixturesPath: string) { + async function setupGitSecretsExecutable(testFixturesPath: string) { const gitSecretsExecutablePath = path.join(testFixturesPath, 'git-secrets') - if (existsSync(gitSecretsExecutablePath)) { + if (await fs.exists(gitSecretsExecutablePath)) { console.log('INFO: git-secrets already installed') } else { console.log('INFO: Installing git-secrets...') @@ -72,13 +72,13 @@ describe('git-secrets', function () { return gitSecretsExecutablePath } - function deleteFileIfExists(filePath: string) { - if (existsSync(filePath)) { - unlinkSync(filePath) + async function deleteFileIfExists(filePath: string) { + if (await fs.exists(filePath)) { + await fs.delete(filePath) } } - function createFileWithSecretKey(accessKeyFilePath: string) { + async function createFileWithSecretKey(accessKeyFilePath: string) { // Create file in project that has secret key value. // Need to build access key string incrementally to not trigger git-secrets. const keyValue = 'yAki21XLhAIBiKvyaxr4p/ltr8OxkZTHISISFAKE' @@ -91,18 +91,18 @@ describe('git-secrets', function () { ${mySecretAccessKey} `.trim() - writeFileSync(accessKeyFilePath, fileContent) + await fs.writeFile(accessKeyFilePath, fileContent) } - before(function () { + before(async function () { if (platform() === 'win32') { this.skip() } toolkitProjectDir = path.resolve() - testFixturesPath = setupTestFixturesDir(toolkitProjectDir) - gitSecrets = setupGitSecretsExecutable(testFixturesPath) - accessKeyFilePath = setupAccessKeyFile(testFixturesPath) + testFixturesPath = await setupTestFixturesDir(toolkitProjectDir) + gitSecrets = await setupGitSecretsExecutable(testFixturesPath) + accessKeyFilePath = await setupAccessKeyFile(testFixturesPath) // Register all patterns with `git-secrets` runCmd([gitSecrets, '--register-aws'], { cwd: toolkitProjectDir }) @@ -110,8 +110,8 @@ ${mySecretAccessKey} setAllowListPatterns(gitSecrets) }) - afterEach(function () { - deleteFileIfExists(accessKeyFilePath) + afterEach(async function () { + await deleteFileIfExists(accessKeyFilePath) }) it('ensures no git secrets are found', function () { @@ -119,8 +119,8 @@ ${mySecretAccessKey} assert.strictEqual(result.status, 0, `Failure output: ${result.stderr.toString()}`) }) - it('sanity check it finds secrets', function () { - createFileWithSecretKey(accessKeyFilePath) + it('sanity check it finds secrets', async function () { + await createFileWithSecretKey(accessKeyFilePath) const result = runCmd([gitSecrets, '--scan', accessKeyFilePath], { cwd: toolkitProjectDir, throws: false }) assert.strictEqual(result.status, 1) }) From fa599aee45e3d2450483483c93d10317bda370df Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 8 Oct 2024 13:31:55 -0400 Subject: [PATCH 2/5] fix breaking test --- .../vue/configEditor/samInvokeBackend.ts | 2 +- packages/core/src/shared/icons.ts | 18 ++++++------ ...etStateMachineDefinitionFromCfnTemplate.ts | 9 ++---- .../cellStatusBarItemProvider.test.ts | 8 +++--- .../commands/downloadSchemaItemCode.test.ts | 4 +-- packages/core/src/test/shared/icons.test.ts | 28 +++++++++---------- .../src/test/shared/vscode/runCommand.test.ts | 3 +- ...teMachineDefinitionFromCfnTemplate.test.ts | 2 +- 8 files changed, 35 insertions(+), 39 deletions(-) diff --git a/packages/core/src/lambda/vue/configEditor/samInvokeBackend.ts b/packages/core/src/lambda/vue/configEditor/samInvokeBackend.ts index 7ae941d72e1..db627850230 100644 --- a/packages/core/src/lambda/vue/configEditor/samInvokeBackend.ts +++ b/packages/core/src/lambda/vue/configEditor/samInvokeBackend.ts @@ -286,7 +286,7 @@ export class SamInvokeWebview extends VueWebview { */ public async invokeLaunchConfig(config: AwsSamDebuggerConfiguration): Promise { const finalConfig = finalizeConfig( - resolveWorkspaceFolderVariable(undefined, config), + await resolveWorkspaceFolderVariable(undefined, config), 'Editor-Created Debug Config' ) const targetUri = await getUriFromLaunchConfig(finalConfig) diff --git a/packages/core/src/shared/icons.ts b/packages/core/src/shared/icons.ts index 01502350d11..f74eef663b7 100644 --- a/packages/core/src/shared/icons.ts +++ b/packages/core/src/shared/icons.ts @@ -11,7 +11,7 @@ import { Uri, ThemeIcon, ThemeColor } from 'vscode' import { isCloud9 } from './extensionUtilities' import { memoize } from './utilities/functionUtils' import { getLogger } from './logger/logger' -import fs from './fs/fs' +import { existsSync } from 'fs' // Animation: // https://code.visualstudio.com/api/references/icons-in-labels#animation @@ -87,17 +87,17 @@ export function addColor(icon: IconPath, color: string | ThemeColor): IconPath { return new Icon(icon.id, icon.source, typeof color === 'string' ? new ThemeColor(color) : color) } -async function resolveIconId( +function resolveIconId( id: IconId, shouldUseCloud9 = isCloud9(), iconsPath = globals.context.asAbsolutePath(path.join('resources', 'icons')) -): Promise { +): IconPath { const [namespace, ...rest] = id.split('-') const name = rest.join('-') // This 'override' logic is to support legacy use-cases, though ideally we wouldn't need it at all - const cloud9Override = shouldUseCloud9 ? await resolvePathsSync(path.join(iconsPath, 'cloud9'), id) : undefined - const override = cloud9Override ?? (await resolvePathsSync(path.join(iconsPath, namespace), name)) + const cloud9Override = shouldUseCloud9 ? resolvePathsSync(path.join(iconsPath, 'cloud9'), id) : undefined + const override = cloud9Override ?? resolvePathsSync(path.join(iconsPath, namespace), name) if (override) { getLogger().verbose(`icons: using override for "${id}"`) return override @@ -105,7 +105,7 @@ async function resolveIconId( // TODO: remove when they support codicons + the contribution point if (shouldUseCloud9) { - const generated = await resolvePathsSync(path.join(iconsPath, 'cloud9', 'generated'), id) + const generated = resolvePathsSync(path.join(iconsPath, 'cloud9', 'generated'), id) if (generated) { return generated @@ -121,16 +121,16 @@ async function resolveIconId( return new Icon(namespace === 'vscode' ? name : id, source) } -async function resolvePathsSync( +function resolvePathsSync( rootDir: string, target: string -): Promise<{ light: Uri; dark: Uri; toString: () => string } | undefined> { +): { light: Uri; dark: Uri; toString: () => string } | undefined { const filename = `${target}.svg` const darkPath = path.join(rootDir, 'dark', filename) const lightPath = path.join(rootDir, 'light', filename) try { - if (!isWeb() && (await fs.exists(darkPath)) && (await fs.exists(lightPath))) { + if (!isWeb() && existsSync(darkPath) && existsSync(lightPath)) { return { dark: Uri.file(darkPath), light: Uri.file(lightPath), toString: () => filename } } } catch (error) { diff --git a/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts b/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts index e550aa6d5d2..6ebd3c706a5 100644 --- a/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts +++ b/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { fs } from '../../../shared' +import { readFileSync } from 'fs' import { getLogger, Logger } from '../../../shared/logger' /** @@ -12,13 +12,10 @@ import { getLogger, Logger } from '../../../shared/logger' * * @returns the escaped ASL Json definition string of the state machine construct */ -export async function getStateMachineDefinitionFromCfnTemplate( - uniqueIdentifier: string, - templatePath: string -): Promise { +export function getStateMachineDefinitionFromCfnTemplate(uniqueIdentifier: string, templatePath: string): string { const logger: Logger = getLogger() try { - const data = await fs.readFileText(templatePath) + const data = readFileSync(templatePath, 'utf-8') const jsonObj = JSON.parse(data) const resources = jsonObj?.Resources if (!resources) { diff --git a/packages/core/src/test/awsService/redshift/notebook/cellStatusBarItemProvider.test.ts b/packages/core/src/test/awsService/redshift/notebook/cellStatusBarItemProvider.test.ts index eb224596590..9c567a0d5f5 100644 --- a/packages/core/src/test/awsService/redshift/notebook/cellStatusBarItemProvider.test.ts +++ b/packages/core/src/test/awsService/redshift/notebook/cellStatusBarItemProvider.test.ts @@ -31,17 +31,17 @@ describe('CellStatusBarItemProvider', function () { sinon.restore() }) - it('provides "Connect" status bar item when cell has no connectionParams', () => { + it('provides "Connect" status bar item when cell has no connectionParams', async () => { const cell = { notebook: { metadata: { connectionParams: undefined } } } const expectedText = '$(notebook-state-error) Connect' - const result = cellStatusBarItemProvider.provideCellStatusBarItems(cell, undefined) + const result = await cellStatusBarItemProvider.provideCellStatusBarItems(cell, undefined) assert(Array.isArray(result)) assert.strictEqual(result.length, 1) assert.strictEqual(result[0].text, expectedText) }) - it('provides status bar with success-icon and connection information', () => { - const result = cellStatusBarItemProvider.provideCellStatusBarItems(cell, token) + it('provides status bar with success-icon and connection information', async () => { + const result = await cellStatusBarItemProvider.provideCellStatusBarItems(cell, token) const expectedText = '$(notebook-state-success) Connected to TestWarehouse' const expectedCommand = { command: 'aws.redshift.notebookConnectClicked', diff --git a/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts b/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts index ff62c2572b8..91c617a9e77 100644 --- a/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts +++ b/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts @@ -473,7 +473,7 @@ describe('CodeExtractor', function () { //Create a zip file that clashes with destination content zipHandler = createZipFileInTempDirectory(fileName, 'Second file content', zipName) - const collisionOccured = codeExtractor.checkFileCollisions(zipName, destinationDirectory) + const collisionOccured = await codeExtractor.checkFileCollisions(zipName, destinationDirectory) assert.strictEqual(collisionOccured, true, 'should confirm that collision occurs') assert(outputChannel.value.includes(expectedMessage), `channel missing msg: ${expectedMessage}`) @@ -491,7 +491,7 @@ describe('CodeExtractor', function () { const fileName2 = 'test2.txt' zipHandler = createZipFileInTempDirectory(fileName2, 'Second file content', zipName) - const collisionOccured = codeExtractor.checkFileCollisions(zipName, destinationDirectory) + const collisionOccured = await codeExtractor.checkFileCollisions(zipName, destinationDirectory) assert.strictEqual( collisionOccured, false, diff --git a/packages/core/src/test/shared/icons.test.ts b/packages/core/src/test/shared/icons.test.ts index c76ccfcb93c..6e6447d9156 100644 --- a/packages/core/src/test/shared/icons.test.ts +++ b/packages/core/src/test/shared/icons.test.ts @@ -11,22 +11,22 @@ import { makeTemporaryToolkitFolder, tryRemoveFolder } from '../../shared/filesy import { fs } from '../../shared' describe('getIcon', function () { - it('returns a ThemeIcon for `vscode` codicons', async function () { - const icon = await getIcon('vscode-gear', false) + it('returns a ThemeIcon for `vscode` codicons', function () { + const icon = getIcon('vscode-gear', false) assert.ok(icon instanceof ThemeIcon) assert.strictEqual(icon.id, 'gear') }) - it('returns a ThemeIcon for `aws` icons', async function () { - const icon = await getIcon('aws-cdk-logo', false) + it('returns a ThemeIcon for `aws` icons', function () { + const icon = getIcon('aws-cdk-logo', false) assert.ok(icon instanceof ThemeIcon) assert.strictEqual(icon.id, 'aws-cdk-logo') }) - it('returns icon URIs for non-codicon icons', async function () { - const icon = await getIcon('vscode-help', false) + it('returns icon URIs for non-codicon icons', function () { + const icon = getIcon('vscode-help', false) assert.ok(!(icon instanceof ThemeIcon)) assert.ok(icon.dark.path.endsWith('/resources/icons/vscode/dark/help.svg')) @@ -34,7 +34,7 @@ describe('getIcon', function () { }) it('can use specific icons for Cloud9', async function () { - const icon = await getIcon('vscode-help', true) + const icon = getIcon('vscode-help', true) assert.ok(!(icon instanceof ThemeIcon)) assert.ok(icon.dark.path.endsWith('/resources/icons/cloud9/dark/vscode-help.svg')) @@ -42,7 +42,7 @@ describe('getIcon', function () { }) it('can use generated icons for Cloud9', async function () { - const icon = await getIcon('aws-cdk-logo', true) + const icon = getIcon('aws-cdk-logo', true) assert.ok(!(icon instanceof ThemeIcon)) assert.ok(icon.dark.path.endsWith('/resources/icons/cloud9/generated/dark/aws-cdk-logo.svg')) @@ -50,7 +50,7 @@ describe('getIcon', function () { }) it('can use codicons for Cloud9', async function () { - const icon = await getIcon('vscode-gear', true) + const icon = getIcon('vscode-gear', true) assert.ok(!(icon instanceof ThemeIcon)) assert.ok(icon.dark.path.endsWith('/resources/icons/cloud9/generated/dark/vscode-gear.svg')) @@ -72,7 +72,7 @@ describe('getIcon', function () { await fs.writeFile(p, '') } - const icon = await getIcon('aws-cdk-logo', false, tempDir) + const icon = getIcon('aws-cdk-logo', false, tempDir) assert.ok(!(icon instanceof ThemeIcon)) assert.strictEqual(icon.dark.fsPath, Uri.file(paths[1]).fsPath) @@ -90,7 +90,7 @@ describe('getIcon', function () { await fs.mkdir(path.dirname(logoPath)) await fs.writeFile(logoPath, '') - const icon = await getIcon('aws-cdk-logo', false, tempDir) + const icon = getIcon('aws-cdk-logo', false, tempDir) assert.ok(icon instanceof ThemeIcon) assert.strictEqual(icon.source?.fsPath, Uri.file(logoPath).fsPath) @@ -102,17 +102,17 @@ describe('getIcon', function () { describe('codicon', function () { it('inserts icon ids', async function () { - const result = codicon`my icon: ${await getIcon('vscode-gear')}` + const result = codicon`my icon: ${getIcon('vscode-gear')}` assert.strictEqual(result, 'my icon: $(gear)') }) it('skips adding icons if no icon font is available', async function () { - const result = codicon`my icon: ${await getIcon('vscode-help')}` + const result = codicon`my icon: ${getIcon('vscode-help')}` assert.strictEqual(result, 'my icon:') }) it('trims the resulting string', async function () { - const result = codicon` some text ${await getIcon('vscode-help')} ` + const result = codicon` some text ${getIcon('vscode-help')} ` assert.strictEqual(result, 'some text') }) }) diff --git a/packages/core/src/test/shared/vscode/runCommand.test.ts b/packages/core/src/test/shared/vscode/runCommand.test.ts index 651585194e5..94a8f692253 100644 --- a/packages/core/src/test/shared/vscode/runCommand.test.ts +++ b/packages/core/src/test/shared/vscode/runCommand.test.ts @@ -158,8 +158,7 @@ describe('runCommand', function () { }) it('nodejs EACCES (not wrapped by toolkit `PermissionsError`)', async function () { - const expectedMsg = - os.platform() === 'win32' ? /EPERM: operation not permitted/ : /EACCES: permission denied/ + const expectedMsg = 'Expected rw-, found r--.' const viewLogsDialog = getTestWindow().waitForMessage(expectedMsg) await Promise.all([ diff --git a/packages/core/src/test/stepFunctions/commands/getStateMachineDefinitionFromCfnTemplate.test.ts b/packages/core/src/test/stepFunctions/commands/getStateMachineDefinitionFromCfnTemplate.test.ts index 3a2cb4243f0..ca2c7876fa4 100644 --- a/packages/core/src/test/stepFunctions/commands/getStateMachineDefinitionFromCfnTemplate.test.ts +++ b/packages/core/src/test/stepFunctions/commands/getStateMachineDefinitionFromCfnTemplate.test.ts @@ -15,7 +15,7 @@ const cdkOutPath = normalize(normalize(__dirname).replace('/dist', '') + '/resou const templatePath = normalize(`${cdkOutPath}/templateJsonTester.template.json`) describe('Get State Machine Definition from Cfn Template', function () { - it('get the correct cfn definition for state machine with correct inputs', function () { + it('get the correct cfn definition for state machine with correct inputs', async function () { let data = getCfnDefinition.getStateMachineDefinitionFromCfnTemplate(uniqueIdendifier, templatePath) data = getCfnDefinition.toUnescapedAslJsonString(data) assert.strictEqual(data, unescapedJsonString) From c6a3c10fd8f23dccf73a9d829482da81fb59878c Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 8 Oct 2024 13:47:56 -0400 Subject: [PATCH 3/5] update eslint with error --- .eslintrc.js | 4 ++++ .../unit/codewhisperer/service/securityScanHandler.test.ts | 1 + packages/core/src/amazonq/lsp/lspController.ts | 1 + .../codewhisperer/service/transformByQ/transformApiHandler.ts | 1 + .../service/transformByQ/transformationResultsViewProvider.ts | 1 + .../core/src/eventSchemas/commands/downloadSchemaItemCode.ts | 1 + packages/core/src/shared/errors.ts | 1 + packages/core/src/shared/extensions/yaml.ts | 1 + packages/core/src/shared/fs/fs.ts | 1 + packages/core/src/shared/fs/templateRegistry.ts | 1 + packages/core/src/shared/handleUninstall.ts | 1 + packages/core/src/shared/icons.ts | 1 + .../core/src/shared/resourcefetcher/httpResourceFetcher.ts | 1 + packages/core/src/shared/sam/debugger/typescriptSamDebug.ts | 2 +- packages/core/src/shared/utilities/streamUtilities.ts | 2 +- packages/core/src/shared/utilities/textUtilities.ts | 1 + .../getStateMachineDefinitionFromCfnTemplate.ts | 1 + packages/core/src/test/awsService/ec2/sshKeyPair.test.ts | 1 + packages/core/src/test/codewhisperer/testUtil.ts | 1 + .../test/eventSchemas/commands/downloadSchemaItemCode.test.ts | 1 + packages/core/src/test/shared/fs/fs.test.ts | 1 + 21 files changed, 24 insertions(+), 2 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index f31c7299181..c0725c5bff0 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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. ', + }, ], }, ], diff --git a/packages/amazonq/test/unit/codewhisperer/service/securityScanHandler.test.ts b/packages/amazonq/test/unit/codewhisperer/service/securityScanHandler.test.ts index d72d422fd0d..3e676702ffc 100644 --- a/packages/amazonq/test/unit/codewhisperer/service/securityScanHandler.test.ts +++ b/packages/amazonq/test/unit/codewhisperer/service/securityScanHandler.test.ts @@ -17,6 +17,7 @@ import { import assert from 'assert' import sinon from 'sinon' import * as vscode from 'vscode' +/* eslint-disable no-restricted-imports */ import * as fs from 'fs' const mockCodeScanFindings = JSON.stringify([ diff --git a/packages/core/src/amazonq/lsp/lspController.ts b/packages/core/src/amazonq/lsp/lspController.ts index 7494f70289d..77c8f3f9e74 100644 --- a/packages/core/src/amazonq/lsp/lspController.ts +++ b/packages/core/src/amazonq/lsp/lspController.ts @@ -6,6 +6,7 @@ import * as vscode from 'vscode' import * as path from 'path' import * as crypto from 'crypto' +/* eslint-disable no-restricted-imports */ import { createWriteStream } from 'fs' import { getLogger } from '../../shared/logger/logger' import { CurrentWsFolders, collectFilesForIndex } from '../../shared/utilities/workspaceUtils' diff --git a/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts b/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts index 6d837ebe5c8..ed8bdd12967 100644 --- a/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts +++ b/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts @@ -46,6 +46,7 @@ import { ExportIntent, TransformationDownloadArtifactType } from '@amzn/codewhis import fs from '../../../shared/fs/fs' import { ChatSessionManager } from '../../../amazonqGumby/chat/storages/chatSession' import { convertToTimeString, encodeHTML } from '../../../shared/utilities/textUtilities' +/* eslint-disable no-restricted-imports */ import { readdirSync } from 'fs' export function getSha256(buffer: Buffer) { diff --git a/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts b/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts index d8842c9f840..a257d53751f 100644 --- a/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts +++ b/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts @@ -20,6 +20,7 @@ import { createCodeWhispererChatStreamingClient } from '../../../shared/clients/ import { ChatSessionManager } from '../../../amazonqGumby/chat/storages/chatSession' import { setContext } from '../../../shared/vscode/setContext' import { fs } from '../../../shared' +/* eslint-disable no-restricted-imports */ import { existsSync, readFileSync, rmSync, writeFileSync } from 'fs' export abstract class ProposedChangeNode { diff --git a/packages/core/src/eventSchemas/commands/downloadSchemaItemCode.ts b/packages/core/src/eventSchemas/commands/downloadSchemaItemCode.ts index 31a40def224..11982618fa8 100644 --- a/packages/core/src/eventSchemas/commands/downloadSchemaItemCode.ts +++ b/packages/core/src/eventSchemas/commands/downloadSchemaItemCode.ts @@ -25,6 +25,7 @@ import { import admZip from 'adm-zip' import globals from '../../shared/extensionGlobals' import { telemetry } from '../../shared/telemetry/telemetry' +/* eslint-disable no-restricted-imports */ import { closeSync, openSync, writeSync } from 'fs' import { fs } from '../../shared' diff --git a/packages/core/src/shared/errors.ts b/packages/core/src/shared/errors.ts index e2822c2fb31..2b47e523d92 100644 --- a/packages/core/src/shared/errors.ts +++ b/packages/core/src/shared/errors.ts @@ -10,6 +10,7 @@ import { isThrottlingError, isTransientError } from '@smithy/service-error-class import { Result } from './telemetry/telemetry' import { CancellationError } from './utilities/timeoutUtils' import { hasKey, isNonNullable } from './utilities/tsUtils' +/* eslint-disable no-restricted-imports */ import type * as nodefs from 'fs' import type * as os from 'os' import { CodeWhispererStreamingServiceException } from '@amzn/codewhisperer-streaming' diff --git a/packages/core/src/shared/extensions/yaml.ts b/packages/core/src/shared/extensions/yaml.ts index ad9593b7369..cac683f8576 100644 --- a/packages/core/src/shared/extensions/yaml.ts +++ b/packages/core/src/shared/extensions/yaml.ts @@ -9,6 +9,7 @@ import { getLogger } from '../logger/logger' import { getIdeProperties } from '../extensionUtilities' import { activateExtension } from '../utilities/vsCodeUtils' import { AWS_SCHEME } from '../constants' +/* eslint-disable no-restricted-imports */ import { readFileSync } from 'fs' // sourced from https://github.com/redhat-developer/vscode-yaml/blob/3d82d61ea63d3e3a9848fe6b432f8f1f452c1bec/src/schema-extension-api.ts diff --git a/packages/core/src/shared/fs/fs.ts b/packages/core/src/shared/fs/fs.ts index cc46a3c66fb..80d2fc9d873 100644 --- a/packages/core/src/shared/fs/fs.ts +++ b/packages/core/src/shared/fs/fs.ts @@ -4,6 +4,7 @@ */ import vscode from 'vscode' import os from 'os' +/* eslint-disable no-restricted-imports */ import { promises as nodefs, constants as nodeConstants, WriteFileOptions } from 'fs' import { chmod } from 'fs/promises' import { isCloud9 } from '../extensionUtilities' diff --git a/packages/core/src/shared/fs/templateRegistry.ts b/packages/core/src/shared/fs/templateRegistry.ts index aef91dc62e0..670649d8e39 100644 --- a/packages/core/src/shared/fs/templateRegistry.ts +++ b/packages/core/src/shared/fs/templateRegistry.ts @@ -17,6 +17,7 @@ import { Timeout } from '../utilities/timeoutUtils' import { localize } from '../utilities/vsCodeUtils' import { PerfLog } from '../logger/perfLogger' import { showMessageWithCancel } from '../utilities/messages' +/* eslint-disable no-restricted-imports */ import { readFileSync } from 'fs' export class CloudFormationTemplateRegistry extends WatchedFiles { diff --git a/packages/core/src/shared/handleUninstall.ts b/packages/core/src/shared/handleUninstall.ts index 6a8b2601a28..88cf59528c0 100644 --- a/packages/core/src/shared/handleUninstall.ts +++ b/packages/core/src/shared/handleUninstall.ts @@ -6,6 +6,7 @@ // Implementation inspired by https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/c864f15af264f0f456a6d8a83290b5c940715349/client/vscode/src/settings/uninstall.ts#L2 import * as vscode from 'vscode' +/* eslint-disable no-restricted-imports */ import { existsSync } from 'fs' import * as semver from 'semver' import { join } from 'path' diff --git a/packages/core/src/shared/icons.ts b/packages/core/src/shared/icons.ts index f74eef663b7..19bf7310c07 100644 --- a/packages/core/src/shared/icons.ts +++ b/packages/core/src/shared/icons.ts @@ -11,6 +11,7 @@ import { Uri, ThemeIcon, ThemeColor } from 'vscode' import { isCloud9 } from './extensionUtilities' import { memoize } from './utilities/functionUtils' import { getLogger } from './logger/logger' +/* eslint-disable no-restricted-imports */ import { existsSync } from 'fs' // Animation: diff --git a/packages/core/src/shared/resourcefetcher/httpResourceFetcher.ts b/packages/core/src/shared/resourcefetcher/httpResourceFetcher.ts index ddda8e8c43c..a731611c7b0 100644 --- a/packages/core/src/shared/resourcefetcher/httpResourceFetcher.ts +++ b/packages/core/src/shared/resourcefetcher/httpResourceFetcher.ts @@ -5,6 +5,7 @@ import * as http from 'http' import * as https from 'https' import * as stream from 'stream' +/* eslint-disable no-restricted-imports */ import * as fs from 'fs' import got, { Response, RequestError, CancelError } from 'got' import urlToOptions from 'got/dist/source/core/utils/url-to-options' diff --git a/packages/core/src/shared/sam/debugger/typescriptSamDebug.ts b/packages/core/src/shared/sam/debugger/typescriptSamDebug.ts index dbbbea29955..231679ebf18 100644 --- a/packages/core/src/shared/sam/debugger/typescriptSamDebug.ts +++ b/packages/core/src/shared/sam/debugger/typescriptSamDebug.ts @@ -2,7 +2,7 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - +/* eslint-disable no-restricted-imports */ import { existsSync, PathLike, readFileSync } from 'fs' import * as path from 'path' import * as vscode from 'vscode' diff --git a/packages/core/src/shared/utilities/streamUtilities.ts b/packages/core/src/shared/utilities/streamUtilities.ts index 159cf0e983f..826fd7f5a9c 100644 --- a/packages/core/src/shared/utilities/streamUtilities.ts +++ b/packages/core/src/shared/utilities/streamUtilities.ts @@ -2,7 +2,7 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ - +/* eslint-disable no-restricted-imports */ import { createReadStream as createRStream, createWriteStream as createWStream } from 'fs' import { Readable, Writable, pipeline } from 'stream' import * as vscode from 'vscode' diff --git a/packages/core/src/shared/utilities/textUtilities.ts b/packages/core/src/shared/utilities/textUtilities.ts index 477a94e8ec2..f1391f606fa 100644 --- a/packages/core/src/shared/utilities/textUtilities.ts +++ b/packages/core/src/shared/utilities/textUtilities.ts @@ -8,6 +8,7 @@ import { default as stripAnsi } from 'strip-ansi' import { isCloud9 } from '../extensionUtilities' import { getLogger } from '../logger' import fs from '../fs/fs' +/* eslint-disable no-restricted-imports */ import { close, openSync, writeSync } from 'fs' /** diff --git a/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts b/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts index 6ebd3c706a5..3e73bb14fcc 100644 --- a/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts +++ b/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts @@ -3,6 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +/* eslint-disable no-restricted-imports */ import { readFileSync } from 'fs' import { getLogger, Logger } from '../../../shared/logger' diff --git a/packages/core/src/test/awsService/ec2/sshKeyPair.test.ts b/packages/core/src/test/awsService/ec2/sshKeyPair.test.ts index 574450d20f9..d408f144a36 100644 --- a/packages/core/src/test/awsService/ec2/sshKeyPair.test.ts +++ b/packages/core/src/test/awsService/ec2/sshKeyPair.test.ts @@ -12,6 +12,7 @@ import { createTestWorkspaceFolder, installFakeClock } from '../../testUtil' import { InstalledClock } from '@sinonjs/fake-timers' import { ChildProcess } from '../../../shared/utilities/processUtils' import { fs, globals } from '../../../shared' +/* eslint-disable no-restricted-imports */ import { statSync } from 'fs' describe('SshKeyUtility', async function () { diff --git a/packages/core/src/test/codewhisperer/testUtil.ts b/packages/core/src/test/codewhisperer/testUtil.ts index d2164c40545..87b085323f6 100644 --- a/packages/core/src/test/codewhisperer/testUtil.ts +++ b/packages/core/src/test/codewhisperer/testUtil.ts @@ -17,6 +17,7 @@ import { getLogger } from '../../shared/logger' import { CodeWhispererCodeCoverageTracker } from '../../codewhisperer/tracker/codewhispererCodeCoverageTracker' import globals from '../../shared/extensionGlobals' import { session } from '../../codewhisperer/util/codeWhispererSession' +/* eslint-disable no-restricted-imports */ import { Dirent } from 'fs' import { DefaultAWSClientBuilder, ServiceOptions } from '../../shared/awsClientBuilder' import { FakeAwsContext } from '../utilities/fakeAwsContext' diff --git a/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts b/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts index 91c617a9e77..7fc11a05f31 100644 --- a/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts +++ b/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts @@ -25,6 +25,7 @@ import admZip from 'adm-zip' import { makeTemporaryToolkitFolder } from '../../../shared/filesystemUtilities' import { DefaultSchemaClient } from '../../../shared/clients/schemaClient' import { fs } from '../../../shared' +/* eslint-disable no-restricted-imports */ import { closeSync, openSync, writeSync } from 'fs' describe('CodeDownloader', function () { diff --git a/packages/core/src/test/shared/fs/fs.test.ts b/packages/core/src/test/shared/fs/fs.test.ts index 528a3bc0bca..08dd6e1d811 100644 --- a/packages/core/src/test/shared/fs/fs.test.ts +++ b/packages/core/src/test/shared/fs/fs.test.ts @@ -7,6 +7,7 @@ import assert from 'assert' import vscode from 'vscode' import * as path from 'path' import * as utils from 'util' +/* eslint-disable no-restricted-imports */ import { existsSync, mkdirSync, promises as nodefs, readFileSync } from 'fs' import { stat } from 'fs/promises' import fs, { FileSystem } from '../../../shared/fs/fs' From 8a9a620d08f961ae830043507b48a08836c41870 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 8 Oct 2024 14:54:38 -0400 Subject: [PATCH 4/5] move to inline comments --- .eslintrc.js | 2 +- packages/amazonq/scripts/build/copyFiles.ts | 3 +-- .../unit/amazonqGumby/transformationResultsHandler.test.ts | 4 ++-- .../unit/codewhisperer/service/securityScanHandler.test.ts | 3 +-- packages/core/scripts/build/copyFiles.ts | 3 +-- packages/core/scripts/build/generateServiceClient.ts | 3 +-- packages/core/src/amazonq/lsp/lspController.ts | 3 +-- packages/core/src/awsService/s3/commands/uploadFile.ts | 2 +- .../core/src/codewhisperer/service/securityScanHandler.ts | 2 +- .../service/transformByQ/transformApiHandler.ts | 3 +-- .../transformByQ/transformationResultsViewProvider.ts | 3 +-- .../core/src/eventSchemas/commands/downloadSchemaItemCode.ts | 3 +-- packages/core/src/shared/errors.ts | 3 +-- packages/core/src/shared/extensions/yaml.ts | 3 +-- packages/core/src/shared/fs/fs.ts | 3 +-- packages/core/src/shared/fs/templateRegistry.ts | 3 +-- packages/core/src/shared/handleUninstall.ts | 3 +-- packages/core/src/shared/icons.ts | 3 +-- .../core/src/shared/resourcefetcher/httpResourceFetcher.ts | 5 ++--- packages/core/src/shared/sam/debugger/typescriptSamDebug.ts | 3 +-- packages/core/src/shared/utilities/streamUtilities.ts | 3 +-- packages/core/src/shared/utilities/textUtilities.ts | 3 +-- .../getStateMachineDefinitionFromCfnTemplate.ts | 3 +-- packages/core/src/test/awsService/ec2/sshKeyPair.test.ts | 3 +-- packages/core/src/test/codewhisperer/testUtil.ts | 3 +-- .../eventSchemas/commands/downloadSchemaItemCode.test.ts | 3 +-- packages/core/src/test/shared/fs/fs.test.ts | 3 +-- packages/core/src/testInteg/sam.test.ts | 2 +- packages/core/src/testInteg/shared/extensions/git.test.ts | 2 +- packages/core/src/testLint/gitSecrets.test.ts | 1 + packages/toolkit/scripts/build/copyFiles.ts | 3 +-- .../toolkit/scripts/build/generateConfigurationAttributes.ts | 2 +- 32 files changed, 34 insertions(+), 57 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index c0725c5bff0..02377d70bb6 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -182,7 +182,7 @@ module.exports = { }, { name: 'fs', - message: 'Avoid fs and use shared/fs/fs.ts when possible. ', + message: 'Avoid fs and use shared/fs/fs.ts when possible.', }, ], }, diff --git a/packages/amazonq/scripts/build/copyFiles.ts b/packages/amazonq/scripts/build/copyFiles.ts index c1b0861321d..2022328a128 100644 --- a/packages/amazonq/scripts/build/copyFiles.ts +++ b/packages/amazonq/scripts/build/copyFiles.ts @@ -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` diff --git a/packages/amazonq/test/unit/amazonqGumby/transformationResultsHandler.test.ts b/packages/amazonq/test/unit/amazonqGumby/transformationResultsHandler.test.ts index f7e0b29a35e..bfba0733509 100644 --- a/packages/amazonq/test/unit/amazonqGumby/transformationResultsHandler.test.ts +++ b/packages/amazonq/test/unit/amazonqGumby/transformationResultsHandler.test.ts @@ -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] @@ -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] diff --git a/packages/amazonq/test/unit/codewhisperer/service/securityScanHandler.test.ts b/packages/amazonq/test/unit/codewhisperer/service/securityScanHandler.test.ts index 3e676702ffc..0eb6733a82a 100644 --- a/packages/amazonq/test/unit/codewhisperer/service/securityScanHandler.test.ts +++ b/packages/amazonq/test/unit/codewhisperer/service/securityScanHandler.test.ts @@ -17,8 +17,7 @@ import { import assert from 'assert' import sinon from 'sinon' import * as vscode from 'vscode' -/* eslint-disable no-restricted-imports */ -import * as fs from 'fs' +import * as fs from 'fs' // eslint-disable-line no-restricted-imports const mockCodeScanFindings = JSON.stringify([ { diff --git a/packages/core/scripts/build/copyFiles.ts b/packages/core/scripts/build/copyFiles.ts index ee5c9492bcb..dc653f87e3e 100644 --- a/packages/core/scripts/build/copyFiles.ts +++ b/packages/core/scripts/build/copyFiles.ts @@ -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` diff --git a/packages/core/scripts/build/generateServiceClient.ts b/packages/core/scripts/build/generateServiceClient.ts index 5ee72653212..5d9f7582531 100644 --- a/packages/core/scripts/build/generateServiceClient.ts +++ b/packages/core/scripts/build/generateServiceClient.ts @@ -3,9 +3,8 @@ * SPDX-License-Identifier: Apache-2.0 */ -/* eslint-disable no-restricted-imports */ import * as proc from 'child_process' -import * as fs from 'fs-extra' +import * as fs from 'fs-extra' // eslint-disable-line no-restricted-imports import * as path from 'path' const repoRoot = path.join(process.cwd(), '../../') // root/packages/toolkit -> root/ diff --git a/packages/core/src/amazonq/lsp/lspController.ts b/packages/core/src/amazonq/lsp/lspController.ts index 77c8f3f9e74..00d067ba6ed 100644 --- a/packages/core/src/amazonq/lsp/lspController.ts +++ b/packages/core/src/amazonq/lsp/lspController.ts @@ -6,8 +6,7 @@ import * as vscode from 'vscode' import * as path from 'path' import * as crypto from 'crypto' -/* eslint-disable no-restricted-imports */ -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' diff --git a/packages/core/src/awsService/s3/commands/uploadFile.ts b/packages/core/src/awsService/s3/commands/uploadFile.ts index 43b17aa81c4..4bfcc91e027 100644 --- a/packages/core/src/awsService/s3/commands/uploadFile.ts +++ b/packages/core/src/awsService/s3/commands/uploadFile.ts @@ -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' diff --git a/packages/core/src/codewhisperer/service/securityScanHandler.ts b/packages/core/src/codewhisperer/service/securityScanHandler.ts index 50db7cc4ada..65eb47b173e 100644 --- a/packages/core/src/codewhisperer/service/securityScanHandler.ts +++ b/packages/core/src/codewhisperer/service/securityScanHandler.ts @@ -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 } 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') diff --git a/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts b/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts index ed8bdd12967..c9d276f5374 100644 --- a/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts +++ b/packages/core/src/codewhisperer/service/transformByQ/transformApiHandler.ts @@ -46,8 +46,7 @@ import { ExportIntent, TransformationDownloadArtifactType } from '@amzn/codewhis import fs from '../../../shared/fs/fs' import { ChatSessionManager } from '../../../amazonqGumby/chat/storages/chatSession' import { convertToTimeString, encodeHTML } from '../../../shared/utilities/textUtilities' -/* eslint-disable no-restricted-imports */ -import { readdirSync } from 'fs' +import { readdirSync } from 'fs' // eslint-disable-line no-restricted-imports export function getSha256(buffer: Buffer) { const hasher = crypto.createHash('sha256') diff --git a/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts b/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts index a257d53751f..fa792e31314 100644 --- a/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts +++ b/packages/core/src/codewhisperer/service/transformByQ/transformationResultsViewProvider.ts @@ -20,8 +20,7 @@ import { createCodeWhispererChatStreamingClient } from '../../../shared/clients/ import { ChatSessionManager } from '../../../amazonqGumby/chat/storages/chatSession' import { setContext } from '../../../shared/vscode/setContext' import { fs } from '../../../shared' -/* eslint-disable no-restricted-imports */ -import { existsSync, readFileSync, rmSync, writeFileSync } from 'fs' +import { existsSync, readFileSync, rmSync, writeFileSync } from 'fs' // eslint-disable-line no-restricted-imports export abstract class ProposedChangeNode { abstract readonly resourcePath: string diff --git a/packages/core/src/eventSchemas/commands/downloadSchemaItemCode.ts b/packages/core/src/eventSchemas/commands/downloadSchemaItemCode.ts index 11982618fa8..7c6811cce35 100644 --- a/packages/core/src/eventSchemas/commands/downloadSchemaItemCode.ts +++ b/packages/core/src/eventSchemas/commands/downloadSchemaItemCode.ts @@ -25,8 +25,7 @@ import { import admZip from 'adm-zip' import globals from '../../shared/extensionGlobals' import { telemetry } from '../../shared/telemetry/telemetry' -/* eslint-disable no-restricted-imports */ -import { closeSync, openSync, writeSync } from 'fs' +import { closeSync, openSync, writeSync } from 'fs' // eslint-disable-line no-restricted-imports import { fs } from '../../shared' enum CodeGenerationStatus { diff --git a/packages/core/src/shared/errors.ts b/packages/core/src/shared/errors.ts index 2b47e523d92..30b07a55314 100644 --- a/packages/core/src/shared/errors.ts +++ b/packages/core/src/shared/errors.ts @@ -10,8 +10,7 @@ import { isThrottlingError, isTransientError } from '@smithy/service-error-class import { Result } from './telemetry/telemetry' import { CancellationError } from './utilities/timeoutUtils' import { hasKey, isNonNullable } from './utilities/tsUtils' -/* eslint-disable no-restricted-imports */ -import type * as nodefs from 'fs' +import type * as nodefs from 'fs' // eslint-disable-line no-restricted-imports import type * as os from 'os' import { CodeWhispererStreamingServiceException } from '@amzn/codewhisperer-streaming' import { driveLetterRegex } from './utilities/pathUtils' diff --git a/packages/core/src/shared/extensions/yaml.ts b/packages/core/src/shared/extensions/yaml.ts index cac683f8576..5e8b54ead2c 100644 --- a/packages/core/src/shared/extensions/yaml.ts +++ b/packages/core/src/shared/extensions/yaml.ts @@ -9,8 +9,7 @@ import { getLogger } from '../logger/logger' import { getIdeProperties } from '../extensionUtilities' import { activateExtension } from '../utilities/vsCodeUtils' import { AWS_SCHEME } from '../constants' -/* eslint-disable no-restricted-imports */ -import { readFileSync } from 'fs' +import { readFileSync } from 'fs' // eslint-disable-line no-restricted-imports // sourced from https://github.com/redhat-developer/vscode-yaml/blob/3d82d61ea63d3e3a9848fe6b432f8f1f452c1bec/src/schema-extension-api.ts // removed everything that is not currently being used diff --git a/packages/core/src/shared/fs/fs.ts b/packages/core/src/shared/fs/fs.ts index 80d2fc9d873..477b16c12ea 100644 --- a/packages/core/src/shared/fs/fs.ts +++ b/packages/core/src/shared/fs/fs.ts @@ -4,8 +4,7 @@ */ import vscode from 'vscode' import os from 'os' -/* eslint-disable no-restricted-imports */ -import { promises as nodefs, constants as nodeConstants, WriteFileOptions } from 'fs' +import { promises as nodefs, constants as nodeConstants, WriteFileOptions } from 'fs' // eslint-disable-line no-restricted-imports import { chmod } from 'fs/promises' import { isCloud9 } from '../extensionUtilities' import _path from 'path' diff --git a/packages/core/src/shared/fs/templateRegistry.ts b/packages/core/src/shared/fs/templateRegistry.ts index 670649d8e39..0db7f9346a4 100644 --- a/packages/core/src/shared/fs/templateRegistry.ts +++ b/packages/core/src/shared/fs/templateRegistry.ts @@ -17,8 +17,7 @@ import { Timeout } from '../utilities/timeoutUtils' import { localize } from '../utilities/vsCodeUtils' import { PerfLog } from '../logger/perfLogger' import { showMessageWithCancel } from '../utilities/messages' -/* eslint-disable no-restricted-imports */ -import { readFileSync } from 'fs' +import { readFileSync } from 'fs' // eslint-disable-line no-restricted-imports export class CloudFormationTemplateRegistry extends WatchedFiles { public name: string = 'CloudFormationTemplateRegistry' diff --git a/packages/core/src/shared/handleUninstall.ts b/packages/core/src/shared/handleUninstall.ts index 88cf59528c0..e6f9bb55478 100644 --- a/packages/core/src/shared/handleUninstall.ts +++ b/packages/core/src/shared/handleUninstall.ts @@ -6,8 +6,7 @@ // Implementation inspired by https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/c864f15af264f0f456a6d8a83290b5c940715349/client/vscode/src/settings/uninstall.ts#L2 import * as vscode from 'vscode' -/* eslint-disable no-restricted-imports */ -import { existsSync } from 'fs' +import { existsSync } from 'fs' // eslint-disable-line no-restricted-imports import * as semver from 'semver' import { join } from 'path' import { getLogger } from './logger/logger' diff --git a/packages/core/src/shared/icons.ts b/packages/core/src/shared/icons.ts index 19bf7310c07..ee399cc8468 100644 --- a/packages/core/src/shared/icons.ts +++ b/packages/core/src/shared/icons.ts @@ -11,8 +11,7 @@ import { Uri, ThemeIcon, ThemeColor } from 'vscode' import { isCloud9 } from './extensionUtilities' import { memoize } from './utilities/functionUtils' import { getLogger } from './logger/logger' -/* eslint-disable no-restricted-imports */ -import { existsSync } from 'fs' +import { existsSync } from 'fs' // eslint-disable-line no-restricted-imports // Animation: // https://code.visualstudio.com/api/references/icons-in-labels#animation diff --git a/packages/core/src/shared/resourcefetcher/httpResourceFetcher.ts b/packages/core/src/shared/resourcefetcher/httpResourceFetcher.ts index a731611c7b0..303ca46d81d 100644 --- a/packages/core/src/shared/resourcefetcher/httpResourceFetcher.ts +++ b/packages/core/src/shared/resourcefetcher/httpResourceFetcher.ts @@ -5,8 +5,7 @@ import * as http from 'http' import * as https from 'https' import * as stream from 'stream' -/* eslint-disable no-restricted-imports */ -import * as fs from 'fs' +import * as fs from 'fs' // eslint-disable-line no-restricted-imports import got, { Response, RequestError, CancelError } from 'got' import urlToOptions from 'got/dist/source/core/utils/url-to-options' import Request from 'got/dist/source/core' @@ -16,7 +15,7 @@ import { ResourceFetcher } from './resourcefetcher' import { Timeout, CancellationError, CancelEvent } from '../utilities/timeoutUtils' import { isCloud9 } from '../extensionUtilities' import { Headers } from 'got/dist/source/core' -import { createWriteStream } from 'fs' +import { createWriteStream } from 'fs' // eslint-disable-line no-restricted-imports // XXX: patched Got module for compatability with older VS Code versions (e.g. Cloud9) // `got` has also deprecated `urlToOptions` diff --git a/packages/core/src/shared/sam/debugger/typescriptSamDebug.ts b/packages/core/src/shared/sam/debugger/typescriptSamDebug.ts index 231679ebf18..f2d05887946 100644 --- a/packages/core/src/shared/sam/debugger/typescriptSamDebug.ts +++ b/packages/core/src/shared/sam/debugger/typescriptSamDebug.ts @@ -2,8 +2,7 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ -/* eslint-disable no-restricted-imports */ -import { existsSync, PathLike, readFileSync } from 'fs' +import { existsSync, PathLike, readFileSync } from 'fs' // eslint-disable-line no-restricted-imports import * as path from 'path' import * as vscode from 'vscode' import { isImageLambdaConfig, NodejsDebugConfiguration } from '../../../lambda/local/debugConfiguration' diff --git a/packages/core/src/shared/utilities/streamUtilities.ts b/packages/core/src/shared/utilities/streamUtilities.ts index 826fd7f5a9c..63b8454f6e0 100644 --- a/packages/core/src/shared/utilities/streamUtilities.ts +++ b/packages/core/src/shared/utilities/streamUtilities.ts @@ -2,8 +2,7 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ -/* eslint-disable no-restricted-imports */ -import { createReadStream as createRStream, createWriteStream as createWStream } from 'fs' +import { createReadStream as createRStream, createWriteStream as createWStream } from 'fs' // eslint-disable-line no-restricted-imports import { Readable, Writable, pipeline } from 'stream' import * as vscode from 'vscode' diff --git a/packages/core/src/shared/utilities/textUtilities.ts b/packages/core/src/shared/utilities/textUtilities.ts index f1391f606fa..2747bb765ed 100644 --- a/packages/core/src/shared/utilities/textUtilities.ts +++ b/packages/core/src/shared/utilities/textUtilities.ts @@ -8,8 +8,7 @@ import { default as stripAnsi } from 'strip-ansi' import { isCloud9 } from '../extensionUtilities' import { getLogger } from '../logger' import fs from '../fs/fs' -/* eslint-disable no-restricted-imports */ -import { close, openSync, writeSync } from 'fs' +import { close, openSync, writeSync } from 'fs' // eslint-disable-line no-restricted-imports /** * Truncates string `s` if it exceeds `n` chars. diff --git a/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts b/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts index 3e73bb14fcc..2ac083b3057 100644 --- a/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts +++ b/packages/core/src/stepFunctions/commands/visualizeStateMachine/getStateMachineDefinitionFromCfnTemplate.ts @@ -3,8 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -/* eslint-disable no-restricted-imports */ -import { readFileSync } from 'fs' +import { readFileSync } from 'fs' // eslint-disable-line no-restricted-imports import { getLogger, Logger } from '../../../shared/logger' /** diff --git a/packages/core/src/test/awsService/ec2/sshKeyPair.test.ts b/packages/core/src/test/awsService/ec2/sshKeyPair.test.ts index d408f144a36..23bba8a6a74 100644 --- a/packages/core/src/test/awsService/ec2/sshKeyPair.test.ts +++ b/packages/core/src/test/awsService/ec2/sshKeyPair.test.ts @@ -12,8 +12,7 @@ import { createTestWorkspaceFolder, installFakeClock } from '../../testUtil' import { InstalledClock } from '@sinonjs/fake-timers' import { ChildProcess } from '../../../shared/utilities/processUtils' import { fs, globals } from '../../../shared' -/* eslint-disable no-restricted-imports */ -import { statSync } from 'fs' +import { statSync } from 'fs' // eslint-disable-line no-restricted-imports describe('SshKeyUtility', async function () { let temporaryDirectory: string diff --git a/packages/core/src/test/codewhisperer/testUtil.ts b/packages/core/src/test/codewhisperer/testUtil.ts index 87b085323f6..6547a57a7ee 100644 --- a/packages/core/src/test/codewhisperer/testUtil.ts +++ b/packages/core/src/test/codewhisperer/testUtil.ts @@ -17,8 +17,7 @@ import { getLogger } from '../../shared/logger' import { CodeWhispererCodeCoverageTracker } from '../../codewhisperer/tracker/codewhispererCodeCoverageTracker' import globals from '../../shared/extensionGlobals' import { session } from '../../codewhisperer/util/codeWhispererSession' -/* eslint-disable no-restricted-imports */ -import { Dirent } from 'fs' +import { Dirent } from 'fs' // eslint-disable-line no-restricted-imports import { DefaultAWSClientBuilder, ServiceOptions } from '../../shared/awsClientBuilder' import { FakeAwsContext } from '../utilities/fakeAwsContext' import { Service } from 'aws-sdk' diff --git a/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts b/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts index 7fc11a05f31..14cf659fe21 100644 --- a/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts +++ b/packages/core/src/test/eventSchemas/commands/downloadSchemaItemCode.test.ts @@ -25,8 +25,7 @@ import admZip from 'adm-zip' import { makeTemporaryToolkitFolder } from '../../../shared/filesystemUtilities' import { DefaultSchemaClient } from '../../../shared/clients/schemaClient' import { fs } from '../../../shared' -/* eslint-disable no-restricted-imports */ -import { closeSync, openSync, writeSync } from 'fs' +import { closeSync, openSync, writeSync } from 'fs' // eslint-disable-line no-restricted-imports describe('CodeDownloader', function () { let tempFolder: string diff --git a/packages/core/src/test/shared/fs/fs.test.ts b/packages/core/src/test/shared/fs/fs.test.ts index 08dd6e1d811..4bd313cd9d3 100644 --- a/packages/core/src/test/shared/fs/fs.test.ts +++ b/packages/core/src/test/shared/fs/fs.test.ts @@ -7,8 +7,7 @@ import assert from 'assert' import vscode from 'vscode' import * as path from 'path' import * as utils from 'util' -/* eslint-disable no-restricted-imports */ -import { existsSync, mkdirSync, promises as nodefs, readFileSync } from 'fs' +import { existsSync, mkdirSync, promises as nodefs, readFileSync } from 'fs' // eslint-disable-line no-restricted-imports import { stat } from 'fs/promises' import fs, { FileSystem } from '../../../shared/fs/fs' import * as os from 'os' diff --git a/packages/core/src/testInteg/sam.test.ts b/packages/core/src/testInteg/sam.test.ts index 81db4b78412..fb078f0212b 100644 --- a/packages/core/src/testInteg/sam.test.ts +++ b/packages/core/src/testInteg/sam.test.ts @@ -5,7 +5,7 @@ import assert from 'assert' import { Runtime } from 'aws-sdk/clients/lambda' -import { mkdtempSync } from 'fs' +import { mkdtempSync } from 'fs' // eslint-disable-line no-restricted-imports import * as path from 'path' import * as semver from 'semver' import * as vscode from 'vscode' diff --git a/packages/core/src/testInteg/shared/extensions/git.test.ts b/packages/core/src/testInteg/shared/extensions/git.test.ts index 5f79dc99888..e2a261c3312 100644 --- a/packages/core/src/testInteg/shared/extensions/git.test.ts +++ b/packages/core/src/testInteg/shared/extensions/git.test.ts @@ -9,7 +9,7 @@ import vscode from 'vscode' import * as GitTypes from '../../../../types/git' import { GitExtension, Repository } from '../../../shared/extensions/git' import { makeTemporaryToolkitFolder } from '../../../shared/filesystemUtilities' -import { realpathSync } from 'fs' +import { realpathSync } from 'fs' // eslint-disable-line no-restricted-imports import { execFileSync } from 'child_process' import { sleep } from '../../../shared/utilities/timeoutUtils' import { getLogger } from '../../../shared/logger/logger' diff --git a/packages/core/src/testLint/gitSecrets.test.ts b/packages/core/src/testLint/gitSecrets.test.ts index a9049e6aa64..fe6e629dc50 100644 --- a/packages/core/src/testLint/gitSecrets.test.ts +++ b/packages/core/src/testLint/gitSecrets.test.ts @@ -8,6 +8,7 @@ import { describe } from 'mocha' import assert from 'assert' import * as path from 'path' import { platform } from 'os' +//import { existsSync, mkdirSync, unlinkSync, writeFileSync } from 'fs' import { runCmd } from './testUtils' import { fs } from '../shared' diff --git a/packages/toolkit/scripts/build/copyFiles.ts b/packages/toolkit/scripts/build/copyFiles.ts index 99c79124637..44eee1f208e 100644 --- a/packages/toolkit/scripts/build/copyFiles.ts +++ b/packages/toolkit/scripts/build/copyFiles.ts @@ -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' // Copies various dependencies into "dist/". diff --git a/packages/toolkit/scripts/build/generateConfigurationAttributes.ts b/packages/toolkit/scripts/build/generateConfigurationAttributes.ts index 85468cc32b0..b0d276f6753 100644 --- a/packages/toolkit/scripts/build/generateConfigurationAttributes.ts +++ b/packages/toolkit/scripts/build/generateConfigurationAttributes.ts @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import * as fs from 'fs' +import * as fs from 'fs' // eslint-disable-line no-restricted-imports import * as path from 'path' import { JSONSchema4 } from 'json-schema' import { compile } from 'json-schema-to-typescript' From 103ae4d35ec9f5af0927bd5be8db3386eb4773e9 Mon Sep 17 00:00:00 2001 From: hkobew Date: Tue, 8 Oct 2024 16:52:35 -0400 Subject: [PATCH 5/5] undo gitSecrets changes --- packages/core/src/testLint/gitSecrets.test.ts | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/packages/core/src/testLint/gitSecrets.test.ts b/packages/core/src/testLint/gitSecrets.test.ts index fe6e629dc50..deea710018a 100644 --- a/packages/core/src/testLint/gitSecrets.test.ts +++ b/packages/core/src/testLint/gitSecrets.test.ts @@ -8,9 +8,8 @@ import { describe } from 'mocha' import assert from 'assert' import * as path from 'path' import { platform } from 'os' -//import { existsSync, mkdirSync, unlinkSync, writeFileSync } from 'fs' +import { existsSync, mkdirSync, unlinkSync, writeFileSync } from 'fs' // eslint-disable-line no-restricted-imports import { runCmd } from './testUtils' -import { fs } from '../shared' /** * NOTES: @@ -41,22 +40,22 @@ describe('git-secrets', function () { }) } - async function setupTestFixturesDir(toolkitProjectDir: string) { + function setupTestFixturesDir(toolkitProjectDir: string) { const testFixturesPath = path.join(toolkitProjectDir, 'src', 'testFixtures', 'bin') - await fs.mkdir(testFixturesPath) + mkdirSync(testFixturesPath, { recursive: true }) return testFixturesPath } - async function setupAccessKeyFile(testFixturesPath: string) { + function setupAccessKeyFile(testFixturesPath: string) { const accessKeyFilePath = path.join(testFixturesPath, 'fileWithAccessKey.ts') - await deleteFileIfExists(accessKeyFilePath) + deleteFileIfExists(accessKeyFilePath) return accessKeyFilePath } - async function setupGitSecretsExecutable(testFixturesPath: string) { + function setupGitSecretsExecutable(testFixturesPath: string) { const gitSecretsExecutablePath = path.join(testFixturesPath, 'git-secrets') - if (await fs.exists(gitSecretsExecutablePath)) { + if (existsSync(gitSecretsExecutablePath)) { console.log('INFO: git-secrets already installed') } else { console.log('INFO: Installing git-secrets...') @@ -73,13 +72,13 @@ describe('git-secrets', function () { return gitSecretsExecutablePath } - async function deleteFileIfExists(filePath: string) { - if (await fs.exists(filePath)) { - await fs.delete(filePath) + function deleteFileIfExists(filePath: string) { + if (existsSync(filePath)) { + unlinkSync(filePath) } } - async function createFileWithSecretKey(accessKeyFilePath: string) { + function createFileWithSecretKey(accessKeyFilePath: string) { // Create file in project that has secret key value. // Need to build access key string incrementally to not trigger git-secrets. const keyValue = 'yAki21XLhAIBiKvyaxr4p/ltr8OxkZTHISISFAKE' @@ -92,18 +91,18 @@ describe('git-secrets', function () { ${mySecretAccessKey} `.trim() - await fs.writeFile(accessKeyFilePath, fileContent) + writeFileSync(accessKeyFilePath, fileContent) } - before(async function () { + before(function () { if (platform() === 'win32') { this.skip() } toolkitProjectDir = path.resolve() - testFixturesPath = await setupTestFixturesDir(toolkitProjectDir) - gitSecrets = await setupGitSecretsExecutable(testFixturesPath) - accessKeyFilePath = await setupAccessKeyFile(testFixturesPath) + testFixturesPath = setupTestFixturesDir(toolkitProjectDir) + gitSecrets = setupGitSecretsExecutable(testFixturesPath) + accessKeyFilePath = setupAccessKeyFile(testFixturesPath) // Register all patterns with `git-secrets` runCmd([gitSecrets, '--register-aws'], { cwd: toolkitProjectDir }) @@ -111,8 +110,8 @@ ${mySecretAccessKey} setAllowListPatterns(gitSecrets) }) - afterEach(async function () { - await deleteFileIfExists(accessKeyFilePath) + afterEach(function () { + deleteFileIfExists(accessKeyFilePath) }) it('ensures no git secrets are found', function () { @@ -120,8 +119,8 @@ ${mySecretAccessKey} assert.strictEqual(result.status, 0, `Failure output: ${result.stderr.toString()}`) }) - it('sanity check it finds secrets', async function () { - await createFileWithSecretKey(accessKeyFilePath) + it('sanity check it finds secrets', function () { + createFileWithSecretKey(accessKeyFilePath) const result = runCmd([gitSecrets, '--scan', accessKeyFilePath], { cwd: toolkitProjectDir, throws: false }) assert.strictEqual(result.status, 1) })