Created
November 4, 2022 19:34
-
-
Save iambriansreed/2a7ca33095fc48fff01531bad1f15fb6 to your computer and use it in GitHub Desktop.
Find files not imported.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { resolve } from 'path'; | |
import { promises, readFileSync } from 'fs'; | |
const { readdir } = promises; | |
const ignoreFolders = ['__generated__', '__tests__']; | |
const notImported: string[] = ['/webpack.config', '/globals.d']; | |
const fileContent: Record<string, string> = {}; | |
(async () => { | |
const files = [ | |
resolve('webpack.config.js'), | |
...(await getFiles('./src', false)), | |
...(await getFiles('./src/components')), | |
...(await getFiles('./src/shared')), | |
...(await getFiles('./src/routes')), | |
]; | |
const data: Data[] = files.map((file) => ({ | |
file, | |
name: file.slice(file.lastIndexOf('/'), file.lastIndexOf('.')), | |
})); | |
console.log('\x1b[36m%s\x1b[0m', `\n${data.length} files found\n`); //cyan | |
const isImported: Data[] = []; | |
const isNotImported: Data[] = []; | |
const checkIfFilesAreImported = async (index: number) => { | |
const { name } = data[index]; | |
if (notImported.includes(name)) return; | |
const found = data.some(({ file }) => readFile(file).includes(name)); | |
if (found) isImported.push(data[index]); | |
else isNotImported.push(data[index]); | |
}; | |
await loader(data.length, checkIfFilesAreImported); | |
console.log(''); | |
console.log('\x1b[36m%s\x1b[0m', `isImported: ${isImported.length}`); //cyan | |
console.log('\x1b[33m%s\x1b[0m', `isNotImported: ${isNotImported.length}`); //yellow | |
console.log(` | |
${isNotImported | |
.map((data) => { | |
return data.file.slice(data.file.indexOf('src')); | |
}) | |
.join('\n')} | |
`); | |
})(); | |
type Data = { file: string; name: string }; | |
async function loader( | |
callbackCount: number, | |
callback: (index: number) => Promise<void>, | |
barLength = 50, | |
) { | |
for (let i = 0; i < callbackCount; i++) { | |
const percent = i / callbackCount; | |
const loadedLength = percent * barLength; | |
const dots = '.'.repeat(loadedLength); | |
const empty = ' '.repeat(barLength - loadedLength); | |
/* use `process.stdout.write` as it doesn't print newline character */ | |
/* \r clear the current line and then print the other characters making it looks like it refresh */ | |
process.stdout.write(`\r[${dots}${empty}] ${Math.round(percent * 100)}%`); | |
await callback(i); | |
} | |
console.log(''); | |
} | |
async function getFiles(dir: string, recursive = true): Promise<string[]> { | |
const dirents = await readdir(dir, { withFileTypes: true }); | |
const files = dirents.map(async (dirent) => { | |
const res = resolve(dir, dirent.name); | |
const isDirectory = dirent.isDirectory(); | |
if (ignoreFolders.some((ignoreFolder) => res.endsWith(ignoreFolder))) | |
return []; | |
if (!recursive && isDirectory) return []; | |
return isDirectory ? await getFiles(res) : [res]; | |
}); | |
return (await Promise.all(files)).flatMap((files) => files); | |
} | |
function readFile(path: string): string { | |
if (!(path in fileContent)) fileContent[path] = readFileSync(path, 'utf-8'); | |
return fileContent[path]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment