Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intro Lens CLI proof of concept with create test profile command #903

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/little-scissors-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lens-protocol/cli": minor
---

**feat:** Initial version of Lens CLI - a CLI tool for Lens developers. Initial commands: `create-profile` and `investigate`.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"eslint.workingDirectories": [
"examples/nextjs",
"examples/lens-next-app",
"examples/node",
"examples/react-native",
"examples/shared",
"examples/web",
"packages/api-bindings",
"packages/blockchain-bindings",
"packages/cli",
"packages/client",
"packages/domain",
"packages/eslint-config",
Expand Down
2 changes: 1 addition & 1 deletion examples/node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "es6"
"target": "ESNext"
},
"include": ["scripts"],
"ts-node": {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
11 changes: 11 additions & 0 deletions packages/cli/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
project: './tsconfig.json',
},
extends: ['@lens-protocol/eslint-config'],
rules: {
'no-console': 'off',
},
};
1 change: 1 addition & 0 deletions packages/cli/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
58 changes: 58 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Lens CLI

CLI tool for Lens developers.

**Please note, this is an experimental tool and it may undergo significant changes in the future.**

## Usage

Install the Lens CLI from npm.

```bash
npm install -g @lens-protocol/cli
```

See the available commands. The first level command decides the environment: `development` or `production`.
You can use shorthand `dev` and `prod` instead.

```bash
lens development --help
```

or

```bash
lens production --help
```

## Local Setup

First install dependencies. Run all commands from the monorepo root directory.

```bash
pnpm install
```

Start the development mode. It will watch for changes and rebuild the CLI.

```bash
pnpm --dir packages/cli dev
```

Then link the package globally.

```bash
pnpm --dir packages/cli link --global
```

Now you can run the CLI from anywhere.

```bash
lens --help
```

Delete the global link when you're done.

```bash
pnpm remove --global @lens-protocol/cli
```
54 changes: 54 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@lens-protocol/cli",
"version": "0.0.0",
"description": "Lens CLI",
"repository": {
"directory": "packages/cli",
"type": "git",
"url": "git://github.com/lens-protocol/lens-sdk.git"
},
"main": "dist/index.js",
"type": "module",
"bin": {
"lens": "./dist/index.js"
},
"scripts": {
"build": "tsup src/index.ts --format esm",
"dev": "tsup src/index.ts --format esm --watch",
"lens": "ts-node --esm ./src/index.ts",
"eslint:fix": "pnpm run eslint --fix",
"eslint": "eslint src",
"lint": "pnpm run prettier && pnpm run eslint && pnpm run tsc",
"lint:fix": "pnpm run prettier:fix && pnpm run eslint:fix && pnpm run tsc",
"prettier:fix": "prettier --write .",
"prettier": "prettier --check .",
"test": "echo \"Not in scope for this example\"",
"tsc": "tsc --noEmit"
},
"license": "MIT",
"dependencies": {
"@commander-js/extra-typings": "^12.0.1",
"@ethersproject/address": "^5.7.0",
"@lens-protocol/client": "*",
"@lens-protocol/shared-kernel": "*",
"chalk": "^5.3.0",
"commander": "^12.0.0",
"nanospinner": "^1.1.0",
"tslib": "^2.6.2"
},
"devDependencies": {
"@lens-protocol/eslint-config": "workspace:*",
"@lens-protocol/prettier-config": "workspace:*",
"@lens-protocol/tsconfig": "workspace:*",
"@types/node": "^18.19.31",
"eslint": "^8.57.0",
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
"tsup": "^8.0.2",
"typescript": "5.2.2"
},
"engines": {
"node": ">=18 <21"
},
"prettier": "@lens-protocol/prettier-config"
}
72 changes: 72 additions & 0 deletions packages/cli/src/commands/createTestProfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Command } from '@commander-js/extra-typings';
import { isAddress } from '@ethersproject/address';
import { isRelaySuccess, isValidHandle } from '@lens-protocol/client';
import { createSpinner } from 'nanospinner';

import { ensureParentCommand, initLensClient } from '../lib/commandToEnvironment.js';
import { output } from '../lib/output.js';

export function createTestProfile() {
const cmd = new Command('create-profile')
.description('Create a new test profile, possible only in the development environment')
.requiredOption('-h, --handle <handle>', 'Test profile handle')
.requiredOption('-a, --address <address>', 'Wallet address')
.action(async (options) => {
const validation = createSpinner(`Validating input data`).start();

if (!isValidHandle(options.handle)) {
validation.error();
output.error(`Invalid handle: ${options.handle}`);
process.exit(1);
}

if (!isAddress(options.address)) {
validation.error();
output.error(`Invalid address: ${options.address}`);
process.exit(1);
}

const parentCommandName = ensureParentCommand(cmd);
const client = initLensClient(parentCommandName);

// check if the requested handle is available
const handleOwnerAddress = await client.handle.resolveAddress({
handle: `lens/${options.handle}`,
});

if (handleOwnerAddress) {
validation.error();
output.error(`The requested handle "${options.handle}" is not available.`);
process.exit(1);
}
validation.success();

const creation = createSpinner(
`Creating new test profile with handle "${options.handle}" for address "${options.address}"`,
).start();

try {
const profileCreateResult = await client.wallet.createProfileWithHandle({
handle: options.handle,
to: options.address,
});

if (!isRelaySuccess(profileCreateResult)) {
creation.error();
output.error(`Something went wrong:`, profileCreateResult);
process.exit(1);
}

await client.transaction.waitUntilComplete({ forTxId: profileCreateResult.txId });

creation.success();
output.success(`Profile created successfully`);
} catch (error) {
creation.error();
output.error(error);
process.exit(1);
}
});

return cmd;
}
Loading
Loading