Skip to content

Commit

Permalink
Merge pull request #64 from cosmology-tech/split-json
Browse files Browse the repository at this point in the history
Split json in assets
  • Loading branch information
Zetazzz authored Jan 31, 2024
2 parents 937cc57 + 80c6273 commit 7ec4d79
Show file tree
Hide file tree
Showing 166 changed files with 482,433 additions and 472,487 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"editor.formatOnSave": false
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"eslint.validate": [
"javascript",
Expand Down
68 changes: 26 additions & 42 deletions packages/assets/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
{
"plugins": [
"@typescript-eslint",
"simple-import-sort",
"unused-imports"
// "react-hooks"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
// "plugin:react/recommended",
// configures both eslint-plugin-prettier and eslint-config-prettier
"plugin:prettier/recommended"
],
"parser": "@typescript-eslint/parser",
"plugins": ["prettier"],
"extends": ["eslint:recommended", "prettier"],
"parser": "@babel/eslint-parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
// "project": "tsconfig.json"
"ecmaVersion": 11,
"requireConfigFile": false,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"env": {
"es6": true,
Expand All @@ -25,32 +17,21 @@
"jest": true
},
"rules": {
// "react-hooks/rules-of-hooks": 2,
// "react-hooks/exhaustive-deps": 1,
"simple-import-sort/imports": 2,
"simple-import-sort/exports": 2,
"prettier/prettier": 2,
"unused-imports/no-unused-imports": 2,
"no-console": "off",
"no-debugger": 2,
"no-alert": 2,
"no-await-in-loop": 0,
"no-prototype-builtins": 0,
"no-return-assign": [
"error",
"except-parens"
],
"no-return-assign": ["error", "except-parens"],
"no-restricted-syntax": [
2,
"ForInStatement",
"LabeledStatement",
"WithStatement"
],
"no-unused-vars": 0,
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": [
"warn",
"no-unused-vars": [
0,
{
"ignoreSiblings": true,
"argsIgnorePattern": "React|res|next|^_"
}
],
Expand All @@ -66,22 +47,25 @@
"allowTaggedTemplates": true
}
],
"comma-dangle": 0,
"jsx-quotes": [
2,
"prefer-double"
],
"linebreak-style": [
"error",
"unix"
],
"no-console": 1,
"comma-dangle": 2,
"jsx-quotes": [2, "prefer-double"],
"linebreak-style": ["error", "unix"],
"quotes": [
2,
"single",
{
"avoidEscape": true,
"allowTemplateLiterals": true
}
],
"prettier/prettier": [
"error",
{
"trailingComma": "none",
"singleQuote": true,
"printWidth": 80
}
]
}
}
}
15 changes: 15 additions & 0 deletions packages/assets/examples/examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// backward compatibility: get asset_lists as before:
import { asset_lists } from '../src';

console.log(asset_lists);

// get mainnet asset lists only
import main_asset_lists from '../src/mainnet/assets';

console.log(main_asset_lists);

// get single assets
import assets from '../src/mainnet/akash';

console.log(assets);

11 changes: 5 additions & 6 deletions packages/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
"build": "cross-env BABEL_ENV=production babel src --out-dir main --delete-dir-on-start --extensions \".tsx,.ts,.js\"",
"build:ts": "tsc --project ./tsconfig.json",
"buidl": "npm run build && npm run build:ts",
"codegen": "babel-node ./scripts/build.js",
"build:pkg": "babel-node ./scripts/build.js",
"codegen": "npm run build:pkg && npm run format",
"prepare": "npm run build",
"lint": "eslint --ext .ts,.tsx,.js .",
"format": "eslint --fix . --ext .ts,.tsx,.js",
"format": "eslint --fix . --ext .ts,.tsx,.js --debug",
"test": "jest",
"test:watch": "jest --watch",
"test:debug": "node --inspect node_modules/.bin/jest --runInBand"
Expand Down Expand Up @@ -51,8 +52,7 @@
"@babel/preset-typescript": "^7.21.4",
"@chain-registry/utils": "^1.18.0",
"@types/jest": "^29.5.1",
"@typescript-eslint/eslint-plugin": "5.59.0",
"@typescript-eslint/parser": "5.59.0",
"babel-eslint": "10.1.0",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "29.5.0",
"babel-watch": "^7.7.2",
Expand All @@ -61,12 +61,11 @@
"eslint": "8.38.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-simple-import-sort": "8.0.0",
"eslint-plugin-unused-imports": "2.0.0",
"jest": "^29.5.0",
"prettier": "^2.8.7",
"regenerator-runtime": "^0.13.11",
"ts-jest": "^29.1.0",
"mkdirp": "3.0.1",
"typescript": "^5.0.4"
},
"dependencies": {
Expand Down
166 changes: 159 additions & 7 deletions packages/assets/scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,173 @@
import { getAssetLists } from '@chain-registry/utils';
import { assets, ibc } from 'chain-registry';
import { writeFileSync } from 'fs';
import { assets, chains, ibc } from 'chain-registry';
import { rmSync, writeFileSync } from 'fs';
import { mkdirpSync } from 'mkdirp';
import path from 'path';

const NON_COSMOS_NETWORK_TYPE = 'noncosmos';

const chainNetworkMap = {};
const networkChainMap = {};

chains.forEach((chain) => {
chainNetworkMap[chain.chain_name] = chain.network_type;
});

const asset_lists = assets.reduce((m, { chain_name }) => {
return [...m, ...getAssetLists(chain_name, ibc, assets)];
}, []);

const write = (file, json, TypeName, isArray = false) => {
const SRC_ROOT = `${__dirname}/../src`;
rmSync(SRC_ROOT, { recursive: true, force: true });

const getValidVarName = (varName) => {
if (!/^[a-zA-Z_$]/.test(varName)) {
return `_${varName}`;
}

return varName;
};

const writeNetworkIndex = (filePath, networkObj) => {
writeFileSync(
filePath,
`${Object.keys(networkObj)
.map((chain_name) => {
return `export * as ${getValidVarName(
chain_name
)} from './${chain_name}'`;
})
.filter(Boolean)
.join(';\n')}`
);
};

const writeNetworkAssets = (filePath, networkObj) => {
const validChain = [];
const importStat = Object.keys(networkObj)
.map((chain_name) => {
validChain.push(chain_name);
return `import _${chain_name} from './${chain_name}'`;
})
.filter(Boolean)
.join(';\n');

if (!validChain.length) {
return false;
}

writeFileSync(
filePath,
`import { AssetList } from '@chain-registry/types';
${importStat}
const assets: AssetList[] = [${validChain
.map((chain_name) => {
return `_${chain_name}`;
})
.join(',')}];
export default assets;
`
);

return true;
};

const write = (filePath, json, TypeName, isArray = false) => {
const strfy = JSON.stringify(json, null, 2);
const exportType = isArray ? TypeName + '[]' : TypeName;
writeFileSync(
`${__dirname}/../src/${file}.ts`,
filePath,
`import { ${TypeName} } from '@chain-registry/types';
const ${file}: ${exportType} = ${strfy};
export default ${file};
const assets: ${exportType} = ${strfy};
export default assets;
`
);
};

write(`asset_lists`, asset_lists, 'AssetList', true);
const writeRootAssets = (filePath, obj) => {
const validNetwork = [];
const importStat = Object.keys(obj)
.map((network_type) => {
validNetwork.push(network_type);
return `import _${network_type} from './${network_type}/assets'`;
})
.filter(Boolean)
.join(';\n');

if (!validNetwork.length) {
return false;
}

writeFileSync(
filePath,
`import { AssetList } from '@chain-registry/types';
${importStat}
const asset_lists: AssetList[] = [${validNetwork
.map((network_type) => {
return `..._${network_type}`;
})
.join(',')}];
export default asset_lists;
`
);

return true;
};

const writeRootIndex = (filePath, obj) => {
let imports = Object.keys(obj)
.map((network_type) => {
return `export * from './${network_type}'`;
})
.filter(Boolean)
.join(';\n');

imports = `${imports}; import asset_lists from './asset_lists';
export default {
asset_lists
};
export { asset_lists };
`;

writeFileSync(filePath, `${imports}`);
};

asset_lists.forEach((list) => {
const network_type =
chainNetworkMap[list.chain_name] ?? NON_COSMOS_NETWORK_TYPE;

if (!networkChainMap[network_type]) {
networkChainMap[network_type] = {};
}
networkChainMap[network_type][list.chain_name] = true;
const networkFolder = path.join(SRC_ROOT, network_type);

mkdirpSync(networkFolder);

const filePath = path.join(networkFolder, `${list.chain_name}.ts`);
write(filePath, list, 'AssetList', false);
});

Object.keys(networkChainMap).forEach((network_type) => {
const networkFolder = path.join(SRC_ROOT, network_type);

const assetsFilePath = path.join(networkFolder, 'assets.ts');
writeNetworkAssets(assetsFilePath, networkChainMap[network_type]);

const indexFilePath = path.join(networkFolder, 'index.ts');
writeNetworkIndex(indexFilePath, networkChainMap[network_type]);
});

const assetsRootFilePath = path.join(SRC_ROOT, 'asset_lists.ts');
writeRootAssets(assetsRootFilePath, networkChainMap);

const indexRootFilePath = path.join(SRC_ROOT, 'index.ts');
writeRootIndex(indexRootFilePath, networkChainMap);
Loading

0 comments on commit 7ec4d79

Please sign in to comment.