UNPKG

monorepo-cache-manager

Version:

Lightweight cache manager for monorepos with cross-workspace cache sharing and optimization

133 lines 4.73 kB
"use strict"; 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.CacheUtils = void 0; const fs = __importStar(require("fs-extra")); const path = __importStar(require("path")); const crypto = __importStar(require("crypto")); const glob_1 = require("glob"); class CacheUtils { static async generateHash(filePath) { const stats = await fs.stat(filePath); if (stats.isFile()) { const content = await fs.readFile(filePath); return crypto.createHash('sha256').update(content).digest('hex'); } if (stats.isDirectory()) { const files = await this.getAllFiles(filePath); const hashes = await Promise.all(files.map(file => this.generateHash(file))); return crypto.createHash('sha256').update(hashes.join('')).digest('hex'); } return ''; } static async getAllFiles(dirPath) { const files = []; try { const items = await fs.readdir(dirPath); for (const item of items) { const fullPath = path.join(dirPath, item); const stats = await fs.stat(fullPath); if (stats.isDirectory()) { const subFiles = await this.getAllFiles(fullPath); files.push(...subFiles); } else { files.push(fullPath); } } } catch (error) { } return files.sort(); } static async getDirectorySize(dirPath) { const files = await this.getAllFiles(dirPath); let totalSize = 0; for (const file of files) { try { const stats = await fs.stat(file); totalSize += stats.size; } catch (error) { } } return totalSize; } static async findWorkspaces(rootDir, patterns) { const workspaces = []; for (const pattern of patterns) { try { const matches = await (0, glob_1.glob)(pattern, { cwd: rootDir, absolute: true, ignore: ['**/node_modules/**', '**/dist/**', '**/build/**'] }); workspaces.push(...matches); } catch (error) { } } return [...new Set(workspaces)]; } static createCacheKey(workspace, dependencies = []) { const key = `${workspace}:${dependencies.sort().join(',')}`; return crypto.createHash('sha256').update(key).digest('hex'); } static async ensureDir(dirPath) { await fs.ensureDir(dirPath); } static async cleanOldFiles(dirPath, ttl) { const now = Date.now(); const files = await this.getAllFiles(dirPath); for (const file of files) { try { const stats = await fs.stat(file); if (now - stats.mtime.getTime() > ttl) { await fs.remove(file); } } catch (error) { } } } static compress(data) { return Buffer.from(data).toString('base64'); } static decompress(data) { return Buffer.from(data, 'base64').toString(); } } exports.CacheUtils = CacheUtils; //# sourceMappingURL=utils.js.map