Skip to content

Instantly share code, notes, and snippets.

@upikoth
Created February 25, 2022 15:20
Show Gist options
  • Save upikoth/e8019fef0522b6f852aa7400c961a9ba to your computer and use it in GitHub Desktop.
Save upikoth/e8019fef0522b6f852aa7400c961a9ba to your computer and use it in GitHub Desktop.
Генерирует файл с путями ко всем файлам с указанными расширениями. Удобно, когда добавляешь линтер в большой проект.
const mainFolder = './';
const mainExtensions = ['.js'];
const mainExcludeDirectories = ['node_modules'];
const fs = require('fs');
async function isDirectory(path) {
return new Promise((res, rej) => {
fs.stat(path, (error, stats) => {
if (error) {
rej(error);
}
res(stats.isDirectory());
});
});
}
async function getFilePaths(folder, extensions, excludeDirectories) {
if (excludeDirectories.some(dir => folder.includes(dir))) {
return [];
}
const filePaths = [];
const promises = fs.readdirSync(folder).map(
name =>
new Promise((res, rej) => {
const pathToCheck = `${folder}${name}`;
isDirectory(pathToCheck)
.then(async isDir => {
if (isDir) {
const nextDirectory = `${pathToCheck}/`;
const filesFromNextDirectory = await getFilePaths(
nextDirectory,
extensions,
excludeDirectories,
);
filePaths.push(...filesFromNextDirectory);
res();
}
if (extensions.some(ext => name.endsWith(ext))) {
const fullFilePath = `${folder}${name}`;
filePaths.push(fullFilePath);
}
res();
})
.catch(error => {
rej(error);
});
}),
);
await Promise.all(promises);
return filePaths;
}
async function writeFilePaths(fileName) {
try {
const resultPaths = (await getFilePaths(mainFolder, mainExtensions, mainExcludeDirectories)).map(el =>
el.replace('./', '/'),
);
fs.writeFileSync(fileName, resultPaths.join('\n'));
} catch (error) {
fs.writeFileSync(fileName, `${error}`);
}
}
writeFilePaths('.eslintignore');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment