From ca150c8ad5dcc416dd4144e654623f2a7849295c Mon Sep 17 00:00:00 2001 From: Charlie Jonas Date: Tue, 15 Oct 2019 23:25:21 -0600 Subject: [PATCH] #6 backup source and restore on error --- .vscode/launch.json | 3 +-- package.json | 7 +++++-- src/commands/force/source/clean.ts | 25 ++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index c5b0237..6aefa45 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,6 @@ "name": "Attach to Remote", "address": "127.0.0.1", "port": 9229, - "localRoot": "${workspaceFolder}" }, { "name": "Unit Tests", @@ -29,4 +28,4 @@ "cwd": "${workspaceRoot}" } ] -} \ No newline at end of file +} diff --git a/package.json b/package.json index 3df2b32..a1f7572 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "force-source-clean", "description": "SFDX plugin which performs the 'force:source:retrieve --manifest' command but removes stale files", - "version": "0.0.2", + "version": "0.0.3", "author": "Charlie Jonas @ChuckJonas", "bugs": "https://github.com/ChuckJonas/force-source-clean/issues", "dependencies": { @@ -11,6 +11,7 @@ "@salesforce/command": "^1.4.1", "@salesforce/core": "^1.3.2", "chalk": "^2.4.2", + "recursive-copy": "^2.0.10", "rimraf": "^3.0.0", "tslib": "^1" }, @@ -20,14 +21,16 @@ "@oclif/test": "^1", "@salesforce/dev-config": "1.4.1", "@types/chai": "^4", + "@types/chalk": "^2.2.0", "@types/mocha": "^5", "@types/node": "^10", - "@types/chalk": "^2.2.0", + "@types/tmp": "^0.1.0", "@types/rimraf": "^2.0.2", "chai": "^4", "globby": "^8", "mocha": "^5", "nyc": "^14", + "tmp": "^0.1.0", "ts-node": "^8", "tslint": "^5" }, diff --git a/src/commands/force/source/clean.ts b/src/commands/force/source/clean.ts index d20c366..3a94f21 100644 --- a/src/commands/force/source/clean.ts +++ b/src/commands/force/source/clean.ts @@ -5,7 +5,9 @@ import * as chalk from 'chalk'; import { spawn, SpawnOptions } from 'child_process'; import * as fs from 'fs'; import * as path from 'path'; +import * as copy from 'recursive-copy'; import * as rimraf from 'rimraf'; +import * as tmp from 'tmp'; // Initialize Messages with the current plugin directory Messages.importMessagesDirectory(__dirname); @@ -52,7 +54,19 @@ export default class Org extends SfdxCommand { } } + const tempDir = await new Promise((resolve, reject) => { + tmp.dir((err, tPath) => { + if (err) { + reject(err); + } + resolve(tPath); + }); + }); + for (const sourcePath of sourcePaths) { + // backup + await copy(sourcePath, path.join(tempDir, sourcePath)); + // mark markContents(sourcePath); } @@ -65,15 +79,20 @@ export default class Org extends SfdxCommand { onStdOut: msg => this.ux.log(msg) }); } catch (e) { - this.ux.warn('Failed to retrieve! All files are "marked" and will need to be manually reset!'); - this.ux.error(e); - + this.ux.stopSpinner('failed'); + this.ux.error(`\n ${e} \n`); + this.ux.log('Restoring source from backup'); + // restore + for (const sourcePath of sourcePaths) { + await copy(path.join(tempDir, sourcePath), sourcePath, {overwrite: true}); + } return; } for (const sourcePath of sourcePaths) { deletedMarked(sourcePath); } + this.ux.stopSpinner('Clean Completed'); return {}; }