var fs = require('fs');
var path = require('path');
var Ajv = require('ajv');
var ajv = new Ajv({ allErrors: true });
var hasErrors = false;

function jsonDiff(actual, expected) {
  var actualLines = actual.split(/\n/);
  var expectedLines = expected.split(/\n/);

  for (var i = 0; i < actualLines.length; i++) {
    if (actualLines[i] !== expectedLines[i]) {
      return [
        '#' + i + '\x1b[0m',
        '    Actual:   ' + actualLines[i],
        '    Expected: ' + expectedLines[i]
      ].join('\n');
    }
  }
}

function checkStyle(filename) {
  var actual = fs.readFileSync(filename, 'utf-8').trim();
  var expected = JSON.stringify(JSON.parse(actual), null, 2);

  if (actual === expected) {
    console.log('\x1b[32m  Style – OK\x1b[0m');
  } else {
    hasErrors = true;
    console.log('\x1b[31m  Style – Error on line ' + jsonDiff(actual, expected));
  }
}

function checkSchema(dataFilename) {
  var schemaFilename = '../compat-data.schema.json';
  var valid = ajv.validate(
    require(schemaFilename),
    require(dataFilename)
  );

  if (valid) {
    console.log('\x1b[32m  JSON schema – OK\x1b[0m');
  } else {
    hasErrors = true;
    console.log('\x1b[31m  JSON schema – ' + ajv.errors.length + ' error(s)\x1b[0m');
    console.log('   ' + ajv.errorsText(ajv.errors, {
      separator: '\n    ',
      dataVar: 'item'
    }));
  }
}


function load(...files) {
  for (let file of files) {
    if (file.indexOf(__dirname) !== 0) {
      file = path.resolve(__dirname, '..', file);
    }

    if (fs.statSync(file).isFile()) {
      if (path.extname(file) === '.json') {
        console.log(file.replace(path.resolve(__dirname, '..') + path.sep, ''));
        checkStyle(file)
        checkSchema(file);
      }

      continue;
    }

    let subFiles = fs.readdirSync(file).map((subfile) => {
      return path.join(file, subfile);
    });

    load(...subFiles);
  }
}

if (process.argv[2]) {
  load(process.argv[2])
} else {
  load(
    'api',
    'css',
    'html',
    'http',
    'javascript',
    'test',
    'webextensions'
  );
}

if (hasErrors) {
  process.exit(1);
}

@@ -862,24 +862,436 @@
 ected));%0A  %7D
+%0A%0A  if (actual.includes(%22//bugzilla.mozilla.org/show_bug.cgi?id=%22) // like https://bugzil.la/1000000%0A    %7C%7C actual.includes(%22//bugs.chromium.org/%22)) %7B // like https://crbug.com/100000%0A    hasErrors = true;%0A    console.log('%5Cx1b%5B33m  Found shortenable url.%5Cx1b%5B0m');%0A  %7D%0A  if (actual.includes(%22href=%5C%5C%5C%22%22)) %7B%0A    hasErrors = true;%0A    console.log('%5Cx1b%5B33m  You can replace %5C%5C%5C%22 with %5C' for %3Ca%3E tag.%5Cx1b%5B0m');%0A  %7D
 %0A%7D%0A%0Afunction

Add two test to test/lint.js