2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-13 22:18:24 +00:00
iconify/packages/library-builder/src/scan.ts

40 lines
884 B
TypeScript

import { promises as fs } from 'fs';
/**
* Scan folder
*/
export async function scanFolder(
rootDir: string,
fileExtensions: string[] = ['ts'],
includeExtension: boolean = false
): Promise<string[]> {
const results: string[] = [];
async function scan(dir: string) {
const files = await fs.readdir(rootDir + dir);
for (let i = 0; i < files.length; i++) {
const file = files[i];
if (file.slice(0, 1) === '.') {
continue;
}
const filename = dir + '/' + file;
const stat = await fs.lstat(rootDir + filename);
if (stat.isDirectory()) {
await scan(filename);
continue;
}
const parts = filename.split('.');
const ext = parts.pop()!;
if (fileExtensions.indexOf(ext) !== -1) {
results.push(includeExtension ? filename : parts.join('.'));
}
}
}
await scan('');
results.sort((a, b) => a.localeCompare(b));
return results;
}