UNPKG

907 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const fs = require("fs-extra");
4async function updateCache(cachePath, cache) {
5 await fs.ensureFile(cachePath);
6 await fs.writeJSON(cachePath, cache);
7}
8exports.updateCache = updateCache;
9function _isStale(cachePath, cacheDuration) {
10 const past = new Date();
11 past.setSeconds(past.getSeconds() - cacheDuration);
12 return past.getTime() > _mtime(cachePath).getTime();
13}
14function _mtime(f) {
15 return fs.statSync(f).mtime;
16}
17async function fetchCache(cachePath, cacheDuration, options) {
18 let cachePresent = fs.existsSync(cachePath);
19 if (cachePresent && !_isStale(cachePath, cacheDuration)) {
20 return fs.readJSON(cachePath);
21 }
22 const cache = await options.cacheFn();
23 // TODO: move this to a fork
24 await updateCache(cachePath, cache);
25 return cache;
26}
27exports.fetchCache = fetchCache;