diff --git a/index.js b/index.js index d6cd01a..45cde39 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,10 @@ /*global require, module*/ +const Flattener = require('./lib/Flattener'); + module.exports = (embark) => { + const flattener = new Flattener(embark); + embark.registerConsoleCommand({ description: "Flattens all or some of your contracts so that they can be verified on etherscan\n\t\tYou can specify which contract to flatten by using their filename (relative to the contract directory specified in embark.json). For multiple contracts, separate them using a comma", matches: (cmd) => { @@ -16,9 +20,9 @@ module.exports = (embark) => { } else { embark.logger.info('Going to flatten all contracts'); } - // TODO actually flatten - callback(null, "Flattening done"); + + flattener.flatten(contractNames, callback); } }); }; diff --git a/lib/Flattener.js b/lib/Flattener.js new file mode 100644 index 0000000..2b3bfc8 --- /dev/null +++ b/lib/Flattener.js @@ -0,0 +1,47 @@ +const path = require('path'); +const async = require('async'); + +class Flattener { + constructor(embark) { + this.embark = embark; + } + + _doFlatten(contracts, callback) { + async.each(contracts, contract => { + const files = [contract.path]; + contract.importRemappings.forEach(remapping => { + files.push(remapping.target); + }); + }); + + callback(); + } + + flatten(contractFileNames, callback) { + if (!contractFileNames) { + return this._doFlatten(this.embark.config.contractsFiles, callback); + } + + contractFileNames = contractFileNames.split(','); + + let contracts; + try { + contracts = contractFileNames.map(contractFileName => { + const file = this.embark.config.contractsFiles.filter(file => path.normalize(file.filename).indexOf(path.normalize(contractFileName)) > -1); + if (file.length === 0) { + throw new Error('No contract file named ' + contractFileName); + } + if (file.length > 1) { + throw new Error(`More then one contract file matched ${contractFileName}. Try to be more specific by adding the directory the contract is in. E.g: "flatten myDir/myContract.sol"`); + } + return file[0]; + }); + } catch (e) { + return callback(null, e.message.red); + } + + this._doFlatten(contracts, callback); + } +} + +module.exports = Flattener; diff --git a/package.json b/package.json index aa3c952..9fdc580 100644 --- a/package.json +++ b/package.json @@ -24,5 +24,6 @@ "eslint": "^4.19.1" }, "dependencies": { + "async": "^2.6.1" } } diff --git a/yarn.lock b/yarn.lock index 939c8b8..c31287a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -68,6 +68,13 @@ argparse@^1.0.7: dependencies: sprintf-js "~1.0.2" +async@^2.6.1: + version "2.6.1" + resolved "https://registry.yarnpkg.com/async/-/async-2.6.1.tgz#b245a23ca71930044ec53fa46aa00a3e87c6a610" + integrity sha512-fNEiL2+AZt6AlAw/29Cr0UDe4sRAHCpEHh54WMz+Bb7QfNcFw4h3loofyJpLeQs4Yx7yuqu/2dLgM5hKOs6HlQ== + dependencies: + lodash "^4.17.10" + babel-code-frame@^6.22.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b" @@ -512,7 +519,7 @@ levn@^0.3.0, levn@~0.3.0: prelude-ls "~1.1.2" type-check "~0.3.2" -lodash@^4.17.4, lodash@^4.3.0: +lodash@^4.17.10, lodash@^4.17.4, lodash@^4.3.0: version "4.17.11" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d" integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==