Skip to content

Commit

Permalink
Add automatic foundry toml detection to foundry plugin (#1738)
Browse files Browse the repository at this point in the history
* Add automatic foundry toml detection

read the foundry toml

use the function

Add test

cleanup

* changeset

* changeset

* dupe

* refactor: use foundry config command

* docs: update

---------

Co-authored-by: Will Cory <[email protected]>
Co-authored-by: Tom Meagher <[email protected]>
  • Loading branch information
3 people authored Jan 31, 2023
1 parent 298728b commit 37c221d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-kings-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@wagmi/cli': patch
---

Added automatic Foundry config detection for artifacts directory.
5 changes: 4 additions & 1 deletion docs/pages/cli/plugins/foundry.en-US.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: 'Foundry plugin'

# Foundry

Plugin for resolving ABIs from [Foundry](https://github.com/foundry-rs/foundry) projects. Supports [`generate`](/cli/commands#generate) `--watch` (`-w`) mode.
Plugin for resolving ABIs from [Foundry](https://github.com/foundry-rs/foundry) projects.

```ts
import { foundry } from '@wagmi/cli/plugins'
Expand All @@ -26,6 +26,9 @@ export default defineConfig({
})
```

- Supports [`generate`](/cli/commands#generate) `--watch` (`-w`) mode.
- Detects Foundry configuration using `forge config --json` command.

## Configuration

### project
Expand Down
4 changes: 3 additions & 1 deletion docs/pages/cli/plugins/hardhat.en-US.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: 'Hardhat plugin'

# Hardhat

Plugin for resolving ABIs from [Hardhat](https://hardhat.org) projects. Supports [`generate`](/cli/commands#generate) `--watch` (`-w`) mode.
Plugin for resolving ABIs from [Hardhat](https://hardhat.org) projects.

```ts
import { hardhat } from '@wagmi/cli/plugins'
Expand All @@ -26,6 +26,8 @@ export default defineConfig({
})
```

- Supports [`generate`](/cli/commands#generate) `--watch` (`-w`) mode.

## Configuration

### project
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
"/dist"
],
"peerDependencies": {
"@wagmi/core": ">=0.8.10",
"@wagmi/core": ">=0.9",
"typescript": ">=4.9.4",
"wagmi": ">=0.10.4"
"wagmi": ">=0.11"
},
"peerDependenciesMeta": {
"@wagmi/core": {
Expand Down
20 changes: 7 additions & 13 deletions packages/cli/src/plugins/foundry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ describe('foundry', () => {
})
})

it(
'contracts',
async () => {
await expect(
foundry({
project: resolve(__dirname, '__fixtures__/foundry/'),
}).contracts(),
).resolves.toMatchInlineSnapshot(`
it('contracts', async () => {
await expect(
foundry({
project: resolve(__dirname, '__fixtures__/foundry/'),
}).contracts(),
).resolves.toMatchInlineSnapshot(`
[
{
"abi": [
Expand Down Expand Up @@ -94,9 +92,5 @@ describe('foundry', () => {
},
]
`)
},
{
timeout: 10_000,
},
)
})
})
36 changes: 32 additions & 4 deletions packages/cli/src/plugins/foundry.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import dedent from 'dedent'
import { execa } from 'execa'
import { execa, execaCommandSync } from 'execa'
import { default as fse } from 'fs-extra'
import { globby } from 'globby'
import { basename, extname, resolve } from 'pathe'
import pc from 'picocolors'
import { z } from 'zod'

import type { ContractConfig, Plugin } from '../config'
import * as logger from '../logger'
Expand Down Expand Up @@ -35,7 +36,7 @@ type FoundryConfig<TProject extends string> = {
*
* Same as your project's `--out` (`-o`) option.
*
* @default 'out/'
* @default foundry.config#out | 'out'
*/
artifacts?: string
/**
Expand Down Expand Up @@ -83,11 +84,16 @@ type FoundryConfig<TProject extends string> = {

type FoundryResult = RequiredBy<Plugin, 'contracts' | 'validate' | 'watch'>

const FoundryConfigSchema = z.object({
out: z.string().default('out').optional(),
src: z.string().default('src').optional(),
})

/**
* Resolves ABIs from [Foundry](https://github.com/foundry-rs/foundry) project.
*/
export function foundry<TProject extends string>({
artifacts = 'out',
artifacts,
deployments = {},
exclude = defaultExcludes,
forge: {
Expand Down Expand Up @@ -123,7 +129,29 @@ export function foundry<TProject extends string>({
}

const project = resolve(process.cwd(), project_)
const artifactsDirectory = `${project}/${artifacts}`

let config: z.infer<typeof FoundryConfigSchema> = {
out: 'out',
src: 'src',
}
try {
config = FoundryConfigSchema.parse(
JSON.parse(
execaCommandSync(`${forgeExecutable} config --json`, {
cwd: project,
}).stdout,
),
)
// eslint-disable-next-line no-empty
} catch {
} finally {
config = {
...config,
out: artifacts ?? config.out,
}
}

const artifactsDirectory = `${project}/${config.out}`

return {
async contracts() {
Expand Down

1 comment on commit 37c221d

@vercel
Copy link

@vercel vercel bot commented on 37c221d Jan 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.