Skip to content
This repository has been archived by the owner on May 23, 2022. It is now read-only.

Commit

Permalink
add some code
Browse files Browse the repository at this point in the history
  • Loading branch information
pikadun committed Dec 10, 2020
1 parent 2307d64 commit 761aed9
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ node_modules/

# Compile output
lib/
types/
types/*.ts
!types/internal/commands.d.ts
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
36 changes: 36 additions & 0 deletions eng/gencmd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import axios from 'axios';
import { parse, Node } from 'node-html-parser';

interface Commands {
command: string;
args: string | null;
summary: string;
}

async function dodo() {
const res = await axios.get('https://redis.io/commands');
const html = await res.data;
const root = parse(html);
const commands = root.querySelectorAll('#commands li a')
.map(e => e.childNodes.filter(cn => cn.nodeType === 1))
.map(e => handleHTMLElement(e));

convertCommandsToTypeScript(commands);
}

function handleHTMLElement(datas: Node[]) {
const command = datas[0]?.childNodes[0]?.rawText.trim();
const args = datas[0]?.childNodes[1]?.childNodes[0]?.rawText.trim().split(/(\n| )+/).join('') || null;
const summary = datas[1]?.childNodes[0]?.rawText.trim();
return {
command,
args,
summary
} as Commands;
}

function convertCommandsToTypeScript(commands: Commands[]) {
console.log(commands);
}

dodo();
28 changes: 26 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "template",
"version": "1.3.0",
"main": "lib/app.js",
"name": "redis",
"version": "0.0.1",
"main": "lib/index.js",
"repository": {
"type": "git",
"url": "https://github.com/pikadun/redis"
Expand All @@ -12,7 +12,8 @@
"coverage": "nyc --nycrc-path=./eng/.nycrc.json npm test",
"format": "ts-node ./eng/lint.ts",
"watch": "tsc --build ./eng/tsconfig.dev.json --watch",
"compile": "tsc --build ./eng/tsconfig.prod.json"
"compile": "tsc --build ./eng/tsconfig.prod.json",
"gencmd": "ts-node ./eng/gencmd.ts"
},
"keywords": [],
"husky": {
Expand All @@ -29,9 +30,11 @@
"@types/node": "^14.14.10",
"@typescript-eslint/eslint-plugin": "^4.9.1",
"@typescript-eslint/parser": "^4.9.1",
"axios": "^0.21.0",
"eslint": "^7.15.0",
"husky": "^4.3.5",
"mocha": "^8.2.1",
"node-html-parser": "^2.0.0",
"nyc": "^15.1.0",
"ts-node": "^9.1.1",
"typescript": "^4.1.2"
Expand Down
32 changes: 32 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Socket } from 'net';

export interface IClientOptions {
host?: string;
port?: number;
auth?: string;
}

interface IClient {
connect(): void;
}

export class Client implements IClient {
private socket = new Socket();
constructor(
private readonly options: IClientOptions = {}
) {
this.initOptions(options);
}

public connect(): void {
this.socket.connect(
this.options.port as number,
this.options.host as string
);
}

private initOptions(options: IClientOptions): void {
options.host = options.host || '127.0.0.1';
options.port = options.port || 6379;
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Client, IClientOptions } from './client';

0 comments on commit 761aed9

Please sign in to comment.