Skip to content

Commit

Permalink
refactor file-processor; factor out get-file-list
Browse files Browse the repository at this point in the history
  • Loading branch information
hashcacher committed Aug 8, 2024
1 parent 26afbe6 commit e3b68de
Showing 1 changed file with 54 additions and 50 deletions.
104 changes: 54 additions & 50 deletions src/core/file-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,12 @@ export const DEFAULT_IGNORES = [
'**/temp',
];

export async function processFiles(
options: ProcessOptions,
): Promise<FileInfo[]> {
export async function getFileList(options: ProcessOptions): Promise<string[]> {
const basePath = path.resolve(options.path ?? '.');
if (!(await fs.pathExists(basePath))) {
throw new Error(`Path does not exist: ${basePath}`);
}

const fileCache = new FileCache(options.cachePath ?? DEFAULT_CACHE_PATH);

const gitignorePath = options.gitignore ?? DEFAULT_GITIGNORE;

const ig = ignore().add(DEFAULT_IGNORES);
Expand All @@ -113,9 +109,6 @@ export async function processFiles(
caseSensitiveMatch: options.caseSensitive,
};

const fileInfos: FileInfo[] = [];
const cachePromises: Promise<void>[] = [];

const filters = options.filter || [];

const normalizedFilters =
Expand Down Expand Up @@ -152,6 +145,7 @@ export async function processFiles(

return new Promise((resolve, reject) => {
const globStream = fastGlob.stream(globPattern, globOptions);
const filePaths: string[] = [];

globStream.on('data', (filePath) => {
const filePathStr = path.resolve(filePath.toString());
Expand All @@ -178,52 +172,62 @@ export async function processFiles(
return;
}

cachePromises.push(
(async () => {
try {
const cached = await fileCache.get(normalizePath(filePathStr));
if (cached) {
fileInfos.push(cached);
return;
}

const stats = await fs.stat(filePathStr);
if (!stats.isFile()) return;

const buffer = await fs.readFile(filePathStr);
if (await isBinaryFile(buffer)) return;

const result = await pool.run({
filePath: filePathStr,
suppressComments: options.suppressComments,
});

if (result) {
await fileCache.set(
normalizePath(filePathStr),
result as FileInfo,
);
fileInfos.push(result as FileInfo);
}
} catch (error) {
console.error(`Error processing file ${filePathStr}:`, error);
}
})(),
);
filePaths.push(filePathStr);
});

globStream.on('end', async () => {
try {
await Promise.all(cachePromises);
fileInfos.sort((a, b) => a.path.localeCompare(b.path));
await fileCache.flush();
resolve(fileInfos);
} catch (error) {
console.error('Error during file processing or cache flushing:', error);
reject(error instanceof Error ? error : new Error(String(error)));
}
globStream.on('end', () => {
resolve(filePaths);
});

globStream.on('error', reject);
});
}

export async function processFiles(
options: ProcessOptions,
): Promise<FileInfo[]> {
const basePath = path.resolve(options.path ?? '.');
const fileCache = new FileCache(options.cachePath ?? DEFAULT_CACHE_PATH);

const filePaths = await getFileList(options);

const fileInfos: FileInfo[] = [];
const cachePromises: Promise<void>[] = [];

for (const filePathStr of filePaths) {
cachePromises.push(
(async () => {
try {
const cached = await fileCache.get(normalizePath(filePathStr));
if (cached) {
fileInfos.push(cached);
return;
}

const stats = await fs.stat(filePathStr);
if (!stats.isFile()) return;

const buffer = await fs.readFile(filePathStr);
if (await isBinaryFile(buffer)) return;

const result = await pool.run({
filePath: filePathStr,
suppressComments: options.suppressComments,
});

if (result) {
await fileCache.set(normalizePath(filePathStr), result as FileInfo);
fileInfos.push(result as FileInfo);
}
} catch (error) {
console.error(`Error processing file ${filePathStr}:`, error);
}
})(),
);
}

await Promise.all(cachePromises);
fileInfos.sort((a, b) => a.path.localeCompare(b.path));
await fileCache.flush();
return fileInfos;
}

0 comments on commit e3b68de

Please sign in to comment.