Skip to content

Commit

Permalink
🧹 Preparation work for lz:deploy task (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Feb 2, 2024
1 parent 4258ef3 commit 40e8dba
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
7 changes: 7 additions & 0 deletions .changeset/metal-trains-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@layerzerolabs/devtools-evm-hardhat": patch
"@layerzerolabs/io-devtools": patch
"@layerzerolabs/devtools": patch
---

Remove CommaSeparatedValuesSchema in favor of splitCommaSeparated; Move LogLevelSchema; Export isLogLevel
33 changes: 5 additions & 28 deletions packages/devtools-evm-hardhat/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,16 @@ import { types as builtInTypes } from 'hardhat/config'
import { HardhatError } from 'hardhat/internal/core/errors'
import { ERRORS } from 'hardhat/internal/core/errors-list'
import type { CLIArgumentType } from 'hardhat/types'
import { z } from 'zod'
import { LogLevel } from '@layerzerolabs/io-devtools'

/**
* Helper zod schema that splits a comma-separated string
* into individual values, trimming the results
*/
const CommaSeparatedValuesSchema = z.string().transform((value) =>
value
.trim()
.split(/\s*,\s*/)
.filter(Boolean)
)

const LogLevelSchema = z.nativeEnum(LogLevel)
import { splitCommaSeparated } from '@layerzerolabs/devtools'
import { isLogLevel, LogLevel } from '@layerzerolabs/io-devtools'

/**
* Hardhat CLI type for a comma separated list of arbitrary strings
*/
const csv: CLIArgumentType<string[]> = {
name: 'csv',
parse(name: string, value: string) {
const result = CommaSeparatedValuesSchema.safeParse(value)
if (!result.success) {
throw new HardhatError(ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, {
value,
name: name,
type: 'csv',
})
}

return result.data
return splitCommaSeparated(value)
},
validate() {},
}
Expand All @@ -46,16 +24,15 @@ const csv: CLIArgumentType<string[]> = {
const logLevel: CLIArgumentType<LogLevel> = {
name: 'logLevel',
parse(name: string, value: string) {
const result = LogLevelSchema.safeParse(value)
if (!result.success) {
if (!isLogLevel(value)) {
throw new HardhatError(ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, {
value,
name: name,
type: 'logLevel',
})
}

return result.data
return value
},
validate() {},
}
Expand Down
1 change: 1 addition & 0 deletions packages/devtools/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './assertion'
export * from './bytes'
export * from './promise'
export * from './strings'
12 changes: 12 additions & 0 deletions packages/devtools/src/common/strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Splits a comma-separated string into individual values
* and discards any whitespace.
*
* @param {string} value
* @returns {string[]}
*/
export const splitCommaSeparated = (value: string): string[] =>
value
.trim()
.split(/\s*,\s*/)
.filter(Boolean)
13 changes: 8 additions & 5 deletions packages/io-devtools/src/stdio/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Format } from 'logform'
import { createLogger as createWinstonLogger, format, transports, type Logger } from 'winston'
import { z } from 'zod'

/**
* Re-export for ease of use
Expand All @@ -19,20 +20,22 @@ export enum LogLevel {
silly = 'silly',
}

const LogLevelSchema = z.nativeEnum(LogLevel)

/**
* Type assertion utility for LogLevel
* Type assertion utility for `LogLevel`
*
* @param value `unknown`
* @returns `value is LogLevel`
* @param {unknown} value
* @returns {boolean}
*/
const isLogLevel = (value: unknown): value is LogLevel => typeof value === 'string' && value in LogLevel
export const isLogLevel = (value: unknown): value is LogLevel => LogLevelSchema.safeParse(value).success

let DEFAULT_LOG_LEVEL = LogLevel.info

/**
* Sets the default log level used when creating new loggers.
*
* @param level `LogLevel`
* @param {string} level
*/
export const setDefaultLogLevel = (level: string) => {
if (!isLogLevel(level)) {
Expand Down

0 comments on commit 40e8dba

Please sign in to comment.