Last active
April 4, 2023 11:16
-
-
Save v-stickykeys/3e5da9eecf2d91ae697744124bb242d7 to your computer and use it in GitHub Desktop.
Solidity pragma version regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function getSolcVersion(filePath: string): Promise<string> { | |
const rl = readline.createInterface({ | |
input: fs.createReadStream(filePath), | |
crlfDelay: Infinity | |
}); | |
let versionFound = false; | |
for await (const line of rl) { | |
if (line.startsWith('pragma')) { | |
versionFound = true; | |
const regex = /^pragma solidity [\^\~\>\<]?=?(?<version>[0-9\.]*);/; | |
const groups = line.match(regex).groups; | |
return groups.version; | |
} | |
}; | |
if (!versionFound) throw Error('No pragma solidity version found'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment