Skip to content

Instantly share code, notes, and snippets.

@anupam-io
Last active January 5, 2021 09:59
Show Gist options
  • Save anupam-io/0e62875c2ce4728dcf93d0172e0b6033 to your computer and use it in GitHub Desktop.
Save anupam-io/0e62875c2ce4728dcf93d0172e0b6033 to your computer and use it in GitHub Desktop.
Compile scripts for solidity
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.');
});
});
@anupam-io
Copy link
Author

anupam-io commented Jan 4, 2021

How to use it?

  • Assuming that you have all of your contracts in contracts folder
  • node compileAll.js

@anupam-io
Copy link
Author

anupam-io commented Jan 4, 2021

Compatible with:

  • solc: 0.8.0
  • fs-extra: 9.0.1
  • web3: 1.3.1

@anupam-io
Copy link
Author

Do checkout solcTemplate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment