Merge pull request #28 from poanetwork/import-position-fix
(Fix) calculation of import position for some cases
This commit is contained in:
commit
a58a42fa23
|
@ -1,6 +1,6 @@
|
|||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
let decomment = require('decomment')
|
||||
const decomment = require('decomment')
|
||||
const findFile = require('./find-file')
|
||||
const constants = require('./constants')
|
||||
|
||||
|
@ -9,7 +9,6 @@ const constants = require('./constants')
|
|||
*/
|
||||
function findAllImportPaths(dir, content) {
|
||||
return new Promise(async (resolve) => {
|
||||
//strip comments from content
|
||||
content = decomment(content, {safe: true})
|
||||
let allImports = []
|
||||
const regex = new RegExp(constants.IMPORT,'gi')
|
||||
|
|
|
@ -19,34 +19,30 @@ async function replaceAllImportsInCurrentLayerInner(i, importObjs, updatedFileCo
|
|||
return resolve(updatedFileContent)
|
||||
}
|
||||
|
||||
// console.log(importObjs)
|
||||
// console.log(dir)
|
||||
let importObj = importObjs[i]
|
||||
importObj = updateImportObjectLocationInTarget(importObj, updatedFileContent)
|
||||
const { alias, contractName, startIndex, endIndex } = importObj
|
||||
let { dependencyPath } = importObj
|
||||
const { importedSrcFiles } = variables
|
||||
let _updatedFileContent
|
||||
|
||||
//replace contracts aliases
|
||||
if (contractName) {
|
||||
_updatedFileContent = updatedFileContent.replace(alias + constants.DOT, contractName + constants.DOT)
|
||||
if (importObj.contractName) {
|
||||
_updatedFileContent = updatedFileContent.replace(importObj.alias + constants.DOT, importObj.contractName + constants.DOT)
|
||||
} else {
|
||||
_updatedFileContent = updatedFileContent
|
||||
}
|
||||
|
||||
let { dependencyPath } = importObj
|
||||
dependencyPath = cleanPath(dependencyPath)
|
||||
let isAbsolutePath = !dependencyPath.startsWith(constants.DOT)
|
||||
let filePath = isAbsolutePath ? dependencyPath : (dir + dependencyPath)
|
||||
filePath = cleanPath(filePath)
|
||||
|
||||
const importStatement = updatedFileContent.substring(startIndex, endIndex)
|
||||
importObj = updateImportObjectLocationInTarget(importObj, _updatedFileContent)
|
||||
const importStatement = _updatedFileContent.substring(importObj.startIndex, importObj.endIndex)
|
||||
const fileBaseName = path.basename(filePath)
|
||||
const fileExists = fs.existsSync(filePath, fs.F_OK)
|
||||
if (fileExists) {
|
||||
log.info(`${filePath} SOURCE FILE WAS FOUND`)
|
||||
const importedFileContentUpdated = await changeRelativePathToAbsolute(filePath)
|
||||
//const importedFileContentUpdated = await replaceRelativeImportPaths(path.dirname(dependencyPath) + constants.SLASH, importedFileContent)
|
||||
if (!importedSrcFiles.hasOwnProperty(fileBaseName)) {
|
||||
importedSrcFiles[fileBaseName] = importedFileContentUpdated
|
||||
if (importedFileContentUpdated.includes(constants.IS)) {
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
function updateImportObjectLocationInTarget(importObj, content) {
|
||||
const startIndexNew = content.indexOf(importObj.fullImportStatement)
|
||||
const endIndexNew = startIndexNew - importObj.startIndex + importObj.endIndex
|
||||
importObj.startIndex = startIndexNew
|
||||
importObj.endIndex = endIndexNew
|
||||
if (content.includes(importObj.fullImportStatement)) {
|
||||
const startIndexNew = content.indexOf(importObj.fullImportStatement)
|
||||
const endIndexNew = startIndexNew - importObj.startIndex + importObj.endIndex
|
||||
importObj.startIndex = startIndexNew
|
||||
importObj.endIndex = endIndexNew
|
||||
} else {
|
||||
importObj.startIndex = 0
|
||||
importObj.endIndex = 0
|
||||
}
|
||||
return importObj
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "solidity-flattener",
|
||||
"version": "3.0.0",
|
||||
"version": "3.0.1",
|
||||
"description": "Combine solidity files to one flat file",
|
||||
"scripts": {
|
||||
"start": "node index.js",
|
||||
|
|
Loading…
Reference in New Issue