Skip to content

Instantly share code, notes, and snippets.

@adrianmcli
Created May 12, 2020 00:56
Show Gist options
  • Save adrianmcli/66a6a88943641b37ec28d68c5e1659e2 to your computer and use it in GitHub Desktop.
Save adrianmcli/66a6a88943641b37ec28d68c5e1659e2 to your computer and use it in GitHub Desktop.
Grab JSON artifacts and extract the ABIs from them into a new folder
const path = require("path");
const fs = require("fs");
// get JSON artifact files
const artifactDir = path.join(__dirname, "build/contracts/");
const files = fs.readdirSync(artifactDir);
const jsonFiles = files.filter(x => x.slice(-4) === "json");
console.log(jsonFiles.length, "json files found");
// create abi directory
const outDir = "./abi/";
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir);
}
// read the files
let count = 0;
jsonFiles.forEach(filename => {
const artifactPath = artifactDir + filename;
const rawdata = fs.readFileSync(artifactPath);
const json = JSON.parse(rawdata);
const abi = json.abi;
if (abi.length > 0) {
const data = JSON.stringify(abi, null, 2);
fs.writeFileSync(`${outDir}${filename}`, data);
console.log("success:", filename);
count++;
}
});
console.log(`${count} file(s) produced`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment