Skip to content

Instantly share code, notes, and snippets.



Created Jan 23, 2018
What would you like to do?
Finds unused react components in a project
const { execSync } = require('child_process');
const directory = 'src';
const findOut = execSync('find -L src').toString();
const filenames = findOut.trim().split('\n')
.filter(name => name.endsWith('.js'))
.map(name => name.replace('.js', '').replace('src/', '').replace(/\/index$/, ''));
console.log('Found', filenames.length, 'files in', directory);
const missing = [];
for (const name of filenames) {
try {
execSync(`grep -Ril "'${name}'" ${directory}`);
} catch (e) {
try {
const localName = `/${name.split('/').pop()}`;
execSync(`grep -Ril "${localName}';" ${directory}`);
} catch (e) {
console.log('Cant find anything that uses', `'${name}'`);
console.log('total missing: ', missing.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment