slint, bunyan dependencies are added

This commit is contained in:
Victor Baranov 2018-09-04 16:17:22 +03:00
parent 2fd1029352
commit f3d2f8b316
15 changed files with 1295 additions and 245 deletions

31
.eslintrc.json Normal file
View File

@ -0,0 +1,31 @@
{
"env": {
"browser": true,
"es6": true,
"amd": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
]
}
}

View File

@ -1,16 +1,17 @@
function changeRelativePathToAbsolute(fileContent, srcFile, importObjs) {
//replace relative paths to absolute path for imports
for (var i = 0; i < importObjs.length; i++) {
let isAbsolutePath = importObjs[i].dependencyPath.indexOf(".") != 0
importObjs.forEach((importObj) => {
let isAbsolutePath = importObj.dependencyPath.indexOf('.') != 0
if (!isAbsolutePath) {
let _fullImportStatement = importObjs[i].fullImportStatement
let srcFileDir = srcFile.substring(0, srcFile.lastIndexOf("/"));
_fullImportStatement = _fullImportStatement.replace(importObjs[i].dependencyPath, srcFileDir + "/" + importObjs[i].dependencyPath)
fileContent = fileContent.replace(importObjs[i].fullImportStatement, _fullImportStatement)
let _fullImportStatement = importObj.fullImportStatement
let srcFileDir = srcFile.substring(0, srcFile.lastIndexOf('/'))
const { dependencyPath } = importObj
_fullImportStatement = _fullImportStatement.replace(dependencyPath, srcFileDir + '/' + dependencyPath)
fileContent = fileContent.replace(importObj.fullImportStatement, _fullImportStatement)
}
}
})
return fileContent;
return fileContent
}
module.exports = changeRelativePathToAbsolute;
module.exports = changeRelativePathToAbsolute

View File

@ -1,64 +1,64 @@
const fs = require('fs');
const path = require('path');
var decomment = require('decomment');
const findFile = require("./find-file.js");
const fs = require('fs')
const path = require('path')
let decomment = require('decomment')
const findFile = require('./find-file')
function findAllImportPaths(dir, content, cb) {
//strip comments from content
//strip comments from content
content = decomment(content, {safe: true})
const subStr = "import ";
let allImports = [];
let regex = new RegExp(subStr,"gi");
var importsCount = (content.match(regex) || []).length;
let importsIterator = 0;
const subStr = 'import '
let allImports = []
let regex = new RegExp(subStr,'gi')
var importsCount = (content.match(regex) || []).length
let importsIterator = 0
let result
while ( (result = regex.exec(content)) ) {
let startImport = result.index;
let endImport = startImport + content.substr(startImport).indexOf(";") + 1;
let fullImportStatement = content.substring(startImport, endImport);
let dependencyPath = fullImportStatement.split("\"").length > 1 ? fullImportStatement.split("\"")[1]: fullImportStatement.split("'")[1];
let alias = fullImportStatement.split(" as ").length > 1?fullImportStatement.split(" as ")[1].split(";")[0]:null;
let contractName;
let startImport = result.index
let endImport = startImport + content.substr(startImport).indexOf(';') + 1
let fullImportStatement = content.substring(startImport, endImport)
let dependencyPath = fullImportStatement.split('"').length > 1 ? fullImportStatement.split('"')[1]: fullImportStatement.split('\'')[1]
let alias = fullImportStatement.split(' as ').length > 1?fullImportStatement.split(' as ')[1].split(';')[0]:null
importObj = {
"startIndex": startImport,
"endIndex": endImport,
"dependencyPath": dependencyPath,
"fullImportStatement": fullImportStatement,
"alias": alias,
"contractName": null
};
let importObj = {
'startIndex': startImport,
'endIndex': endImport,
'dependencyPath': dependencyPath,
'fullImportStatement': fullImportStatement,
'alias': alias,
'contractName': null
}
if (alias) {
alias = alias.replace(/\s/g,'');
var fileExists = fs.existsSync(dependencyPath, fs.F_OK);
alias = alias.replace(/\s/g,'')
var fileExists = fs.existsSync(dependencyPath, fs.F_OK)
if (fileExists) {
importsIterator++;
let fileContent = fs.readFileSync(dependencyPath, "utf8");
if (fileContent.indexOf("contract ") > -1) {
importObj.contractName = getContractName(fileContent);
importsIterator++
let fileContent = fs.readFileSync(dependencyPath, 'utf8')
if (fileContent.indexOf('contract ') > -1) {
importObj.contractName = getContractName(fileContent)
}
allImports.push(importObj);
allImports.push(importObj)
} else {
findFile.byName(dir.substring(0, dir.lastIndexOf("/")), path.basename(dependencyPath), function(fileContent) {
importsIterator++;
if (fileContent.indexOf("contract ") > -1) {
importObj.contractName = getContractName(fileContent);
findFile.byName(dir.substring(0, dir.lastIndexOf('/')), path.basename(dependencyPath), function(fileContent) {
importsIterator++
if (fileContent.indexOf('contract ') > -1) {
importObj.contractName = getContractName(fileContent)
}
allImports.push(importObj);
allImports.push(importObj)
if (importsIterator == importsCount) cb(allImports);
});
if (importsIterator == importsCount) cb(allImports)
})
}
} else {
importsIterator++;
allImports.push(importObj);
importsIterator++
allImports.push(importObj)
}
}
if (importsIterator == importsCount) cb(allImports);
if (importsIterator == importsCount) cb(allImports)
}
function getContractName(fileContent) {
return fileContent.substring((fileContent.indexOf("contract ") + ("contract ").length), fileContent.indexOf("{")).replace(/\s/g,'')
return fileContent.substring((fileContent.indexOf('contract ') + ('contract ').length), fileContent.indexOf('{')).replace(/\s/g,'')
}
module.exports = findAllImportPaths;
module.exports = findAllImportPaths

View File

@ -1,88 +1,88 @@
const fs = require('fs');
const glob = require("glob");
const path = require("path");
const variables = require("./variables.js");
const changeRelativePathToAbsolute = require("./change-relative-path-to-absolute.js");
const fs = require('fs')
const glob = require('glob')
const path = require('path')
const variables = require('./variables')
const changeRelativePathToAbsolute = require('./change-relative-path-to-absolute')
const log = require('./logger')
function byName(dir, fileName, cb) {
glob(dir + "/**/*.sol", function(err, srcFiles) {
if (err) return console.log(err.message);
glob(dir + '/**/*.sol', function(err, srcFiles) {
if (err) return log.error(err.message)
for (var j = 0; j < srcFiles.length; j++) {
if (path.basename(srcFiles[j]) == fileName) {
var fileContent = fs.readFileSync(srcFiles[j], "utf8");
cb(fileContent);
return;
var fileContent = fs.readFileSync(srcFiles[j], 'utf8')
cb(fileContent)
return
}
}
dir = dir.substring(0, dir.lastIndexOf("/"));
byName(dir, fileName, cb);
});
dir = dir.substring(0, dir.lastIndexOf('/'))
byName(dir, fileName, cb)
})
}
function byNameAndReplace(dir, filePath, updatedFileContent, importStatement, cb) {
glob(dir + "/**/*.sol", function(err, srcFiles) {
if (err) return console.log(err.message);
glob(dir + '/**/*.sol', function(err, srcFiles) {
if (err) return log.error(err.message)
var importIsReplacedBefore = false;
let importIsReplacedBefore = false
byNameAndReplaceInner(importStatement, updatedFileContent, dir, filePath, srcFiles, 0, cb, function() {
if (importIsReplacedBefore) {
updatedFileContent = updatedFileContent.replace(importStatement, "");
cb(updatedFileContent);
updatedFileContent = updatedFileContent.replace(importStatement, '')
cb(updatedFileContent)
} else {
if (dir.indexOf("/") > -1) {
dir = dir.substring(0, dir.lastIndexOf("/"));
byNameAndReplace(dir, filePath, updatedFileContent, importStatement, cb);
if (dir.indexOf('/') > -1) {
dir = dir.substring(0, dir.lastIndexOf('/'))
byNameAndReplace(dir, filePath, updatedFileContent, importStatement, cb)
} else {
updatedFileContent = updatedFileContent.replace(importStatement, "");
cb(updatedFileContent);
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
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");
if (fs.existsSync(filePath)) fileContent = fs.readFileSync(filePath, 'utf8')
else fileContent = fs.readFileSync(srcFiles[j], 'utf8')
findAllImportPaths(dir, fileContent, function(_importObjs) {
fileContent = changeRelativePathToAbsolute(fileContent, srcFiles[j], _importObjs);
fileContent = changeRelativePathToAbsolute(fileContent, srcFiles[j], _importObjs)
if (fileContent.indexOf(" is ") > -1) {
updatedFileContent = updatedFileContent.replace(importStatement, fileContent);
if (fileContent.indexOf(' is ') > -1) {
updatedFileContent = updatedFileContent.replace(importStatement, fileContent)
} else {
//updatedFileContent = updatedFileContent.replace(importStatement, fileContent);
updatedFileContent = updatedFileContent.replace(importStatement, "");
updatedFileContent = fileContent + updatedFileContent;
updatedFileContent = updatedFileContent.replace(importStatement, '')
updatedFileContent = fileContent + updatedFileContent
}
variables.importedSrcFiles[path.basename(srcFiles[j])] = fileContent;
return cb(updatedFileContent);
});
variables.importedSrcFiles[path.basename(srcFiles[j])] = fileContent
return cb(updatedFileContent)
})
} else {
updatedFileContent = updatedFileContent.replace(importStatement, "");
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;
&& 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++;
j++
byNameAndReplaceInner(importStatement, updatedFileContent, dir, filePath, srcFiles, j, cb, cbInner)
}
} else {
j++;
j++
byNameAndReplaceInner(importStatement, updatedFileContent, dir, filePath, srcFiles, j, cb, cbInner)
}
}
@ -90,4 +90,4 @@ function byNameAndReplaceInner(importStatement, updatedFileContent, dir, filePat
module.exports = {
byName: byName,
byNameAndReplace: byNameAndReplace
};
}

4
helpers/logger.js Normal file
View File

@ -0,0 +1,4 @@
var bunyan = require('bunyan')
var log = bunyan.createLogger({name: 'solidity-flattener'})
module.exports = log

View File

@ -1,22 +1,23 @@
//const removeTabs = require("./remove-tabs.js");
//const removeTabs = require("./remove-tabs");
function removeDoubledSolidityVersion(content) {
const subStr = "pragma solidity";
const subStr = 'pragma solidity'
//1st pragma solidity declaration
let firstIndex = content.indexOf(subStr);
let lastIndex = firstIndex + content.substr(firstIndex).indexOf(";") + 1;
let contentPart = content.substr(lastIndex);
let contentFiltered = contentPart;
let firstIndex = content.indexOf(subStr)
let lastIndex = firstIndex + content.substr(firstIndex).indexOf(';') + 1
let contentPart = content.substr(lastIndex)
let contentFiltered = contentPart
//remove other pragma solidity declarations
let regex = new RegExp(subStr,"gi");
let regex = new RegExp(subStr,'gi')
let result
while ( (result = regex.exec(contentPart)) ) {
let start = result.index;
let end = start + contentPart.substr(start).indexOf(";") + 1;
if (start != firstIndex) contentFiltered = contentFiltered.replace(contentPart.substring(start, end), "");
let start = result.index
let end = start + contentPart.substr(start).indexOf(';') + 1
if (start != firstIndex) contentFiltered = contentFiltered.replace(contentPart.substring(start, end), '')
}
let finalContent = content.substr(0, lastIndex) + contentFiltered;
let finalContent = content.substr(0, lastIndex) + contentFiltered
return finalContent;//removeTabs(finalContent); //#10
return finalContent//removeTabs(finalContent); //#10
}
module.exports = removeDoubledSolidityVersion;
module.exports = removeDoubledSolidityVersion

View File

@ -1,6 +1,6 @@
function removeTabs(content) {
content = content.replace(/[\r\n]+/g, '\n'); //removes tabs
return content;
content = content.replace(/[\r\n]+/g, '\n') //removes tabs
return content
}
module.exports = removeTabs;
module.exports = removeTabs

View File

@ -1,88 +1,89 @@
const fs = require('fs');
const path = require("path");
const variables = require("./variables.js");
const findFile = require("./find-file.js");
const replaceRelativeImportPaths = require("./replace-relative-import-paths.js");
const updateImportObjectLocationInTarget = require("./update-import-object-location-in-target.js");
const changeRelativePathToAbsolute = require("./change-relative-path-to-absolute.js");
const findAllImportPaths = require("./find-all-import-paths.js");
const fs = require('fs')
const path = require('path')
const variables = require('./variables')
const findFile = require('./find-file')
const replaceRelativeImportPaths = require('./replace-relative-import-paths')
const updateImportObjectLocationInTarget = require('./update-import-object-location-in-target')
const changeRelativePathToAbsolute = require('./change-relative-path-to-absolute')
const findAllImportPaths = require('./find-all-import-paths')
const log = require('./logger')
function replaceAllImportsInCurrentLayer(i, importObjs, updatedFileContent, dir, cb) {
if (i < importObjs.length) {
var importObj = importObjs[i];
importObj = updateImportObjectLocationInTarget(importObj, updatedFileContent);
var importObj = importObjs[i]
importObj = updateImportObjectLocationInTarget(importObj, updatedFileContent)
//replace contracts aliases
if (importObj.contractName) {
updatedFileContent = updatedFileContent.replace(importObj.alias + ".", importObj.contractName + ".");
updatedFileContent = updatedFileContent.replace(importObj.alias + '.', importObj.contractName + '.')
}
let importStatement = updatedFileContent.substring(importObj.startIndex, importObj.endIndex);
let importStatement = updatedFileContent.substring(importObj.startIndex, importObj.endIndex)
let fileExists
let filePath
let isRelativePath = importObj.dependencyPath.indexOf(".") == 0
let isRelativePath = importObj.dependencyPath.indexOf('.') == 0
if (isRelativePath) {
filePath = dir + importObj.dependencyPath
fileExists = fs.existsSync(filePath, fs.F_OK);
fileExists = fs.existsSync(filePath, fs.F_OK)
}
else {
filePath = importObj.dependencyPath
fileExists = fs.existsSync(filePath, fs.F_OK);
fileExists = fs.existsSync(filePath, fs.F_OK)
}
if (fileExists) {
console.log("###" + importObj.dependencyPath + " SOURCE FILE FOUND###");
var importedFileContent = fs.readFileSync(filePath, "utf8");
log.info('###' + importObj.dependencyPath + ' SOURCE FILE FOUND###')
var importedFileContent = fs.readFileSync(filePath, 'utf8')
findAllImportPaths(dir, importedFileContent, function(_importObjs) {
importedFileContent = changeRelativePathToAbsolute(importedFileContent, filePath, _importObjs);
replaceRelativeImportPaths(importedFileContent, path.dirname(importObj.dependencyPath) + "/", function(importedFileContentUpdated) {
importedFileContent = changeRelativePathToAbsolute(importedFileContent, filePath, _importObjs)
replaceRelativeImportPaths(importedFileContent, path.dirname(importObj.dependencyPath) + '/', function(importedFileContentUpdated) {
if (!variables.importedSrcFiles.hasOwnProperty(path.basename(filePath))) {
variables.importedSrcFiles[path.basename(filePath)] = importedFileContentUpdated;
if (importedFileContentUpdated.indexOf(" is ") > -1) {
updatedFileContent = updatedFileContent.replace(importStatement, importedFileContentUpdated);
variables.importedSrcFiles[path.basename(filePath)] = importedFileContentUpdated
if (importedFileContentUpdated.indexOf(' is ') > -1) {
updatedFileContent = updatedFileContent.replace(importStatement, importedFileContentUpdated)
} else {
updatedFileContent = updatedFileContent.replace(importStatement, "");
updatedFileContent = importedFileContentUpdated + updatedFileContent;
updatedFileContent = updatedFileContent.replace(importStatement, '')
updatedFileContent = importedFileContentUpdated + updatedFileContent
}
}
else {
updatedFileContent = updatedFileContent.replace(importStatement, "");
updatedFileContent = updatedFileContent.replace(importStatement, '')
//issue #1.
if (updatedFileContent.indexOf(variables.importedSrcFiles[path.basename(filePath)] > -1)
&& updatedFileContent.indexOf("import ") == -1) {
updatedFileContent = updatedFileContent.replace(variables.importedSrcFiles[path.basename(filePath)], "");
updatedFileContent = importedFileContentUpdated + updatedFileContent;
&& updatedFileContent.indexOf('import ') == -1) {
updatedFileContent = updatedFileContent.replace(variables.importedSrcFiles[path.basename(filePath)], '')
updatedFileContent = importedFileContentUpdated + updatedFileContent
}
}
i++;
replaceAllImportsInCurrentLayer(i, importObjs, updatedFileContent, dir, cb);
});
i++
replaceAllImportsInCurrentLayer(i, importObjs, updatedFileContent, dir, cb)
})
})
} else {
if (!variables.importedSrcFiles.hasOwnProperty(path.basename(filePath))) {
console.log("!!!" + importObj.dependencyPath + " SOURCE FILE NOT FOUND. TRY TO FIND IT RECURSIVELY!!!");
log.info('!!!' + importObj.dependencyPath + ' SOURCE FILE NOT FOUND. TRY TO FIND IT RECURSIVELY!!!')
var directorySeperator;
if (process.platform === "win32") {
directorySeperator = "\\";
var directorySeperator
if (process.platform === 'win32') {
directorySeperator = '\\'
} else {
directorySeperator = "/";
directorySeperator = '/'
}
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);
});
i++
log.info('###' + importObj.dependencyPath + ' SOURCE FILE FOUND###')
replaceAllImportsInCurrentLayer(i, importObjs, _updatedFileContent, dir, cb)
})
} else {
updatedFileContent = updatedFileContent.replace(importStatement, "");
i++;
replaceAllImportsInCurrentLayer(i, importObjs, updatedFileContent, dir, cb);
updatedFileContent = updatedFileContent.replace(importStatement, '')
i++
replaceAllImportsInCurrentLayer(i, importObjs, updatedFileContent, dir, cb)
}
}
} else cb(updatedFileContent);
} else cb(updatedFileContent)
}
module.exports = replaceAllImportsInCurrentLayer;
module.exports = replaceAllImportsInCurrentLayer

View File

@ -1,16 +1,16 @@
const findAllImportPaths = require("./find-all-import-paths.js");
const replaceAllImportsInCurrentLayer = require("./replace-all-imports-in-current-layer");
const findAllImportPaths = require('./find-all-import-paths')
const replaceAllImportsInCurrentLayer = require('./replace-all-imports-in-current-layer')
function replaceAllImportsRecursively(fileContent, dir, cb) {
let updatedFileContent = fileContent;
let updatedFileContent = fileContent
findAllImportPaths(dir, updatedFileContent, function(_importObjs) {
if (!_importObjs) return cb(updatedFileContent);
if (_importObjs.length == 0) return cb(updatedFileContent);
if (!_importObjs) return cb(updatedFileContent)
if (_importObjs.length == 0) return cb(updatedFileContent)
replaceAllImportsInCurrentLayer(0, _importObjs, updatedFileContent, dir, function(_updatedFileContent) {
replaceAllImportsRecursively(_updatedFileContent, dir, cb);
});
});
};
replaceAllImportsRecursively(_updatedFileContent, dir, cb)
})
})
}
module.exports = replaceAllImportsRecursively;
module.exports = replaceAllImportsRecursively

View File

@ -1,33 +1,33 @@
const updateImportObjectLocationInTarget = require("./update-import-object-location-in-target.js");
const findAllImportPaths = require("./find-all-import-paths.js");
const updateImportObjectLocationInTarget = require('./update-import-object-location-in-target')
const findAllImportPaths = require('./find-all-import-paths')
function replaceRelativeImportPaths(fileContent, curDir, cb) {
let updatedFileContent = fileContent;
let updatedFileContent = fileContent
findAllImportPaths(curDir, fileContent, function(importObjs) {
if (!importObjs) return cb(updatedFileContent);
if (importObjs.length == 0) return cb(updatedFileContent);
if (!importObjs) return cb(updatedFileContent)
if (importObjs.length == 0) return cb(updatedFileContent)
for (let j = 0; j < importObjs.length; j++) {
let importObj = importObjs[j];
let importObj = importObjs[j]
importObj = updateImportObjectLocationInTarget(importObj, updatedFileContent);
let importStatement = updatedFileContent.substring(importObj.startIndex, importObj.endIndex);
importObj = updateImportObjectLocationInTarget(importObj, updatedFileContent)
let importStatement = updatedFileContent.substring(importObj.startIndex, importObj.endIndex)
let newPath;
if (importObj.dependencyPath.indexOf("../") == 0) {
newPath = curDir + importObj.dependencyPath;
let newPath
if (importObj.dependencyPath.indexOf('../') == 0) {
newPath = curDir + importObj.dependencyPath
}
else if (importObj.dependencyPath.indexOf("./") == 0) {
newPath = curDir + importObj.dependencyPath;
else if (importObj.dependencyPath.indexOf('./') == 0) {
newPath = curDir + importObj.dependencyPath
}
else {
newPath = importObj.dependencyPath;
newPath = importObj.dependencyPath
}
let importStatementNew = importStatement.replace(importObj.dependencyPath, newPath);
updatedFileContent = updatedFileContent.replace(importStatement, importStatementNew);
let importStatementNew = importStatement.replace(importObj.dependencyPath, newPath)
updatedFileContent = updatedFileContent.replace(importStatement, importStatementNew)
}
cb(updatedFileContent);
});
cb(updatedFileContent)
})
}
module.exports = replaceRelativeImportPaths;
module.exports = replaceRelativeImportPaths

View File

@ -1,9 +1,9 @@
function updateImportObjectLocationInTarget(importObj, content) {
let startIndexNew = content.indexOf(importObj.fullImportStatement);
let endIndexNew = startIndexNew - importObj.startIndex + importObj.endIndex;
importObj.startIndex = startIndexNew;
importObj.endIndex = endIndexNew;
return importObj;
let startIndexNew = content.indexOf(importObj.fullImportStatement)
let endIndexNew = startIndexNew - importObj.startIndex + importObj.endIndex
importObj.startIndex = startIndexNew
importObj.endIndex = endIndexNew
return importObj
}
module.exports = updateImportObjectLocationInTarget;
module.exports = updateImportObjectLocationInTarget

View File

@ -1,24 +1,24 @@
const path = require("path");
const fs = require('fs');
const path = require('path')
const fs = require('fs')
const configPath = "./config.json";
let configExists = fs.existsSync(configPath, fs.F_OK);
let config;
if (configExists) config = JSON.parse(fs.readFileSync(configPath, "utf8"));
const configPath = './config.json'
let configExists = fs.existsSync(configPath, fs.F_OK)
let config
if (configExists) config = JSON.parse(fs.readFileSync(configPath, 'utf8'))
//Input solidity file path
let args = process.argv.slice(2);
let inputFilePath = args.length > 0?args[0]:config?config.inputFilePath:"";
let args = process.argv.slice(2)
let inputFilePath = args.length > 0?args[0]:config?config.inputFilePath:''
//Input solidity file dir name
let inputFileDir = path.dirname(inputFilePath);
let inputFileDir = path.dirname(inputFilePath)
//Input parent dir
let parentDir = inputFileDir;
let parentDir = inputFileDir
//Output directory to store flat combined solidity file
let outDir = args.length > 1?args[1]:config?config.outputDir:"./out";
let flatContractPrefix = args.length > 2?args[2]:path.basename(inputFilePath, ".sol");
let outDir = args.length > 1?args[1]:config?config.outputDir:'./out'
let flatContractPrefix = args.length > 2?args[2]:path.basename(inputFilePath, '.sol')
let allSrcFiles = [];
let importedSrcFiles = {};
let allSrcFiles = []
let importedSrcFiles = {}
module.exports = {
@ -28,6 +28,6 @@ module.exports = {
parentDir: parentDir,
outDir: outDir,
allSrcFiles: allSrcFiles,
importedSrcFiles: importedSrcFiles,
flatContractPrefix: flatContractPrefix
importedSrcFiles: importedSrcFiles,
flatContractPrefix: flatContractPrefix
}

View File

@ -1,32 +1,32 @@
#! /usr/bin/env node
const fs = require('fs');
const glob = require("glob");
const pathLib = require("path");
const variables = require("./helpers/variables.js");
const removeDoubledSolidityVersion = require("./helpers/remove-doubled-solidity-version.js");
const replaceAllImportsRecursively = require("./helpers/replace-all-imports-recursively.js");
const fs = require('fs')
const glob = require('glob')
const variables = require('./helpers/variables')
const log = require('./helpers/logger')
const removeDoubledSolidityVersion = require('./helpers/remove-doubled-solidity-version')
const replaceAllImportsRecursively = require('./helpers/replace-all-imports-recursively')
fs.readFile(variables.inputFilePath, "utf8", readInputFileCallBack);
fs.readFile(variables.inputFilePath, 'utf8', readInputFileCallBack)
function readInputFileCallBack(err, inputFileContent) {
if (err) return console.log(err.message);
if (err) return log.error(err.message)
generateFlatFile(variables.parentDir + "/", variables.parentDir + "/**/*.sol", inputFileContent);
generateFlatFile(variables.parentDir + '/', variables.parentDir + '/**/*.sol', inputFileContent)
}
function generateFlatFile(dir, path, inputFileContent) {
glob(path, function(err, srcFiles) {
variables.allSrcFiles = srcFiles;
if (err) return console.log(err.message);
getAllSolFilesCallBack(inputFileContent, dir, path, srcFiles);
});
variables.allSrcFiles = srcFiles
if (err) return log.error(err.message)
getAllSolFilesCallBack(inputFileContent, dir)
})
}
function getAllSolFilesCallBack(inputFileContent, dir, path, srcFiles) {
function getAllSolFilesCallBack(inputFileContent, dir) {
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");
});
outputFileContent = removeDoubledSolidityVersion(outputFileContent)
if (!fs.existsSync(variables.outDir)) fs.mkdirSync(variables.outDir)
fs.writeFileSync(variables.outDir + '/' + variables.flatContractPrefix + '_flat.sol', outputFileContent)
log.info('Success! Flat file is generated to ' + variables.outDir + ' directory')
})
}

1036
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,8 @@
"version": "2.0.0",
"description": "Combine solidity files to one flat file",
"scripts": {
"start": "node index.js"
"start": "node index.js",
"lint": "./node_modules/.bin/eslint ."
},
"bin": {
"poa-solidity-flattener": "index.js"
@ -17,6 +18,7 @@
"license": "MIT",
"homepage": "https://poa.network/",
"dependencies": {
"bunyan": "^1.8.12",
"decomment": "^0.9.1",
"glob": "^7.1.2",
"path": "^0.12.7"
@ -28,5 +30,7 @@
"url": "/issues"
},
"main": "index.js",
"devDependencies": {}
"devDependencies": {
"eslint": "^5.5.0"
}
}