Skip to content

Commit

Permalink
Merge pull request #965 from bertdeblock/use-system-temp-for-backing-…
Browse files Browse the repository at this point in the history
…up-package-manager-files

Use the system temp's folder for backing up package-manager files
  • Loading branch information
Bert De Block committed Sep 7, 2023
2 parents f68085c + 01c179e commit 2438dca
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 217 deletions.
4 changes: 0 additions & 4 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,3 @@
/coverage/
!.*
.eslintrc.js

# ember-try
/bower.json.ember-try
/package.json.ember-try
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
/testem.log
/yarn-error.log

# ember-try
/bower.json.ember-try
/package.json.ember-try

/.nyc_output
*.tgz
.scratch
4 changes: 0 additions & 4 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
/yarn.lock
.gitkeep

# ember-try
/bower.json.ember-try
/package.json.ember-try

# custom
/.codeclimate.yml
/test
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ In order to use an alternate config path or to group various scenarios, you can

#### `ember try:reset`

This command restores the original `package.json` from `package.json.ember-try`, `rm -rf`s `node_modules` and runs `npm install`. For use if any of the other commands fail to clean up after (they run this by default on completion).
This command restores all original files, and installs the original node modules again. For use if any of the other commands fail to clean up after (they run this by default on completion) or after running with ‘—skip-cleanup’.

#### `ember try:ember <semver-string>`

Expand Down
72 changes: 9 additions & 63 deletions lib/dependency-manager-adapters/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,23 @@

const CoreObject = require('core-object');
const fs = require('fs-extra');
const util = require('util');
const copy = util.promisify(fs.copy);
const path = require('path');
const debug = require('debug')('ember-try:dependency-manager-adapter:npm');
const rimraf = util.promisify(require('rimraf'));
const chalk = require('chalk');
const semver = require('semver');
const Backup = require('../utils/backup');

module.exports = CoreObject.extend({
init() {
this._super.apply(this, arguments);
this.backup = new Backup({ cwd: this.cwd });
this.run = this.run || require('../utils/run');
},
useYarnCommand: false,
yarnLock: 'yarn.lock',
yarnLockBackupFileName: 'yarn.lock.ember-try',
configKey: 'npm',
packageJSON: 'package.json',
packageJSONBackupFileName: 'package.json.ember-try',
packageLock: 'package-lock.json',
packageLockBackupFileName: 'package-lock.json.ember-try',

async setup(options) {
if (!options) {
Expand Down Expand Up @@ -57,20 +53,6 @@ module.exports = CoreObject.extend({
async cleanup() {
try {
await this._restoreOriginalDependencies();

debug('Remove backup package.json and node_modules');

let cleanupTasks = [rimraf(path.join(this.cwd, this.packageJSONBackupFileName))];

if (fs.existsSync(path.join(this.cwd, this.yarnLockBackupFileName))) {
cleanupTasks.push(rimraf(path.join(this.cwd, this.yarnLockBackupFileName)));
}

if (fs.existsSync(path.join(this.cwd, this.packageLockBackupFileName))) {
cleanupTasks.push(rimraf(path.join(this.cwd, this.packageLockBackupFileName)));
}

return await Promise.all(cleanupTasks);
} catch (e) {
console.log('Error cleaning up npm scenario:', e); // eslint-disable-line no-console
}
Expand Down Expand Up @@ -139,7 +121,7 @@ module.exports = CoreObject.extend({
return;
}

let backupPackageJSON = path.join(this.cwd, this.packageJSONBackupFileName);
let backupPackageJSON = this.backup.pathForFile(this.packageJSON);
let packageJSONFile = path.join(this.cwd, this.packageJSON);
let packageJSON = JSON.parse(fs.readFileSync(backupPackageJSON));
let newPackageJSON = this._packageJSONForDependencySet(packageJSON, depSet);
Expand Down Expand Up @@ -197,49 +179,13 @@ module.exports = CoreObject.extend({
});
},

_restoreOriginalDependencies() {
debug('Restoring original package.json and node_modules');

let restoreTasks = [
copy(
path.join(this.cwd, this.packageJSONBackupFileName),
path.join(this.cwd, this.packageJSON)
),
];

let yarnLockBackupFileName = path.join(this.cwd, this.yarnLockBackupFileName);
if (fs.existsSync(yarnLockBackupFileName)) {
restoreTasks.push(copy(yarnLockBackupFileName, path.join(this.cwd, this.yarnLock)));
}

let packageLockBackupFileName = path.join(this.cwd, this.packageLockBackupFileName);
if (fs.existsSync(packageLockBackupFileName)) {
restoreTasks.push(copy(packageLockBackupFileName, path.join(this.cwd, this.packageLock)));
}

return Promise.all(restoreTasks).then(() => this._install());
async _restoreOriginalDependencies() {
await this.backup.restoreFiles([this.packageJSON, this.packageLock, this.yarnLock]);
await this.backup.cleanUp();
await this._install();
},

_backupOriginalDependencies() {
debug('Backing up package.json and node_modules');

let backupTasks = [
copy(
path.join(this.cwd, this.packageJSON),
path.join(this.cwd, this.packageJSONBackupFileName)
),
];

let yarnLockPath = path.join(this.cwd, this.yarnLock);
if (fs.existsSync(yarnLockPath)) {
backupTasks.push(copy(yarnLockPath, path.join(this.cwd, this.yarnLockBackupFileName)));
}

let packageLockPath = path.join(this.cwd, this.packageLock);
if (fs.existsSync(packageLockPath)) {
backupTasks.push(copy(packageLockPath, path.join(this.cwd, this.packageLockBackupFileName)));
}

return Promise.all(backupTasks);
async _backupOriginalDependencies() {
await this.backup.addFiles([this.packageJSON, this.packageLock, this.yarnLock]);
},
});
43 changes: 7 additions & 36 deletions lib/dependency-manager-adapters/pnpm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,24 @@ const CoreObject = require('core-object');
const fs = require('fs-extra');
const path = require('path');
const debug = require('debug')('ember-try:dependency-manager-adapter:pnpm');
const Backup = require('../utils/backup');

const PACKAGE_JSON = 'package.json';
const PACKAGE_JSON_BACKUP = 'package.json.ember-try';
const PNPM_LOCKFILE = 'pnpm-lock.yaml';

// Note: the upstream convention is to append `.ember-try` _after_ the file
// extension, however this breaks syntax highlighting, so I've chosen to
// insert it right before the file extension.
const PNPM_LOCKFILE_BACKUP = 'pnpm-lock.ember-try.yaml';

module.exports = CoreObject.extend({
// This still needs to be `npm` because we're still reading the dependencies
// from the `npm` key of the ember-try config.
configKey: 'npm',

init() {
this._super.apply(this, arguments);
this.backup = new Backup({ cwd: this.cwd });
this.run = this.run || require('../utils/run');
},

async setup() {
let pkg = path.join(this.cwd, PACKAGE_JSON);
let pkgBackup = path.join(this.cwd, PACKAGE_JSON_BACKUP);
debug(`Copying ${PACKAGE_JSON}`);
await fs.copy(pkg, pkgBackup);

let lockFile = path.join(this.cwd, PNPM_LOCKFILE);
let lockFileBackup = path.join(this.cwd, PNPM_LOCKFILE_BACKUP);
if (fs.existsSync(lockFile)) {
debug(`Copying ${PNPM_LOCKFILE}`);
await fs.copy(lockFile, lockFileBackup);
}
await this.backup.addFiles([PACKAGE_JSON, PNPM_LOCKFILE]);
},

async changeToDependencySet(depSet) {
Expand All @@ -60,18 +46,8 @@ module.exports = CoreObject.extend({

async cleanup() {
try {
debug(`Restoring original ${PACKAGE_JSON}`);
let pkg = path.join(this.cwd, PACKAGE_JSON);
let pkgBackup = path.join(this.cwd, PACKAGE_JSON_BACKUP);
await fs.copy(pkgBackup, pkg);
await fs.remove(pkgBackup);

debug(`Restoring original ${PNPM_LOCKFILE}`);
let lockFile = path.join(this.cwd, PNPM_LOCKFILE);
let lockFileBackup = path.join(this.cwd, PNPM_LOCKFILE_BACKUP);
await fs.copy(lockFileBackup, lockFile);
await fs.remove(lockFileBackup);

await this.backup.restoreFiles([PACKAGE_JSON, PNPM_LOCKFILE]);
await this.backup.cleanUp();
await this._install();
} catch (e) {
console.log('Error cleaning up scenario:', e); // eslint-disable-line no-console
Expand Down Expand Up @@ -118,7 +94,7 @@ module.exports = CoreObject.extend({
return;
}

let backupPackageJSON = path.join(this.cwd, PACKAGE_JSON_BACKUP);
let backupPackageJSON = this.backup.pathForFile(PACKAGE_JSON);
let packageJSONFile = path.join(this.cwd, PACKAGE_JSON);
let packageJSON = JSON.parse(fs.readFileSync(backupPackageJSON));
let newPackageJSON = this._packageJSONForDependencySet(packageJSON, depSet);
Expand All @@ -129,12 +105,7 @@ module.exports = CoreObject.extend({
// We restore the original lockfile here, so that we always create a minimal
// diff compared to the original locked dependency set.

let lockFile = path.join(this.cwd, PNPM_LOCKFILE);
let lockFileBackup = path.join(this.cwd, PNPM_LOCKFILE_BACKUP);
if (fs.existsSync(lockFileBackup)) {
debug(`Restoring original ${PNPM_LOCKFILE}`);
await fs.copy(lockFileBackup, lockFile);
}
await this.backup.restoreFile(PNPM_LOCKFILE);
},

_packageJSONForDependencySet(packageJSON, depSet) {
Expand Down
95 changes: 95 additions & 0 deletions lib/utils/backup.js
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)));
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"resolve": "^1.20.0",
"rimraf": "^3.0.2",
"semver": "^7.5.4",
"temp-dir": "^2.0.0",
"walk-sync": "^2.2.0"
},
"devDependencies": {
Expand Down
6 changes: 0 additions & 6 deletions smoke-test-app/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,3 @@
!.*
.*/
.eslintcache

# ember-try
/bower.json.ember-try
/package.json.ember-try
/package-lock.json.ember-try
/yarn.lock.ember-try
6 changes: 0 additions & 6 deletions smoke-test-app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,5 @@
/testem.log
/yarn-error.log

# ember-try
/bower.json.ember-try
/package.json.ember-try
/package-lock.json.ember-try
/yarn.lock.ember-try

# broccoli-debug
/DEBUG/
6 changes: 0 additions & 6 deletions smoke-test-app/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,3 @@
/yarn-error.log
/yarn.lock
.gitkeep

# ember-try
/bower.json.ember-try
/package.json.ember-try
/package-lock.json.ember-try
/yarn.lock.ember-try
6 changes: 0 additions & 6 deletions smoke-test-app/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,3 @@
!.*
.eslintcache
.lint-todo/

# ember-try
/bower.json.ember-try
/package.json.ember-try
/package-lock.json.ember-try
/yarn.lock.ember-try

0 comments on commit 2438dca

Please sign in to comment.