Skip to content

Commit 529ebd9

Browse files
committed
feat: support generating typings for specific contracts by patterns
1 parent 4c97734 commit 529ebd9

File tree

5 files changed

+93
-2
lines changed

5 files changed

+93
-2
lines changed

.changeset/sour-trainers-float.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@typechain/hardhat': patch
3+
---
4+
5+
It allows generating typings only for contracts that match the patterns.

packages/hardhat/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export function getDefaultTypechainConfig(config: HardhatConfig): TypechainConfi
66
const defaultConfig: TypechainConfig = {
77
outDir: 'typechain-types',
88
target: 'ethers-v6',
9+
artifacts: undefined,
910
alwaysGenerateOverloads: false,
1011
discriminateTypes: false,
1112
tsNocheck: false,

packages/hardhat/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ subtask(TASK_TYPECHAIN_GENERATE_TYPES)
6767
}
6868
const cwd = config.paths.root
6969

70+
const artifactPatterns = typechainCfg.artifacts ?? [
71+
`${config.paths.artifacts}/!(build-info)/**/+([a-zA-Z0-9_]).json`,
72+
]
73+
7074
const { glob } = await import('typechain')
71-
const allFiles = glob(cwd, [`${config.paths.artifacts}/!(build-info)/**/+([a-zA-Z0-9_]).json`])
75+
const allFiles = glob(cwd, artifactPatterns)
7276
if (typechainCfg.externalArtifacts) {
7377
allFiles.push(...glob(cwd, typechainCfg.externalArtifacts, false))
7478
}
@@ -90,7 +94,7 @@ subtask(TASK_TYPECHAIN_GENERATE_TYPES)
9094
const { runTypeChain } = await import('typechain')
9195
const result = await runTypeChain({
9296
...typechainOptions,
93-
filesToProcess: needsFullRebuild ? allFiles : glob(cwd, artifactPaths), // only process changed files if not doing full rebuild
97+
filesToProcess: needsFullRebuild ? allFiles : allFiles.filter((x: string) => artifactPaths.includes(x)), // only process changed files if not doing full rebuild
9498
})
9599

96100
if (!quiet) {

packages/hardhat/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export interface TypechainConfig {
22
outDir: string
33
target: string
4+
artifacts?: string[] | undefined
45
alwaysGenerateOverloads: boolean
56
discriminateTypes: boolean
67
tsNocheck: boolean

packages/hardhat/test/project.test.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,86 @@ describe('Typechain x Hardhat', function () {
129129
expect(consoleLogMock).toHaveBeenCalledWith(['Successfully generated 14 typings for external artifacts!'])
130130
})
131131
})
132+
133+
describe('when setting custom artifact glob', () => {
134+
let oldArtifactGlob: string[] | undefined
135+
beforeEach(function () {
136+
oldArtifactGlob = this.hre.config.typechain.artifacts
137+
})
138+
afterEach(function () {
139+
this.hre.config.typechain.artifacts = oldArtifactGlob
140+
})
141+
;[true, false].forEach((forcedCompilation) => {
142+
describe(`when type generation is ${forcedCompilation ? '' : 'not'} forced`, () => {
143+
let subject: () => Promise<void>
144+
beforeEach(async function () {
145+
if (forcedCompilation) {
146+
await this.hre.run('compile', { noTypechain: true })
147+
}
148+
subject = () => {
149+
if (forcedCompilation) {
150+
return this.hre.run('typechain')
151+
} else {
152+
return this.hre.run('compile')
153+
}
154+
}
155+
})
156+
157+
describe('when glob matches some files', () => {
158+
beforeEach(function () {
159+
this.hre.config.typechain.artifacts = ['**/EdgeCases.json']
160+
})
161+
162+
it('includes build artifacts that match the glob', async function () {
163+
const exists = existsSync(this.hre.config.typechain.outDir)
164+
expect(exists).toEqual(false)
165+
166+
await subject()
167+
168+
const dir = await readdir(this.hre.config.typechain.outDir)
169+
expect(dir.includes('EdgeCases.ts')).toEqual(true)
170+
})
171+
172+
it('excludes build artifacts that do not match the glob', async function () {
173+
const exists = existsSync(this.hre.config.typechain.outDir)
174+
expect(exists).toEqual(false)
175+
176+
await subject()
177+
178+
const dir = await readdir(this.hre.config.typechain.outDir)
179+
expect(dir.includes('TestContract.ts')).toEqual(false)
180+
expect(dir.includes('TestContract1.ts')).toEqual(false)
181+
})
182+
})
183+
describe('when glob matches no files', () => {
184+
beforeEach(function () {
185+
this.hre.config.typechain.artifacts = ['**/THISDOESNTMATCHANYTHING.json']
186+
})
187+
188+
describe('when no external artifacts are specified', () => {
189+
it('does not generate any types', async function () {
190+
const exists = existsSync(this.hre.config.typechain.outDir)
191+
expect(exists).toEqual(false)
192+
193+
await subject()
194+
expect(existsSync(this.hre.config.typechain.outDir)).toEqual(false)
195+
})
196+
})
197+
198+
describe('when external artifacts are specified', () => {
199+
it('only generates types for external artifacts', async function () {
200+
const exists = existsSync(this.hre.config.typechain.outDir)
201+
expect(exists).toEqual(false)
202+
203+
this.hre.config.typechain.externalArtifacts = ['externalArtifacts/*.json']
204+
await subject()
205+
expect(existsSync(this.hre.config.typechain.outDir)).toEqual(true)
206+
})
207+
})
208+
})
209+
})
210+
})
211+
})
132212
})
133213

134214
describe('dontOverrideCompile', function () {

0 commit comments

Comments
 (0)