Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support generating typings for specific contracts by patterns #806

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ten-dots-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@typechain/hardhat': patch
---

It allows generating typings only for contracts that match the patterns.
1 change: 1 addition & 0 deletions packages/hardhat/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export function getDefaultTypechainConfig(config: HardhatConfig): TypechainConfi
const defaultConfig: TypechainConfig = {
outDir: 'typechain-types',
target: 'ethers-v6',
artifacts: undefined,
alwaysGenerateOverloads: false,
discriminateTypes: false,
tsNocheck: false,
Expand Down
8 changes: 6 additions & 2 deletions packages/hardhat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ subtask(TASK_TYPECHAIN_GENERATE_TYPES)
}
const cwd = config.paths.root

const artifactPatterns = typechainCfg.artifacts ?? [
`${config.paths.artifacts}/!(build-info)/**/+([a-zA-Z0-9_]).json`,
]

const { glob } = await import('typechain')
const allFiles = glob(cwd, [`${config.paths.artifacts}/!(build-info)/**/+([a-zA-Z0-9_]).json`])
const allFiles = glob(cwd, artifactPatterns)
if (typechainCfg.externalArtifacts) {
allFiles.push(...glob(cwd, typechainCfg.externalArtifacts, false))
}
Expand All @@ -90,7 +94,7 @@ subtask(TASK_TYPECHAIN_GENERATE_TYPES)
const { runTypeChain } = await import('typechain')
const result = await runTypeChain({
...typechainOptions,
filesToProcess: needsFullRebuild ? allFiles : glob(cwd, artifactPaths), // only process changed files if not doing full rebuild
filesToProcess: needsFullRebuild ? allFiles : allFiles.filter((x: string) => artifactPaths.includes(x)), // only process changed files if not doing full rebuild
})

if (!quiet) {
Expand Down
1 change: 1 addition & 0 deletions packages/hardhat/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface TypechainConfig {
outDir: string
target: string
artifacts?: string[] | undefined
alwaysGenerateOverloads: boolean
discriminateTypes: boolean
tsNocheck: boolean
Expand Down
80 changes: 80 additions & 0 deletions packages/hardhat/test/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,86 @@ describe('Typechain x Hardhat', function () {
expect(consoleLogMock).toHaveBeenCalledWith(['Successfully generated 14 typings for external artifacts!'])
})
})

describe('when setting custom artifact glob', () => {
let oldArtifactGlob: string[] | undefined
beforeEach(function () {
oldArtifactGlob = this.hre.config.typechain.artifacts
})
afterEach(function () {
this.hre.config.typechain.artifacts = oldArtifactGlob
})
;[true, false].forEach((forcedCompilation) => {
describe(`when type generation is ${forcedCompilation ? '' : 'not'} forced`, () => {
let subject: () => Promise<void>
beforeEach(async function () {
if (forcedCompilation) {
await this.hre.run('compile', { noTypechain: true })
}
subject = () => {
if (forcedCompilation) {
return this.hre.run('typechain')
} else {
return this.hre.run('compile')
}
}
})

describe('when glob matches some files', () => {
beforeEach(function () {
this.hre.config.typechain.artifacts = ['**/EdgeCases.json']
})

it('includes build artifacts that match the glob', async function () {
const exists = existsSync(this.hre.config.typechain.outDir)
expect(exists).toEqual(false)

await subject()

const dir = await readdir(this.hre.config.typechain.outDir)
expect(dir.includes('EdgeCases.ts')).toEqual(true)
})

it('excludes build artifacts that do not match the glob', async function () {
const exists = existsSync(this.hre.config.typechain.outDir)
expect(exists).toEqual(false)

await subject()

const dir = await readdir(this.hre.config.typechain.outDir)
expect(dir.includes('TestContract.ts')).toEqual(false)
expect(dir.includes('TestContract1.ts')).toEqual(false)
})
})
describe('when glob matches no files', () => {
beforeEach(function () {
this.hre.config.typechain.artifacts = ['**/THISDOESNTMATCHANYTHING.json']
})

describe('when no external artifacts are specified', () => {
it('does not generate any types', async function () {
const exists = existsSync(this.hre.config.typechain.outDir)
expect(exists).toEqual(false)

await subject()
expect(existsSync(this.hre.config.typechain.outDir)).toEqual(false)
})
})

describe('when external artifacts are specified', () => {
it('only generates types for external artifacts', async function () {
const exists = existsSync(this.hre.config.typechain.outDir)
expect(exists).toEqual(false)

this.hre.config.typechain.externalArtifacts = ['externalArtifacts/*.json']
await subject()
expect(existsSync(this.hre.config.typechain.outDir)).toEqual(true)
})
})
})
})
})
})
})

describe('dontOverrideCompile', function () {
Expand Down