Skip to content

Commit d555134

Browse files
author
Rodrigo Fernandes
committed
Merge pull request #5 from rtfpessoa/support-diffy.org
initial diffy support
2 parents b9fdabd + 29c4a75 commit d555134

File tree

3 files changed

+86
-28
lines changed

3 files changed

+86
-28
lines changed

bin/diff2html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env node
22

3-
require("../src/main.js").interpret(process.argv);
3+
require("../src/main.js");

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "diff2html-cli",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"homepage": "https://www.github.com/rtfpessoa/diff2html-cli",
55
"description": "Fast Diff to colorized HTML",
66
"keywords": [
@@ -43,8 +43,9 @@
4343
},
4444
"main": "./src/main.js",
4545
"dependencies": {
46-
"yargs": "3.24.*",
46+
"yargs": "3.27.*",
4747
"extend": "3.0.*",
48+
"request": "2.65.*",
4849
"diff2html": "*"
4950
},
5051
"devDependencies": {

src/main.js

+82-25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env node
12
/*
23
*
34
* Diff to HTML CLI (main.js)
@@ -57,6 +58,15 @@ var argv = require('yargs')
5758
default: 'preview'
5859
}
5960
})
61+
.options({
62+
'u': {
63+
alias: 'diffy',
64+
describe: 'Upload to diffy.org',
65+
nargs: 1,
66+
type: 'string',
67+
choices: ['browser', 'pbcopy', 'print']
68+
}
69+
})
6070
.options({
6171
'F': {
6272
alias: 'file',
@@ -66,8 +76,8 @@ var argv = require('yargs')
6676
}
6777
})
6878
.example('diff2html -s line -f html -d word -i command -o preview -- -M HEAD~1',
69-
'diff last commit, line by line, word comparison between lines,' +
70-
'previewed in the browser and input from git diff command')
79+
'diff last commit, line by line, word comparison between lines,' +
80+
'previewed in the browser and input from git diff command')
7181
.example('diff2html -i file -- my-file-diff.diff', 'reading the input from a file')
7282
.example('diff2html -f json -o stdout -- -M HEAD~1', 'print json format to stdout')
7383
.example('diff2html -F my-pretty-diff.html -- -M HEAD~1', 'print to file')
@@ -77,31 +87,9 @@ var argv = require('yargs')
7787
.help('h')
7888
.alias('h', 'help')
7989
.epilog('© 2015 rtfpessoa\n' +
80-
'For support, check out https://github.com/rtfpessoa/diff2html-cli')
90+
'For support, check out https://github.com/rtfpessoa/diff2html-cli')
8191
.argv;
8292

83-
main();
84-
85-
function main() {
86-
var input = getInput();
87-
if (input) {
88-
var content = getOutput(input);
89-
90-
if (argv.file) {
91-
writeFile(argv.file, content);
92-
} else if (argv.output === 'preview') {
93-
preview(content);
94-
} else {
95-
print(content);
96-
}
97-
} else {
98-
error('The input is empty. Try again.');
99-
argv.help();
100-
}
101-
102-
process.exit(0);
103-
}
104-
10593
function getInput() {
10694
if (argv.input === 'file') {
10795
return readFile(argv._[0]);
@@ -153,6 +141,52 @@ function prepareHTML(content) {
153141
.replace('<!--diff-->', content);
154142
}
155143

144+
function postToDiffy(diff, postType) {
145+
var jsonParams = {udiff: diff};
146+
147+
post('http://diffy.org/api/new', jsonParams, function(err, response) {
148+
if (err) {
149+
print(err);
150+
return;
151+
}
152+
153+
if (response.status != 'error') {
154+
print("Link powered by diffy.org:");
155+
print(response.url);
156+
157+
if (postType === 'browser') {
158+
runCmd('open ' + response.url);
159+
} else if (postType === 'pbcopy') {
160+
runCmd('echo "' + response.url + '" | pbcopy');
161+
}
162+
} else {
163+
print("Error: " + message);
164+
}
165+
});
166+
}
167+
168+
function post(url, payload, callback) {
169+
var request = require('request');
170+
171+
request({
172+
url: url,
173+
method: 'POST',
174+
form: payload
175+
})
176+
.on('response', function(response) {
177+
response.on('data', function(body) {
178+
try {
179+
callback(null, JSON.parse(body.toString('utf8')));
180+
} catch (err) {
181+
callback(new Error('could not parse response'));
182+
}
183+
})
184+
})
185+
.on('error', function(err) {
186+
callback(err);
187+
});
188+
}
189+
156190
function prepareJSON(content) {
157191
return JSON.stringify(content);
158192
}
@@ -179,3 +213,26 @@ function runCmd(cmd) {
179213
var childProcess = require('child_process');
180214
return childProcess.execSync(cmd).toString('utf8');
181215
}
216+
217+
/*
218+
* CLI code
219+
*/
220+
221+
var input = getInput();
222+
223+
if (input && argv.diffy) {
224+
postToDiffy(input, argv.diffy);
225+
} else if (input) {
226+
var content = getOutput(input);
227+
228+
if (argv.file) {
229+
writeFile(argv.file, content);
230+
} else if (argv.output === 'preview') {
231+
preview(content);
232+
} else {
233+
print(content);
234+
}
235+
} else {
236+
error('The input is empty. Try again.');
237+
argv.help();
238+
}

0 commit comments

Comments
 (0)