Last active
August 31, 2017 07:12
-
-
Save vitiko/4a5e1a9b92b4c669c0e35538e77e9d99 to your computer and use it in GitHub Desktop.
Truffle compile solidity smart contract - fast alternative using solc binary, dumping results in truffle build file format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const fs = require('fs'); | |
const { exec } = require('child_process'); | |
const truffleBuildTemplate = { | |
contract_name: null, | |
abi: null, | |
unlinked_binary: null, | |
networks: {}, | |
schema_version: "0.0.5", | |
updated_at: null | |
}; | |
exec(`solc --optimize --abi --bin ${process.argv[2]}`, { maxBuffer: 1024 * 10000 }, (err, stdout, stderr) => { | |
if (err) { | |
console.error(`exec error: ${err}`); | |
return; | |
} | |
let parts = stdout.split('======='); | |
for (let pos = 1; pos < parts.length; pos += 2) { | |
let [contractPath, contractName] = parts[pos].trim().split(':'); | |
console.log(contractName); | |
let [, , binary, , abi] = parts[pos + 1].split("\n"); | |
let truffleBuildFile = `./build/contracts/${contractName}.json`; | |
let truffleBuild; | |
if (fs.existsSync(truffleBuildFile)) { | |
truffleBuild = require(truffleBuildFile); | |
console.log('build exists, updated at ', (new Date (truffleBuild.updated_at))); | |
} | |
else truffleBuild = Object.assign({}, truffleBuildTemplate); | |
truffleBuild.contract_name = contractName; | |
truffleBuild.abi = JSON.parse(abi); | |
truffleBuild.unlinked_binary = binary; | |
truffleBuild.updated_at = Date.now(); | |
fs.writeFileSync(truffleBuildFile, JSON.stringify(truffleBuild, null, 2)); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment