Skip to content

Commit

Permalink
feat(cli): foundry plugin resolves config (#1779)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm authored Feb 3, 2023
1 parent 3cef111 commit 9734675
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 84 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-ears-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@wagmi/cli': patch
---

Made `project` optional for Foundry plugin
40 changes: 17 additions & 23 deletions docs/pages/cli/plugins/foundry.en-US.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,6 @@ export default defineConfig({

## Configuration

### project

Path to Foundry project.

```ts {7}
import { defineConfig } from '@wagmi/cli'
import { foundry } from '@wagmi/cli/plugins'

export default defineConfig({
plugins: [
foundry({
project: '../hello_foundry',
}),
],
})
```

### artifacts (optional)

Project's artifacts directory. Defaults to `'out/'`. Same as your `foundry.toml`/`forge`s `--out` (`-o`) option.
Expand All @@ -60,7 +43,6 @@ export default defineConfig({
plugins: [
foundry({
artifacts: 'out/',
project: '../hello_foundry',
}),
],
})
Expand All @@ -70,14 +52,13 @@ export default defineConfig({

Mapping of addresses to attach to artifacts.

```ts {8-13}
```ts {7-12}
import { defineConfig } from '@wagmi/cli'
import { foundry } from '@wagmi/cli/plugins'

export default defineConfig({
plugins: [
foundry({
project: '../hello_foundry',
deployments: {
Counter: {
1: '0x314159265dd8dbb310642f98f50c066173c1259b',
Expand Down Expand Up @@ -119,7 +100,6 @@ export default defineConfig({
'**.s.sol/*.json',
'**.t.sol/*.json',
],
project: '../hello_foundry',
}),
],
})
Expand All @@ -142,7 +122,6 @@ export default defineConfig({
path: 'path/to/forge',
rebuild: true,
},
project: '../hello_foundry',
}),
],
})
Expand All @@ -168,7 +147,6 @@ export default defineConfig({
// the following patterns are included by default
'*.json',
],
project: '../hello_foundry',
}),
],
})
Expand All @@ -186,6 +164,22 @@ export default defineConfig({
plugins: [
foundry({
namePrefix: 'HelloFoundry',
}),
],
})
```

### project (optional)

Path to Foundry project.

```ts {7}
import { defineConfig } from '@wagmi/cli'
import { foundry } from '@wagmi/cli/plugins'

export default defineConfig({
plugins: [
foundry({
project: '../hello_foundry',
}),
],
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"find-up": "^6.3.0",
"fs-extra": "^10.1.0",
"globby": "^13.1.3",
"node-fetch": "^3.2.9",
"node-fetch": "^3.3.0",
"ora": "^6.1.2",
"pathe": "^1.0.0",
"picocolors": "^1.0.0",
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/src/plugins/__fixtures__/foundry/foundry.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[profile.default]
src = 'src'
out = 'out'
libs = ['lib']
out = 'out'
solc = '0.8.13'
src = 'src'

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
# See more config options https://github.com/foundry-rs/foundry/tree/master/config
17 changes: 0 additions & 17 deletions packages/cli/src/plugins/_types.ts

This file was deleted.

16 changes: 8 additions & 8 deletions packages/cli/src/plugins/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { default as fse } from 'fs-extra'
import type { RequestInfo, RequestInit, Response } from 'node-fetch'
import { default as nodeFetch } from 'node-fetch'
import { default as fetch_ } from 'node-fetch'
import { join } from 'pathe'

import type { ContractConfig, Plugin } from '../config'
import type { RequiredBy } from '../types'
import { homedir } from 'os'

type Request = { url: RequestInfo; init?: RequestInit }

const cacheDir = `${homedir}/.wagmi-cli/plugins/fetch/cache`

export type FetchConfig = {
/**
* Duration in milliseconds to cache ABIs from request.
Expand Down Expand Up @@ -44,7 +41,9 @@ export type FetchConfig = {
*/
request(config: {
address?: ContractConfig['address']
}): Promise<Request> | Request
}):
| Promise<{ url: RequestInfo; init?: RequestInit }>
| { url: RequestInfo; init?: RequestInit }
/**
* Duration in milliseconds before request times out.
*
Expand All @@ -69,13 +68,14 @@ export function fetch({
}: FetchConfig): FetchResult {
return {
async contracts() {
const cacheDir = join(homedir(), '.wagmi-cli/plugins/fetch/cache')
await fse.ensureDir(cacheDir)

const timestamp = Date.now() + cacheDuration
const contracts = []
for (const contract of contractConfigs) {
const cacheKey = getCacheKey({ contract })
const cacheFilePath = `${cacheDir}/${cacheKey}.json`
const cacheFilePath = join(cacheDir, `${cacheKey}.json`)
const cachedFile = await fse.readJSON(cacheFilePath).catch(() => null)
let abi
if (cachedFile?.timestamp > Date.now()) abi = cachedFile.abi
Expand All @@ -88,7 +88,7 @@ export function fetch({
try {
const { url, init } = await request(contract)
// TODO: Replace `node-fetch` with native `fetch` when Node 18 is more widely used.
const response = await nodeFetch(url, {
const response = await fetch_(url, {
...init,
// TODO: Use `AbortSignal.timeout` when Node 18 is more widely used.
signal: controller.signal,
Expand Down
58 changes: 58 additions & 0 deletions packages/cli/src/plugins/foundry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ describe('foundry', () => {
)
}
})

it('validates without project', async () => {
const dir = resolve(__dirname, '__fixtures__/foundry/')
const spy = vi.spyOn(process, 'cwd')
spy.mockImplementation(() => dir)

await expect(foundry().validate()).resolves.toBeUndefined()
})
})

it('contracts', async () => {
Expand Down Expand Up @@ -93,4 +101,54 @@ describe('foundry', () => {
]
`)
})

it('contracts without project', async () => {
const dir = resolve(__dirname, '__fixtures__/foundry/')
const spy = vi.spyOn(process, 'cwd')
spy.mockImplementation(() => dir)

await expect(foundry().contracts()).resolves.toMatchInlineSnapshot(`
[
{
"abi": [
{
"inputs": [],
"name": "increment",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function",
},
{
"inputs": [],
"name": "number",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256",
},
],
"stateMutability": "view",
"type": "function",
},
{
"inputs": [
{
"internalType": "uint256",
"name": "newNumber",
"type": "uint256",
},
],
"name": "setNumber",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function",
},
],
"address": undefined,
"name": "Counter",
},
]
`)
})
})
Loading

1 comment on commit 9734675

@vercel
Copy link

@vercel vercel bot commented on 9734675 Feb 3, 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.