Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
156 lines (124 loc) · 4.46 KB
/
Copy pathcli.js
File metadata and controls
156 lines (124 loc) · 4.46 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
*
* Diff to HTML CLI (cli.js)
* Author: rtfpessoa
*
*/
(function() {
var os = require('os');
var path = require('path');
var diff2Html = require('diff2html').Diff2Html;
var log = require('./logger.js').Logger;
var http = require('./http-utils.js').HttpUtils;
var utils = require('./utils.js').Utils;
var open = require('open');
var ncp = require('copy-paste');
function Diff2HtmlInterface() {
}
/*
* Input
*/
Diff2HtmlInterface.prototype.getInput = function getInput(inputType, inputArgs, callback) {
var that = this;
switch (inputType) {
case 'file':
utils.readFile(inputArgs[0], callback);
break;
case 'stdin':
utils.readStdin(callback);
break;
default:
that._runGitDiff(inputArgs, callback);
}
};
Diff2HtmlInterface.prototype._runGitDiff = function(gitArgsArr, callback) {
var gitArgs;
if (gitArgsArr.length && gitArgsArr[0]) {
gitArgs = gitArgsArr.map(function(arg) {
return '"' + arg + '"'; // wrap parameters
}).join(' ');
} else {
gitArgs = '-M -C HEAD';
}
var diffCommand = 'git diff ' + gitArgs;
return callback(null, utils.runCmd(diffCommand));
};
/*
* Output
*/
Diff2HtmlInterface.prototype.getOutput = function(baseConfig, input, callback) {
var that = this;
var config = baseConfig;
config.wordByWord = (baseConfig.diff === 'word');
config.charByChar = (baseConfig.diff === 'char');
var jsonContent = diff2Html.getJsonFromDiff(input, config);
if (baseConfig.format === 'html') {
config.inputFormat = 'json';
if (baseConfig.style === 'side') {
config.outputFormat = 'side-by-side';
} else {
config.outputFormat = 'line-by-line';
}
if (baseConfig.summary === 'hidden') {
config.showFiles = false;
} else {
config.showFiles = true;
config.showFilesOpen = baseConfig.summary === 'open';
}
config.synchronisedScroll = (baseConfig.synchronisedScroll === 'enabled');
var htmlContent = diff2Html.getPrettyHtml(jsonContent, config);
return callback(null, that._prepareHTML(htmlContent, config));
} else if (baseConfig.format === 'json') {
return callback(null, JSON.stringify(jsonContent));
}
return callback(new Error('Wrong output format `' + baseConfig.format + '`!'));
};
Diff2HtmlInterface.prototype._prepareHTML = function(content, config) {
var templatePath = path.resolve(__dirname, '..', 'dist', 'template.html');
var template = utils.readFileSync(templatePath);
var diff2htmlPath = path.join(path.dirname(require.resolve('diff2html')), '..');
var cssFilePath = path.resolve(diff2htmlPath, 'dist', 'diff2html.min.css');
var cssContent = utils.readFileSync(cssFilePath);
var jsUiFilePath = path.resolve(diff2htmlPath, 'dist', 'diff2html-ui.min.js');
var jsUiContent = utils.readFileSync(jsUiFilePath);
return template
.replace('<!--diff2html-css-->', '<style>\n' + cssContent + '\n</style>')
.replace('<!--diff2html-js-ui-->', '<script>\n' + jsUiContent + '\n</script>')
.replace('//diff2html-fileListCloseable', 'diff2htmlUi.fileListCloseable("#diff", ' + config.showFilesOpen + ');')
.replace('//diff2html-synchronisedScroll', 'diff2htmlUi.synchronisedScroll("#diff", ' + config.synchronisedScroll + ');')
.replace('<!--diff2html-diff-->', content);
};
/*
* Output destination
*/
Diff2HtmlInterface.prototype.preview = function(content, format) {
var filename = 'diff.' + format;
var filePath = path.resolve(os.tmpdir(), filename);
utils.writeFile(filePath, content);
open(filePath);
};
Diff2HtmlInterface.prototype.postToDiffy = function(diff, postType, callback) {
var jsonParams = {udiff: diff};
http.post('http://diffy.org/api/new', jsonParams, function(err, response) {
if (err) {
log.error(err);
return;
}
if (response.status !== 'error') {
log.print('Link powered by diffy.org:');
log.print(response.url);
if (postType === 'browser') {
open(response.url);
return callback(null, response.url);
} else if (postType === 'pbcopy') {
ncp.copy(response.url, function() {
return callback(null, response.url);
});
}
} else {
log.error('Error: ' + response.statusCode);
}
});
};
module.exports.Diff2HtmlInterface = new Diff2HtmlInterface();
})();