diff --git a/package.json b/package.json index 66c3913..b523bcb 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -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", diff --git a/src/cli.js b/src/cli.js index f6c6629..f34fada 100644 --- a/src/cli.js +++ b/src/cli.js @@ -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() { } @@ -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; @@ -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('', '') @@ -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) { @@ -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); diff --git a/src/main.js b/src/main.js index 4346e5a..8993ebc 100644 --- a/src/main.js +++ b/src/main.js @@ -126,6 +126,10 @@ var argv = require('yargs') * CLI code */ +function noop(ignore) { + return ignore; +} + function onInput(err, input) { if (err) { log.error(err); @@ -138,7 +142,7 @@ function onInput(err, input) { } if (argv.diffy) { - cli.postToDiffy(input, argv.diffy); + cli.postToDiffy(input, argv.diffy, noop); return; } diff --git a/src/utils.js b/src/utils.js index 98f1a08..4997b0c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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) {