From be3af0c3b3d20371546c4b38e3e388eb4c7a01a6 Mon Sep 17 00:00:00 2001 From: 0xGorilla <84932007+0xGorilla@users.noreply.github.com> Date: Wed, 10 Jan 2024 18:29:42 +0100 Subject: [PATCH] fix: check if path in directory (#8) --- src/utils.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 75501fb..9bbe4f3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -48,12 +48,20 @@ export async function getProjectCompiledSources(rootPath: string, contractsPath: return new ASTReader() .read(compiledFiles.data, ASTKind.Any, compiledFiles.files) // avoid processing files that are not in the specified directory, e.g. node modules or other imported files - .filter(sourceUnit => sourceUnit.absolutePath.startsWith(contractsPath)); + .filter(sourceUnit => isFileInDirectory(contractsPath, sourceUnit.absolutePath)); } - export async function getFileCompiledSource(filePath: string): Promise { const compiledFile = await compileSol(filePath, 'auto'); return new ASTReader() .read(compiledFile.data, ASTKind.Any, compiledFile.files)[0] }; + +export function isFileInDirectory(directory: string, filePath: string): boolean { + // Convert both paths to absolute and normalize them + const absoluteDirectoryPath = path.resolve(directory) + path.sep; + const absoluteFilePath = path.resolve(filePath); + + // Check if the file path starts with the directory path + return absoluteFilePath.startsWith(absoluteDirectoryPath); +} \ No newline at end of file