Skip to content

Instantly share code, notes, and snippets.

@kaievns
Created January 23, 2018 01:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaievns/97a446b2bed01263ab086f2749f49295 to your computer and use it in GitHub Desktop.
Save kaievns/97a446b2bed01263ab086f2749f49295 to your computer and use it in GitHub Desktop.
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) {
missing.push(name);
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