UNPKG

@salesforce/core

Version:

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

57 lines 2.62 kB
"use strict"; 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 node_path_1 = require("node:path"); const fs_1 = require("../fs/fs"); 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_1.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_1.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