Skip to content

Commit

Permalink
🧹 CLI: Add TypeScript support; Fix missing --log-level CLI flag [4/N] (…
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Jul 17, 2024
1 parent 8ed825a commit ac2ce91
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-oranges-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@layerzerolabs/devtools-cli": patch
---

Add TypeScript support
39 changes: 27 additions & 12 deletions packages/devtools-cli/src/commands/oapp/wire/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import { Command } from 'commander'
import { printLogo } from '@layerzerolabs/io-devtools/swag'
import { createLogger, setDefaultLogLevel } from '@layerzerolabs/io-devtools'
import { type WithLogLevelOption, type WithSetupOption, createSetupFileOption } from '@/commands/options'
import {
type WithLogLevelOption,
type WithSetupOption,
WithTsConfigOption,
createLogLevelOption,
createSetupFileOption,
createTsConfigFileOption,
} from '@/commands/options'
import { setupTypescript } from '@/setup'

interface Args extends WithLogLevelOption, WithSetupOption {}
interface Args extends WithLogLevelOption, WithSetupOption, WithTsConfigOption {}

export const wire = new Command('wire').addOption(createSetupFileOption()).action(async ({ setup, logLevel }: Args) => {
printLogo()
export const wire = new Command('wire')
.addOption(createSetupFileOption())
.addOption(createTsConfigFileOption())
.addOption(createLogLevelOption())
.action(async ({ setup, logLevel, tsConfig: tsConfigPath }: Args) => {
printLogo()

// We'll set the global logging level to get as much info as needed
setDefaultLogLevel(logLevel)
// We'll set the global logging level to get as much info as needed
setDefaultLogLevel(logLevel)

const logger = createLogger(logLevel)
// We'll setup TypeScript support so that we can dynamically load TypeScript config files
setupTypescript(tsConfigPath)

logger.debug(`Loading setup from ${setup}`)
const logger = createLogger(logLevel)

logger.warn(
`This command is just a placeholder. Please use @layerzerolabs/toolbox-hardhat package for the time being.`
)
})
logger.debug(`Loading setup from ${setup}`)

logger.warn(
`This command is just a placeholder. Please use @layerzerolabs/toolbox-hardhat package for the time being.`
)
})
7 changes: 7 additions & 0 deletions packages/devtools-cli/src/commands/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ export const createLogLevelOption = () =>

return value
})

export interface WithTsConfigOption {
tsConfig?: string
}

export const createTsConfigFileOption = () =>
new Option('--ts-config <path>', 'Path to TypeScript config file (tsconfig.json)').default('tsconfig.json')
55 changes: 55 additions & 0 deletions packages/devtools-cli/src/setup/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { createModuleLogger } from '@layerzerolabs/io-devtools'
import { resolve } from 'path'

export const setupTypescript = (tsConfigPath?: string): void => {
const logger = createModuleLogger('TypeScript support')

try {
require.resolve('typescript')
} catch {
return (
logger.debug(
`typescript module not available. To enable TypeScript support for devtools, please install typescript NPM module`
),
undefined
)
}

try {
require.resolve('ts-node')
} catch {
return (
logger.debug(
`ts-node module not available. To enable TypeScript support for devtools, please install ts-node NPM module`
),
undefined
)
}

// In case a custom tsconfig is required, we specify it using an env variable
if (tsConfigPath !== undefined) {
logger.debug(`Using tsconfig from ${tsConfigPath} (${resolve(tsConfigPath)})`)

process.env.TS_NODE_PROJECT = resolve(tsConfigPath)
}

if (process.env.TS_NODE_FILES === undefined) {
process.env.TS_NODE_FILES = 'true'
}

try {
// tsup will optimize requires if string lterals are used directly in require()
//
// So require('ts-node/register') would result in ts-node being bundled with the module
const tsNode = 'ts-node/register/transpile-only'

require(tsNode)
} catch (error) {
return (
logger.debug(
`Failed to register ts-node. TypeScript support for devtools will not be available. Error:\n\n${error}`
),
undefined
)
}
}

0 comments on commit ac2ce91

Please sign in to comment.