Skip to content

Commit

Permalink
feat: support generating typings for specific contracts by patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
zouguangxian committed Feb 14, 2023
1 parent 7f53b3b commit 686650c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
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-v5',
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 @@ -66,8 +66,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 @@ -88,7 +92,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

0 comments on commit 686650c

Please sign in to comment.