forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathedit-file-diff.js
61 lines (51 loc) · 1.79 KB
/
edit-file-diff.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
var fs = require('fs');
var Promise = require('../ext/promise');
var readFile = Promise.denodeify(fs.readFile);
var writeFile = Promise.denodeify(fs.writeFile);
var jsdiff = require('diff');
var temp = require('temp').track();
var path = require('path');
var SilentError = require('silent-error');
var openEditor = require('../utilities/open-editor');
function EditFileDiff(options) {
this.info = options.info;
}
EditFileDiff.prototype.edit = function() {
return Promise.hash({
input: this.info.render(),
output: readFile(this.info.outputPath)
})
.then(invokeEditor.bind(this))
.then(applyPatch.bind(this))
.finally(cleanUp.bind(this));
};
function cleanUp() {
temp.cleanupSync();
}
function applyPatch(resultHash) {
/*jshint validthis:true */
return Promise.hash({
diffString: readFile(resultHash.diffPath),
currentString: readFile(resultHash.outputPath)
}).then(function(result) {
var appliedDiff = jsdiff.applyPatch(result.currentString.toString(), result.diffString.toString());
if (!appliedDiff) {
var message = 'Patch was not cleanly applied.';
this.info.ui.writeLine(message + ' Please choose another action.');
throw new SilentError(message);
}
return writeFile(resultHash.outputPath, appliedDiff);
}.bind(this));
}
function invokeEditor(result) {
var info = this.info; // jshint ignore:line
var diff = jsdiff.createPatch(info.outputPath, result.output.toString(), result.input);
var diffPath = path.join(temp.mkdirSync(), 'currentDiff.diff');
return writeFile(diffPath, diff).then(function() {
return openEditor(diffPath);
}).then(function() {
return { outputPath: info.outputPath, diffPath: diffPath };
});
}
module.exports = EditFileDiff;