Skip to content

Commit

Permalink
Merge 8f8ff3d into a58a42f
Browse files Browse the repository at this point in the history
  • Loading branch information
vbaranov committed Aug 2, 2019
2 parents a58a42f + 8f8ff3d commit bcd5b22
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
33 changes: 22 additions & 11 deletions helpers/remove-doubled-solidity-version.js
@@ -1,26 +1,37 @@
const constants = require('./constants')
const { SEMICOLON, EMPTY } = require('./constants')
const pragmaSubstr = 'pragma solidity'

/*
* Leaves only 1st pragma solidity instruction and removes others
* Removes all pragma solidity instruction
*/
function removeDoubledSolidityVersion(content) {
const subStr = 'pragma solidity'
//1st pragma solidity declaration
const firstIndex = content.indexOf(subStr)
const lastIndex = firstIndex + content.substr(firstIndex).indexOf(constants.SEMICOLON) + 1
const { firstIndex, lastIndex } = getFirstPragma(content)
const contentPart = content.substr(lastIndex)
let contentFiltered = contentPart
//remove other pragma solidity declarations
const regex = new RegExp(subStr,'gi')
const regex = new RegExp(pragmaSubstr,'gi')
let result
while ( (result = regex.exec(contentPart)) ) {
const start = result.index
const end = start + contentPart.substr(start).indexOf(constants.SEMICOLON) + 1
if (start != firstIndex) contentFiltered = contentFiltered.replace(contentPart.substring(start, end), constants.EMPTY)
const end = start + contentPart.substr(start).indexOf(SEMICOLON) + 1
if (start != firstIndex) contentFiltered = contentFiltered.replace(contentPart.substring(start, end), EMPTY)
}
const finalContent = content.substr(0, lastIndex) + contentFiltered

return finalContent
return contentFiltered
}

module.exports = removeDoubledSolidityVersion
/*
* Gets 1st pragma solidity instruction from content
*/
function getFirstPragma(content) {
const firstIndex = content.indexOf(pragmaSubstr)
const lastIndex = firstIndex + content.substr(firstIndex).indexOf(SEMICOLON) + 1
const pragma = content.substr(0, lastIndex)
return { pragma, firstIndex, lastIndex}
}

module.exports = {
removeDoubledSolidityVersion,
getFirstPragma,
}
4 changes: 3 additions & 1 deletion index.js
Expand Up @@ -5,7 +5,7 @@ const variables = require('./helpers/variables')
const log = require('./helpers/logger')
const constants = require('./helpers/constants')
const cleanPath = require('./helpers/clean-path')
const removeDoubledSolidityVersion = require('./helpers/remove-doubled-solidity-version')
const { removeDoubledSolidityVersion, getFirstPragma } = require('./helpers/remove-doubled-solidity-version')
const replaceAllImportsRecursively = require('./helpers/replace-all-imports-recursively')

flatten()
Expand All @@ -29,8 +29,10 @@ async function getSourceFiles(dir, path) {
}

async function replaceImports(inputFileContent, dir) {
const { pragma: firstPragma } = getFirstPragma(inputFileContent)
let outputFileContent = await replaceAllImportsRecursively(inputFileContent, dir)
outputFileContent = removeDoubledSolidityVersion(outputFileContent)
outputFileContent = firstPragma + outputFileContent
if (!fs.existsSync(variables.outDir)) fs.mkdirSync(variables.outDir)
const fileName = `${variables.flatContractPrefix}_flat.sol`
const filePath = `${variables.outDir}/${fileName}`
Expand Down

0 comments on commit bcd5b22

Please sign in to comment.