@openzeppelin/hardhat-upgrades
Version:
[](https://docs.openzeppelin.com/upgrades-plugins/hardhat-upgrades) [](https://www.npmjs.org/package/@openzeppelin/h
71 lines • 2.44 kB
JavaScript
import { promises as fs } from 'fs';
import path from 'path';
import lockfile from 'proper-lockfile';
import { concatRunData, isCurrentValidationData, } from '@openzeppelin/upgrades-core';
async function lock(file) {
await fs.mkdir(path.dirname(file), { recursive: true });
return lockfile.lock(file, { retries: { minTimeout: 50, factor: 1.3 }, realpath: false });
}
export async function writeValidations(hre, newRunData) {
const cachePath = getValidationsCachePath(hre);
let releaseLock;
try {
releaseLock = await lock(cachePath);
const storedData = await readValidations(hre, false).catch(e => {
// No prior data to append to: either the cache file is absent, or it was
// outdated and readValidations has already removed it. In both cases,
// write from scratch.
if (e instanceof ValidationsCacheNotFound || e instanceof ValidationsCacheOutdated) {
return undefined;
}
else {
throw e;
}
});
const validations = concatRunData(newRunData, storedData);
await fs.writeFile(cachePath, JSON.stringify(validations, null, 2));
}
finally {
await releaseLock?.();
}
}
export async function readValidations(hre, acquireLock = true) {
const cachePath = getValidationsCachePath(hre);
let releaseLock;
try {
if (acquireLock) {
releaseLock = await lock(cachePath);
}
const data = JSON.parse(await fs.readFile(cachePath, 'utf8'));
if (!isCurrentValidationData(data)) {
await fs.unlink(cachePath);
throw new ValidationsCacheOutdated();
}
return data;
}
catch (e) {
if (e.code === 'ENOENT') {
throw new ValidationsCacheNotFound();
}
else {
throw e;
}
}
finally {
await releaseLock?.();
}
}
export class ValidationsCacheNotFound extends Error {
constructor() {
super('Validations cache not found. Recompile with `hardhat compile --force`');
}
}
export class ValidationsCacheOutdated extends Error {
constructor() {
super('Validations cache is outdated. Recompile with `hardhat compile --force`');
}
}
function getValidationsCachePath(hre) {
return path.join(hre.config.paths.cache, 'validations.json');
}
//# sourceMappingURL=validations.js.map