|
2 | 2 |
|
3 | 3 | const CoreObject = require('core-object'); |
4 | 4 | const fs = require('fs-extra'); |
5 | | -const util = require('util'); |
6 | | -const copy = util.promisify(fs.copy); |
7 | 5 | const path = require('path'); |
8 | 6 | const debug = require('debug')('ember-try:dependency-manager-adapter:npm'); |
9 | | -const rimraf = util.promisify(require('rimraf')); |
10 | 7 | const chalk = require('chalk'); |
11 | 8 | const semver = require('semver'); |
| 9 | +const Backup = require('../utils/backup'); |
12 | 10 |
|
13 | 11 | module.exports = CoreObject.extend({ |
14 | 12 | init() { |
15 | 13 | this._super.apply(this, arguments); |
| 14 | + this.backup = new Backup({ cwd: this.cwd }); |
16 | 15 | this.run = this.run || require('../utils/run'); |
17 | 16 | }, |
18 | 17 | useYarnCommand: false, |
19 | 18 | yarnLock: 'yarn.lock', |
20 | | - yarnLockBackupFileName: 'yarn.lock.ember-try', |
21 | 19 | configKey: 'npm', |
22 | 20 | packageJSON: 'package.json', |
23 | | - packageJSONBackupFileName: 'package.json.ember-try', |
24 | 21 | packageLock: 'package-lock.json', |
25 | | - packageLockBackupFileName: 'package-lock.json.ember-try', |
26 | 22 |
|
27 | 23 | async setup(options) { |
28 | 24 | if (!options) { |
@@ -57,20 +53,6 @@ module.exports = CoreObject.extend({ |
57 | 53 | async cleanup() { |
58 | 54 | try { |
59 | 55 | await this._restoreOriginalDependencies(); |
60 | | - |
61 | | - debug('Remove backup package.json and node_modules'); |
62 | | - |
63 | | - let cleanupTasks = [rimraf(path.join(this.cwd, this.packageJSONBackupFileName))]; |
64 | | - |
65 | | - if (fs.existsSync(path.join(this.cwd, this.yarnLockBackupFileName))) { |
66 | | - cleanupTasks.push(rimraf(path.join(this.cwd, this.yarnLockBackupFileName))); |
67 | | - } |
68 | | - |
69 | | - if (fs.existsSync(path.join(this.cwd, this.packageLockBackupFileName))) { |
70 | | - cleanupTasks.push(rimraf(path.join(this.cwd, this.packageLockBackupFileName))); |
71 | | - } |
72 | | - |
73 | | - return await Promise.all(cleanupTasks); |
74 | 56 | } catch (e) { |
75 | 57 | console.log('Error cleaning up npm scenario:', e); // eslint-disable-line no-console |
76 | 58 | } |
@@ -139,7 +121,7 @@ module.exports = CoreObject.extend({ |
139 | 121 | return; |
140 | 122 | } |
141 | 123 |
|
142 | | - let backupPackageJSON = path.join(this.cwd, this.packageJSONBackupFileName); |
| 124 | + let backupPackageJSON = this.backup.pathForFile(this.packageJSON); |
143 | 125 | let packageJSONFile = path.join(this.cwd, this.packageJSON); |
144 | 126 | let packageJSON = JSON.parse(fs.readFileSync(backupPackageJSON)); |
145 | 127 | let newPackageJSON = this._packageJSONForDependencySet(packageJSON, depSet); |
@@ -197,49 +179,13 @@ module.exports = CoreObject.extend({ |
197 | 179 | }); |
198 | 180 | }, |
199 | 181 |
|
200 | | - _restoreOriginalDependencies() { |
201 | | - debug('Restoring original package.json and node_modules'); |
202 | | - |
203 | | - let restoreTasks = [ |
204 | | - copy( |
205 | | - path.join(this.cwd, this.packageJSONBackupFileName), |
206 | | - path.join(this.cwd, this.packageJSON) |
207 | | - ), |
208 | | - ]; |
209 | | - |
210 | | - let yarnLockBackupFileName = path.join(this.cwd, this.yarnLockBackupFileName); |
211 | | - if (fs.existsSync(yarnLockBackupFileName)) { |
212 | | - restoreTasks.push(copy(yarnLockBackupFileName, path.join(this.cwd, this.yarnLock))); |
213 | | - } |
214 | | - |
215 | | - let packageLockBackupFileName = path.join(this.cwd, this.packageLockBackupFileName); |
216 | | - if (fs.existsSync(packageLockBackupFileName)) { |
217 | | - restoreTasks.push(copy(packageLockBackupFileName, path.join(this.cwd, this.packageLock))); |
218 | | - } |
219 | | - |
220 | | - return Promise.all(restoreTasks).then(() => this._install()); |
| 182 | + async _restoreOriginalDependencies() { |
| 183 | + await this.backup.restoreFiles([this.packageJSON, this.packageLock, this.yarnLock]); |
| 184 | + await this.backup.cleanUp(); |
| 185 | + await this._install(); |
221 | 186 | }, |
222 | 187 |
|
223 | | - _backupOriginalDependencies() { |
224 | | - debug('Backing up package.json and node_modules'); |
225 | | - |
226 | | - let backupTasks = [ |
227 | | - copy( |
228 | | - path.join(this.cwd, this.packageJSON), |
229 | | - path.join(this.cwd, this.packageJSONBackupFileName) |
230 | | - ), |
231 | | - ]; |
232 | | - |
233 | | - let yarnLockPath = path.join(this.cwd, this.yarnLock); |
234 | | - if (fs.existsSync(yarnLockPath)) { |
235 | | - backupTasks.push(copy(yarnLockPath, path.join(this.cwd, this.yarnLockBackupFileName))); |
236 | | - } |
237 | | - |
238 | | - let packageLockPath = path.join(this.cwd, this.packageLock); |
239 | | - if (fs.existsSync(packageLockPath)) { |
240 | | - backupTasks.push(copy(packageLockPath, path.join(this.cwd, this.packageLockBackupFileName))); |
241 | | - } |
242 | | - |
243 | | - return Promise.all(backupTasks); |
| 188 | + async _backupOriginalDependencies() { |
| 189 | + await this.backup.addFiles([this.packageJSON, this.packageLock, this.yarnLock]); |
244 | 190 | }, |
245 | 191 | }); |
0 commit comments