Skip to content
Closed
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
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
key: dependency-cache-{{ checksum "yarn.lock" }}
paths:
- ./node_modules
- run: npm run build
- run: npm run coverage
- run: npm run check-coverage
- run: npm run report
Expand Down
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Skip bin and coverage folders
bin/**
coverage/**
built/**
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ coverage/

# Bower
bower_components/

# TypeScript
built/
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
},
"preferGlobal": true,
"scripts": {
"build": "tsc",
"lint": "eslint .",
"style": "yarn run lint",
"test": "yarn run coverage",
"test": "yarn build && yarn run coverage",
"coverage": "nyc mocha",
"coverage-html": "nyc report --reporter=html && open ./coverage/index.html",
"check-coverage": "nyc check-coverage --statements 40 --functions 40 --branches 0 --lines 40",
"check-coverage": "nyc check-coverage --statements 35 --functions 35 --branches 0 --lines 35",
"report": "nyc report",
"coverage-lcov": "nyc report --reporter=lcov",
"codacy": "cat ./coverage/lcov.info | codacy-coverage",
Expand All @@ -56,18 +57,20 @@
"clipboardy": "^2.1.0",
"diff2html": "^2.7.0",
"extend": "^3.0.2",
"open": "^6.0.0",
"open": "^6.4.0",
"request": "^2.88.0",
"yargs": "^12.0.5"
"yargs": "^13.2.1"
},
"devDependencies": {
"@types/node": "^11.9.4",
"codacy-coverage": "^3.4.0",
"eslint": "^5.13.0",
"eslint": "^5.14.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"mocha": "^5.2.0",
"nyc": "^13.2.0",
"sinon": "^7.2.3"
"mocha": "^6.0.0",
"nyc": "^13.3.0",
"sinon": "^7.2.4",
"typescript": "^3.3.3"
},
"license": "MIT",
"files": [
Expand Down
103 changes: 52 additions & 51 deletions src/cli.js → src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,30 @@
*
*/

(function() {
var fs = require('fs');
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 clipboardy = require('clipboardy');

function Diff2HtmlInterface() {
}

/*
* Input
type InputType = 'file' | 'stdin' | 'command';
type PostType = 'browser' | 'pbcopy';

const clipboardy = require('clipboardy');
const fs = require('fs');
const opn = require('open');
const os = require('os');
const path = require('path');

const diff2Html = require('diff2html').Diff2Html;
const http = require('./http-utils.js').HttpUtils;
const log = require('./logger.js').Logger;
const utils = require('./utils.js').Utils;

module.exports = {

/**
* Get input for the diff
* @param inputType - a string `file`, `stdin`, or `command`
* @param inputArgs - a string array
* @param ignore - a string array
* @param callback - the callback function
*/

Diff2HtmlInterface.prototype.getInput = function getInput(inputType, inputArgs, ignore, callback) {
getInput(inputType: InputType, inputArgs: any[], ignore: string[], callback) {
var that = this;
switch (inputType) {
case 'file':
Expand All @@ -38,15 +40,15 @@
break;

default:
that._runGitDiff(inputArgs, ignore, callback);
that.runGitDiff(inputArgs, ignore, callback);
}
};
},

Diff2HtmlInterface.prototype._runGitDiff = function(gitArgsArr, ignore, callback) {
var gitArgs;
runGitDiff(gitArgsArr: string[], ignore: string[], callback) {
let gitArgs: string;

if (gitArgsArr.length && gitArgsArr[0]) {
gitArgs = gitArgsArr.map(function(arg) {
gitArgs = gitArgsArr.map((arg) => {
return '"' + arg + '"'; // wrap parameters
}).join(' ');
} else {
Expand All @@ -57,24 +59,24 @@
gitArgs += ' --no-color';
}

var ignoreString = '';
let ignoreString = '';

if (ignore) {
ignoreString = ignore.map(function(file) {
ignoreString = ignore.map((file) => {
return ' ":(exclude)' + file + '" ';
}).join(' ');
}

var diffCommand = 'git diff ' + gitArgs + ignoreString;
const diffCommand: string = 'git diff ' + gitArgs + ignoreString;

return callback(null, utils.runCmd(diffCommand));
};
},

/*
* Output
*/

Diff2HtmlInterface.prototype.getOutput = function(baseConfig, input, callback) {
getOutput(baseConfig, input, callback) {
var that = this;
var config = baseConfig;
var defaultTemplate = path.resolve(__dirname, '..', 'dist', 'template.html');
Expand Down Expand Up @@ -107,15 +109,15 @@
config.synchronisedScroll = (baseConfig.synchronisedScroll === 'enabled');

var htmlContent = diff2Html.getPrettyHtml(jsonContent, config);
return callback(null, that._prepareHTML(htmlContent, 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) {
prepareHTML(content, config) {
var templatePath = config.template;
var template = utils.readFileSync(templatePath);

Expand All @@ -133,40 +135,39 @@
.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);
preview(content: string, format: string) {
const filename: string = 'diff.' + format;
const filePath: string = path.resolve(os.tmpdir(), filename);
utils.writeFile(filePath, content);
open(filePath);
};
opn(filePath, { wait: false });
},

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

http.put('https://diffy.org/api/diff/', jsonParams, function(err, url) {
http.put('https://diffy.org/api/diff/', jsonParams, (err, response) => {
if (err) {
log.error(err);
return;
}

log.print('Link powered by https://diffy.org');
log.print(url);
log.print(response);

if (postType === 'browser') {
open(url);
return callback(null, url);
opn(response);
return callback(null, response);
} else if (postType === 'pbcopy') {
clipboardy.writeSync(url);
return callback(null, url);
clipboardy.writeSync(response);
return callback(null, response);
}
});
};
}

module.exports.Diff2HtmlInterface = new Diff2HtmlInterface();
})();
}
5 changes: 3 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*
*/

var cli = require('./cli.js').Diff2HtmlInterface;
const cli = require('./cli');

var log = require('./logger.js').Logger;
var utils = require('./utils.js').Utils;

Expand Down Expand Up @@ -151,7 +152,7 @@ var argv = yargs.usage('Usage: diff2html [options] -- [diff args]')
.example('diff2html -i file -- my-file-diff.diff', 'reading the input from a file')
.example('diff2html -f json -o stdout -- -M HEAD~1', 'print json format to stdout')
.example('diff2html -F my-pretty-diff.html -- -M HEAD~1', 'print to file')
.example('diff2html --ig -- package-lock.json --ig yarn.lock',
.example('diff2html --ig package-lock.json --ig yarn.lock',
'ignore two particular files when generating the diff')
.help('h')
.alias('v', 'version')
Expand Down
10 changes: 5 additions & 5 deletions test/cli-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ var assert = require('assert');

var sinon = require('sinon');

var Cli = require('../src/cli.js').Diff2HtmlInterface;
var http = require('../src/http-utils.js').HttpUtils;
var Utils = require('../src/utils.js').Utils;
var Cli = require('../built/cli.js');
var http = require('../built/http-utils.js').HttpUtils;
var Utils = require('../built/utils.js').Utils;

describe('Cli', function() {
describe('getInput', function() {
Expand All @@ -24,8 +24,8 @@ describe('Cli', function() {
spy.restore();
});

it('should _runGitDiff by default', function() {
var spy = sinon.stub(Cli, '_runGitDiff');
it('should runGitDiff by default', function() {
var spy = sinon.stub(Cli, 'runGitDiff');
Cli.getInput('abc', ['lol', 'foo'], 'ignore', 'callback');
assert(spy.calledOnce);
assert(spy.calledWith(['lol', 'foo'], 'ignore', 'callback'));
Expand Down
2 changes: 1 addition & 1 deletion test/utils-tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var assert = require('assert');

var Utils = require('../src/utils.js').Utils;
var Utils = require('../built/utils.js').Utils;

describe('Utils', function() {
describe('IO', function() {
Expand Down
10 changes: 10 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"outDir": "./built",
"allowJs": true,
"target": "es5"
},
"include": [
"./src/**/*"
]
}
Loading