diff --git a/tools/code-analyzer/src/lib/hook-changes.ts b/tools/code-analyzer/src/lib/hook-changes.ts index 0bf4f3da5c3..04cbb576871 100644 --- a/tools/code-analyzer/src/lib/hook-changes.ts +++ b/tools/code-analyzer/src/lib/hook-changes.ts @@ -24,7 +24,7 @@ export type HookChangeDescription = { ghLink: string; }; -export const scanForHookChanges = ( +export const scanForHookChanges = async ( content: string, version: string, tmpRepoPath: string @@ -51,6 +51,7 @@ export const scanForHookChanges = ( if ( ! patchWithHook ) { continue; } + const results = patchWithHook.match( newRegEx ); if ( ! results ) { @@ -81,37 +82,39 @@ export const scanForHookChanges = ( if ( ! hookName[ 2 ].startsWith( '-' ) ) { let ghLink = ''; - fs.readFile( - tmpRepoPath + filePath, - 'utf-8', - function ( err, data ) { - if ( err ) { - console.error( err ); + try { + const data = await fs.promises.readFile( + tmpRepoPath + filePath, + 'utf-8' + ); + + const reg = new RegExp( name ); + + data.split( '\n' ).forEach( ( line, index ) => { + if ( line.match( reg ) ) { + const lineNum = index + 1; + + ghLink = `https://github.com/woocommerce/woocommerce/blob/${ version }/${ filePath.replace( + /(^\/)/, + '' + ) }#L${ lineNum }`; } + } ); - const reg = new RegExp( name ); - data.split( '\n' ).forEach( ( line, index ) => { - if ( line.match( reg ) ) { - const lineNum = index + 1; - - ghLink = `https://github.com/woocommerce/woocommerce/blob/${ version }/${ filePath.replace( - /(^\/)/, - '' - ) }#L${ lineNum }`; - } - } ); - - changes.set( filePath, { - filePath, - name, - hookType, - description, - changeType, - version, - ghLink, - } ); + changes.set( filePath, { + filePath, + name, + hookType, + description, + changeType, + version, + ghLink, + } ); + } catch ( error ) { + if ( error ) { + console.error( error ); } - ); + } } } } diff --git a/tools/code-analyzer/src/lib/scan-changes.ts b/tools/code-analyzer/src/lib/scan-changes.ts index f89e95069e9..829f4c84d65 100644 --- a/tools/code-analyzer/src/lib/scan-changes.ts +++ b/tools/code-analyzer/src/lib/scan-changes.ts @@ -50,7 +50,11 @@ export const scanForChanges = async ( const pluginPath = join( tmpRepoPath, 'plugins/woocommerce' ); Logger.startTask( 'Detecting hook changes...' ); - const hookChanges = scanForHookChanges( diff, sinceVersion, tmpRepoPath ); + const hookChanges = await scanForHookChanges( + diff, + sinceVersion, + tmpRepoPath + ); Logger.endTask(); Logger.startTask( 'Detecting template changes...' );