@nomiclabs/buidler
Version:
Buidler is an extensible developer tool that helps smart contract developers increase productivity by reliably bringing together the tools they want.
92 lines • 4.13 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = __importDefault(require("fs-extra"));
const isEqual_1 = __importDefault(require("lodash/isEqual"));
const path_1 = __importDefault(require("path"));
const constants_1 = require("../../internal/constants");
const glob_1 = require("../../internal/util/glob");
const packageInfo_1 = require("../../internal/util/packageInfo");
// Checks the earliest date of modification for compiled files against the latest date for source files (including libraries).
// Furthermore, cache is invalidated if Buidler's version changes, or a different solc version is set in the buidler config.
async function areArtifactsCached(sourceTimestamps, newSolcConfig, paths) {
const oldConfig = await getLastUsedConfig(paths.cache);
if (oldConfig === undefined ||
!compareSolcConfigs(oldConfig.solc, newSolcConfig) ||
!(await compareBuidlerVersion(oldConfig.buidlerVersion))) {
return false;
}
const maxSourceDate = getMaxSourceDate(sourceTimestamps);
const minArtifactDate = await getMinArtifactDate(paths.artifacts);
if (!(await fs_extra_1.default.pathExists(path_1.default.join(paths.cache, constants_1.SOLC_INPUT_FILENAME)))) {
return false;
}
if (!(await fs_extra_1.default.pathExists(path_1.default.join(paths.cache, constants_1.SOLC_OUTPUT_FILENAME)))) {
return false;
}
const lastConfigTimestamp = await getLastUsedConfigTimestamp(paths.cache);
if (lastConfigTimestamp !== undefined &&
lastConfigTimestamp > maxSourceDate) {
return true;
}
return maxSourceDate < minArtifactDate;
}
exports.areArtifactsCached = areArtifactsCached;
async function getModificationDatesInDir(dir) {
const pattern = path_1.default.join(dir, "**", "*");
const files = await glob_1.glob(pattern);
return Promise.all(files.map(async (file) => (await fs_extra_1.default.stat(file)).ctimeMs));
}
function getMaxSourceDate(sourceTimestamps) {
return Math.max(...sourceTimestamps);
}
async function getMinArtifactDate(artifactsPath) {
const timestamps = await getModificationDatesInDir(artifactsPath);
if (timestamps.length === 0) {
return 0;
}
return Math.min(...timestamps);
}
const LAST_CONFIG_USED_FILENAME = "last-solc-config.json";
function getPathToCachedLastConfigPath(cachePath) {
const pathToLastConfigUsed = path_1.default.join(cachePath, LAST_CONFIG_USED_FILENAME);
return pathToLastConfigUsed;
}
async function getLastUsedConfig(cachePath) {
const pathToConfig = getPathToCachedLastConfigPath(cachePath);
if (!(await fs_extra_1.default.pathExists(pathToConfig))) {
return undefined;
}
return module.require(pathToConfig);
}
async function getLastUsedConfigTimestamp(cachePath) {
const pathToConfig = getPathToCachedLastConfigPath(cachePath);
if (!(await fs_extra_1.default.pathExists(pathToConfig))) {
return undefined;
}
return (await fs_extra_1.default.stat(pathToConfig)).ctimeMs;
}
async function cacheBuidlerConfig(paths, config) {
const pathToLastConfigUsed = getPathToCachedLastConfigPath(paths.cache);
const newJson = {
solc: config,
buidlerVersion: await getCurrentBuidlerVersion(),
};
await fs_extra_1.default.ensureDir(path_1.default.dirname(pathToLastConfigUsed));
return fs_extra_1.default.writeFile(pathToLastConfigUsed, JSON.stringify(newJson, undefined, 2), "utf-8");
}
exports.cacheBuidlerConfig = cacheBuidlerConfig;
function compareSolcConfigs(oldConfig, newConfig) {
return isEqual_1.default(oldConfig, newConfig);
}
async function getCurrentBuidlerVersion() {
const packageJson = await packageInfo_1.getPackageJson();
return packageJson.version;
}
async function compareBuidlerVersion(lastBuidlerVersion) {
const currentVersion = await getCurrentBuidlerVersion();
return lastBuidlerVersion === currentVersion;
}
//# sourceMappingURL=cache.js.map
;