Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "diff2html-cli",
"version": "1.3.0",
"version": "1.4.0",
"homepage": "https://www.github.com/rtfpessoa/diff2html-cli",
"description": "Fast Diff to colorized HTML",
"keywords": [
Expand Down Expand Up @@ -45,10 +45,12 @@
},
"main": "./src/main.js",
"dependencies": {
"yargs": "^3.31.0",
"copy-paste": "^1.1.4",
"diff2html": "~1.2.0",
"extend": "^3.0.0",
"open": "^0.0.5",
"request": "^2.67.0",
"diff2html": "~1.2.0"
"yargs": "^3.31.0"
},
"devDependencies": {
"codacy-coverage": "^1.1.3",
Expand Down
34 changes: 23 additions & 11 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@

(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() {
}

Expand Down Expand Up @@ -41,7 +47,7 @@
if (gitArgsArr.length && gitArgsArr[0]) {
gitArgs = gitArgsArr.join(' ');
} else {
gitArgs = '-M HEAD~1';
gitArgs = '-M HEAD';
}

var diffCommand = 'git diff ' + gitArgs;
Expand Down Expand Up @@ -83,13 +89,15 @@
};

Diff2HtmlInterface.prototype._prepareHTML = function(content) {
var template = utils.readFileSync(__dirname + '/../dist/template.html');
var templatePath = path.resolve(__dirname, '..', 'dist', 'template.html');
var template = utils.readFileSync(templatePath);

var cssFilePath = path.resolve(__dirname, '..', 'node_modules', 'diff2html', 'css', 'diff2html.css');
var cssFallbackPath = path.resolve(__dirname, '..', 'dist', 'diff2html.css');

var cssFile = __dirname + '/../node_modules/diff2html/css/diff2html.css';
var cssFallbackFile = __dirname + '/../dist/diff2html.css';
if (utils.existsSync(cssFile)) cssFile = cssFallbackFile;
if (!utils.existsSync(cssFilePath)) cssFilePath = cssFallbackPath;

var cssContent = utils.readFileSync(cssFile);
var cssContent = utils.readFileSync(cssFilePath);

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

Diff2HtmlInterface.prototype.preview = function(content, format) {
var filePath = '/tmp/diff.' + format;
var filename = 'diff.' + format;
var filePath = path.resolve(os.tmpdir(), filename);
utils.writeFile(filePath, content);
utils.runCmd('open ' + filePath);
open(filePath);
};

Diff2HtmlInterface.prototype.postToDiffy = function(diff, postType) {
Diff2HtmlInterface.prototype.postToDiffy = function(diff, postType, callback) {
var jsonParams = {udiff: diff};

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

if (postType === 'browser') {
utils.runCmd('open ' + response.url);
open(response.url);
return callback(null, response.url);
} else if (postType === 'pbcopy') {
utils.runCmd('echo "' + response.url + '" | pbcopy');
ncp.copy(response.url, function() {
return callback(null, response.url);
});
}
} else {
log.error('Error: ' + message);
Expand Down
6 changes: 5 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ var argv = require('yargs')
* CLI code
*/

function noop(ignore) {
return ignore;
}

function onInput(err, input) {
if (err) {
log.error(err);
Expand All @@ -138,7 +142,7 @@ function onInput(err, input) {
}

if (argv.diffy) {
cli.postToDiffy(input, argv.diffy);
cli.postToDiffy(input, argv.diffy, noop);
return;
}

Expand Down
8 changes: 5 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
};

Utils.prototype.existsSync = function(filePath) {
var result = false;

try {
fs.existsSync(filePath);
result = fs.existsSync(filePath);
} catch (ignore) {
return false;
result = false;
}

return true;
return result || false;
};

Utils.prototype.readFileSync = function(filePath) {
Expand Down