-
-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Labels
enhancementNew feature or requestNew feature or requestgood first issueGood for newcomersGood for newcomers
Milestone
Description
There should be more information added to the CLI, and the pages.json, about which selectors are ultimately used/unused.
I wrote a separate js file for determining this, but it would be nice if this were a core feature.
This is what I wrote that generally works.
const fs = require('fs');
if (process.argv.length <= 2) {
console.error('needed name of file');
process.exit(1);
}
const fileName = process.argv[2];
const outputFileName = fileName.replace('.pages.json', '');
function getResultsData(fileName) {
if (!fileName) return null;
try {
const file = fs.readFileSync(fileName, 'utf8');
const fileData = JSON.parse(file);
return fileData;
} catch (fileError) {
console.error(fileError);
}
}
function filterUsedAndUnused(pagesWithSelector) {
if (!pagesWithSelector) throw new Error('not an object');
const unusedSet = new Set();
const usedSet = new Set();
pagesWithSelector.forEach(page => {
const { unusedSelectors, usedSelectors} = page;
unusedSelectors.forEach(unusedSelector => unusedSet.add(unusedSelector));
usedSelectors.forEach(usedSelector => usedSet.add(usedSelector));
});
unusedSet.forEach((selector) => {
if (usedSet.has(selector)) {
unusedSet.delete(selector);
}
});
return {unusedSet, usedSet};
}
function outputFile(data) {
const json = JSON.stringify(data, null, 2);
try {
fs.writeFile(`${outputFileName}.filtered.json`, json, writeError => {
if (writeError) {
console.error(writeError)
}
})
} catch (outputErr) {
console.error(outputErr);
}
}
try {
const searchResults = getResultsData(fileName);
const { pagesWithSelector } = searchResults;
const {unusedSet, usedSet} = filterUsedAndUnused(pagesWithSelector);
const convertedUsed = [...usedSet];
const convertedUnused = [...unusedSet];
const totalUsedSize = convertedUsed.length;
const totalUnusedSize = convertedUnused.length;
const results = {
totalUsed : totalUsedSize,
totalUnused : totalUnusedSize,
totalSelectors : totalUsedSize + totalUnusedSize,
used: convertedUsed,
unused: convertedUnused
};
outputFile(results);
console.log(`\n=====UNUSED: ${totalUnusedSize}=====`);
console.log(unusedSet);
console.log(`\n=====USED: ${totalUsedSize}=====`);
console.log(usedSet);
} catch (readErr) {
console.error(readError);
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requestgood first issueGood for newcomersGood for newcomers