Last active
January 5, 2021 09:59
-
-
Save anupam-io/0e62875c2ce4728dcf93d0172e0b6033 to your computer and use it in GitHub Desktop.
Compile scripts for solidity
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
var path = require('path'); | |
var solc = require('solc'); | |
var fs = require('fs-extra'); | |
const buildPath = path.resolve(__dirname, 'build'); | |
fs.removeSync(buildPath); | |
fs.mkdirSync(buildPath); | |
fs.readdir(path.resolve(__dirname, 'contracts'), function (err, files) { | |
//listing all files using forEach | |
files.forEach(function (fileName) { | |
// Compile each .sol file and dump output in JSON format | |
console.log('Compiling ', fileName, '...'); | |
const filePath = path.resolve(__dirname, 'contracts', fileName); | |
const source = fs.readFileSync(filePath, 'utf-8'); | |
var input = { | |
language: 'Solidity', | |
sources: { | |
fileName: { | |
content: source | |
} | |
}, | |
settings: { | |
outputSelection: { | |
'*': { | |
'*': ['*'] | |
} | |
} | |
} | |
}; | |
var output = JSON.parse(solc.compile(JSON.stringify(input))); | |
for (let contract in output.contracts.fileName) { | |
fs.outputJSONSync( | |
path.resolve(buildPath, contract+'.json'), | |
output.contracts.fileName[contract] | |
); | |
} | |
console.log(fileName, ' compiled.'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use it?
contracts
foldernode compileAll.js