UNPKG

4.13 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const fs_extra_1 = __importDefault(require("fs-extra"));
7const isEqual_1 = __importDefault(require("lodash/isEqual"));
8const path_1 = __importDefault(require("path"));
9const constants_1 = require("../../internal/constants");
10const glob_1 = require("../../internal/util/glob");
11const packageInfo_1 = require("../../internal/util/packageInfo");
12// Checks the earliest date of modification for compiled files against the latest date for source files (including libraries).
13// Furthermore, cache is invalidated if Buidler's version changes, or a different solc version is set in the buidler config.
14async function areArtifactsCached(sourceTimestamps, newSolcConfig, paths) {
15 const oldConfig = await getLastUsedConfig(paths.cache);
16 if (oldConfig === undefined ||
17 !compareSolcConfigs(oldConfig.solc, newSolcConfig) ||
18 !(await compareBuidlerVersion(oldConfig.buidlerVersion))) {
19 return false;
20 }
21 const maxSourceDate = getMaxSourceDate(sourceTimestamps);
22 const minArtifactDate = await getMinArtifactDate(paths.artifacts);
23 if (!(await fs_extra_1.default.pathExists(path_1.default.join(paths.cache, constants_1.SOLC_INPUT_FILENAME)))) {
24 return false;
25 }
26 if (!(await fs_extra_1.default.pathExists(path_1.default.join(paths.cache, constants_1.SOLC_OUTPUT_FILENAME)))) {
27 return false;
28 }
29 const lastConfigTimestamp = await getLastUsedConfigTimestamp(paths.cache);
30 if (lastConfigTimestamp !== undefined &&
31 lastConfigTimestamp > maxSourceDate) {
32 return true;
33 }
34 return maxSourceDate < minArtifactDate;
35}
36exports.areArtifactsCached = areArtifactsCached;
37async function getModificationDatesInDir(dir) {
38 const pattern = path_1.default.join(dir, "**", "*");
39 const files = await glob_1.glob(pattern);
40 return Promise.all(files.map(async (file) => (await fs_extra_1.default.stat(file)).ctimeMs));
41}
42function getMaxSourceDate(sourceTimestamps) {
43 return Math.max(...sourceTimestamps);
44}
45async function getMinArtifactDate(artifactsPath) {
46 const timestamps = await getModificationDatesInDir(artifactsPath);
47 if (timestamps.length === 0) {
48 return 0;
49 }
50 return Math.min(...timestamps);
51}
52const LAST_CONFIG_USED_FILENAME = "last-solc-config.json";
53function getPathToCachedLastConfigPath(cachePath) {
54 const pathToLastConfigUsed = path_1.default.join(cachePath, LAST_CONFIG_USED_FILENAME);
55 return pathToLastConfigUsed;
56}
57async function getLastUsedConfig(cachePath) {
58 const pathToConfig = getPathToCachedLastConfigPath(cachePath);
59 if (!(await fs_extra_1.default.pathExists(pathToConfig))) {
60 return undefined;
61 }
62 return module.require(pathToConfig);
63}
64async function getLastUsedConfigTimestamp(cachePath) {
65 const pathToConfig = getPathToCachedLastConfigPath(cachePath);
66 if (!(await fs_extra_1.default.pathExists(pathToConfig))) {
67 return undefined;
68 }
69 return (await fs_extra_1.default.stat(pathToConfig)).ctimeMs;
70}
71async function cacheBuidlerConfig(paths, config) {
72 const pathToLastConfigUsed = getPathToCachedLastConfigPath(paths.cache);
73 const newJson = {
74 solc: config,
75 buidlerVersion: await getCurrentBuidlerVersion(),
76 };
77 await fs_extra_1.default.ensureDir(path_1.default.dirname(pathToLastConfigUsed));
78 return fs_extra_1.default.writeFile(pathToLastConfigUsed, JSON.stringify(newJson, undefined, 2), "utf-8");
79}
80exports.cacheBuidlerConfig = cacheBuidlerConfig;
81function compareSolcConfigs(oldConfig, newConfig) {
82 return isEqual_1.default(oldConfig, newConfig);
83}
84async function getCurrentBuidlerVersion() {
85 const packageJson = await packageInfo_1.getPackageJson();
86 return packageJson.version;
87}
88async function compareBuidlerVersion(lastBuidlerVersion) {
89 const currentVersion = await getCurrentBuidlerVersion();
90 return lastBuidlerVersion === currentVersion;
91}
92//# sourceMappingURL=cache.js.map
\No newline at end of file