UNPKG

ethers-opt

Version:

Collection of heavily optimized functions for ethers.js V6

38 lines (29 loc) 1.33 kB
import { writeFile, readFile } from 'fs/promises'; import { glob } from 'glob'; import { task } from 'hardhat/config.js'; import { type HardhatRuntimeEnvironment } from 'hardhat/types/runtime.js'; const importLineRegex = /^.*from\s+(['"])(\.[^'"]*?)(?<!\.[jt]s|\.json)\1.*$/gm; export async function typechainFix(taskArgs?: { dir?: string }, hre?: HardhatRuntimeEnvironment) { const dir = taskArgs?.dir || (hre?.config as unknown as { typechain?: { outDir?: string } })?.typechain?.outDir || './typechain-types'; console.log(`typechain:fix: scanning type files from ${dir}`); const files = await glob(`${dir}/**/*.ts`, { ignore: 'node_modules/**' }); for (const file of files) { let context = await readFile(file, { encoding: 'utf8' }); if (context.match(importLineRegex)) { console.log(`typechain:fix: fixed ${file}`); context = context.replace( /from\s+(['"])(\.[^'"]*?)(?<!\.[jt]s|\.json)\1/g, 'from$1$2/index.js$1', ); } await writeFile(file, context); } } task('typechain:fix', 'Fix Typechain definitions for ESM generated by Hardhat') .addParam('dir', 'Typechain Directory', '') .setAction(async (taskArgs, hre) => { await typechainFix(taskArgs, hre); });