Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code for readability, error handling, and modern JavaScript … #4630

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
122 changes: 66 additions & 56 deletions scripts/region-checker/index.js
@@ -1,75 +1,85 @@
var fs = require('fs');
var path = require('path');
var allowlist = require('./allowlist').allowlist;
const fs = require('fs');
const path = require('path');
const allowlist = require('./allowlist').allowlist;

function checkFile(location) {
var file = fs.readFileSync(location);
var code = file.toString();
var lines = code.split('\n');
var regionMatches = [];
try {
const code = fs.readFileSync(location, 'utf8');
const lines = code.split('\n');
const 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
});
}
});
lines.forEach((line, idx) => {
const matches = line.match(/(us|eu|ap|sa|ca)-\w+-\d+/g);
if (matches) {
regionMatches.push({
file: location,
line: idx,
code: line
});
}
});

return regionMatches;
return regionMatches;
} catch (error) {
console.error(`Error reading file ${location}: ${error}`);
return [];
}
}

function recursiveGetFilesIn(directory, extensions) {
var filenames = [];
async function recursiveGetFilesIn(directory, extensions) {
try {
const filenames = [];

var keys = fs.readdirSync(directory);
const keys = await fs.promises.readdir(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));
for (const key of keys) {
const keyPath = path.join(directory, key);
const stats = await fs.promises.stat(keyPath);
if (stats.isDirectory()) {
filenames.push(...await recursiveGetFilesIn(keyPath, extensions));
} else if (extensions.includes(path.extname(keyPath))) {
filenames.push(keyPath);
}
}
}

return filenames;
return filenames;
} catch (error) {
console.error(`Error reading directory ${directory}: ${error}`);
return [];
}
}

function checkForRegions() {
var libPath = path.join(__dirname, '..', '..', 'lib');
var filePaths = recursiveGetFilesIn(libPath, ['.js']);
var regionMatches = [];
var warnings = [];
const libPath = path.join(__dirname, '..', '..', 'lib');
recursiveGetFilesIn(libPath, ['.js'])
.then(filePaths => {
const regionMatches = [];
const warnings = [];

filePaths.forEach(function(filePath) {
regionMatches = regionMatches.concat(checkFile(filePath));
});
for (const filePath of filePaths) {
regionMatches.push(...checkFile(filePath));
}

regionMatches.forEach(function(match) {
var normalizedPath = match.file.substring(libPath.length);
if (allowlist[normalizedPath] && allowlist[normalizedPath].indexOf(match.line) >= 0) {
return;
}
warnings.push('File: ' + normalizedPath + '\tLine ' + match.line + ':\t' + match.code.trim());
});
for (const match of regionMatches) {
const normalizedPath = match.file.substring(libPath.length);
if (allowlist[normalizedPath] && allowlist[normalizedPath].includes(match.line)) {
continue;
}
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);
if (warnings.length) {
console.error('Hard-coded regions detected. This should only be done if absolutely certain!');
warnings.forEach(warning => {
console.error(warning);
});
process.exit(1);
}
})
.catch(error => {
console.error(`Error checking for regions: ${error}`);
process.exit(1);
});
process.exit(1);
}
}

checkForRegions();
checkForRegions();