Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: read remappings from foundry config #29

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,40 @@ export function isFileInDirectory(directory: string, filePath: string): boolean
}

export async function getRemappings(rootPath: string): Promise<string[]> {
// First try the remappings.txt file
try {
const filePath = path.join(rootPath, 'remappings.txt');
const fileContent = await fs.readFile(filePath, 'utf8');
return await getRemappingsFromFile(path.join(rootPath, 'remappings.txt'));
} catch (e) {
// If the remappings file does not exist, try foundry.toml
try {
return await getRemappingsFromConfig(path.join(rootPath, 'foundry.toml'));
} catch {
return [];
}
}
}

return fileContent
.split('\n')
export async function getRemappingsFromFile(remappingsPath: string): Promise<string[]> {
const remappingsContent = await fs.readFile(remappingsPath, 'utf8');

return remappingsContent
.split('\n')
.map((line) => line.trim())
.filter((line) => line.length)
.map((line) => (line.slice(-1) === '/' ? line : line + '/'));
}

export async function getRemappingsFromConfig(foundryConfigPath: string): Promise<string[]> {
const foundryConfigContent = await fs.readFile(foundryConfigPath, 'utf8');
const regex = /\n+remappings[\s|\n]*\=[\s\n]*\[\n*\s*(?<remappings>[a-zA-Z-="'\/_,\n\s]+)/;
const matches = foundryConfigContent.match(regex);
if (matches) {
return matches
.groups!.remappings.split('\n')
.map((line) => line.trim())
.filter((line) => line.length)
.map((line) => (line.slice(-1) === '/' ? line : line + '/'));
} catch (e) {
.map((line) => line.replace(',', ''));
} else {
return [];
}
}
Expand Down
Loading