Skip to content

Instantly share code, notes, and snippets.

@nathakits
Last active January 15, 2020 03:51
Show Gist options
  • Save nathakits/9525af5443545edd89b4df7d6f0e7719 to your computer and use it in GitHub Desktop.
Save nathakits/9525af5443545edd89b4df7d6f0e7719 to your computer and use it in GitHub Desktop.
Find image tags with no alt prop
const fs = require('fs');
const glob = require('glob');
const fileType = 'pages/**/*.vue';
const altString = /alt=/g;
const vImgString = /v-img|v-carousel-item/g;
function matchString(data, string) {
const alt = data.match(string);
if (alt) {
return alt.length;
} else {
return '0';
}
}
glob(fileType, function (err, files) {
files.forEach(file => {
const data = fs.readFileSync(file, 'utf8');
if (err) {
console.log(err);
} else {
if (matchString(data, vImgString) > 0) {
console.log(` `);
console.log(`${file}`);
console.log(`-------------`);
console.log(`imgs: ${matchString(data, vImgString)}`);
console.log(`altTags: ${matchString(data, altString)}`);
if (matchString(data, altString) === matchString(data, vImgString)) {
console.log("\x1b[32m%s\x1b[0m", `Pass ✔️`);
} else {
console.log("\x1b[31m%s\x1b[0m", `Failed`);
}
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment