forked from ember-cli/ember-try
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the system temp's folder for backing up package-manager files
- Loading branch information
1 parent
f68085c
commit 9758624
Showing
17 changed files
with
198 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,3 @@ | |
/coverage/ | ||
!.* | ||
.eslintrc.js | ||
|
||
# ember-try | ||
/bower.json.ember-try | ||
/package.json.ember-try |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'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))); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.