Skip to content

Commit

Permalink
fix: check if path in directory
Browse files Browse the repository at this point in the history
  • Loading branch information
0xGorilla committed Jan 10, 2024
1 parent 48fe40c commit 0512734
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SourceUnit> {
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);
}

0 comments on commit 0512734

Please sign in to comment.