Skip to content

Commit

Permalink
another try
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysu committed Apr 9, 2024
1 parent 52bb9ac commit fc7f91f
Show file tree
Hide file tree
Showing 12 changed files with 946 additions and 172 deletions.
1 change: 1 addition & 0 deletions examples/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"devDependencies": {
"@lens-protocol/eslint-config": "workspace:*",
"@lens-protocol/prettier-config": "workspace:*",
"@swc/core": "^1.4.13",
"@types/node": "^18.18.12",
"@types/uuid": "^9.0.7",
"prettier": "^2.8.8",
Expand Down
5 changes: 3 additions & 2 deletions examples/node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "es6"
"target": "ESNext"
},
"include": ["scripts"],
"ts-node": {
"transpileOnly": true
"transpileOnly": true,
"swc": true
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@
"packages": [
"packages/api-bindings",
"packages/blockchain-bindings",
"packages/cli",
"packages/client",
"packages/domain",
"packages/gated-content",
"packages/react-native",
"packages/react-web",
"packages/react-native",
"packages/react",
"packages/shared-kernel",
"packages/storage",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ First install dependencies. Run all commands from the monorepo root directory.
pnpm install
```

You might consider prepbuilding LensClient package for faster execution.
Start the development mode. It will watch for changes and rebuild the CLI.

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

Then link the package globally.
Expand Down
40 changes: 14 additions & 26 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,49 @@
"type": "git",
"url": "git://github.com/lens-protocol/lens-sdk.git"
},
"private": true,
"main": "dist/lens-protocol-cli.cjs.js",
"module": "dist/lens-protocol-cli.esm.js",
"main": "dist/index.js",
"type": "module",
"bin": {
"lens": "./dist/lens-protocol-cli.cjs.js"
"lens": "./dist/index.js"
},
"scripts": {
"build": "preconstruct build && pnpm run postbuild",
"postbuild": "node scripts/add-shebang.mjs",
"build": "tsup src/index.ts --format esm",
"dev": "tsup src/index.ts --format esm --watch",
"lens": "ts-node ./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": "jest --passWithNoTests",
"test:watch": "jest --watch",
"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": "workspace:*",
"@lens-protocol/shared-kernel": "workspace:*",
"@lens-protocol/client": "*",
"@lens-protocol/shared-kernel": "*",
"chalk": "^5.3.0",
"commander": "^12.0.0",
"nanospinner": "^1.1.0",
"tslib": "^2.6.2"
},
"devDependencies": {
"@babel/core": "^7.23.3",
"@babel/preset-env": "^7.23.3",
"@babel/preset-typescript": "^7.23.3",
"@lens-protocol/eslint-config": "workspace:*",
"@lens-protocol/prettier-config": "workspace:*",
"@lens-protocol/tsconfig": "workspace:*",
"@swc/core": "^1.4.13",
"@types/node": "^18.18.12",
"eslint": "^8.54.0",
"jest": "^29.7.0",
"prettier": "^3.1.0",
"ts-node": "^10.9.2",
"tsup": "^8.0.2",
"typescript": "5.2.2"
},
"engines": {
"node": ">=18 <21"
},
"prettier": "@lens-protocol/prettier-config",
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-typescript"
]
},
"preconstruct": {
"entrypoints": [
"index.ts"
]
}
"prettier": "@lens-protocol/prettier-config"
}
14 changes: 0 additions & 14 deletions packages/cli/scripts/add-shebang.mjs

This file was deleted.

22 changes: 10 additions & 12 deletions packages/cli/src/commands/createTestProfile.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import { Command } from '@commander-js/extra-typings';
import { isAddress } from '@ethersproject/address';
import { isRelaySuccess, isValidHandle } from '@lens-protocol/client';
import { assertError } from '@lens-protocol/shared-kernel';
import { createSpinner } from 'nanospinner';

import {
ensureParentCommand,
getHandlePrefix,
initLensClient,
} from '../utils/commandToEnvironment';
} from '../utils/commandToEnvironment.js';

const createTestProfile = 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 validatorSpinner = createSpinner(`Validating input data`).start();
const validation = createSpinner(`Validating input data`).start();

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

if (!isAddress(options.address)) {
validatorSpinner.error();
validation.error();
console.error(`Invalid address: ${options.address}`);
process.exit(1);
}
Expand All @@ -38,13 +37,13 @@ const createTestProfile = new Command('create-profile')
});

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

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

Expand All @@ -55,18 +54,17 @@ const createTestProfile = new Command('create-profile')
});

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

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

spinner.success();
creation.success();
console.log(`Profile created successfully`);
} catch (error) {
assertError(error);
spinner.error();
creation.error();
console.error(error);
process.exit(1);
}
Expand Down
38 changes: 13 additions & 25 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
// preconstruct doesn't like the shebang line, it is added in postbuild script
// #!/usr/bin/env node
#!/usr/bin/env node

import { program, Command } from '@commander-js/extra-typings';
import { program } from '@commander-js/extra-typings';

import { createTestProfile } from './commands/createTestProfile';
import { createTestProfile } from './commands/createTestProfile.js';
import './utils/logger.js';

program.name('lens').description('Lens CLI');

// top level commands - define the environment
const development = new Command('development');
development.description('Command will run in development environment');
program
.command('development')
.alias('dev')
.description('Command will run in the development environment')
.addCommand(createTestProfile);

const dev = new Command('dev');
dev.description('Command will run in development environment');

const production = new Command('production');
production.description('Command will run in production environment');

const prod = new Command('prod');
prod.description('Command will run in production environment');

// add subcommands
development.addCommand(createTestProfile);
dev.addCommand(createTestProfile);

// add to main program
program.addCommand(development);
program.addCommand(dev);
program.addCommand(production);
program.addCommand(prod);
program
.command('production')
.alias('prod')
.description('Command will run in the production environment');

program.parse();
6 changes: 1 addition & 5 deletions packages/cli/src/utils/commandToEnvironment.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Command } from '@commander-js/extra-typings';
import { LensClient, development, production } from '@lens-protocol/client';
import { LensClient, production, development } from '@lens-protocol/client';

const commandToEnvironmentMap = {
production: production,
prod: production,
development: development,
dev: development,
};

type EnvCommandName = keyof typeof commandToEnvironmentMap;
Expand Down Expand Up @@ -33,10 +31,8 @@ export function initLensClient(name: EnvCommandName) {
export function getHandlePrefix(name: EnvCommandName) {
switch (name) {
case 'production':
case 'prod':
return 'lens';
case 'development':
case 'dev':
return 'test';
}
}
12 changes: 12 additions & 0 deletions packages/cli/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import chalk from 'chalk';

const log = console.log;
const error = console.error;

console.log = (...args: Parameters<typeof console.log>) => {
log(chalk.green(...args));
};

console.error = (...args: Parameters<typeof console.error>) => {
error(chalk.red(...args));
};
11 changes: 7 additions & 4 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"extends": "@lens-protocol/tsconfig/base.json",
"compilerOptions": {
"esModuleInterop": true,
"module": "ESNext",
"moduleResolution": "node",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "dist"
},
"include": ["./src"]
"include": ["src"],
"ts-node": {
"transpileOnly": true,
"swc": true
}
}
Loading

0 comments on commit fc7f91f

Please sign in to comment.