From e5cc13e03280fbefb430d86af3547a87bf4d55fb Mon Sep 17 00:00:00 2001 From: Adam R <13562139+catenocrypt@users.noreply.github.com> Date: Mon, 8 Mar 2021 11:38:22 +0100 Subject: [PATCH] [Internal] Assets fix script: format all JSON files, not only eth-fork info files (#5853) * Assets fix script: format all JSON files, not only eth-fork info files. * More debug message * Cleanup Co-authored-by: Catenocrypt --- script/generic/eth-forks.ts | 20 -------------------- script/generic/json-format.ts | 23 ++++++++++++++++++++++- script/generic/json.ts | 11 +++++++++-- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/script/generic/eth-forks.ts b/script/generic/eth-forks.ts index 22c16017d..19268a72c 100644 --- a/script/generic/eth-forks.ts +++ b/script/generic/eth-forks.ts @@ -3,15 +3,12 @@ import { getChainAssetsPath, getChainAssetsList, getChainAssetPath, - getChainAssetInfoPath, getChainAssetFilesList, - isChainAssetInfoExistSync, logoName, logoExtension, logoFullName, getChainAssetLogoPath } from "../generic/repo-structure"; -import { formatJsonFile } from "../generic/json"; import { getFileName, getFileExt, @@ -23,22 +20,6 @@ import { toChecksum } from "../generic/eth-address"; import { ActionInterface, CheckStepInterface } from "../generic/interface"; import * as bluebird from "bluebird"; -async function formatInfos() { - console.log(`Formatting info files...`); - await bluebird.each(ethForkChains, async (chain) => { - let count = 0; - const chainAssets = getChainAssetsList(chain); - await bluebird.each(chainAssets, async (address) => { - if (isChainAssetInfoExistSync(chain, address)) { - const chainAssetInfoPath = getChainAssetInfoPath(chain, address); - formatJsonFile(chainAssetInfoPath); - ++count; - } - }) - console.log(`Formatted ${count} info files for chain ${chain} (total ${chainAssets.length})`); - }) -} - function checkAddressChecksum(assetsFolderPath: string, address: string, chain: string) { const checksumAddress = toChecksum(address, chain); if (checksumAddress !== address) { @@ -101,7 +82,6 @@ export class EthForks implements ActionInterface { } async sanityFix(): Promise { - await formatInfos(); await checkAddressChecksums(); } } diff --git a/script/generic/json-format.ts b/script/generic/json-format.ts index 089bc5f30..2151c4e65 100644 --- a/script/generic/json-format.ts +++ b/script/generic/json-format.ts @@ -1,9 +1,26 @@ import { chainsPath } from "../generic/repo-structure"; import { findFiles } from "../generic/filesystem"; import { ActionInterface, CheckStepInterface } from "../generic/interface"; -import { isValidJSON } from "../generic/json"; +import { formatJsonFile, isValidJSON } from "../generic/json"; import * as bluebird from "bluebird"; +async function formatInfos(): Promise { + console.log(`Formatting json files...`); + + const files = [ + ...findFiles(chainsPath, 'json'), + ]; + let count1 = 0; + let count2 = 0; + await bluebird.each(files, async (file) => { + if (formatJsonFile(file)) { + ++count2; + } + ++count1; + }); + console.log(`Formatted ${count2} json files (total ${count1})`); +} + export class JsonAction implements ActionInterface { getName(): string { return "Json files"; } @@ -27,4 +44,8 @@ export class JsonAction implements ActionInterface { }, ]; } + + async sanityFix(): Promise { + await formatInfos(); + } } diff --git a/script/generic/json.ts b/script/generic/json.ts index b7f66b33e..16cdeaf6a 100644 --- a/script/generic/json.ts +++ b/script/generic/json.ts @@ -22,9 +22,16 @@ export function formatSortJson(content: unknown[]): string { return JSON.stringify(sortElements(content), null, 4); } -export function formatJsonFile(filename: string): void { - writeFileSync(filename, formatJson(JSON.parse(readFileSync(filename)))); +// Return if updated +export function formatJsonFile(filename: string): boolean { + const origText: string = readFileSync(filename); + const newText: string = formatJson(JSON.parse(origText)); + if (newText == origText) { + return false; + } + writeFileSync(filename, newText); console.log(`Formatted json file ${filename}`); + return true; } export function formatSortJsonFile(filename: string): void {