Skip to content

Commit

Permalink
♻️ chore(config): Convert to jsdoc explicit exports (#589)
Browse files Browse the repository at this point in the history
## Description

Converts @evmts/config to js with jsdoc. Makes public exports explicit

Addresses #586
Addresses #517

## Testing

Explain the quality checks that have been done on the code changes

## 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 16, 2023
1 parent 5b8dab6 commit 32c7f25
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 166 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-owls-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@evmts/config": patch
---

Converted @evmts/config to js with jsdoc and explicit exports
2 changes: 1 addition & 1 deletion config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
],
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.js",
"module": "src/index.js",
"types": "types/src/index.d.ts",
"files": [
"dist",
Expand Down
21 changes: 21 additions & 0 deletions config/src/Config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getDefaultSolcVersion } from './getDefaultSolcVersion.js'
import { z } from 'zod'

export const CompilerConfigValidator = z
.strictObject({
name: z.literal('@evmts/ts-plugin').optional(),
solcVersion: z.string().optional(),
foundryProject: z.union([z.boolean(), z.string()]).optional(),
libs: z.array(z.string()).optional(),
remappings: z.record(z.string()).optional(),
})
.describe('Configuration for EVMts')

export const defaultConfig = {
get solcVersion() {
return getDefaultSolcVersion()
},
foundryProject: false,
remappings: {},
libs: [],
}
22 changes: 9 additions & 13 deletions config/src/defineConfig.ts → config/src/defineConfig.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import {
type CompilerConfig,
CompilerConfigValidator,
type ResolvedCompilerConfig,
defaultConfig,
} from './Config.js'
import { CompilerConfigValidator, defaultConfig } from './Config.js'
import { execSync } from 'child_process'
import * as path from 'path'

export type DefineConfig = (configFactory: () => CompilerConfig) => {
configFn: (configFilePath: string) => ResolvedCompilerConfig
}

export const defineConfig: DefineConfig = (configFactory) => ({
/**
* @type {import("./types.js").DefineConfig}
*/
export const defineConfig = (configFactory) => ({
configFn: (configFilePath) => {
const parsedConfig = CompilerConfigValidator.safeParse(
configFactory() ?? {},
Expand Down Expand Up @@ -51,8 +45,10 @@ or lib directly in your EVMts compiler config
)
}

// Process remappings
const remappings: Record<string, string> = {}
/**
* @type {Record<string, string>}
*/
const remappings = {}
if (forgeConfig.remappings) {
for (const remap of forgeConfig.remappings) {
const parts = remap.split('=')
Expand Down
3 changes: 1 addition & 2 deletions config/src/defineConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { type CompilerConfig, defaultConfig } from './Config.js'
import { defineConfig } from './defineConfig.js'
import { type CompilerConfig, defaultConfig, defineConfig } from './index.js'
import { execSync } from 'child_process'
import { type MockedFunction, describe, expect, it, vi } from 'vitest'

Expand Down
23 changes: 23 additions & 0 deletions config/src/fileExists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { constants } from 'fs'
import { access } from 'fs/promises'

/**
* Checks if a file exists at the given path
* @param {string} path - path to check
* @returns {Promise<boolean>} true if the file exists, false otherwise
* @example
* ```typescript
* import { fileExists } from '@eth-optimism/config'
* await fileExists('./someFile.txt')
* ```
*/
export const fileExists = async (path) => {
try {
// TODO not the most robust check for existence here
await access(path, constants.F_OK)
return true
} catch (e) {
// TODO should be inspecting the error here
return false
}
}
13 changes: 0 additions & 13 deletions config/src/fileExists.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { createRequire } from 'module'

/**
* Gets the solc version of node_modules
* @returns {string} version
* @example
* getDefaultSolcVersion() // "0.8.16"
*/
export const getDefaultSolcVersion = () => {
const moduleRequire = createRequire(import.meta.url ?? __dirname)
const solc = moduleRequire('solc')
Expand Down
9 changes: 9 additions & 0 deletions config/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @typedef {import('./types.js').CompilerConfig} CompilerConfig
* @typedef {import('./types.js').ResolvedCompilerConfig} ResolvedCompilerConfig
*/

export { defaultConfig } from './Config.js'
export { loadConfig } from './loadConfig.js'
export { loadConfigAsync } from './loadConfigAsync.js'
export { defineConfig } from './defineConfig.js'
4 changes: 0 additions & 4 deletions config/src/index.ts

This file was deleted.

31 changes: 14 additions & 17 deletions config/src/loadConfig.ts → config/src/loadConfig.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type { CompilerConfig, ResolvedCompilerConfig } from './Config.js'
import { defineConfig } from './defineConfig.js'
import { existsSync, readFileSync } from 'fs'
import { parse } from 'jsonc-parser'
import * as path from 'path'

type LoadConfig = (
configFilePath: string,
logger?: Pick<typeof console, 'error' | 'warn'>,
) => ResolvedCompilerConfig

export const loadConfig: LoadConfig = (configFilePath, logger = console) => {
/**
* @type {import("./types.js").LoadConfig}
*/
export const loadConfig = (configFilePath, logger = console) => {
/**
* evmts.config.ts currently doesn't work for ts-plugin because it is not syncronous
* for now load config will load from tsconfig instead until fixed
Expand All @@ -27,12 +24,10 @@ export const loadConfig: LoadConfig = (configFilePath, logger = console) => {
`Failed to read the file at ${tsConfigPath}. Make sure the file exists and is accessible.`,
)
}
let configJson: {
compilerOptions: {
plugins?: Array<{ name: '@evmts/ts-plugin' } & CompilerConfig>
baseUrl?: string
}
}
/**
* @type {{compilerOptions: {plugins?: Array<{ name: '@evmts/ts-plugin' } & import("./types.js").CompilerConfig>, baseUrl?: string }}}
*/
let configJson
try {
configJson = parse(configStr)
if (!configJson.compilerOptions) {
Expand All @@ -43,10 +38,12 @@ export const loadConfig: LoadConfig = (configFilePath, logger = console) => {
throw new Error(`tsconfig.json at ${tsConfigPath} is not valid json`)
}

let config: CompilerConfig | undefined =
configJson?.compilerOptions?.plugins?.find(
(plugin) => plugin.name === '@evmts/ts-plugin',
)
/**
* @type {import("./types.js").CompilerConfig | undefined}
*/
let config = configJson?.compilerOptions?.plugins?.find(
(plugin) => plugin.name === '@evmts/ts-plugin',
)

if (!config) {
logger.warn(
Expand Down
32 changes: 14 additions & 18 deletions config/src/loadConfigAsync.ts → config/src/loadConfigAsync.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import type { CompilerConfig, ResolvedCompilerConfig } from './Config.js'
import { defineConfig } from './defineConfig.js'
import { fileExists as defaultFileExists } from './fileExists.js'
import { readFile } from 'fs/promises'
import { parse } from 'jsonc-parser'
import * as path from 'path'

export type LoadConfigAsync = (
configFilePath: string,
logger?: Pick<typeof console, 'error' | 'warn'>,
fileExists?: typeof defaultFileExists,
) => Promise<ResolvedCompilerConfig>

export const loadConfigAsync: LoadConfigAsync = async (
/**
* @type {import("./types.js").LoadConfigAsync}
*/
export const loadConfigAsync = async (
configFilePath,
logger = console,
fileExists = defaultFileExists,
Expand All @@ -33,12 +29,10 @@ export const loadConfigAsync: LoadConfigAsync = async (
`Failed to read the file at ${tsConfigPath}. Make sure the file exists and is accessible.`,
)
}
let configJson: {
compilerOptions: {
plugins?: Array<{ name: '@evmts/ts-plugin' } & CompilerConfig>
baseUrl?: string
}
}
/**
* @type {{compilerOptions: {plugins?: Array<{ name: '@evmts/ts-plugin' } & import("./types.js").CompilerConfig>, baseUrl?: string }}}
*/
let configJson
try {
configJson = parse(configStr)
if (!configJson.compilerOptions) {
Expand All @@ -49,10 +43,12 @@ export const loadConfigAsync: LoadConfigAsync = async (
throw new Error(`tsconfig.json at ${tsConfigPath} is not valid json`)
}

let config: CompilerConfig | undefined =
configJson?.compilerOptions?.plugins?.find(
(plugin) => plugin.name === '@evmts/ts-plugin',
)
/**
* @type {import("./types.js").CompilerConfig | undefined}
*/
let config = configJson?.compilerOptions?.plugins?.find(
(plugin) => plugin.name === '@evmts/ts-plugin',
)

if (!config) {
logger.warn(
Expand Down
33 changes: 14 additions & 19 deletions config/src/Config.ts → config/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getDefaultSolcVersion } from './getDefaultSolcVersion.js'
import { z } from 'zod'
import { fileExists as defaultFileExists } from './fileExists.js'

/**
* Configuration of the solidity compiler
Expand Down Expand Up @@ -29,21 +28,17 @@ export type CompilerConfig = {

export type ResolvedCompilerConfig = Required<CompilerConfig>

export const CompilerConfigValidator = z
.strictObject({
name: z.literal('@evmts/ts-plugin').optional(),
solcVersion: z.string().optional(),
foundryProject: z.union([z.boolean(), z.string()]).optional(),
libs: z.array(z.string()).optional(),
remappings: z.record(z.string()).optional(),
})
.describe('Configuration for EVMts')

export const defaultConfig = {
get solcVersion() {
return getDefaultSolcVersion()
},
foundryProject: false,
remappings: {},
libs: [],
export type DefineConfig = (configFactory: () => CompilerConfig) => {
configFn: (configFilePath: string) => ResolvedCompilerConfig
}

export type LoadConfig = (
configFilePath: string,
logger?: Pick<typeof console, 'error' | 'warn'>,
) => ResolvedCompilerConfig

export type LoadConfigAsync = (
configFilePath: string,
logger?: Pick<typeof console, 'error' | 'warn'>,
fileExists?: typeof defaultFileExists,
) => Promise<ResolvedCompilerConfig>
4 changes: 2 additions & 2 deletions config/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { defineConfig } from 'tsup'

export default defineConfig({
name: '@evmts/core',
entry: ['src/index.ts'],
entry: ['src/index.js'],
outDir: 'dist',
format: ['esm', 'cjs'],
format: ['cjs'],
splitting: false,
sourcemap: true,
clean: true,
Expand Down
2 changes: 1 addition & 1 deletion config/typedoc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://typedoc.org/schema.json",
"out": "../docs/reference/config",
"entryPoints": ["./src/index.ts"],
"entryPoints": ["./src/index.js"],
"publicPath": "/reference/config/",
"plugin": ["typedoc-plugin-markdown"],
"gitRevision": "main"
Expand Down
Loading

1 comment on commit 32c7f25

@vercel
Copy link

@vercel vercel bot commented on 32c7f25 Oct 16, 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-evmts.vercel.app
evmts.dev
evmts-docs-git-main-evmts.vercel.app

Please sign in to comment.