cli.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var colorize, diff, fs, tty;
  4. fs = require('fs');
  5. tty = require('tty');
  6. diff = require('./index').diff;
  7. colorize = require('./colorize').colorize;
  8. module.exports = function(argv) {
  9. var data1, data2, json1, json2, options, result;
  10. options = require('dreamopt')(["Usage: json-diff [-vjCk] first.json second.json", "Arguments:", " first.json Old file #var(file1) #required", " second.json New file #var(file2) #required", "General options:", " -v, --verbose Output progress info", " -C, --[no-]color Colored output", " -j, --raw-json Display raw JSON encoding of the diff #var(raw)", " -k, --keys-only Compare only the keys, ignore the differences in values #var(keysOnly)"], argv);
  11. if (options.verbose) {
  12. process.stderr.write((JSON.stringify(options, null, 2)) + "\n");
  13. }
  14. if (options.verbose) {
  15. process.stderr.write("Loading files...\n");
  16. }
  17. data1 = fs.readFileSync(options.file1, 'utf8');
  18. data2 = fs.readFileSync(options.file2, 'utf8');
  19. if (options.verbose) {
  20. process.stderr.write("Parsing old file...\n");
  21. }
  22. json1 = JSON.parse(data1);
  23. if (options.verbose) {
  24. process.stderr.write("Parsing new file...\n");
  25. }
  26. json2 = JSON.parse(data2);
  27. if (options.verbose) {
  28. process.stderr.write("Running diff...\n");
  29. }
  30. result = diff(json1, json2, options);
  31. if (options.color == null) {
  32. options.color = tty.isatty(process.stdout.fd);
  33. }
  34. if (result) {
  35. if (options.raw) {
  36. if (options.verbose) {
  37. process.stderr.write("Serializing JSON output...\n");
  38. }
  39. process.stdout.write(JSON.stringify(result, null, 2));
  40. } else {
  41. if (options.verbose) {
  42. process.stderr.write("Producing colored output...\n");
  43. }
  44. process.stdout.write(colorize(result, {
  45. color: options.color
  46. }));
  47. }
  48. } else {
  49. if (options.verbose) {
  50. process.stderr.write("No diff");
  51. }
  52. }
  53. if (result) {
  54. return process.exit(1);
  55. }
  56. };
  57. }).call(this);