Skip to content

Instantly share code, notes, and snippets.

@v-stickykeys
Last active April 4, 2023 11:16
Show Gist options
  • Save v-stickykeys/3e5da9eecf2d91ae697744124bb242d7 to your computer and use it in GitHub Desktop.
Save v-stickykeys/3e5da9eecf2d91ae697744124bb242d7 to your computer and use it in GitHub Desktop.
Solidity pragma version regex
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