Skip to content

Instantly share code, notes, and snippets.

@minademian
Last active June 14, 2018 09:14
Show Gist options
  • Save minademian/368c57cb0f64d392ece91178ea875a3d to your computer and use it in GitHub Desktop.
Save minademian/368c57cb0f64d392ece91178ea875a3d to your computer and use it in GitHub Desktop.
Internationalization project - automating the 1) process of checking if existing copy keys are being used, 2) if keys conform to BEM
{
"presets": ["es2016"]
}
const yaml = require('js-yaml');
const fs = require('fs');
const exec = require('child_process').exec;
let doc;
const map = new Map();
const copyRepoDir = '<redacted>';
function getListing(callback) {
let child = exec(`ls ${copyRepoDir}`, function(err, stdout, stderr) {
let output = stdout.split('\n');
callback(output);
});
}
getListing(function(output) {
let cleaned = output.filter(key => key.length > 0);
Object.keys(cleaned).forEach(key => {
if (key !== "") {
let filename = cleaned[key];
console.log(`Examining ${filename}...`);
try {
doc = yaml.safeLoad(fs.readFileSync(`${copyRepoDir}/${filename}`, 'utf8'));
} catch (e) {
console.log(e);
}
Object.keys(doc).forEach(key => {
// H/t Samir and https://regex101.com/r/MTuw3Y/1, JavaScript flavor
let regex = RegExp('^((([a-z0-9]+(_[a-z0-9]+)?)+((--)([a-z0-9]+(-[a-z0-9]+)?)+)?)|(([a-z0-9]+(_[a-z0-9]+)?)+__([a-z0-9]+(_[a-z0-9]+)?)+((--)([a-z0-9]+(-[a-z0-9]+)?)+)?))$');
let result = regex.test(key);
if (!result) {
console.log(`${key} is not valid BEM`);
}
});
console.log('----------');
}
});
});
console.log(`Done checking components in ${copyRepoDir}`);
const exec = require('child_process').exec;
const data = {
'key': '...'
...
};
const map = new Map();
Object.keys(data).forEach(key => {
map.set(key, data[key]);
});
const appDir = '<redacted>';
map.forEach(function(item, index) {
let command = `grep -rnwl '${index}' ${appDir}/**/*.html`;
exec(command, outputString, function (err, stdout, stderr) {
if (err && err.code === 1) {
count = 0; // Status 1 means no match, so we don't have to parse anything.
console.log(`${index} not found.`);
// console.log(`stderr: ${stderr}`);
} else {
console.log(`${index} was found, ${stdout}`);
}
});
});
{
"name": "speedo",
"version": "0.0.1",
"description": "",
"main": "checker.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files",
"start": "npm run build && node dist/checker.js"
},
"author": "Mina Demian",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2016": "^6.24.1",
"rimraf": "^2.6.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment