33 * SPDX-License-Identifier: Apache-2.0
44 */
55import * as vscode from 'vscode'
6- import * as nodefs from 'fs'
76import * as path from 'path'
87import * as os from 'os'
98import * as codeWhisperer from '../../client/codewhisperer'
@@ -47,6 +46,7 @@ import { ExportIntent, TransformationDownloadArtifactType } from '@amzn/codewhis
4746import fs from '../../../shared/fs/fs'
4847import { ChatSessionManager } from '../../../amazonqGumby/chat/storages/chatSession'
4948import { convertToTimeString , encodeHTML } from '../../../shared/utilities/textUtilities'
49+ import { readdirSync } from 'fs'
5050
5151export function getSha256 ( buffer : Buffer ) {
5252 const hasher = crypto . createHash ( 'sha256' )
@@ -109,7 +109,7 @@ export async function uploadArtifactToS3(
109109) {
110110 throwIfCancelled ( )
111111 try {
112- const uploadFileByteSize = ( await nodefs . promises . stat ( fileName ) ) . size
112+ const uploadFileByteSize = ( await fs . stat ( fileName ) ) . size
113113 getLogger ( ) . info (
114114 `Uploading project artifact at %s with checksum %s using uploadId: %s and size %s kB` ,
115115 fileName ,
@@ -249,7 +249,7 @@ function isExcludedDependencyFile(path: string): boolean {
249249 * getFilesRecursively on the source code folder.
250250 */
251251function getFilesRecursively ( dir : string , isDependenciesFolder : boolean ) : string [ ] {
252- const entries = nodefs . readdirSync ( dir , { withFileTypes : true } )
252+ const entries = readdirSync ( dir , { withFileTypes : true } )
253253 const files = entries . flatMap ( ( entry ) => {
254254 const res = path . resolve ( dir , entry . name )
255255 // exclude 'target' directory from ZIP (except if zipping dependencies) due to issues in backend
@@ -301,14 +301,14 @@ export async function zipCode({ dependenciesFolder, humanInTheLoopFlag, modulePa
301301 const sourceFiles = getFilesRecursively ( modulePath , false )
302302 let sourceFilesSize = 0
303303 for ( const file of sourceFiles ) {
304- if ( nodefs . statSync ( file ) . isDirectory ( ) ) {
304+ if ( await fs . existsDir ( file ) ) {
305305 getLogger ( ) . info ( 'CodeTransformation: Skipping directory, likely a symlink' )
306306 continue
307307 }
308308 const relativePath = path . relative ( modulePath , file )
309309 const paddedPath = path . join ( 'sources' , relativePath )
310310 zip . addLocalFile ( file , path . dirname ( paddedPath ) )
311- sourceFilesSize += ( await nodefs . promises . stat ( file ) ) . size
311+ sourceFilesSize += ( await fs . stat ( file ) ) . size
312312 }
313313 getLogger ( ) . info ( `CodeTransformation: source code files size = ${ sourceFilesSize } ` )
314314 }
@@ -330,7 +330,7 @@ export async function zipCode({ dependenciesFolder, humanInTheLoopFlag, modulePa
330330 // const paddedPath = path.join(`dependencies/${dependenciesFolder.name}`, relativePath)
331331 const paddedPath = path . join ( `dependencies/` , relativePath )
332332 zip . addLocalFile ( file , path . dirname ( paddedPath ) )
333- dependencyFilesSize += ( await nodefs . promises . stat ( file ) ) . size
333+ dependencyFilesSize += ( await fs . stat ( file ) ) . size
334334 }
335335 getLogger ( ) . info ( `CodeTransformation: dependency files size = ${ dependencyFilesSize } ` )
336336 dependenciesCopied = true
@@ -365,7 +365,7 @@ export async function zipCode({ dependenciesFolder, humanInTheLoopFlag, modulePa
365365 }
366366 }
367367
368- const zipSize = ( await nodefs . promises . stat ( tempFilePath ) ) . size
368+ const zipSize = ( await fs . stat ( tempFilePath ) ) . size
369369
370370 const exceedsLimit = zipSize > CodeWhispererConstants . uploadZipSizeLimitInBytes
371371
@@ -408,8 +408,8 @@ export async function startJob(uploadId: string) {
408408 }
409409}
410410
411- export function getImageAsBase64 ( filePath : string ) {
412- const fileContents = nodefs . readFileSync ( filePath , { encoding : 'base64' } )
411+ export async function getImageAsBase64 ( filePath : string ) {
412+ const fileContents = Buffer . from ( await fs . readFileBytes ( filePath ) ) . toString ( 'base64' )
413413 return `data:image/svg+xml;base64,${ fileContents } `
414414}
415415
@@ -418,7 +418,7 @@ export function getImageAsBase64(filePath: string) {
418418 * ex. getIcon('transform-file') returns the 'transform-file-light.svg' icon if user has a light theme enabled,
419419 * otherwise 'transform-file-dark.svg' is returned.
420420 */
421- export function getTransformationIcon ( name : string ) {
421+ export async function getTransformationIcon ( name : string ) {
422422 let iconPath = ''
423423 switch ( name ) {
424424 case 'linesOfCode' :
@@ -448,7 +448,7 @@ export function getTransformationIcon(name: string) {
448448 } else {
449449 iconPath += '-dark.svg'
450450 }
451- return getImageAsBase64 ( globals . context . asAbsolutePath ( path . join ( 'resources/icons/aws/amazonq' , iconPath ) ) )
451+ return await getImageAsBase64 ( globals . context . asAbsolutePath ( path . join ( 'resources/icons/aws/amazonq' , iconPath ) ) )
452452}
453453
454454export function getFormattedString ( s : string ) {
@@ -495,17 +495,17 @@ export function getTableMapping(stepZeroProgressUpdates: ProgressUpdates) {
495495 return map
496496}
497497
498- export function getJobStatisticsHtml ( jobStatistics : any ) {
498+ export async function getJobStatisticsHtml ( jobStatistics : any ) {
499499 let htmlString = ''
500500 if ( jobStatistics . length === 0 ) {
501501 return htmlString
502502 }
503503 htmlString += `<div style="flex: 1; margin-left: 20px; border: 1px solid #424750; border-radius: 8px; padding: 10px;">`
504- jobStatistics . forEach ( ( stat : { name : string ; value : string } ) => {
505- htmlString += `<p style="margin-bottom: 4px"><img src="${ getTransformationIcon (
504+ for ( const stat of jobStatistics ) {
505+ htmlString += `<p style="margin-bottom: 4px"><img src="${ await getTransformationIcon (
506506 stat . name
507507 ) } " style="vertical-align: middle;"> ${ getFormattedString ( stat . name ) } : ${ stat . value } </p>`
508- } )
508+ }
509509 htmlString += `</div>`
510510 return htmlString
511511}
@@ -533,9 +533,9 @@ export async function getTransformationPlan(jobId: string) {
533533 const jobStatistics = JSON . parse ( tableMapping [ '0' ] ) . rows // ID of '0' reserved for job statistics table
534534
535535 // get logo directly since we only use one logo regardless of color theme
536- const logoIcon = getTransformationIcon ( 'transformLogo' )
536+ const logoIcon = await getTransformationIcon ( 'transformLogo' )
537537
538- const arrowIcon = getTransformationIcon ( 'upArrow' )
538+ const arrowIcon = await getTransformationIcon ( 'upArrow' )
539539
540540 let plan = `<style>table {border: 1px solid #424750;}</style>\n\n<a id="top"></a><br><p style="font-size: 24px;"><img src="${ logoIcon } " style="margin-right: 15px; vertical-align: middle;"></img><b>${ CodeWhispererConstants . planTitle } </b></p><br>`
541541 const authType = await getAuthType ( )
@@ -547,7 +547,7 @@ export async function getTransformationPlan(jobId: string) {
547547 }
548548 plan += `<div style="display: flex;"><div style="flex: 1; border: 1px solid #424750; border-radius: 8px; padding: 10px;"><p>${
549549 CodeWhispererConstants . planIntroductionMessage
550- } </p></div>${ getJobStatisticsHtml ( jobStatistics ) } </div>`
550+ } </p></div>${ await getJobStatisticsHtml ( jobStatistics ) } </div>`
551551 plan += `<div style="margin-top: 32px; border: 1px solid #424750; border-radius: 8px; padding: 10px;"><p style="font-size: 18px; margin-bottom: 4px;"><b>${ CodeWhispererConstants . planHeaderMessage } </b></p><i>${ CodeWhispererConstants . planDisclaimerMessage } <a href="https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/code-transformation.html">Read more.</a></i><br><br>`
552552 response . transformationPlan . transformationSteps . slice ( 1 ) . forEach ( ( step ) => {
553553 plan += `<div style="border: 1px solid #424750; border-radius: 8px; padding: 20px;"><div style="display:flex; justify-content:space-between; align-items:center;"><p style="font-size: 16px; margin-bottom: 4px;">${ step . name } </p><a href="#top">Scroll to top <img src="${ arrowIcon } " style="vertical-align: middle"></a></div><p>${ step . description } </p>`
0 commit comments