diff --git a/bin/plugin/commands/performance.js b/bin/plugin/commands/performance.js index 7f0c95a3184290..c475d19a00a34d 100644 --- a/bin/plugin/commands/performance.js +++ b/bin/plugin/commands/performance.js @@ -17,6 +17,7 @@ const { runShellScript, readJSONFile, writeJSONFile, + getFileHash, } = require( '../lib/utils' ); const config = require( '../config' ); @@ -423,8 +424,35 @@ async function runPerformanceTests( refs, options ) { .reset( 'hard', `origin/${ ref }` ) .checkout( ref ); - logRefAction( 'Installing dependencies' ); - await runShellScript( 'npm ci', buildDir ); + // Compare package-lock.json files to see if we can use the + // node modules already installed within the root directory. + const rootPackageLockHash = getFileHash( + path.join( CWD, 'package-lock.json' ) + ); + const targetPackageLockHash = getFileHash( + path.join( buildDir, 'package-lock.json' ) + ); + + if ( rootPackageLockHash === targetPackageLockHash ) { + logRefAction( 'Using existing dependencies' ); + + const sourceNodeModulesPath = path.join( + CWD, + 'node_modules' + ); + const targetNodeModulesPath = path.join( + buildDir, + 'node_modules' + ); + + await runShellScript( + `ln -s ${ sourceNodeModulesPath } ${ targetNodeModulesPath }` + ); + } else { + logRefAction( 'Installing dependencies' ); + await runShellScript( 'npm ci', buildDir ); + } + doBuild = true; } } diff --git a/bin/plugin/lib/utils.js b/bin/plugin/lib/utils.js index 3924ffb66f3222..5cd34d24308911 100644 --- a/bin/plugin/lib/utils.js +++ b/bin/plugin/lib/utils.js @@ -8,6 +8,7 @@ const childProcess = require( 'child_process' ); const { v4: uuid } = require( 'uuid' ); const path = require( 'path' ); const os = require( 'os' ); +const crypto = require( 'crypto' ); /** * Internal dependencies @@ -134,6 +135,19 @@ function getRandomTemporaryPath() { return path.join( os.tmpdir(), uuid() ); } +/** + * Function to get the SHA256 hash of a file. + * + * @param {string} filePath Path to the file to be hashed. + * @return {string} SHA of the given file. + */ +function getFileHash( filePath ) { + const fileContent = fs.readFileSync( filePath ); + const hash = crypto.createHash( 'sha256' ); + hash.update( fileContent ); + return hash.digest( 'hex' ); +} + module.exports = { askForConfirmation, runStep, @@ -141,4 +155,5 @@ module.exports = { writeJSONFile, runShellScript, getRandomTemporaryPath, + getFileHash, };