Skip to content

Commit

Permalink
🧹 CLI: Add type exports to CLI package [2/N] (#704)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Jul 17, 2024
1 parent 776f9cd commit 059fbc8
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-planets-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@layerzerolabs/devtools-cli": patch
---

Add exports to CLI package to expose types
54 changes: 54 additions & 0 deletions packages/devtools-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@

This package provides a network-agnostic CLI interface for configuring LayerZero OApp contracts.

---

**Please note** that this package is in a **beta** state and backwards-incompatible changes might be introduced in future releases. The functionality is not yet on par with <a href="https://www.npmjs.com/package/@layerzerolabs/toolbox-hardhat"><code>@layerzerolabs/toolbox-hardhat</code></a>. While we strive to comply to [semver](https://semver.org/), we can not guarantee to avoid breaking changes in minor releases.

---

### Quick start

#### Installation

The CLI is distributed as an NPM package, we recommend the following ways of running it:

```bash
npx @layerzerolabs/devtools-cli@latest
# or
Expand All @@ -29,3 +41,45 @@ pnpm @layerzerolabs/devtools-cli
# or
bunx @layerzerolabs/devtools-cli
```

#### Configuration

The configuration of the CLI consists of two parts:

- **OApp configuration** that describes the desired state of your OApp (typically called `layerzero.config.ts`)
- **CLI setup** that creates the necessary functionality for the CLI to run - SDKs, configuration functions, signing functions (typically called `layerzero.setup.ts`)

The main difference between this CLI and `@layerzerolabs/toolbox-hardhat` is the `layerzero.setup.ts` file. While in `@layerzerolabs/toolbox-hardhat` lot of the functionality can be inferred based on `hardhat`, it is not possible to infer this functionality in a generic CLI environment.

##### CLI setup

The following is an example of a setup file that loads the EVM functionality based on `hardhat`.

```typescript
import {
createConnectedContractFactory,
createSignerFactory,
createDefaultContext,
} from "@layerzerolabs/devtools-evm-hardhat";
import { createOAppFactory } from "@layerzerolabs/ua-devtools-evm";

import type { CLISetup } from "@layerzerolabs/devtools-cli";

/**
* Since we are not in hardhat CLI, we'll need to create the context first
*/
createDefaultContext();

/**
* This is a setup file for @layerzerolabs/devtools-cli.
*
* At the moment, @layerzerolabs/devtools-cli is in development
* and will be available
*/
const setup: CLISetup = {
createSdk: createOAppFactory(createConnectedContractFactory()),
createSigner: createSignerFactory(),
};

export default setup;
```
2 changes: 1 addition & 1 deletion packages/devtools-cli/cli.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node

import('./dist/index.js');
import('./dist/cli.js');
12 changes: 11 additions & 1 deletion packages/devtools-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
"directory": "packages/devtools-cli"
},
"license": "MIT",
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.js",
"import": "./dist/index.mjs"
}
},
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"bin": {
"@layerzerolabs/devtools-cli": "./cli.js"
},
Expand All @@ -22,7 +32,7 @@
],
"scripts": {
"prebuild": "tsc -noEmit",
"build": "$npm_execpath tsup",
"build": "$npm_execpath tsup --clean",
"clean": "rm -rf dist",
"dev": "$npm_execpath tsup --watch",
"lint": "$npm_execpath eslint '**/*.{js,ts,json}'",
Expand Down
5 changes: 5 additions & 0 deletions packages/devtools-cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Command } from 'commander'
import { version, name } from '../package.json'
import { oapp } from './commands/oapp'

new Command(name).description('CLI for configuring LayerZero OApps').version(version).addCommand(oapp).parseAsync()
6 changes: 1 addition & 5 deletions packages/devtools-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
import { Command } from 'commander'
import { version, name } from '../package.json'
import { oapp } from './commands/oapp'

new Command(name).description('CLI for configuring LayerZero OApps').version(version).addCommand(oapp).parseAsync()
export * from './types'
2 changes: 1 addition & 1 deletion packages/devtools-cli/src/setup/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './loading'
export * from './schema'
export * from './types'
export * from './typescript'
2 changes: 1 addition & 1 deletion packages/devtools-cli/src/setup/loading.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createModuleLogger, importDefault, isFile, isReadable, printZodErrors } from '@layerzerolabs/io-devtools'
import { resolve } from 'path'
import { CLISetup } from './types'
import { CLISetup } from '@/types'
import { CLISetupSchema } from './schema'

export const createSetupLoader =
Expand Down
4 changes: 3 additions & 1 deletion packages/devtools-cli/src/setup/schema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { z } from 'zod'
import type { CLISetup } from './types'
import type { CLISetup } from '@/types'
import { EndpointIdSchema, OmniPointSchema } from '@layerzerolabs/devtools'

export const CLISetupSchema: z.ZodSchema<CLISetup, z.ZodTypeDef, unknown> = z.object({
createSdk: z.function().args(OmniPointSchema).returns(z.any()),
createSigner: z.function().args(EndpointIdSchema).returns(z.any()),
configure: z.function().args(z.any(), z.any()).returns(z.any()).optional(),
loadConfig: z.function().args(z.string()).returns(z.any()).optional(),
})
6 changes: 0 additions & 6 deletions packages/devtools-cli/src/setup/types.ts

This file was deleted.

15 changes: 15 additions & 0 deletions packages/devtools-cli/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type {
Configurator,
Factory,
IOmniSDK,
OmniGraph,
OmniSDKFactory,
OmniSignerFactory,
} from '@layerzerolabs/devtools'

export interface CLISetup<TOmniGraph extends OmniGraph = OmniGraph<unknown, unknown>, TOmniSDK = IOmniSDK> {
createSdk: OmniSDKFactory<TOmniSDK>
createSigner: OmniSignerFactory
configure?: Configurator<TOmniGraph, TOmniSDK>
loadConfig?: Factory<[configPath: string], TOmniGraph>
}
38 changes: 24 additions & 14 deletions packages/devtools-cli/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { defineConfig } from 'tsup'

export default defineConfig({
entry: ['src/index.ts'],
outDir: './dist',
clean: true,
dts: false,
minify: true,
sourcemap: false,
splitting: false,
treeshake: true,
format: ['cjs'],
env: {
NODE_ENV: 'production',
export default defineConfig([
{
entry: ['src/cli.ts'],
outDir: './dist',
dts: false,
minify: false,
sourcemap: false,
splitting: false,
treeshake: true,
format: ['cjs'],
env: {
NODE_ENV: 'production',
},
external: ['yoga-layout-prebuilt'],
},
external: ['yoga-layout-prebuilt'],
})
{
entry: ['src/index.ts'],
outDir: './dist',
dts: true,
sourcemap: true,
splitting: false,
treeshake: true,
format: ['cjs', 'esm'],
},
])

0 comments on commit 059fbc8

Please sign in to comment.