This commit is contained in:
viktor 2017-11-12 20:48:23 +03:00
parent ef9b6245a8
commit 6b69f175a9
3 changed files with 84 additions and 51 deletions

View File

@ -20,55 +20,78 @@ function byName(dir, fileName, cb) {
});
}
function byNameAndReplace(dir, fileName, updatedFileContent, importStatement, cb) {
function byNameAndReplace(dir, filePath, updatedFileContent, importStatement, cb) {
glob(dir + "/**/*.sol", function(err, srcFiles) {
if (err) return console.log(err.message);
var importIsReplacedBefore = false;
for (var j = 0; j < srcFiles.length; j++) {
if (path.basename(srcFiles[j]) == fileName) {
if (!variables.importedSrcFiles.hasOwnProperty(path.basename(srcFiles[j]))) {
var fileContent = fs.readFileSync(srcFiles[j], "utf8");
if (fileContent.indexOf(" is ") > -1) {
updatedFileContent = updatedFileContent.replace(importStatement, fileContent);
} else {
//updatedFileContent = updatedFileContent.replace(importStatement, fileContent);
updatedFileContent = updatedFileContent.replace(importStatement, "");
updatedFileContent = fileContent + updatedFileContent;
}
variables.importedSrcFiles[path.basename(srcFiles[j])] = fileContent;
cb(updatedFileContent);
return;
} else {
updatedFileContent = updatedFileContent.replace(importStatement, "");
//issue #2.
if (updatedFileContent.indexOf(variables.importedSrcFiles[path.basename(dir + importObj.dependencyPath)] > -1)
&& updatedFileContent.indexOf("import ") == -1) {
var fileContent = fs.readFileSync(srcFiles[j], "utf8");
updatedFileContent = updatedFileContent.replace(variables.importedSrcFiles[path.basename(dir + importObj.dependencyPath)], "");
updatedFileContent = fileContent + updatedFileContent;
}
importIsReplacedBefore = true;
}
break;
}
}
if (importIsReplacedBefore) {
updatedFileContent = updatedFileContent.replace(importStatement, "");
cb(updatedFileContent);
} else {
if (dir.indexOf("/") > -1) {
dir = dir.substring(0, dir.lastIndexOf("/"));
byNameAndReplace(dir, fileName, updatedFileContent, importStatement, cb);
} else {
byNameAndReplaceInner(importStatement, updatedFileContent, dir, filePath, srcFiles, 0, cb, function() {
if (importIsReplacedBefore) {
updatedFileContent = updatedFileContent.replace(importStatement, "");
cb(updatedFileContent);
} else {
if (dir.indexOf("/") > -1) {
dir = dir.substring(0, dir.lastIndexOf("/"));
byNameAndReplace(dir, filePath, updatedFileContent, importStatement, cb);
} else {
updatedFileContent = updatedFileContent.replace(importStatement, "");
cb(updatedFileContent);
}
}
}
})
});
}
function byNameAndReplaceInner(importStatement, updatedFileContent, dir, filePath, srcFiles, j, cb, cbInner) {
if (j >= srcFiles.length) return cbInner()
const findAllImportPaths = require("./find-all-import-paths.js");
let isAbsolutePath = filePath.indexOf(".") != 0
if (isAbsolutePath && srcFiles[j].indexOf(filePath) > -1) {
if (!variables.importedSrcFiles.hasOwnProperty(path.basename(srcFiles[j]))
|| fs.existsSync(filePath)) {
let fileContent
if (fs.existsSync(filePath)) fileContent = fs.readFileSync(filePath, "utf8")
else fileContent = fs.readFileSync(srcFiles[j], "utf8");
findAllImportPaths(dir, fileContent, function(_importObjs) {
//replace relative paths to absolute path for imports
for (var i = 0; i < _importObjs.length; i++) {
let _fullImportStatement = _importObjs[i].fullImportStatement
let srcFileDir = srcFiles[j].substring(0, srcFiles[j].lastIndexOf("/"));
_fullImportStatement = _fullImportStatement.replace(_importObjs[i].dependencyPath, srcFileDir + "/" + _importObjs[i].dependencyPath)
fileContent = fileContent.replace(_importObjs[i].fullImportStatement, _fullImportStatement)
}
if (fileContent.indexOf(" is ") > -1) {
updatedFileContent = updatedFileContent.replace(importStatement, fileContent);
} else {
//updatedFileContent = updatedFileContent.replace(importStatement, fileContent);
updatedFileContent = updatedFileContent.replace(importStatement, "");
updatedFileContent = fileContent + updatedFileContent;
}
variables.importedSrcFiles[path.basename(srcFiles[j])] = fileContent;
return cb(updatedFileContent);
});
} else {
updatedFileContent = updatedFileContent.replace(importStatement, "");
//issue #2.
if (updatedFileContent.indexOf(variables.importedSrcFiles[path.basename(dir + importObj.dependencyPath)] > -1)
&& updatedFileContent.indexOf("import ") == -1) {
var fileContent = fs.readFileSync(srcFiles[j], "utf8");
updatedFileContent = updatedFileContent.replace(variables.importedSrcFiles[path.basename(dir + importObj.dependencyPath)], "");
updatedFileContent = fileContent + updatedFileContent;
}
importIsReplacedBefore = true;
j++;
byNameAndReplaceInner(importStatement, updatedFileContent, dir, filePath, srcFiles, j, cb, cbInner)
}
} else {
j++;
byNameAndReplaceInner(importStatement, updatedFileContent, dir, filePath, srcFiles, j, cb, cbInner)
}
}
module.exports = {
byName: byName,
byNameAndReplace: byNameAndReplace

View File

@ -15,15 +15,25 @@ function replaceAllImportsInCurrentLayer(i, importObjs, updatedFileContent, dir,
updatedFileContent = updatedFileContent.replace(importObj.alias + ".", importObj.contractName + ".");
}
var importStatement = updatedFileContent.substring(importObj.startIndex, importObj.endIndex);
let importStatement = updatedFileContent.substring(importObj.startIndex, importObj.endIndex);
var fileExists = fs.existsSync(dir + importObj.dependencyPath, fs.F_OK);
let fileExists
let filePath
let isRelativePath = importObj.dependencyPath.indexOf(".") == 0
if (isRelativePath) {
filePath = dir + importObj.dependencyPath
fileExists = fs.existsSync(filePath, fs.F_OK);
}
else {
filePath = importObj.dependencyPath
fileExists = fs.existsSync(filePath, fs.F_OK);
}
if (fileExists) {
console.log("###" + importObj.dependencyPath + " SOURCE FILE FOUND###");
var importedFileContent = fs.readFileSync(dir + importObj.dependencyPath, "utf8");
var importedFileContent = fs.readFileSync(filePath, "utf8");
replaceRelativeImportPaths(importedFileContent, path.dirname(importObj.dependencyPath) + "/", function(importedFileContentUpdated) {
if (!variables.importedSrcFiles.hasOwnProperty(path.basename(dir + importObj.dependencyPath))) {
variables.importedSrcFiles[path.basename(dir + importObj.dependencyPath)] = importedFileContentUpdated;
if (!variables.importedSrcFiles.hasOwnProperty(path.basename(filePath))) {
variables.importedSrcFiles[path.basename(filePath)] = importedFileContentUpdated;
if (importedFileContentUpdated.indexOf(" is ") > -1) {
updatedFileContent = updatedFileContent.replace(importStatement, importedFileContentUpdated);
} else {
@ -34,9 +44,9 @@ function replaceAllImportsInCurrentLayer(i, importObjs, updatedFileContent, dir,
else {
updatedFileContent = updatedFileContent.replace(importStatement, "");
//issue #1.
if (updatedFileContent.indexOf(variables.importedSrcFiles[path.basename(dir + importObj.dependencyPath)] > -1)
if (updatedFileContent.indexOf(variables.importedSrcFiles[path.basename(filePath)] > -1)
&& updatedFileContent.indexOf("import ") == -1) {
updatedFileContent = updatedFileContent.replace(variables.importedSrcFiles[path.basename(dir + importObj.dependencyPath)], "");
updatedFileContent = updatedFileContent.replace(variables.importedSrcFiles[path.basename(filePath)], "");
updatedFileContent = importedFileContentUpdated + updatedFileContent;
}
}
@ -45,7 +55,7 @@ function replaceAllImportsInCurrentLayer(i, importObjs, updatedFileContent, dir,
replaceAllImportsInCurrentLayer(i, importObjs, updatedFileContent, dir, cb);
});
} else {
if (!variables.importedSrcFiles.hasOwnProperty(path.basename(dir + importObj.dependencyPath))) {
if (!variables.importedSrcFiles.hasOwnProperty(path.basename(filePath))) {
console.log("!!!" + importObj.dependencyPath + " SOURCE FILE NOT FOUND. TRY TO FIND IT RECURSIVELY!!!");
var directorySeperator;
@ -55,7 +65,7 @@ function replaceAllImportsInCurrentLayer(i, importObjs, updatedFileContent, dir,
directorySeperator = "/";
}
findFile.byNameAndReplace(dir.substring(0, dir.lastIndexOf(directorySeperator)), path.basename(importObj.dependencyPath), updatedFileContent, importStatement, function(_updatedFileContent) {
findFile.byNameAndReplace(dir.substring(0, dir.lastIndexOf(directorySeperator)), importObj.dependencyPath, updatedFileContent, importStatement, function(_updatedFileContent) {
i++;
console.log("###" + importObj.dependencyPath + " SOURCE FILE FOUND###");
replaceAllImportsInCurrentLayer(i, importObjs, _updatedFileContent, dir, cb);

View File

@ -24,12 +24,12 @@ function generateFlatFile(dir, path, inputFileContent) {
}
function getAllSolFilesCallBack(inputFileContent, dir, path, srcFiles) {
addLibraries(variables.parentDir, inputFileContent, variables.allSrcFiles, function(intermediateFileContent) {
replaceAllImportsRecursively(intermediateFileContent, dir, function(outputFileContent) {
//addLibraries(variables.parentDir, inputFileContent, variables.allSrcFiles, function(intermediateFileContent) {
replaceAllImportsRecursively(inputFileContent, dir, function(outputFileContent) {
outputFileContent = removeDoubledSolidityVersion(outputFileContent);
if (!fs.existsSync(variables.outDir)) fs.mkdirSync(variables.outDir);
fs.writeFileSync(variables.outDir + "/" + variables.flatContractPrefix + "_flat.sol", outputFileContent);
console.log("Success! Flat file is generated to " + variables.outDir + " directory");
});
});
//});
}