trustwallet-assets/src/test/helpers.ts
Viktor Radchenko abbe34d2f9
Check if all files have valid json (#1026)
* Check if all files have valid json

* Optimised images with calibre/image-actions

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2020-01-15 08:02:14 +09:00

123 lines
5.2 KiB
TypeScript

import * as fs from "fs"
import * as path from "path"
const axios = require('axios')
const Web3 = require('web3')
const web3 = new Web3('ws://localhost:8546');
import { CoinTypeUtils, CoinType } from "@trustwallet/types";
const sizeOf = require("image-size");
export const getChainName = (id: CoinType): string => CoinTypeUtils.id(id)
export const Binance = getChainName(CoinType.binance)
export const Classic = getChainName(CoinType.classic)
export const Cosmos = getChainName(CoinType.cosmos)
export const Ethereum = getChainName(CoinType.ethereum)
export const GoChain = getChainName(CoinType.gochain)
export const IoTeX = getChainName(CoinType.iotex)
export const POA = getChainName(CoinType.poa)
export const Tezos = getChainName(CoinType.tezos)
export const ThunderCore = getChainName(CoinType.thundertoken)
export const Terra = getChainName(CoinType.terra)
export const TomoChain = getChainName(CoinType.tomochain)
export const Tron = getChainName(CoinType.tron)
export const Wanchain = getChainName(CoinType.wanchain)
export const Waves = getChainName(CoinType.waves)
const whiteList = 'whitelist.json'
const blackList = 'blacklist.json'
export const logo = `logo.png`
export const root = './'
export const chainsFolderPath = './blockchains'
export const pricingFolderPath = './pricing'
export const getChainLogoPath = chain => `${chainsFolderPath}/${chain}/info/${logo}`
export const getChainAssetsPath = (chain: string): string => `${chainsFolderPath}/${chain}/assets`
export const minLogoWidth = 64
export const minLogoHeight = 64
export const maxLogoWidth = 512
export const maxLogoHeight = 512
export const getChainAssetLogoPath = (chain, address) => `${getChainAssetsPath(chain)}/${address}/${logo}`
export const getChainValidatorsPath = chain => `${chainsFolderPath}/${chain}/validators`
export const getChainValidatorsAssets = chain => readDirSync(getChainValidatorsAssetsPath(chain))
export const getChainValidatorsListPath = chain => `${(getChainValidatorsPath(chain))}/list.json`
export const getChainValidatorsAssetsPath = chain => `${getChainValidatorsPath(chain)}/assets`
export const getChainValidatorAssetLogoPath = (chain, asset) => `${getChainValidatorsAssetsPath(chain)}/${asset}/${logo}`
export const getChainWhitelistPath = chain => `${chainsFolderPath}/${chain}/${whiteList}`
export const getChainBlacklistPath = chain => `${chainsFolderPath}/${chain}/${blackList}`
export const readDirSync = path => fs.readdirSync(path)
export const isPathExistsSync = path => fs.existsSync(path)
export const isChainWhitelistExistSync = chain => isPathExistsSync(getChainWhitelistPath(chain))
export const isChainBlacklistExistSync = chain => isPathExistsSync(getChainBlacklistPath(chain))
export const readFileSync = path => fs.readFileSync(path, 'utf8')
export const writeFileSync = (path, str) => fs.writeFileSync(path, str)
export const isLowerCase = str => str.toLowerCase() === str
export const isUpperCase = str => str.toUpperCase() === str
export const isChecksum = address => web3.utils.checkAddressChecksum(address)
export const toChecksum = address => web3.utils.toChecksumAddress(address)
export const getBinanceBEP2Symbols = async () => axios.get(`https://dex-atlantic.binance.org/api/v1/tokens?limit=1000`).then(res => res.data.map(({symbol}) => symbol))
export const isTRC10 = id => (/^\d+$/.test(id))
export const isTRC20 = address => {
return address.length == 34 &&
address.startsWith("T") &&
isLowerCase(address) == false &&
isUpperCase(address) == false
}
export const sortDesc = arr => arr.sort((a, b) => a - b)
export const getUnique = arr => Array.from(new Set(arr))
export const mapList = arr => {
return arr.reduce((acm, val) => {
acm[val] = ""
return acm
}, {})
}
export const getImageDimentions = (path: string) => sizeOf(path)
export const isLogoOK = (path: string): [boolean, string] => {
const { width, height } = getImageDimentions(path)
if (((width >= minLogoWidth && width <= maxLogoWidth) && (height >= minLogoHeight && height <= maxLogoHeight))) {
return [true, '']
} else {
return [false, `Image at path ${path} must have dimensions: min:${minLogoWidth}x${minLogoHeight} and max:${maxLogoWidth}x${maxLogoHeight} insted ${width}x${height}`]
}
}
export const calculateAspectRatioFit = (srcWidth: number, srcHeight: number, maxWidth: number, maxHeight: number) => {
const ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight)
return { width: Math.round(srcWidth * ratio), height: Math.round(srcHeight * ratio) }
}
export const findFiles = (base: string, ext: string, files: string[] = [], result: string[] = []) => {
files = fs.readdirSync(base) || files
result = result || result
files.forEach(
function (file) {
var newbase = path.join(base, file)
if ( fs.statSync(newbase).isDirectory()) {
result = findFiles(newbase, ext, fs.readdirSync(newbase), result)
} else {
if ( file.substr(-1*(ext.length+1)) == '.' + ext) {
result.push(newbase)
}
}
}
)
return result
}
export const isValidJSON = (path: string) => {
let rawdata = fs.readFileSync(path, 'utf8')
try {
JSON.parse(rawdata)
return true
} catch {
return false
}
}