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

Add a feature to create a final used/unused selector report #18

Open
paceaux opened this issue Sep 18, 2023 · 1 comment
Open

Add a feature to create a final used/unused selector report #18

paceaux opened this issue Sep 18, 2023 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@paceaux
Copy link
Owner

paceaux commented Sep 18, 2023

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);
}
@paceaux paceaux added enhancement New feature or request good first issue Good for newcomers labels Sep 18, 2023
@paceaux
Copy link
Owner Author

paceaux commented Sep 20, 2023

Note: this could be a "report" flag, in case we wanted to add other summarizing / learning that comes after the pages.json is generated.

Reporting could include:

  • list of used/unused selectors
  • list of pages with matches
  • element details?

@paceaux paceaux added this to the 2.3.0 milestone Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant