convoker 0.3.0 → 0.3.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/dist/color.d.ts +153 -200
- package/dist/color.js +142 -48
- package/dist/command.d.ts +218 -555
- package/dist/command.js +513 -448
- package/dist/error.d.ts +107 -864
- package/dist/error.js +100 -10
- package/dist/index.d.ts +6 -1224
- package/dist/index.js +6 -13
- package/dist/input.d.ts +182 -252
- package/dist/input.js +185 -10
- package/dist/log.d.ts +61 -0
- package/dist/log.js +216 -0
- package/dist/{prompt.d.ts → prompt/index.d.ts} +193 -258
- package/dist/prompt/index.js +273 -0
- package/dist/prompt/raw.js +105 -9
- package/dist/standard-schema.d.ts +62 -0
- package/dist/standard-schema.js +16 -0
- package/dist/utils.d.ts +30 -0
- package/dist/utils.js +56 -0
- package/package.json +4 -5
- package/dist/chunks/__vite-browser-external-DQYBmsno.js +0 -80
- package/dist/chunks/color-CiruG_zQ.js +0 -153
- package/dist/chunks/error-CyKscMUD.js +0 -95
- package/dist/chunks/index-BluQjWvw.js +0 -198
- package/dist/chunks/input-WNu16aNE.js +0 -138
- package/dist/chunks/standard-schema-BHKzvwIS.js +0 -12
- package/dist/chunks/utils-DdmSEjLc.js +0 -22
- package/dist/prompt.js +0 -17
- package/dist/raw.d.ts +0 -38
package/dist/log.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type Theme } from "./color";
|
|
2
|
+
/**
|
|
3
|
+
* The logger configuration.
|
|
4
|
+
*/
|
|
5
|
+
export interface LogConfig {
|
|
6
|
+
/**
|
|
7
|
+
* The format to print as.
|
|
8
|
+
*/
|
|
9
|
+
format: "text" | "json" | "xml" | "yaml" | "csv";
|
|
10
|
+
/**
|
|
11
|
+
* Standard output.
|
|
12
|
+
*/
|
|
13
|
+
stdout: WritableStream<string>;
|
|
14
|
+
/**
|
|
15
|
+
* Standard error.
|
|
16
|
+
*/
|
|
17
|
+
stderr: WritableStream<string>;
|
|
18
|
+
/**
|
|
19
|
+
* Standard input.
|
|
20
|
+
*/
|
|
21
|
+
stdin: ReadableStream<string>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Sets a new theme.
|
|
25
|
+
* @param t The theme.
|
|
26
|
+
*/
|
|
27
|
+
export declare function setTheme(t: Theme): void;
|
|
28
|
+
/**
|
|
29
|
+
* Sets new configuration.
|
|
30
|
+
* @param c The config.
|
|
31
|
+
*/
|
|
32
|
+
export declare function setConfig(c: Partial<LogConfig>): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Sets default configuration.
|
|
35
|
+
*/
|
|
36
|
+
export declare function setup(): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Prints debug information.
|
|
39
|
+
* @param msgs The messages to write.
|
|
40
|
+
*/
|
|
41
|
+
export declare function trace(...msgs: any[]): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Prints information.
|
|
44
|
+
* @param msgs The messages to write.
|
|
45
|
+
*/
|
|
46
|
+
export declare function info(...msgs: any[]): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Prints warnings.
|
|
49
|
+
* @param msgs The messages to write.
|
|
50
|
+
*/
|
|
51
|
+
export declare function warn(...msgs: any[]): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Prints errors.
|
|
54
|
+
* @param msgs The messages to write.
|
|
55
|
+
*/
|
|
56
|
+
export declare function error(...msgs: any[]): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Prints errors and exits.
|
|
59
|
+
* @param msgs The messages to write.
|
|
60
|
+
*/
|
|
61
|
+
export declare function fatal(...msgs: any[]): Promise<void>;
|
package/dist/log.js
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { DEFAULT_THEME } from "./color";
|
|
2
|
+
import { merge, isNode, isDeno, isBun } from "./utils";
|
|
3
|
+
/**
|
|
4
|
+
* Gets the default stdout, in a cross-runtime way.
|
|
5
|
+
* @returns The default stdout.
|
|
6
|
+
*/
|
|
7
|
+
async function getDefaultStdout() {
|
|
8
|
+
if (isNode && process.stdout?.writable) {
|
|
9
|
+
const { Writable } = await import("node:stream");
|
|
10
|
+
return Writable.toWeb(process.stdout);
|
|
11
|
+
}
|
|
12
|
+
if (isDeno && Deno.stdout?.writable) {
|
|
13
|
+
return Deno.stdout.writable;
|
|
14
|
+
}
|
|
15
|
+
if (isBun && Bun.stdout) {
|
|
16
|
+
return Bun.stdout;
|
|
17
|
+
}
|
|
18
|
+
// Workers: emulate with console.log
|
|
19
|
+
return new WritableStream({
|
|
20
|
+
write(chunk) {
|
|
21
|
+
console.log(String(chunk));
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Gets the default stderr, in a cross-runtime way.
|
|
27
|
+
* @returns The default stderr.
|
|
28
|
+
*/
|
|
29
|
+
async function getDefaultStderr() {
|
|
30
|
+
if (isNode && process.stderr?.writable) {
|
|
31
|
+
const { Writable } = await import("node:stream");
|
|
32
|
+
return Writable.toWeb(process.stderr);
|
|
33
|
+
}
|
|
34
|
+
if (isDeno && Deno.stderr?.writable) {
|
|
35
|
+
return Deno.stderr.writable;
|
|
36
|
+
}
|
|
37
|
+
if (isBun && Bun.stderr) {
|
|
38
|
+
return Bun.stderr;
|
|
39
|
+
}
|
|
40
|
+
// Workers: emulate with console.error
|
|
41
|
+
return new WritableStream({
|
|
42
|
+
write(chunk) {
|
|
43
|
+
console.error(String(chunk));
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Gets the default stdin, in a cross-runtime way.
|
|
49
|
+
* @returns The default stdin.
|
|
50
|
+
*/
|
|
51
|
+
async function getDefaultStdin() {
|
|
52
|
+
if (isNode && process.stdin?.readable) {
|
|
53
|
+
const { Readable } = await import("node:stream");
|
|
54
|
+
return Readable.toWeb(process.stdin);
|
|
55
|
+
}
|
|
56
|
+
if (isDeno && Deno.stdin?.readable) {
|
|
57
|
+
return Deno.stdin.readable;
|
|
58
|
+
}
|
|
59
|
+
if (isBun) {
|
|
60
|
+
return Bun.stdin.stream();
|
|
61
|
+
}
|
|
62
|
+
// Workers don't support stdin
|
|
63
|
+
return new ReadableStream({
|
|
64
|
+
start(controller) {
|
|
65
|
+
controller.close();
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
let theme = DEFAULT_THEME;
|
|
70
|
+
let config = undefined;
|
|
71
|
+
/**
|
|
72
|
+
* Sets a new theme.
|
|
73
|
+
* @param t The theme.
|
|
74
|
+
*/
|
|
75
|
+
export function setTheme(t) {
|
|
76
|
+
theme = t;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Sets new configuration.
|
|
80
|
+
* @param c The config.
|
|
81
|
+
*/
|
|
82
|
+
export async function setConfig(c) {
|
|
83
|
+
config = merge({
|
|
84
|
+
format: "text",
|
|
85
|
+
stdout: await getDefaultStdout(),
|
|
86
|
+
stderr: await getDefaultStderr(),
|
|
87
|
+
stdin: await getDefaultStdin(),
|
|
88
|
+
}, c);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Sets default configuration.
|
|
92
|
+
*/
|
|
93
|
+
export async function setup() {
|
|
94
|
+
await setConfig({});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Formats a message to the correct format.
|
|
98
|
+
* @param level The level of mesage.
|
|
99
|
+
* @param msgs The messages to format.
|
|
100
|
+
* @returns The formatted message.
|
|
101
|
+
*/
|
|
102
|
+
function formatMessages(level, ...msgs) {
|
|
103
|
+
const timestamp = new Date().toISOString();
|
|
104
|
+
const msg = msgs
|
|
105
|
+
.map((m) => (typeof m === "string" ? m : JSON.stringify(m, null, 2)))
|
|
106
|
+
.join(" ");
|
|
107
|
+
switch (config.format) {
|
|
108
|
+
case "json":
|
|
109
|
+
return JSON.stringify({ timestamp, level, message: msg }) + "\n";
|
|
110
|
+
case "xml":
|
|
111
|
+
return `<log>
|
|
112
|
+
<timestamp>${timestamp}</timestamp>
|
|
113
|
+
<level>${level}</level>
|
|
114
|
+
<message>${msg}</message>
|
|
115
|
+
</log>\n`;
|
|
116
|
+
case "yaml":
|
|
117
|
+
return `- timestamp: ${timestamp}
|
|
118
|
+
level: ${level}
|
|
119
|
+
message: "${msg.replace(/"/g, '\\"')}"\n`;
|
|
120
|
+
case "csv":
|
|
121
|
+
return `"${timestamp}","${level}","${msg.replace(/"/g, '""')}"\n`;
|
|
122
|
+
case "text":
|
|
123
|
+
default:
|
|
124
|
+
return `[${timestamp}] [${level.toUpperCase()}] ${msg}\n`;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Colorizes text.
|
|
129
|
+
* @param level The log level.
|
|
130
|
+
* @param text The text to colorize.
|
|
131
|
+
* @returns The colorized text.
|
|
132
|
+
*/
|
|
133
|
+
function colorize(level, text) {
|
|
134
|
+
switch (level) {
|
|
135
|
+
case "trace":
|
|
136
|
+
return theme.secondary ? theme.secondary(text) : text;
|
|
137
|
+
case "info":
|
|
138
|
+
return theme.info ? theme.info(text) : text;
|
|
139
|
+
case "warn":
|
|
140
|
+
return theme.warning ? theme.warning(text) : text;
|
|
141
|
+
case "error":
|
|
142
|
+
return theme.error ? theme.error(text) : text;
|
|
143
|
+
case "fatal":
|
|
144
|
+
return theme.error
|
|
145
|
+
? theme.error(theme.styles?.bold?.(text) ?? text)
|
|
146
|
+
: text;
|
|
147
|
+
default:
|
|
148
|
+
return text;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Writes to a stream.
|
|
153
|
+
* @param stream The stream to write to.
|
|
154
|
+
* @param msg The message to write.
|
|
155
|
+
*/
|
|
156
|
+
async function writeToStream(stream, msg) {
|
|
157
|
+
const writer = stream.getWriter();
|
|
158
|
+
try {
|
|
159
|
+
await writer.write(msg);
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
writer.releaseLock();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Prints debug information.
|
|
167
|
+
* @param msgs The messages to write.
|
|
168
|
+
*/
|
|
169
|
+
export async function trace(...msgs) {
|
|
170
|
+
const formatted = formatMessages("trace", ...msgs);
|
|
171
|
+
const colored = colorize("trace", formatted);
|
|
172
|
+
await writeToStream(config.stdout, colored);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Prints information.
|
|
176
|
+
* @param msgs The messages to write.
|
|
177
|
+
*/
|
|
178
|
+
export async function info(...msgs) {
|
|
179
|
+
const formatted = formatMessages("info", ...msgs);
|
|
180
|
+
const colored = colorize("info", formatted);
|
|
181
|
+
await writeToStream(config.stdout, colored);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Prints warnings.
|
|
185
|
+
* @param msgs The messages to write.
|
|
186
|
+
*/
|
|
187
|
+
export async function warn(...msgs) {
|
|
188
|
+
const formatted = formatMessages("warn", ...msgs);
|
|
189
|
+
const colored = colorize("warn", formatted);
|
|
190
|
+
await writeToStream(config.stdout, colored);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Prints errors.
|
|
194
|
+
* @param msgs The messages to write.
|
|
195
|
+
*/
|
|
196
|
+
export async function error(...msgs) {
|
|
197
|
+
const formatted = formatMessages("error", ...msgs);
|
|
198
|
+
const colored = colorize("error", formatted);
|
|
199
|
+
await writeToStream(config.stderr, colored);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Prints errors and exits.
|
|
203
|
+
* @param msgs The messages to write.
|
|
204
|
+
*/
|
|
205
|
+
export async function fatal(...msgs) {
|
|
206
|
+
const formatted = formatMessages("fatal", ...msgs);
|
|
207
|
+
const colored = colorize("fatal", formatted);
|
|
208
|
+
await writeToStream(config.stderr, colored);
|
|
209
|
+
// Exit depending on runtime
|
|
210
|
+
if (isDeno) {
|
|
211
|
+
Deno.exit(-1);
|
|
212
|
+
}
|
|
213
|
+
else if (isNode || isBun) {
|
|
214
|
+
process.exit(-1);
|
|
215
|
+
}
|
|
216
|
+
}
|