Skip to content

Commit e3b6bff

Browse files
committed
moving terminal out to its own folder
1 parent 691d059 commit e3b6bff

3 files changed

Lines changed: 171 additions & 1 deletion

File tree

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ import RouteEmitter from './emitter/RouteEmitter.js';
9898
import Request, { withUnknownHost } from './router/Request.js';
9999
import Response from './router/Response.js';
100100
import Router from './router/Router.js';
101-
import Terminal, { control } from './router/Terminal.js';
101+
102+
import control from './terminal/control.js';
103+
import Terminal from './terminal/Terminal.js';
102104

103105
import Exception from './Exception.js';
104106
import Reflection from './Reflection.js';

src/terminal/Terminal.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
//lib
3+
import type { TypeOf } from '../types.js';
4+
//data
5+
import { objectFromArgs } from '../data/Nest.js';
6+
//local
7+
import Router from '../router/Router.js';
8+
import control from './control.js';
9+
10+
export default class Terminal<R = unknown, S = unknown>
11+
extends Router<R, S, Terminal<R, S>>
12+
{
13+
//the event command
14+
public readonly command: string;
15+
//input and output controls
16+
protected _control: ReturnType<typeof control>;
17+
//cached cli args
18+
protected _args: string[];
19+
//cached terminal params (parsed argv)
20+
protected _data?: Record<string, any>;
21+
22+
/**
23+
* Returns the cli arguments
24+
*/
25+
public get args() {
26+
return [ ...this._args ];
27+
}
28+
29+
/**
30+
* Returns the terminal brand
31+
*/
32+
public get brand() {
33+
return this._control.brand;
34+
}
35+
36+
/**
37+
* Returns the terminal controls
38+
*/
39+
public get control() {
40+
return Object.freeze(this._control);
41+
}
42+
43+
/**
44+
* Returns the cli parameters
45+
*/
46+
public get data() {
47+
if (!this._data) {
48+
this._data = objectFromArgs(this._args.join(' '));
49+
}
50+
return { ...this._data };
51+
}
52+
53+
/**
54+
* Preloads the input and output settings
55+
*/
56+
constructor(args: string[], brand = '') {
57+
super();
58+
//set event
59+
this.command = args[0] || '';
60+
//set cli args
61+
this._args = args.slice(1);
62+
//set controls
63+
this._control = control(brand);
64+
}
65+
66+
/**
67+
* Retrieves the first value found from the given flag/s in cli
68+
*/
69+
public expect<T>(flags: string[], defaults: TypeOf<T>) {
70+
for (const flag of flags) {
71+
if (this.data[flag]) {
72+
return this.data[flag] as T;
73+
}
74+
}
75+
return defaults;
76+
}
77+
78+
/**
79+
* Runs the command
80+
*/
81+
public run<T = unknown>() {
82+
const req = this.request({ data: this.data });
83+
const res = this.response();
84+
return this.resolve<T>(this.command, req, res);
85+
}
86+
}

src/terminal/control.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//modules
2+
import { input } from '@inquirer/prompts';
3+
4+
/**
5+
* Returns a list of control methods for the terminal
6+
*/
7+
export default function control(brand = '') {
8+
const controls = {
9+
brand,
10+
11+
/**
12+
* Outputs an colorful (red) log
13+
*/
14+
error(message: string, variables: string[] = []) {
15+
controls.output(message, variables, '\x1b[31m%s\x1b[0m');
16+
},
17+
18+
/**
19+
* Outputs an colorful (blue) log
20+
*/
21+
info(message: string, variables: string[] = []) {
22+
controls.output(message, variables, '\x1b[34m%s\x1b[0m');
23+
},
24+
25+
/**
26+
* Asks for input
27+
*/
28+
async input(question: string, answer?: string) {
29+
return await input({
30+
message: question,
31+
default: answer,
32+
required: typeof answer !== 'string'
33+
});
34+
},
35+
36+
/**
37+
* Outputs a log
38+
*/
39+
output(
40+
message: string,
41+
variables: string[] = [],
42+
color?: string
43+
) {
44+
//add variables to message
45+
for (const variable of variables) {
46+
message = message.replace('%s', variable);
47+
}
48+
//add brand to message
49+
message = `${controls.brand} ${message}`.trim();
50+
//colorize the message
51+
if (color) {
52+
console.log(color, message);
53+
return;
54+
}
55+
//or just output the message
56+
console.log(message);
57+
},
58+
59+
/**
60+
* Outputs a success log
61+
*/
62+
success(message: string, variables: string[] = []) {
63+
controls.output(message, variables, '\x1b[32m%s\x1b[0m');
64+
},
65+
66+
/**
67+
* Outputs a system log
68+
*/
69+
system(message: string, variables: string[] = []) {
70+
controls.output(message, variables, '\x1b[35m%s\x1b[0m');
71+
},
72+
73+
/**
74+
* Outputs a warning log
75+
*/
76+
warning(message: string, variables: string[] = []) {
77+
controls.output(message, variables, '\x1b[33m%s\x1b[0m');
78+
}
79+
};
80+
81+
return controls;
82+
};

0 commit comments

Comments
 (0)