1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const fs_extra_1 = __importDefault(require("fs-extra"));
|
7 | const isEqual_1 = __importDefault(require("lodash/isEqual"));
|
8 | const path_1 = __importDefault(require("path"));
|
9 | const constants_1 = require("../../internal/constants");
|
10 | const glob_1 = require("../../internal/util/glob");
|
11 | const packageInfo_1 = require("../../internal/util/packageInfo");
|
12 |
|
13 |
|
14 | async 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 | }
|
36 | exports.areArtifactsCached = areArtifactsCached;
|
37 | async 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 | }
|
42 | function getMaxSourceDate(sourceTimestamps) {
|
43 | return Math.max(...sourceTimestamps);
|
44 | }
|
45 | async function getMinArtifactDate(artifactsPath) {
|
46 | const timestamps = await getModificationDatesInDir(artifactsPath);
|
47 | if (timestamps.length === 0) {
|
48 | return 0;
|
49 | }
|
50 | return Math.min(...timestamps);
|
51 | }
|
52 | const LAST_CONFIG_USED_FILENAME = "last-solc-config.json";
|
53 | function getPathToCachedLastConfigPath(cachePath) {
|
54 | const pathToLastConfigUsed = path_1.default.join(cachePath, LAST_CONFIG_USED_FILENAME);
|
55 | return pathToLastConfigUsed;
|
56 | }
|
57 | async 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 | }
|
64 | async 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 | }
|
71 | async 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 | }
|
80 | exports.cacheBuidlerConfig = cacheBuidlerConfig;
|
81 | function compareSolcConfigs(oldConfig, newConfig) {
|
82 | return isEqual_1.default(oldConfig, newConfig);
|
83 | }
|
84 | async function getCurrentBuidlerVersion() {
|
85 | const packageJson = await packageInfo_1.getPackageJson();
|
86 | return packageJson.version;
|
87 | }
|
88 | async function compareBuidlerVersion(lastBuidlerVersion) {
|
89 | const currentVersion = await getCurrentBuidlerVersion();
|
90 | return lastBuidlerVersion === currentVersion;
|
91 | }
|
92 |
|
\ | No newline at end of file |