UNPKG

@salesforce/core

Version:

Core libraries to interact with SFDX projects, orgs, and APIs.

80 lines 3.64 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOldLogFiles = exports.cleanup = void 0; /* * Copyright (c) 2023, salesforce.com, inc. * All rights reserved. * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ const fs = __importStar(require("node:fs")); const node_path_1 = require("node:path"); const global_1 = require("../global"); const logger_1 = require("./logger"); /** * the odds of running are 1 in CLEAN_ODDS * ex: CLEAN_ODDS=100 implies 1 in 100 * ex: CLEAN_ODDS=1 implies 1 in 1 (run every time) * */ const CLEAN_ODDS = 100; const MAX_FILE_AGE_DAYS = 7; const MAX_FILE_AGE_MS = 1000 * 60 * 60 * 24 * MAX_FILE_AGE_DAYS; const shouldClean = Math.random() * CLEAN_ODDS > CLEAN_ODDS - 1; /** * New logger (Summer 2023) changes how file rotation works. Each day, the logger writes to a new file * To get old files cleaned up, this can be called when a new root logger is instantiated * based on CLEAN_ODDS, it could exit OR delete some old log files * * to start this without waiting, use void cleanup() * * accepts params to override the default behavior (used to cleanup huge log file during perf tests) */ const cleanup = async (maxMs = MAX_FILE_AGE_MS, force = false) => { if (shouldClean || force) { try { const filesToConsider = await fs.promises // get the files in that dir .readdir(global_1.Global.SF_DIR); const filesToDelete = (0, exports.getOldLogFiles)(filesToConsider, maxMs); await Promise.all(filesToDelete.map((f) => fs.promises.unlink((0, node_path_1.join)(global_1.Global.SF_DIR, f)))); } catch (e) { // we never, ever, ever throw since we're not awaiting this promise, so just log a warning (await logger_1.Logger.child('cleanup')).warn('Failed to cleanup old log files', e); } } }; exports.cleanup = cleanup; const getOldLogFiles = (files, maxMs = MAX_FILE_AGE_MS) => files .filter((f) => f.endsWith('.log')) // map of filename and the date sf-YYYY-MM-DD.log => YYYY-MM-DD .map((f) => ({ file: f, date: f.match(/sf-(\d{4}-\d{2}-\d{2}).*\.log/)?.[1] })) .filter(hasDate) .map((f) => ({ file: f.file, date: new Date(f.date) })) .filter((f) => f.date < new Date(Date.now() - maxMs)) .map((f) => f.file); exports.getOldLogFiles = getOldLogFiles; const hasDate = (f) => typeof f === 'object' && f !== null && 'date' in f && typeof f.date === 'string'; //# sourceMappingURL=cleanup.js.map