@factorial-finance/blueprint-node
Version:
blueprint-node-plugin
134 lines (133 loc) • 5.12 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.AliasCache = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const logger_1 = require("./logger");
class AliasCache {
// Get cache directory path
static getCacheDir() {
// Priority:
// 1. Project local .cache directory
// 2. node_modules/.cache (when installed as npm package)
// 3. OS cache directory
const cwd = process.cwd();
const localCacheDir = path.join(cwd, '.cache', 'blueprint-node');
// Check project local cache directory
if (fs.existsSync(path.join(cwd, 'package.json'))) {
return localCacheDir;
}
// Cache inside node_modules
const nodeModulesCache = path.join(cwd, 'node_modules', '.cache', 'blueprint-node');
if (fs.existsSync(path.join(cwd, 'node_modules'))) {
return nodeModulesCache;
}
// OS cache directory
const homeDir = os.homedir();
return path.join(homeDir, '.cache', 'blueprint-node');
}
static getCachePath() {
const cacheDir = this.getCacheDir();
return path.join(cacheDir, this.CACHE_FILENAME);
}
// Load cache
static async loadCache() {
try {
const cachePath = this.getCachePath();
if (!fs.existsSync(cachePath)) {
logger_1.Logger.debug('Alias cache not found');
return null;
}
const cacheContent = fs.readFileSync(cachePath, 'utf-8');
const cached = JSON.parse(cacheContent);
// Check version
if (cached.version !== this.CACHE_VERSION) {
logger_1.Logger.info('Cache version mismatch, invalidating cache');
return null;
}
// Cache validity check (24 hours)
const cacheAge = Date.now() - cached.timestamp;
const maxAge = 24 * 60 * 60 * 1000; // 24 hours
if (cacheAge > maxAge) {
logger_1.Logger.info('Cache expired, will refresh in background');
}
logger_1.Logger.info(`Loaded alias cache (age: ${Math.round(cacheAge / 1000 / 60)} minutes)`);
return cached;
}
catch (error) {
logger_1.Logger.warn('Failed to load alias cache:', error);
return null;
}
}
// Save cache
static async saveCache(aliases) {
try {
const cacheDir = this.getCacheDir();
const cachePath = this.getCachePath();
// Create cache directory
if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir, { recursive: true });
}
const cacheData = {
version: this.CACHE_VERSION,
timestamp: Date.now(),
aliases
};
fs.writeFileSync(cachePath, JSON.stringify(cacheData, null, 2));
logger_1.Logger.info(`Saved alias cache to ${cachePath}`);
}
catch (error) {
logger_1.Logger.warn('Failed to save alias cache:', error);
}
}
// Invalidate cache
static async invalidateCache() {
try {
const cachePath = this.getCachePath();
if (fs.existsSync(cachePath)) {
fs.unlinkSync(cachePath);
logger_1.Logger.info('Alias cache invalidated');
}
}
catch (error) {
logger_1.Logger.warn('Failed to invalidate cache:', error);
}
}
}
exports.AliasCache = AliasCache;
AliasCache.CACHE_VERSION = '1.0.0';
AliasCache.CACHE_FILENAME = 'blueprint-aliases-cache.json';