[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 <catenocrypt@users.noreply.github.com>
This commit is contained in:
Adam R 2021-03-08 11:38:22 +01:00 committed by GitHub
parent f2739b230f
commit e5cc13e032
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 23 deletions

View File

@ -3,15 +3,12 @@ import {
getChainAssetsPath, getChainAssetsPath,
getChainAssetsList, getChainAssetsList,
getChainAssetPath, getChainAssetPath,
getChainAssetInfoPath,
getChainAssetFilesList, getChainAssetFilesList,
isChainAssetInfoExistSync,
logoName, logoName,
logoExtension, logoExtension,
logoFullName, logoFullName,
getChainAssetLogoPath getChainAssetLogoPath
} from "../generic/repo-structure"; } from "../generic/repo-structure";
import { formatJsonFile } from "../generic/json";
import { import {
getFileName, getFileName,
getFileExt, getFileExt,
@ -23,22 +20,6 @@ import { toChecksum } from "../generic/eth-address";
import { ActionInterface, CheckStepInterface } from "../generic/interface"; import { ActionInterface, CheckStepInterface } from "../generic/interface";
import * as bluebird from "bluebird"; 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) { function checkAddressChecksum(assetsFolderPath: string, address: string, chain: string) {
const checksumAddress = toChecksum(address, chain); const checksumAddress = toChecksum(address, chain);
if (checksumAddress !== address) { if (checksumAddress !== address) {
@ -101,7 +82,6 @@ export class EthForks implements ActionInterface {
} }
async sanityFix(): Promise<void> { async sanityFix(): Promise<void> {
await formatInfos();
await checkAddressChecksums(); await checkAddressChecksums();
} }
} }

View File

@ -1,9 +1,26 @@
import { chainsPath } from "../generic/repo-structure"; import { chainsPath } from "../generic/repo-structure";
import { findFiles } from "../generic/filesystem"; import { findFiles } from "../generic/filesystem";
import { ActionInterface, CheckStepInterface } from "../generic/interface"; import { ActionInterface, CheckStepInterface } from "../generic/interface";
import { isValidJSON } from "../generic/json"; import { formatJsonFile, isValidJSON } from "../generic/json";
import * as bluebird from "bluebird"; import * as bluebird from "bluebird";
async function formatInfos(): Promise<void> {
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 { export class JsonAction implements ActionInterface {
getName(): string { return "Json files"; } getName(): string { return "Json files"; }
@ -27,4 +44,8 @@ export class JsonAction implements ActionInterface {
}, },
]; ];
} }
async sanityFix(): Promise<void> {
await formatInfos();
}
} }

View File

@ -22,9 +22,16 @@ export function formatSortJson(content: unknown[]): string {
return JSON.stringify(sortElements(content), null, 4); return JSON.stringify(sortElements(content), null, 4);
} }
export function formatJsonFile(filename: string): void { // Return if updated
writeFileSync(filename, formatJson(JSON.parse(readFileSync(filename)))); 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}`); console.log(`Formatted json file ${filename}`);
return true;
} }
export function formatSortJsonFile(filename: string): void { export function formatSortJsonFile(filename: string): void {