UNPKG

playwright-cleanup

Version:
52 lines (51 loc) 1.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Cleanup = void 0; const logger_1 = require("./logger"); class Cleanup { constructor(options) { this._supressLogging = options.suppressLogging; this._cleanupList = new Array(); ; } /** * * @param cleanupFunction Insert a cleanup function to the cleanup stack. */ addCleanup(cleanupFunction) { this._cleanupList.push(cleanupFunction); } /** * @deprecated Don't use this method directly. */ async finalize() { if (this._cleanupList.length <= 0) { return; } const errors = []; const logger = new logger_1.Logger(this._supressLogging); const processId = process.pid; logger.info(`Playwright-cleanup [${processId}]: ### Cleanup initialized ###`, false); this._cleanupList.reverse(); for (let i = 0; i < this._cleanupList.length; i++) { try { await this._cleanupList[i](); const message = `Playwright-cleanup [🙂 ${processId}]: Successfully executed '${this._cleanupList[i].toString()}'`; logger.info(message, false); } catch (err) { const message = `Playwright-cleanup [😕 ${processId}]: Failed to execute '${this._cleanupList[i].toString()}': ${err.message}, ${err.stack}`; errors.push(message); } } if (errors.length > 0) { logger.error(`Cleanup for [${processId}] finished with ${errors.length} error(s):`); errors.forEach(error => { logger.error(error); }); } this._cleanupList.length = 0; logger.info(`Playwright-cleanup [${processId}]: ### Cleanup done ###`, false); } } exports.Cleanup = Cleanup;