UNPKG

ember-try

Version:

An ember-cli addon to test against multiple dependencies, such as ember and ember-data.

96 lines (79 loc) 2.5 kB
'use strict'; const debug = require('debug')('ember-try:backup'); const { copy, existsSync } = require('fs-extra'); const { createHash } = require('node:crypto'); const { join } = require('node:path'); const { promisify } = require('node:util'); const remove = promisify(require('rimraf')); const tempDir = require('temp-dir'); module.exports = class Backup { constructor({ cwd }) { this.cwd = cwd; this.dir = join(tempDir, 'ember-try', createHash('sha256').update(cwd).digest('hex')); debug(`Created backup directory ${this.dir}`); } /** * Adds a file to the backup directory. * * @param {String} filename Filename relative to the current working directory. * @returns {Promise<void>} */ addFile(filename) { let originalFile = join(this.cwd, filename); if (existsSync(originalFile)) { debug(`Adding ${originalFile} to backup directory`); return copy(originalFile, this.pathForFile(filename)); } return Promise.resolve(); } /** * Adds multiple files to the backup directory. * * @param {Array<String>} filenames Filenames relative to the current working directory. * @returns {Promise<void>} */ addFiles(filenames) { return Promise.all(filenames.map((filename) => this.addFile(filename))); } /** * Cleans up the backup directory. * * @returns {Promise<void>} */ cleanUp() { debug(`Cleaning up backup directory ${this.dir}`); return remove(this.dir); } /** * Returns the absolute path for a file in the backup directory. * * @param {String} filename Filename relative to the current working directory. * @returns {String} */ pathForFile(filename) { return join(this.dir, filename); } /** * Restores a file from the backup directory. * * @param {String} filename Filename relative to the current working directory. * @returns {Promise<void>} */ restoreFile(filename) { let backupFile = this.pathForFile(filename); if (existsSync(backupFile)) { debug(`Restoring ${backupFile} from backup directory`); return copy(backupFile, join(this.cwd, filename)); } return Promise.resolve(); } /** * Restores multiple files from the backup directory. * * @param {Array<String>} filenames Filenames relative to the current working directory. * @returns {Promise<void>} */ restoreFiles(filenames) { return Promise.all(filenames.map((filename) => this.restoreFile(filename))); } };