Skip to content

Commit

Permalink
♻️ chore(@evmts/bundler): Convert utils to effect (#538)
Browse files Browse the repository at this point in the history
## Description

work towards #518

## Testing

Refactor utils of bundler to use Effect

## Additional Information

- [ ] I read the [contributing docs](../docs/contributing.md) (if this
is your first contribution)

Your ENS/address:

---------

Co-authored-by: Will Cory <[email protected]>
  • Loading branch information
roninjin10 and Will Cory authored Oct 14, 2023
1 parent 8d3dfb7 commit 281d20c
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 73 deletions.
4 changes: 3 additions & 1 deletion blockexplorer/src/blockExplorers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { StandardBlockExplorer } from './blockExplorer.js'
import {
InvalidAddressError, InvalidBytesError, InvalidUrlError
InvalidAddressError,
InvalidBytesError,
InvalidUrlError,
} from '@evmts/schemas'
import { optimism } from 'viem/chains'
import { describe, expect, expectTypeOf, it } from 'vitest'
Expand Down
2 changes: 1 addition & 1 deletion bundlers/bundler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</a>
<a href="https://bundlephobia.com/package/@evmts/core@latest" target="\_parent">
<img alt="" src="https://badgen.net/bundlephobia/minzip/@evmts/core" />
</a><a href="#badge">
</a>

# @evmts/bundler

Expand Down
1 change: 1 addition & 0 deletions bundlers/bundler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@evmts/tsconfig": "workspace:^",
"@types/node": "^20.7.2",
"@types/resolve": "^1.20.3",
"effect": "2.0.0-next.47",
"glob": "^10.3.10",
"resolve": "^1.22.6",
"solidity-ast": "^0.4.52",
Expand Down
3 changes: 1 addition & 2 deletions bundlers/bundler/src/solc/compileContracts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,8 @@ describe('compileContract', () => {
),
).rejects.toThrow('Resolution Error')

expect(mockLogger.error).toHaveBeenCalledWith(mockError as any)
expect(mockLogger.error).toHaveBeenCalledWith(
`there was an error resolving ${mockFilePath}`,
`There was an error resolving ${mockFilePath}`,
)
})

Expand Down
8 changes: 6 additions & 2 deletions bundlers/bundler/src/solc/compileContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import type {
ModuleInfo,
} from '../types'
import { invariant } from '../utils/invariant'
import { resolvePromise } from '../utils/resolvePromise'
import { resolveEffect } from '../utils/resolvePromise'
import { moduleFactory } from './moduleFactory'
import { type SolcInputDescription, solcCompile } from './solc'
import type { ResolvedCompilerConfig } from '@evmts/config'
import { Effect } from 'effect'

// Compile the Solidity contract and return its ABI
export const compileContract = async <TIncludeAsts extends boolean = boolean>(
Expand All @@ -24,7 +25,10 @@ export const compileContract = async <TIncludeAsts extends boolean = boolean>(
const entryModule = await moduleFactory(
filePath,
await fao
.readFile(await resolvePromise(filePath, basedir, fao, logger), 'utf8')
.readFile(
await Effect.runPromise(resolveEffect(filePath, basedir, fao, logger)),
'utf8',
)
.then((code) => {
return code
}),
Expand Down
6 changes: 6 additions & 0 deletions bundlers/bundler/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
export * from './isRelativeSolidity'
export * from './isSolidity'
export * from './isImportLocal'
export * from './formatPath'
export * from './invariant'
export * from './isSolidity'
export * from './isRelativeSolidity'
export * from './resolvePromise'
136 changes: 79 additions & 57 deletions bundlers/bundler/src/utils/resolvePromise.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FileAccessObject, Logger } from '..'
import { resolvePromise } from './resolvePromise'
import { resolveEffect } from './resolvePromise'
import { Effect } from 'effect'
import fs from 'fs'
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest'

Expand Down Expand Up @@ -30,11 +31,8 @@ describe('resolvePromise', () => {
})

it('should resolve a file path in the base directory', async () => {
const resolvedPath = await resolvePromise(
'./resolvePromise.spec.ts',
__dirname,
fao,
logger,
const resolvedPath = await Effect.runPromise(
resolveEffect('./resolvePromise.spec.ts', __dirname, fao, logger),
)
expect(
resolvedPath.endsWith(
Expand All @@ -46,75 +44,99 @@ describe('resolvePromise', () => {
it('should handle readFile throwing', async () => {
fao.readFile = () => Promise.reject('readFile error')
await expect(
resolvePromise('./resolvePromise.spec.tst', './src/utils', fao, logger),
Effect.runPromise(
resolveEffect('./resolvePromise.spec.tst', './src/utils', fao, logger),
),
).rejects.toThrowErrorMatchingInlineSnapshot('"readFile error"')
expect((logger.error as Mock).mock.calls).toMatchInlineSnapshot(`
[
[
"readFile error",
],
[
"Error reading file",
],
[
"readFile error",
],
[
"there was an error resolving ./resolvePromise.spec.tst",
],
[
"readFile error",
],
[
"Error reading file",
],
[
"readFile error",
],
[
"there was an error resolving ./resolvePromise.spec.tst",
],
[
"readFile error",
],
[
"Error reading file",
],
[
"readFile error",
],
[
"there was an error resolving ./resolvePromise.spec.tst",
],
]
`)
[
[
"readFile error",
],
[
"Error reading file",
],
[
"readFile error",
],
[
"There was an error resolving ./resolvePromise.spec.tst",
],
[
"readFile error",
],
[
"Error reading file",
],
[
"readFile error",
],
[
"There was an error resolving ./resolvePromise.spec.tst",
],
[
"readFile error",
],
[
"Error reading file",
],
[
"readFile error",
],
[
"There was an error resolving ./resolvePromise.spec.tst",
],
[
"readFile error",
],
[
"Error reading file",
],
[
"readFile error",
],
[
"There was an error resolving ./resolvePromise.spec.tst",
],
[
[Error: Cannot find module './resolvePromise.spec.tst' from './src/utils'],
],
[
"There was an error resolving ./resolvePromise.spec.tst",
],
]
`)
})

it('should throw an error for non-existent file', async () => {
fao.existsSync = () => false
await expect(
resolvePromise('./resolvePromise.spec.tst', './src/utils', fao, logger),
Effect.runPromise(
resolveEffect('./resolvePromise.spec.tst', './src/utils', fao, logger),
),
).rejects.toThrowErrorMatchingInlineSnapshot(
"\"Cannot find module './resolvePromise.spec.tst' from './src/utils'\"",
)
expect((logger.error as Mock).mock.calls).toMatchInlineSnapshot(`
[
[
[Error: Cannot find module './resolvePromise.spec.tst' from './src/utils'],
],
[
"there was an error resolving ./resolvePromise.spec.tst",
],
]
`)
[
[
[Error: Cannot find module './resolvePromise.spec.tst' from './src/utils'],
],
[
"There was an error resolving ./resolvePromise.spec.tst",
],
]
`)
})

it('should throw an error if existsSync throws', () => {
fao.existsSync = () => {
throw new Error('existsSync error')
}
expect(
resolvePromise('./resolvePromise.spec.ts', './src/utils', fao, logger),
Effect.runPromise(
resolveEffect('./resolvePromise.spec.ts', './src/utils', fao, logger),
),
).rejects.toThrowErrorMatchingInlineSnapshot('"existsSync error"')
expect(
(logger.error as Mock).mock.calls[0].slice(0, 2),
Expand Down
22 changes: 12 additions & 10 deletions bundlers/bundler/src/utils/resolvePromise.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import type { FileAccessObject, Logger } from '../types'
import { type FileAccessObject, type Logger } from '../types'
import { Effect } from 'effect'
import resolve from 'resolve'

export const resolvePromise = (
export const resolveEffect = (
filePath: string,
basedir: string,
fao: FileAccessObject,
logger: Logger,
): Promise<string> => {
return new Promise<string>((promiseResolve, promiseReject) => {
): Effect.Effect<never, Error, string> => {
return Effect.async<never, Error, string>((resume) => {
resolve(
filePath,
{
basedir,
readFile: (file, cb) => {
fao
.readFile(file, 'utf8')
.then((file) => {
cb(null, file)
.then((fileContent) => {
cb(null, fileContent)
})
.catch((e) => {
logger.error(e)
Expand All @@ -31,17 +32,18 @@ export const resolvePromise = (
cb(e as Error)
logger.error(e as any)
logger.error(`Error checking if isFile ${file}`)
throw e
resume(Effect.fail(e as Error)) // resume with a failure effect when error occurs
return
}
},
},
(err, res) => {
if (err) {
logger.error(err as any)
logger.error(`there was an error resolving ${filePath}`)
promiseReject(err)
logger.error(`There was an error resolving ${filePath}`)
resume(Effect.fail(err)) // resume with a failure effect when error occurs
} else {
promiseResolve(res as string)
resume(Effect.succeed(res as string)) // resume with a success effect when the operation succeeds
}
},
)
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 281d20c

@vercel
Copy link

@vercel vercel bot commented on 281d20c Oct 14, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

evmts-docs – ./

evmts-docs-git-main-evmts.vercel.app
evmts.dev
evmts-docs-evmts.vercel.app

Please sign in to comment.