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 761aed9 commit dfa1fe3
Show file tree
Hide file tree
Showing 7 changed files with 1,355 additions and 27 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ node_modules/

# Compile output
lib/
types/*.ts
!types/internal/commands.d.ts
types/
49 changes: 44 additions & 5 deletions eng/gencmd.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import axios from 'axios';
import { parse, Node } from 'node-html-parser';
import fs from 'fs';

interface Commands {
interface Command {
command: string;
args: string | null;
summary: string;
Expand All @@ -15,7 +16,8 @@ async function dodo() {
.map(e => e.childNodes.filter(cn => cn.nodeType === 1))
.map(e => handleHTMLElement(e));

convertCommandsToTypeScript(commands);
fs.writeFileSync('./src/commands.json', JSON.stringify(commands));
generateTypescriptInterface(commands);
}

function handleHTMLElement(datas: Node[]) {
Expand All @@ -26,11 +28,48 @@ function handleHTMLElement(datas: Node[]) {
command,
args,
summary
} as Commands;
} as Command;
}

function convertCommandsToTypeScript(commands: Commands[]) {
console.log(commands);
const result = new Array<string>();
const tab = '\u0020\u0020\u0020\u0020';

function generateTypescriptInterface(commands: Command[]) {
appendHeader();
result.push('export interface ClientCommand {\n');
for (let i = 0; i < commands.length; i++) {
const command = commands[i] as Command;
appendComment(command);
appendMethod(command);
}
result.push('}');
fs.writeFileSync('./src/clientCommand.ts', result.join(''));
}

function appendHeader() {
result.push('/**\n');
result.push(`\u0020*\u0020Automatically generated on ${new Date()}\n`);
result.push('\u0020*/\n');
result.push('\n');

result.push('/* eslint-disable @typescript-eslint/no-explicit-any */\n');
}

function appendComment(command: Command) {
result.push(`${tab}/**\n`);
result.push(`${tab}\u0020*\u0020${command.summary}\n`);
if (command.args !== null) {
result.push(`${tab}\u0020*\u0020@param\u0020args\u0020${command.args}\n`);
}
result.push(`${tab}\u0020*/\n`);
}

function appendMethod(command: Command) {
result.push(`${tab}${command.command.replace(/( |-)/g, '')}<T>(`);
if (command.args !== null) {
result.push('...args: any');
}
result.push('): Promise<T>;\n');
}

dodo();
7 changes: 0 additions & 7 deletions src/app.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Socket } from 'net';
import { ClientCommand } from './clientCommand';

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

interface IClient {
export interface Client extends ClientCommand {
connect(): void;
}

export class Client implements IClient {
export class Client implements Client {
private socket = new Socket();
constructor(
private readonly options: IClientOptions = {}
Expand Down
Loading

0 comments on commit dfa1fe3

Please sign in to comment.