Skip to content

Commit

Permalink
#6 backup source and restore on error
Browse files Browse the repository at this point in the history
  • Loading branch information
ChuckJonas committed Oct 16, 2019
1 parent 8940de6 commit ca150c8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
3 changes: 1 addition & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"name": "Attach to Remote",
"address": "127.0.0.1",
"port": 9229,
"localRoot": "${workspaceFolder}"
},
{
"name": "Unit Tests",
Expand All @@ -29,4 +28,4 @@
"cwd": "${workspaceRoot}"
}
]
}
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
},
Expand All @@ -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"
},
Expand Down
25 changes: 22 additions & 3 deletions src/commands/force/source/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -52,7 +54,19 @@ export default class Org extends SfdxCommand {
}
}

const tempDir = await new Promise<string>((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);
}

Expand All @@ -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 {};
}
Expand Down

0 comments on commit ca150c8

Please sign in to comment.