Skip to content

Commit 44a468e

Browse files
committed
Merge pull request #10 from rtfpessoa/improve-project
Improve project code quality, style and docs
2 parents cfbe267 + 184abc2 commit 44a468e

File tree

11 files changed

+465
-218
lines changed

11 files changed

+465
-218
lines changed

config.jscs.json renamed to .jscsrc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@
66
"else"
77
],
88
"disallowMixedSpacesAndTabs": true,
9-
"disallowMultipleVarDecl": "exceptUndefined",
9+
"disallowMultipleVarDecl": {
10+
"allExcept": [
11+
"undefined"
12+
]
13+
},
1014
"disallowNewlineBeforeBlockStatements": true,
11-
"disallowQuotedKeysInObjects": null,
1215
"disallowSpaceAfterObjectKeys": true,
1316
"disallowSpaceAfterPrefixUnaryOperators": true,
1417
"disallowSpacesInFunction": {
1518
"beforeOpeningRoundBrace": true
1619
},
1720
"disallowSpacesInsideParentheses": true,
1821
"disallowTrailingWhitespace": true,
19-
"maximumLineLength": 90,
22+
"maximumLineLength": 130,
2023
"requireCamelCaseOrUpperCaseIdentifiers": true,
2124
"requireCapitalizedComments": true,
2225
"requireCapitalizedConstructors": true,
23-
"requireCurlyBraces": true,
2426
"requireSpaceAfterKeywords": [
2527
"if",
2628
"else",
@@ -44,6 +46,5 @@
4446
},
4547
"requireTrailingComma": null,
4648
"validateIndentation": 2,
47-
"validateLineBreaks": "LF",
48-
"validateQuoteMarks": "'"
49+
"validateLineBreaks": "LF"
4950
}

CONTRIBUTING.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
## How to contribute to diff2html-cli
2+
3+
### Main rules
4+
5+
* Before you open a ticket or send a pull request, [search](https://github.com/rtfpessoa/diff2html-cli/issues) for previous discussions about the same feature or issue. Add to the earlier ticket if you find one.
6+
7+
* If you're proposing a new feature, make sure you create an issue to let other contributors know what you are working on.
8+
9+
* Before sending a pull request make sure your code is tested.
10+
11+
* Before sending a pull request for a feature, be sure to run tests with `npm test`.
12+
13+
* Use the same coding style as the rest of the codebase, most of the check can be performed with `npm run style`.
14+
15+
* Use `git rebase` (not `git merge`) to sync your work from time to time with the master branch.
16+
17+
* After creating your pull request make sure the build is passing on [CircleCI](https://circleci.com/gh/rtfpessoa/diff2html-cli)
18+
and that [Codacy](https://www.codacy.com/app/Codacy/diff2html-cli) is also confident in the code quality.
19+
20+
* In your pull request, do not commit the `dist` or `build` folder if you needed to build the release files.
21+
22+
### Commit Style
23+
24+
Writing good commit logs is important. A commit log should describe what changed and why.
25+
Follow these guidelines when writing one:
26+
27+
1. The first line should be 50 characters or less and contain a short
28+
description of the change prefixed with the name of the changed
29+
subsystem (e.g. "net: add localAddress and localPort to Socket").
30+
2. Keep the second line blank.
31+
3. Wrap all other lines at 72 columns.
32+
33+
A good commit log can look something like this:
34+
35+
```
36+
subsystem: explaining the commit in one line
37+
38+
Body of commit message is a few lines of text, explaining things
39+
in more detail, possibly giving some background about the issue
40+
being fixed, etc. etc.
41+
42+
The body of the commit message can be several paragraphs, and
43+
please do proper word-wrap and keep columns shorter than about
44+
72 characters or so. That way `git log` will show things
45+
nicely even when it is indented.
46+
```
47+
48+
### Developer's Certificate of Origin 1.0
49+
50+
By making a contribution to this project, I certify that:
51+
52+
* (a) The contribution was created in whole or in part by me and I
53+
have the right to submit it under the open source license indicated
54+
in the file; or
55+
* (b) The contribution is based upon previous work that, to the best
56+
of my knowledge, is covered under an appropriate open source license
57+
and I have the right under that license to submit that work with
58+
modifications, whether created in whole or in part by me, under the
59+
same open source license (unless I am permitted to submit under a
60+
different license), as indicated in the file; or
61+
* (c) The contribution was provided directly to me by some other
62+
person who certified (a), (b) or (c) and I have not modified it.

README.md

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
# Diff to Html CLI by [rtfpessoa](https://github.com/rtfpessoa)
1+
# diff2html-cli
22

3-
[![Codacy Badge](https://api.codacy.com/project/badge/grade/e6139937d72f40ed8b3920d53c74298a)](https://www.codacy.com/app/Codacy/diff2html-cli)
3+
[![Circle CI](https://circleci.com/gh/rtfpessoa/diff2html-cli.svg?style=svg)](https://circleci.com/gh/rtfpessoa/diff2html-cli)
4+
[![Codacy Code Badge](https://api.codacy.com/project/badge/grade/e6139937d72f40ed8b3920d53c74298a)](https://www.codacy.com/app/Codacy/diff2html-cli)
5+
[![Codacy Coverage Badge](https://api.codacy.com/project/badge/coverage/e6139937d72f40ed8b3920d53c74298a)](https://www.codacy.com/app/Codacy/diff2html-cli)
6+
7+
[![npm](https://img.shields.io/npm/v/diff2html-cli.svg)](https://www.npmjs.com/package/diff2html-cli)
8+
[![David](https://img.shields.io/david/rtfpessoa/diff2html-cli.svg)](https://david-dm.org/rtfpessoa/diff2html-cli)
9+
[![David](https://img.shields.io/david/dev/rtfpessoa/diff2html-cli.svg)](https://david-dm.org/rtfpessoa/diff2html-cli)
10+
11+
[![node](https://img.shields.io/node/v/diff2html-cli.svg)]()
12+
[![npm](https://img.shields.io/npm/l/diff2html-cli.svg)]()
13+
[![npm](https://img.shields.io/npm/dm/diff2html-cli.svg)](https://www.npmjs.com/package/diff2html-cli)
14+
[![Gitter](https://badges.gitter.im/rtfpessoa/diff2html.svg)](https://gitter.im/rtfpessoa/diff2html?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
415

516
Diff to Html generates pretty HTML diffs from git diff output in your terminal
617

@@ -16,6 +27,8 @@ Diff to Html generates pretty HTML diffs from git diff output in your terminal
1627

1728
* Code syntax highlight
1829

30+
* Line similarity matching
31+
1932
## Online Example
2033

2134
> Go to [Diff2HTML](http://rtfpessoa.github.io/diff2html/)
@@ -38,47 +51,43 @@ Diff to Html generates pretty HTML diffs from git diff output in your terminal
3851

3952
## Usage
4053

41-
diff2html [options] -- [diff args]
42-
54+
Usage: diff2html [options] -- [diff args]
4355

4456
Options:
45-
-s, --style Output style
46-
[string] [choices: "line", "side"] [default: "line"]
47-
-f, --format Output format
48-
[string] [choices: "html", "json"] [default: "html"]
49-
-d, --diff Diff style [string] [choices: "word", "char"] [default: "word"]
50-
-i, --input Diff input source
51-
[string] [choices: "file", "command", "stdin"] [default: "command"]
52-
-o, --output Output destination
53-
[string] [choices: "preview", "stdout"] [default: "preview"]
54-
-u, --diffy Upload to diffy.org
55-
[string] [choices: "browser", "pbcopy", "print"]
56-
-F, --file Send output to file (overrides output option) [string]
57-
--version Show version number [boolean]
58-
-h, --help Show help [boolean]
59-
57+
-s, --style Output style [string] [choices: "line", "side"] [default: "line"]
58+
--su, --summary Show files summary [boolean] [default: "true"]
59+
--lm, --matching Diff line matching type [string] [choices: "lines", "words", "none"] [default: "none"]
60+
--lmt, --matchWordsThreshold Diff line matching word threshold [string] [default: "0.25"]
61+
-f, --format Output format [string] [choices: "html", "json"] [default: "html"]
62+
-d, --diff Diff style [string] [choices: "word", "char"] [default: "word"]
63+
-i, --input Diff input source [string] [choices: "file", "command", "stdin"] [default: "command"]
64+
-o, --output Output destination [string] [choices: "preview", "stdout"] [default: "preview"]
65+
-u, --diffy Upload to diffy.org [string] [choices: "browser", "pbcopy", "print"]
66+
-F, --file Send output to file (overrides output option) [string]
67+
--version Show version number [boolean]
68+
-h, --help Show help [boolean]
6069

6170
Examples:
62-
diff2html -s line -f html -d word -i diff last commit, line by line, word
63-
command -o preview -- -M HEAD~1 comparison between lines,previewed
64-
in the browser and input from git
65-
diff command
66-
diff2html -i file -- my-file-diff.diff reading the input from a file
67-
diff2html -f json -o stdout -- -M HEAD~1 print json format to stdout
68-
diff2html -F my-pretty-diff.html -- -M print to file
69-
HEAD~1
70-
71-
72-
© 2015 rtfpessoa
71+
diff2html -s line -f html -d word -i command -o preview -- -M HEAD~1
72+
-> diff last commit, line by line, word comparison between lines,previewed
73+
in the browser and input from git diff command
74+
diff2html -i file -- my-file-diff.diff
75+
-> reading the input from a file
76+
diff2html -f json -o stdout -- -M HEAD~1
77+
-> print json format to stdout
78+
diff2html -F my-pretty-diff.html -- -M HEAD~1
79+
-> print to file
80+
81+
© 2014 rtfpessoa
7382
For support, check out https://github.com/rtfpessoa/diff2html-cli
7483

7584
> NOTE: notice the `--` in the examples
7685
7786
## Contributions
7887

79-
All the contributions are welcome.
80-
81-
To contribute just send a pull request with your changes and I will review it asap.
88+
This is a developer friendly project, all the contributions are welcome.
89+
To contribute just send a pull request with your changes following the guidelines described in `CONTRIBUTING.md`.
90+
I will try to review them as soon as possible.
8291

8392
## License
8493

circle.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
test:
2+
override:
3+
- nvm install 0.12 && npm test
4+
- nvm install 4 && npm test
5+
post:
6+
- npm install
7+
- istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec
8+
- cat ./coverage/lcov.info | ./node_modules/.bin/codacy-coverage
9+
- rm -rf ./coverage

package.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,25 @@
3636
},
3737
"preferGlobal": "true",
3838
"scripts": {
39-
"test": ""
39+
"test": "mocha",
40+
"style": "jscs src test",
41+
"codacy": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/.bin/codacy-coverage && rm -rf ./coverage"
4042
},
4143
"bin": {
4244
"diff2html": "./bin/diff2html"
4345
},
4446
"main": "./src/main.js",
4547
"dependencies": {
46-
"yargs": "3.31.*",
47-
"extend": "3.0.*",
48-
"request": "2.67.*",
49-
"diff2html": "*"
48+
"yargs": "^3.31.0",
49+
"extend": "^3.0.0",
50+
"request": "^2.67.0",
51+
"diff2html": "1.2.0"
5052
},
5153
"devDependencies": {
54+
"codacy-coverage": "^1.1.3",
55+
"istanbul": "^0.4.1",
56+
"jscs": "^2.7.0",
57+
"mocha": "^2.3.4"
5258
},
5359
"license": "MIT",
5460
"files": [

src/cli.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
*
3+
* Diff to HTML CLI (cli.js)
4+
* Author: rtfpessoa
5+
*
6+
*/
7+
8+
(function() {
9+
10+
var diff2Html = require('diff2html').Diff2Html;
11+
12+
var log = require('./logger.js').Logger;
13+
var http = require('./http-utils.js').HttpUtils;
14+
var utils = require('./utils.js').Utils;
15+
16+
function Diff2HtmlInterface() {
17+
}
18+
19+
/*
20+
* Input
21+
*/
22+
23+
Diff2HtmlInterface.prototype.getInput = function getInput(inputType, inputArgs, callback) {
24+
var that = this;
25+
switch (inputType) {
26+
case 'file':
27+
utils.readFile(inputArgs[0], callback);
28+
break;
29+
30+
case 'stdin':
31+
utils.readStdin(callback);
32+
break;
33+
34+
default:
35+
that._runGitDiff(inputArgs, callback);
36+
}
37+
};
38+
39+
Diff2HtmlInterface.prototype._runGitDiff = function(gitArgsArr, callback) {
40+
var gitArgs;
41+
if (gitArgsArr.length && gitArgsArr[0]) {
42+
gitArgs = gitArgsArr.join(' ');
43+
} else {
44+
gitArgs = '-M HEAD~1';
45+
}
46+
47+
var diffCommand = 'git diff ' + gitArgs;
48+
return callback(null, utils.runCmd(diffCommand));
49+
};
50+
51+
/*
52+
* Output
53+
*/
54+
55+
Diff2HtmlInterface.prototype.getOutput = function(baseConfig, input, callback) {
56+
var that = this;
57+
var config = baseConfig;
58+
config.wordByWord = (baseConfig.diff === 'word');
59+
config.charByChar = (baseConfig.diff === 'char');
60+
61+
var jsonContent = diff2Html.getJsonFromDiff(input, config);
62+
63+
if (baseConfig.format === 'html') {
64+
config.inputFormat = 'json';
65+
66+
if (baseConfig.style === 'side') {
67+
config.outputFormat = 'side-by-side';
68+
} else {
69+
config.outputFormat = 'line-by-line';
70+
}
71+
72+
if (baseConfig.summary) {
73+
config.showFiles = true;
74+
}
75+
76+
var htmlContent = Diff2Html.getPrettyHtml(jsonContent, config);
77+
return callback(null, that._prepareHTML(htmlContent));
78+
} else if (baseConfig.format === 'json') {
79+
return callback(null, JSON.stringify(jsonContent));
80+
}
81+
82+
return callback(new Error('Wrong output format `' + baseConfig.format + '`!'));
83+
};
84+
85+
Diff2HtmlInterface.prototype._prepareHTML = function(content) {
86+
var template = utils.readFileSync(__dirname + '/../dist/template.html');
87+
88+
var cssFile = __dirname + '/../node_modules/diff2html/css/diff2html.css';
89+
var cssFallbackFile = __dirname + '/../dist/diff2html.css';
90+
if (utils.existsSync(cssFile)) cssFile = cssFallbackFile;
91+
92+
var cssContent = utils.readFileSync(cssFile);
93+
94+
return template
95+
.replace('<!--css-->', '<style>\n' + cssContent + '\n</style>')
96+
.replace('<!--diff-->', content);
97+
};
98+
99+
/*
100+
* Output destination
101+
*/
102+
103+
Diff2HtmlInterface.prototype.preview = function(content, format) {
104+
var filePath = '/tmp/diff.' + format;
105+
utils.writeFile(filePath, content);
106+
utils.runCmd('open ' + filePath);
107+
};
108+
109+
Diff2HtmlInterface.prototype.postToDiffy = function(diff, postType) {
110+
var jsonParams = {udiff: diff};
111+
112+
http.post('http://diffy.org/api/new', jsonParams, function(err, response) {
113+
if (err) {
114+
log.error(err);
115+
return;
116+
}
117+
118+
if (response.status !== 'error') {
119+
log.print('Link powered by diffy.org:');
120+
log.print(response.url);
121+
122+
if (postType === 'browser') {
123+
utils.runCmd('open ' + response.url);
124+
} else if (postType === 'pbcopy') {
125+
utils.runCmd('echo "' + response.url + '" | pbcopy');
126+
}
127+
} else {
128+
log.error('Error: ' + message);
129+
}
130+
});
131+
};
132+
133+
module.exports.Diff2HtmlInterface = new Diff2HtmlInterface();
134+
135+
})();

0 commit comments

Comments
 (0)