index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. var fs = require('fs');
  2. var path = require('path');
  3. var allowlist = require('./allowlist').allowlist;
  4. function checkFile(location) {
  5. var file = fs.readFileSync(location);
  6. var code = file.toString();
  7. var lines = code.split('\n');
  8. var regionMatches = [];
  9. lines.forEach(function(line, idx) {
  10. var matches = line.match(/(us|eu|ap|sa|ca)-\w+-\d+/g);
  11. if (matches) {
  12. regionMatches.push({
  13. file: location,
  14. line: idx,
  15. code: line
  16. });
  17. }
  18. });
  19. return regionMatches;
  20. }
  21. function recursiveGetFilesIn(directory, extensions) {
  22. var filenames = [];
  23. var keys = fs.readdirSync(directory);
  24. for (var i = 0, iLen = keys.length; i < iLen; i++) {
  25. // check if it is a file
  26. var keyPath = path.join(directory, keys[i]);
  27. var stats = fs.statSync(keyPath);
  28. if (stats.isDirectory()) {
  29. filenames = filenames.concat(
  30. recursiveGetFilesIn(keyPath, extensions)
  31. );
  32. continue;
  33. }
  34. if (extensions.indexOf(path.extname(keyPath)) >= 0) {
  35. filenames.push(path.join(keyPath));
  36. }
  37. }
  38. return filenames;
  39. }
  40. function checkForRegions() {
  41. var libPath = path.join(__dirname, '..', '..', 'lib');
  42. var filePaths = recursiveGetFilesIn(libPath, ['.js']);
  43. var regionMatches = [];
  44. var warnings = [];
  45. filePaths.forEach(function(filePath) {
  46. regionMatches = regionMatches.concat(checkFile(filePath));
  47. });
  48. regionMatches.forEach(function(match) {
  49. var normalizedPath = match.file.substring(libPath.length);
  50. if (allowlist[normalizedPath] && allowlist[normalizedPath].indexOf(match.line) >= 0) {
  51. return;
  52. }
  53. warnings.push('File: ' + normalizedPath + '\tLine ' + match.line + ':\t' + match.code.trim());
  54. });
  55. if (warnings.length) {
  56. console.error('Hard-coded regions detected. This should only be done if absolutely certain!');
  57. warnings.forEach(function(warning) {
  58. console.error(warning);
  59. });
  60. process.exit(1);
  61. }
  62. }
  63. checkForRegions();