Stable babel transformation cache key

Summary:
This diff makes babel transformation cache key stable. With the previous approach it was changed on each CI build so it was not possible to persist cache between builds. Now it is computed from the transformation package content after each build. Because of that it is equal for every CI build while Babel transformations unchanged and so we could use same cache on different Sandcastle agents.

In addition to that, it makes it possible to specify directory for Metro cache so we can save/restore it in CI builds.

Reviewed By: mweststrate

Differential Revision: D26877989

fbshipit-source-id: 7cb04a177f86e61986585e5a74d9c7396ddddc18
This commit is contained in:
Anton Nikolaev
2021-03-10 08:06:19 -08:00
committed by Facebook GitHub Bot
parent baeb8ba5be
commit c065760d15
10 changed files with 142 additions and 34 deletions

View File

@@ -18,28 +18,22 @@ import {default as flipperEnv} from './flipper-env';
import fs from 'fs-extra';
import path from 'path';
let baseHash = '';
const tsbuildinfoPath = path.resolve(__dirname, '..', 'tsconfig.tsbuildinfo');
const packageJsonPath = path.resolve(__dirname, '..', 'package.json');
if (fs.pathExistsSync(tsbuildinfoPath)) {
/**
* tsconfig.tsbuildinfo is changed each time TS incremental build detects changes and rebuilds the package,
* so we can use its modification date as cache key to invalidate the cache each time when babel transformations changed.
*/
baseHash = fs.lstatSync(tsbuildinfoPath).ctime.toUTCString();
} else if (fs.pathExistsSync(packageJsonPath)) {
/**
* tsconfig.tsbuildinfo will not exist in case if the package is installed from npm rather than built locally.
* In such case we should use version of npm package as hash key to invalidate the cache after updates.
*/
baseHash = fs.readJsonSync(packageJsonPath).version;
let selfChecksum: string | undefined;
function getSelfChecksum() {
if (!selfChecksum) {
selfChecksum = fs
.readFileSync(path.resolve(__dirname, '..', 'lib', 'checksum.txt'))
.toString();
}
return selfChecksum;
}
export default function getCacheKey() {
return [
baseHash,
const key = [
getSelfChecksum(),
...Object.entries(flipperEnv)
.sort(([name1, _value1], [name2, _value2]) => name1.localeCompare(name2))
.map(([name, value]) => `${name}=${value}`),
].join('|');
return key;
}