Skip to content

Commit

Permalink
Use existing modules if package locks match
Browse files Browse the repository at this point in the history
  • Loading branch information
WunderBart committed Apr 13, 2023
1 parent 53cf2a1 commit 62f5d99
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
32 changes: 30 additions & 2 deletions bin/plugin/commands/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
runShellScript,
readJSONFile,
writeJSONFile,
getFileHash,
} = require( '../lib/utils' );
const config = require( '../config' );

Expand Down Expand Up @@ -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;
}
}
Expand Down
15 changes: 15 additions & 0 deletions bin/plugin/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -134,11 +135,25 @@ 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,
readJSONFile,
writeJSONFile,
runShellScript,
getRandomTemporaryPath,
getFileHash,
};

0 comments on commit 62f5d99

Please sign in to comment.