UNPKG

@stryker-mutator/core

Version:

The extendable JavaScript mutation testing framework

50 lines 1.9 kB
import path from 'path'; import fs from 'fs'; import { commonTokens, tokens } from '@stryker-mutator/api/plugin'; import { fileUtils } from './file-utils.js'; import { objectUtils } from './object-utils.js'; export class TemporaryDirectory { constructor(log, options) { this.log = log; this.isInitialized = false; this.temporaryDirectory = path.resolve(options.tempDirName); this.removeDuringDisposal = Boolean(options.cleanTempDir); } async initialize() { this.log.debug('Using temp directory "%s"', this.temporaryDirectory); await fs.promises.mkdir(this.temporaryDirectory, { recursive: true }); this.isInitialized = true; } getRandomDirectory(prefix) { return path.resolve(this.temporaryDirectory, `${prefix}${objectUtils.random()}`); } /** * Creates a new random directory with the specified prefix. * @returns The path to the directory. */ async createDirectory(name) { if (!this.isInitialized) { throw new Error('initialize() was not called!'); } await fs.promises.mkdir(path.resolve(this.temporaryDirectory, name), { recursive: true }); } /** * Deletes the Stryker-temp directory */ async dispose() { if (!this.isInitialized) { throw new Error('initialize() was not called!'); } if (this.removeDuringDisposal) { this.log.debug('Deleting stryker temp directory %s', this.temporaryDirectory); try { await fileUtils.deleteDir(this.temporaryDirectory); } catch (e) { this.log.info(`Failed to delete stryker temp directory ${this.temporaryDirectory}`); } } } } TemporaryDirectory.inject = tokens(commonTokens.logger, commonTokens.options); //# sourceMappingURL=temporary-directory.js.map