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