Skip to content

Commit

Permalink
introduce contract-json-cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
moshmage committed Jul 7, 2022
1 parent 3737c20 commit 8e1ed25
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ build/
.idea
migrations/
src/
contracts/
contracts/
tools/
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
},
"files": [
"dist",
"tooling",
"README.md"
],
"scripts": {
"compile": "node tools/compile.mjs",
"postcompile": "node tools/contract-json-cleaner.mjs build/contracts/",
"prebuild": "npm run compile",
"prepublishOnly": "npm run build",
"test": "mocha --bail --timeout 60000 --exit --require ts-node/register --require tsconfig-paths/register 'test/**/*.spec.{ts,tsx}' --pretty",
Expand Down
27 changes: 27 additions & 0 deletions tools/contract-json-cleaner.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {lstatSync, readFileSync, writeFileSync, readdirSync} from "fs";
import {resolve} from "path";

const KEEP_ROOT_KEYS = ['abi', 'bytecode'];

function cleanFile(file = "") {
const origin = JSON.parse(readFileSync(file, "utf-8"));
return JSON.stringify(KEEP_ROOT_KEYS.reduce((prev, curr) => ({...prev, [curr]: origin[curr]}), {}));
}

try {
const jsonOrPath = process.argv[2];
const stat = lstatSync(jsonOrPath);

if (stat.isFile())
writeFileSync(jsonOrPath, cleanFile(jsonOrPath), "utf-8");
else
readdirSync(jsonOrPath)
.filter(file => file.endsWith(".json"))
.forEach(file => {
const pathToFile = resolve(jsonOrPath, file);
writeFileSync(pathToFile, cleanFile(pathToFile), "utf-8")
});
} catch (e) {
console.error('Error', e);
process.exit(1);
}

0 comments on commit 8e1ed25

Please sign in to comment.