hightjs 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -16
- package/dist/router.js +26 -12
- package/package.json +3 -2
- package/src/adapters/express.ts +87 -0
- package/src/adapters/factory.ts +112 -0
- package/src/adapters/fastify.ts +104 -0
- package/src/adapters/native.ts +234 -0
- package/src/api/console.ts +305 -0
- package/src/api/http.ts +535 -0
- package/src/bin/hightjs.js +252 -0
- package/src/builder.js +631 -0
- package/src/client/DefaultNotFound.tsx +119 -0
- package/src/client/client.ts +25 -0
- package/src/client/clientRouter.ts +153 -0
- package/src/client/entry.client.tsx +526 -0
- package/src/components/Link.tsx +38 -0
- package/src/global/global.ts +171 -0
- package/src/helpers.ts +631 -0
- package/src/hotReload.ts +569 -0
- package/src/index.ts +557 -0
- package/src/loaders.js +53 -0
- package/src/renderer.tsx +421 -0
- package/src/router.ts +744 -0
- package/src/types/framework.ts +58 -0
- package/src/types.ts +258 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of the HightJS Project.
|
|
3
|
+
* Copyright (c) 2025 itsmuzin
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import boxen, { Options as BoxenOptions } from 'boxen';
|
|
18
|
+
import readline from 'node:readline';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Um "handle" para uma linha dinâmica. As instâncias desta classe
|
|
22
|
+
* são retornadas por `Console.dynamicLine()` e usadas para controlar
|
|
23
|
+
* o conteúdo da linha.
|
|
24
|
+
*/
|
|
25
|
+
export class DynamicLine {
|
|
26
|
+
// A ID é usada internamente pela classe Console para rastrear esta linha.
|
|
27
|
+
private readonly _id = Symbol();
|
|
28
|
+
|
|
29
|
+
constructor(initialContent: string) {
|
|
30
|
+
// Registra esta nova linha na classe Console para que ela seja renderizada.
|
|
31
|
+
Console['registerDynamicLine'](this._id, initialContent);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Atualiza o conteúdo da linha no console.
|
|
36
|
+
* @param newContent O novo texto a ser exibido.
|
|
37
|
+
*/
|
|
38
|
+
update(newContent: string): void {
|
|
39
|
+
Console['updateDynamicLine'](this._id, newContent);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Finaliza a linha, opcionalmente com um texto final, e a torna estática.
|
|
44
|
+
* @param finalContent O texto final a ser exibido.
|
|
45
|
+
*/
|
|
46
|
+
end(finalContent: string): void {
|
|
47
|
+
Console['endDynamicLine'](this._id, finalContent);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export enum Colors {
|
|
52
|
+
Reset = "\x1b[0m",
|
|
53
|
+
Bright = "\x1b[1m",
|
|
54
|
+
Dim = "\x1b[2m",
|
|
55
|
+
Underscore = "\x1b[4m",
|
|
56
|
+
Blink = "\x1b[5m",
|
|
57
|
+
Reverse = "\x1b[7m",
|
|
58
|
+
Hidden = "\x1b[8m",
|
|
59
|
+
|
|
60
|
+
FgBlack = "\x1b[30m",
|
|
61
|
+
FgRed = "\x1b[31m",
|
|
62
|
+
FgGreen = "\x1b[32m",
|
|
63
|
+
FgYellow = "\x1b[33m",
|
|
64
|
+
FgBlue = "\x1b[34m",
|
|
65
|
+
FgMagenta = "\x1b[35m",
|
|
66
|
+
FgCyan = "\x1b[36m",
|
|
67
|
+
FgWhite = "\x1b[37m",
|
|
68
|
+
FgGray = "\x1b[90m", // ← adicionado
|
|
69
|
+
|
|
70
|
+
BgBlack = "\x1b[40m",
|
|
71
|
+
BgRed = "\x1b[41m",
|
|
72
|
+
BgGreen = "\x1b[42m",
|
|
73
|
+
BgYellow = "\x1b[43m",
|
|
74
|
+
BgBlue = "\x1b[44m",
|
|
75
|
+
BgMagenta = "\x1b[45m",
|
|
76
|
+
BgCyan = "\x1b[46m",
|
|
77
|
+
BgWhite = "\x1b[47m",
|
|
78
|
+
BgGray = "\x1b[100m", // ← adicionado
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
export enum Levels {
|
|
83
|
+
ERROR = "ERROR",
|
|
84
|
+
WARN = "WARN",
|
|
85
|
+
INFO = "INFO",
|
|
86
|
+
DEBUG = "DEBUG",
|
|
87
|
+
SUCCESS = "SUCCESS"
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export default class Console {
|
|
91
|
+
// Armazena o estado de todas as linhas dinâmicas ativas
|
|
92
|
+
private static activeLines: { id: symbol; content: string }[] = [];
|
|
93
|
+
|
|
94
|
+
// Quantas linhas foram efetivamente renderizadas na última operação.
|
|
95
|
+
// Usamos esse contador para evitar off-by-one ao mover o cursor para
|
|
96
|
+
// redesenhar o bloco dinâmico.
|
|
97
|
+
private static lastRenderedLines = 0;
|
|
98
|
+
|
|
99
|
+
// --- MÉTODOS PRIVADOS PARA GERENCIAR A RENDERIZAÇÃO ---
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Limpa todas as linhas dinâmicas da tela e as redesenha com o conteúdo atualizado.
|
|
103
|
+
* Observação: usamos lastRenderedLines para saber quantas linhas mover
|
|
104
|
+
* o cursor para cima (isso evita mover para cima demais quando uma nova
|
|
105
|
+
* linha foi registrada).
|
|
106
|
+
*/
|
|
107
|
+
private static redrawDynamicLines(): void {
|
|
108
|
+
const stream = process.stdout;
|
|
109
|
+
|
|
110
|
+
// Se anteriormente renderizamos N linhas, movemos o cursor para cima N
|
|
111
|
+
// para chegar ao topo do bloco dinâmico. Se N for 0, não movemos.
|
|
112
|
+
if (this.lastRenderedLines > 0) {
|
|
113
|
+
try {
|
|
114
|
+
readline.moveCursor(stream, 0, -this.lastRenderedLines);
|
|
115
|
+
} catch (_e) {
|
|
116
|
+
// Em terminais estranhos a movimentação pode falhar — ignoramos.
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Posiciona no início da linha e limpa tudo abaixo (removendo o bloco antigo).
|
|
121
|
+
readline.cursorTo(stream, 0);
|
|
122
|
+
readline.clearScreenDown(stream);
|
|
123
|
+
|
|
124
|
+
// Reescreve as linhas com o conteúdo atualizado
|
|
125
|
+
if (this.activeLines.length > 0) {
|
|
126
|
+
stream.write(this.activeLines.map(l => l.content).join('\n') + '\n');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Atualiza o contador de linhas que agora estão renderizadas.
|
|
130
|
+
this.lastRenderedLines = this.activeLines.length;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Envolve a escrita de texto estático (logs normais) para não interferir
|
|
135
|
+
* com as linhas dinâmicas.
|
|
136
|
+
*/
|
|
137
|
+
private static writeStatic(content: string): void {
|
|
138
|
+
const stream = process.stdout;
|
|
139
|
+
|
|
140
|
+
// Se há um bloco dinâmico na tela, removemos (limpamos) ele antes de
|
|
141
|
+
// escrever o conteúdo estático — assim evitamos misturar a saída.
|
|
142
|
+
if (this.lastRenderedLines > 0) {
|
|
143
|
+
try {
|
|
144
|
+
readline.moveCursor(stream, 0, -this.lastRenderedLines);
|
|
145
|
+
} catch (_e) {}
|
|
146
|
+
readline.cursorTo(stream, 0);
|
|
147
|
+
readline.clearScreenDown(stream);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Garante que o conteúdo termine com quebra de linha.
|
|
151
|
+
if (!content.endsWith('\n')) content += '\n';
|
|
152
|
+
stream.write(content);
|
|
153
|
+
|
|
154
|
+
// Depois de escrever o log estático, redesenhamos o bloco dinâmico abaixo dele.
|
|
155
|
+
if (this.activeLines.length > 0) {
|
|
156
|
+
stream.write(this.activeLines.map(l => l.content).join('\n') + '\n');
|
|
157
|
+
this.lastRenderedLines = this.activeLines.length;
|
|
158
|
+
} else {
|
|
159
|
+
this.lastRenderedLines = 0;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// --- MÉTODOS CHAMADOS PELA CLASSE DynamicLine ---
|
|
164
|
+
private static registerDynamicLine(id: symbol, content: string): void {
|
|
165
|
+
// Adiciona a nova linha à lista de linhas ativas
|
|
166
|
+
this.activeLines.push({ id, content });
|
|
167
|
+
// Redesenha todo o bloco (usa lastRenderedLines internamente)
|
|
168
|
+
this.redrawDynamicLines();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private static updateDynamicLine(id: symbol, newContent: string): void {
|
|
172
|
+
const line = this.activeLines.find(l => l.id === id);
|
|
173
|
+
if (line) {
|
|
174
|
+
line.content = newContent;
|
|
175
|
+
this.redrawDynamicLines();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
private static endDynamicLine(id: symbol, finalContent: string): void {
|
|
180
|
+
const lineIndex = this.activeLines.findIndex(l => l.id === id);
|
|
181
|
+
if (lineIndex > -1) {
|
|
182
|
+
// Remove a linha da lista de ativas.
|
|
183
|
+
this.activeLines.splice(lineIndex, 1);
|
|
184
|
+
|
|
185
|
+
// Escreve o conteúdo final como uma linha estática.
|
|
186
|
+
// writeStatic cuidará de limpar e redesenhar as linhas dinâmicas
|
|
187
|
+
// restantes no lugar certo.
|
|
188
|
+
this.writeStatic(finalContent + '\n');
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// --- MÉTODOS DE LOG PÚBLICOS (MODIFICADOS) ---
|
|
193
|
+
static error(...args: any[]): void { this.log(Levels.ERROR, ...args); }
|
|
194
|
+
static warn(...args: any[]): void { this.log(Levels.WARN, ...args); }
|
|
195
|
+
static info(...args: any[]): void { this.log(Levels.INFO, ...args); }
|
|
196
|
+
static success(...args: any[]): void { this.log(Levels.SUCCESS, ...args); }
|
|
197
|
+
static debug(...args: any[]): void { this.log(Levels.DEBUG, ...args); }
|
|
198
|
+
|
|
199
|
+
static logCustomLevel(levelName: string, without: boolean = true, color?: Colors, ...args: any[]): void {
|
|
200
|
+
if (without) { this.logWithout(levelName as Levels, color, ...args); }
|
|
201
|
+
else { this.log(levelName as Levels, color, ...args); }
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
static logWithout(level: Levels, colors?:Colors, ...args: any[]): void {
|
|
205
|
+
|
|
206
|
+
const color = colors ? colors : level === Levels.ERROR ? Colors.BgRed : level === Levels.WARN ? Colors.BgYellow : level === Levels.INFO ? Colors.BgMagenta : level === Levels.SUCCESS ? Colors.BgGreen : Colors.BgCyan;
|
|
207
|
+
|
|
208
|
+
let output = "";
|
|
209
|
+
for (const arg of args) {
|
|
210
|
+
let msg = (arg instanceof Error) ? arg.stack : (typeof arg === 'string') ? arg : JSON.stringify(arg, null, 2);
|
|
211
|
+
if (msg) output += ` ${color} ${level} ${Colors.Reset} ${msg}\n`;
|
|
212
|
+
}
|
|
213
|
+
this.writeStatic(output);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
static log(level: Levels, colors?: Colors, ...args: any[]): void {
|
|
217
|
+
const color = colors || level === Levels.ERROR ? Colors.BgRed : level === Levels.WARN ? Colors.BgYellow : level === Levels.INFO ? Colors.BgMagenta : level === Levels.SUCCESS ? Colors.BgGreen : Colors.BgCyan;
|
|
218
|
+
let output = "\n";
|
|
219
|
+
for (const arg of args) {
|
|
220
|
+
let msg = (arg instanceof Error) ? arg.stack : (typeof arg === 'string') ? arg : JSON.stringify(arg, null, 2);
|
|
221
|
+
if (msg) output += ` ${color} ${level} ${Colors.Reset} ${msg}\n`;
|
|
222
|
+
}
|
|
223
|
+
this.writeStatic(output);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// --- OUTROS MÉTODOS ---
|
|
227
|
+
static async ask(question: string, defaultValue?: string): Promise<string> {
|
|
228
|
+
// Garantimos que o bloco dinâmico atual seja temporariamente removido
|
|
229
|
+
// (redesenhado depois) para não poluir o prompt.
|
|
230
|
+
const stream = process.stdout;
|
|
231
|
+
if (this.lastRenderedLines > 0) {
|
|
232
|
+
try { readline.moveCursor(stream, 0, -this.lastRenderedLines); } catch (_e) {}
|
|
233
|
+
readline.cursorTo(stream, 0);
|
|
234
|
+
readline.clearScreenDown(stream);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const readlineInterface = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
238
|
+
const coloredQuestion = ` ${Colors.FgMagenta}${question}${Colors.Reset}`;
|
|
239
|
+
const defaultValuePart = defaultValue ? ` [${Colors.Reset}${Colors.FgCyan}${defaultValue}${Colors.Reset}]` : '';
|
|
240
|
+
const fullPrompt = `${coloredQuestion}${defaultValuePart}:\n > `;
|
|
241
|
+
|
|
242
|
+
return new Promise(resolve => {
|
|
243
|
+
readlineInterface.question(fullPrompt, ans => {
|
|
244
|
+
readlineInterface.close();
|
|
245
|
+
const value = ans.trim();
|
|
246
|
+
// Redesenha as linhas dinâmicas após o prompt
|
|
247
|
+
this.redrawDynamicLines();
|
|
248
|
+
resolve(value === '' && defaultValue !== undefined ? defaultValue : value);
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
static async confirm(message: string, defaultYes = false): Promise<boolean> {
|
|
254
|
+
const suffix = defaultYes ? 'yes' : 'no';
|
|
255
|
+
while (true) {
|
|
256
|
+
const ans = (await this.ask(message + " (yes/no)", suffix)).toLowerCase();
|
|
257
|
+
if (!ans) return defaultYes;
|
|
258
|
+
if (['y','yes'].includes(ans)) return true;
|
|
259
|
+
if (['n','no'].includes(ans)) return false;
|
|
260
|
+
this.warn('Resposta inválida, digite y ou n.');
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
static table(data: Record<string, any> | Array<{ Field: string, Value: any }>): void {
|
|
265
|
+
let rows: Array<{ Field: string, Value: any }>;
|
|
266
|
+
if (Array.isArray(data)) {
|
|
267
|
+
rows = data.map(row => ({ Field: String(row.Field), Value: String(row.Value) }));
|
|
268
|
+
} else {
|
|
269
|
+
rows = Object.entries(data).map(([Field, Value]) => ({ Field, Value: String(Value) }));
|
|
270
|
+
}
|
|
271
|
+
const fieldLen = Math.max(...rows.map(r => r.Field.length), 'Field'.length);
|
|
272
|
+
const valueLen = Math.max(...rows.map(r => r.Value.length), 'Value'.length);
|
|
273
|
+
const sep = `+${'-'.repeat(fieldLen+2)}+${'-'.repeat(valueLen+2)}+`;
|
|
274
|
+
let output = sep + '\n';
|
|
275
|
+
output += `| ${Colors.FgGreen}${'Field'.padEnd(fieldLen)}${Colors.Reset} | ${Colors.FgGreen}${'Value'.padEnd(valueLen)}${Colors.Reset} |\n`;
|
|
276
|
+
output += sep + '\n';
|
|
277
|
+
for (const row of rows) {
|
|
278
|
+
output += `| ${row.Field.padEnd(fieldLen)} | ${row.Value.padEnd(valueLen)} |\n`;
|
|
279
|
+
}
|
|
280
|
+
output += sep + '\n';
|
|
281
|
+
this.writeStatic(output);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
static box(content: string, options?: BoxenOptions): void {
|
|
285
|
+
const defaultOptions: BoxenOptions = {
|
|
286
|
+
padding: 1,
|
|
287
|
+
margin: 1,
|
|
288
|
+
borderStyle: 'round',
|
|
289
|
+
borderColor: 'whiteBright',
|
|
290
|
+
titleAlignment: 'left',
|
|
291
|
+
};
|
|
292
|
+
const finalOptions = { ...defaultOptions, ...options };
|
|
293
|
+
const boxedContent = boxen(content, finalOptions);
|
|
294
|
+
this.writeStatic(boxedContent + '\n');
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Cria e retorna um controlador para uma linha dinâmica no console.
|
|
299
|
+
* @param initialContent O conteúdo inicial a ser exibido.
|
|
300
|
+
* @returns Uma instância de DynamicLine para controlar a linha.
|
|
301
|
+
*/
|
|
302
|
+
static dynamicLine(initialContent: string): DynamicLine {
|
|
303
|
+
return new DynamicLine(initialContent);
|
|
304
|
+
}
|
|
305
|
+
}
|