@adinsure-ops/ops-cli
Version:
Operations CLI for working with AdInsure
38 lines (37 loc) • 1.46 kB
JavaScript
import { __awaiter } from "tslib";
import fs from 'fs-extra';
import path from 'path';
export default class LocalCachePlugin {
constructor(path) {
this._accessTokenPath = path;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
beforeCacheAccess(cacheContext) {
return __awaiter(this, void 0, void 0, function* () {
// Create folder if doesn't exists
if (!fs.pathExistsSync(path.dirname(this._accessTokenPath))) {
fs.mkdirSync(path.dirname(this._accessTokenPath), {
recursive: true,
});
}
// Load file only if exists
if (fs.existsSync(this._accessTokenPath)) {
cacheContext.tokenCache.deserialize(yield fs.readFile(this._accessTokenPath, 'utf8'));
}
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
afterCacheAccess(cacheContext) {
return __awaiter(this, void 0, void 0, function* () {
// Create folder if doesn't exists
if (!fs.pathExistsSync(path.dirname(this._accessTokenPath))) {
fs.mkdirSync(path.dirname(this._accessTokenPath), {
recursive: true,
});
}
if (cacheContext.cacheHasChanged) {
yield fs.writeFile(this._accessTokenPath, cacheContext.tokenCache.serialize());
}
});
}
}