Skip to content

Commit 6b31fec

Browse files
committed
Merge pull request #14 from rtfpessoa/windows-compatibility
Fix Windows compatibility
2 parents 4ef1570 + 2e19af6 commit 6b31fec

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "diff2html-cli",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"homepage": "https://www.github.com/rtfpessoa/diff2html-cli",
55
"description": "Fast Diff to colorized HTML",
66
"keywords": [
@@ -45,10 +45,12 @@
4545
},
4646
"main": "./src/main.js",
4747
"dependencies": {
48-
"yargs": "^3.31.0",
48+
"copy-paste": "^1.1.4",
49+
"diff2html": "~1.2.0",
4950
"extend": "^3.0.0",
51+
"open": "^0.0.5",
5052
"request": "^2.67.0",
51-
"diff2html": "~1.2.0"
53+
"yargs": "^3.31.0"
5254
},
5355
"devDependencies": {
5456
"codacy-coverage": "^1.1.3",

src/cli.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@
77

88
(function() {
99

10+
var os = require('os');
11+
var path = require('path');
12+
1013
var diff2Html = require('diff2html').Diff2Html;
1114

1215
var log = require('./logger.js').Logger;
1316
var http = require('./http-utils.js').HttpUtils;
1417
var utils = require('./utils.js').Utils;
1518

19+
var open = require('open');
20+
var ncp = require("copy-paste");
21+
1622
function Diff2HtmlInterface() {
1723
}
1824

@@ -41,7 +47,7 @@
4147
if (gitArgsArr.length && gitArgsArr[0]) {
4248
gitArgs = gitArgsArr.join(' ');
4349
} else {
44-
gitArgs = '-M HEAD~1';
50+
gitArgs = '-M HEAD';
4551
}
4652

4753
var diffCommand = 'git diff ' + gitArgs;
@@ -83,13 +89,15 @@
8389
};
8490

8591
Diff2HtmlInterface.prototype._prepareHTML = function(content) {
86-
var template = utils.readFileSync(__dirname + '/../dist/template.html');
92+
var templatePath = path.resolve(__dirname, '..', 'dist', 'template.html');
93+
var template = utils.readFileSync(templatePath);
94+
95+
var cssFilePath = path.resolve(__dirname, '..', 'node_modules', 'diff2html', 'css', 'diff2html.css');
96+
var cssFallbackPath = path.resolve(__dirname, '..', 'dist', 'diff2html.css');
8797

88-
var cssFile = __dirname + '/../node_modules/diff2html/css/diff2html.css';
89-
var cssFallbackFile = __dirname + '/../dist/diff2html.css';
90-
if (utils.existsSync(cssFile)) cssFile = cssFallbackFile;
98+
if (!utils.existsSync(cssFilePath)) cssFilePath = cssFallbackPath;
9199

92-
var cssContent = utils.readFileSync(cssFile);
100+
var cssContent = utils.readFileSync(cssFilePath);
93101

94102
return template
95103
.replace('<!--css-->', '<style>\n' + cssContent + '\n</style>')
@@ -101,12 +109,13 @@
101109
*/
102110

103111
Diff2HtmlInterface.prototype.preview = function(content, format) {
104-
var filePath = '/tmp/diff.' + format;
112+
var filename = 'diff.' + format;
113+
var filePath = path.resolve(os.tmpdir(), filename);
105114
utils.writeFile(filePath, content);
106-
utils.runCmd('open ' + filePath);
115+
open(filePath);
107116
};
108117

109-
Diff2HtmlInterface.prototype.postToDiffy = function(diff, postType) {
118+
Diff2HtmlInterface.prototype.postToDiffy = function(diff, postType, callback) {
110119
var jsonParams = {udiff: diff};
111120

112121
http.post('http://diffy.org/api/new', jsonParams, function(err, response) {
@@ -120,9 +129,12 @@
120129
log.print(response.url);
121130

122131
if (postType === 'browser') {
123-
utils.runCmd('open ' + response.url);
132+
open(response.url);
133+
return callback(null, response.url);
124134
} else if (postType === 'pbcopy') {
125-
utils.runCmd('echo "' + response.url + '" | pbcopy');
135+
ncp.copy(response.url, function() {
136+
return callback(null, response.url);
137+
});
126138
}
127139
} else {
128140
log.error('Error: ' + message);

src/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ var argv = require('yargs')
126126
* CLI code
127127
*/
128128

129+
function noop(ignore) {
130+
return ignore;
131+
}
132+
129133
function onInput(err, input) {
130134
if (err) {
131135
log.error(err);
@@ -138,7 +142,7 @@ function onInput(err, input) {
138142
}
139143

140144
if (argv.diffy) {
141-
cli.postToDiffy(input, argv.diffy);
145+
cli.postToDiffy(input, argv.diffy, noop);
142146
return;
143147
}
144148

src/utils.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
};
2323

2424
Utils.prototype.existsSync = function(filePath) {
25+
var result = false;
26+
2527
try {
26-
fs.existsSync(filePath);
28+
result = fs.existsSync(filePath);
2729
} catch (ignore) {
28-
return false;
30+
result = false;
2931
}
3032

31-
return true;
33+
return result || false;
3234
};
3335

3436
Utils.prototype.readFileSync = function(filePath) {

0 commit comments

Comments
 (0)