convoker 0.3.4 → 0.4.0
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/LICENSE +1 -1
- package/dist/color/index.d.mts +2 -0
- package/dist/{color.mjs → color/index.mjs} +1 -2
- package/dist/{color-OlJQTTxb.mjs → color-DiZvJ0Fc.mjs} +40 -11
- package/dist/color-DiZvJ0Fc.mjs.map +1 -0
- package/dist/command/index.d.mts +5 -0
- package/dist/command/index.mjs +8 -0
- package/dist/{command-D2UiQBNA.mjs → command-8P8qXJ2o.mjs} +98 -195
- package/dist/command-8P8qXJ2o.mjs.map +1 -0
- package/dist/{index-Dikc5KAP.d.mts → index-BYLskLxk.d.mts} +5 -4
- package/dist/{color-BuHvMolk.d.mts → index-BtbthYjp.d.mts} +13 -3
- package/dist/{input-B12iaqb8.d.mts → index-ClhbwSD8.d.mts} +20 -5
- package/dist/{command-BXmfoT-l.d.mts → index-Cnx4H4D-.d.mts} +10 -25
- package/dist/index.d.mts +33 -69
- package/dist/index.mjs +91 -9
- package/dist/index.mjs.map +1 -0
- package/dist/input/index.d.mts +3 -0
- package/dist/input/index.mjs +4 -0
- package/dist/{input-DRy_sVxZ.mjs → input-XUsy1LCQ.mjs} +5 -3
- package/dist/input-XUsy1LCQ.mjs.map +1 -0
- package/dist/prompt/index.d.mts +4 -4
- package/dist/prompt/index.mjs +4 -6
- package/dist/prompt/raw.d.mts +1 -1
- package/dist/prompt/raw.mjs +1 -2
- package/dist/{prompt-Cvufljin.mjs → prompt-a5Ix_Hyc.mjs} +30 -58
- package/dist/prompt-a5Ix_Hyc.mjs.map +1 -0
- package/dist/{raw--889icsd.mjs → raw-DEtZFeMv.mjs} +4 -21
- package/dist/raw-DEtZFeMv.mjs.map +1 -0
- package/dist/{raw-BqvlveTU.d.mts → raw-cqTp2vds.d.mts} +2 -1
- package/dist/standard-schema-DTuaYJjO.mjs +32 -0
- package/dist/standard-schema-DTuaYJjO.mjs.map +1 -0
- package/dist/{standard-schema-DLeKaehR.d.mts → standard-schema-DXS-QnwX.d.mts} +3 -2
- package/package.json +14 -17
- package/dist/color.d.mts +0 -2
- package/dist/command.d.mts +0 -5
- package/dist/command.mjs +0 -10
- package/dist/error-C1S1gs8L.mjs +0 -115
- package/dist/error.d.mts +0 -5
- package/dist/error.mjs +0 -3
- package/dist/input.d.mts +0 -3
- package/dist/input.mjs +0 -5
- package/dist/standard-schema-DBXbMy6L.mjs +0 -17
- package/dist/utils-ChmY93uA.mjs +0 -45
|
@@ -1,8 +1,22 @@
|
|
|
1
|
-
import { t as StandardSchemaV1 } from "./standard-schema-
|
|
1
|
+
import { t as StandardSchemaV1 } from "./standard-schema-DXS-QnwX.mjs";
|
|
2
2
|
|
|
3
|
-
//#region src/input.d.ts
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
//#region src/input/error.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Thrown when the command fails to validate an input.
|
|
6
|
+
*/
|
|
7
|
+
declare class InputValidationError extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* A list of messages.
|
|
10
|
+
*/
|
|
11
|
+
messages: string[];
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new input validation error.
|
|
14
|
+
* @param messages The messages.
|
|
15
|
+
*/
|
|
16
|
+
constructor(messages: string[]);
|
|
17
|
+
}
|
|
18
|
+
declare namespace index_d_exports {
|
|
19
|
+
export { BasicKind, InferEntry, InferInput, Input, InputValidationError, Kind, Option, Positional, TypeOf, argument, convert, option, positional };
|
|
6
20
|
}
|
|
7
21
|
/**
|
|
8
22
|
* An input object.
|
|
@@ -184,4 +198,5 @@ declare function positional<T extends Kind>(kind: T): Positional<T>;
|
|
|
184
198
|
*/
|
|
185
199
|
declare function argument<T extends Kind>(kind: T): Positional<T>;
|
|
186
200
|
//#endregion
|
|
187
|
-
export { Kind as a, TypeOf as c,
|
|
201
|
+
export { Kind as a, TypeOf as c, index_d_exports as d, option as f, Input as i, argument as l, InputValidationError as m, InferEntry as n, Option as o, positional as p, InferInput as r, Positional as s, BasicKind as t, convert as u };
|
|
202
|
+
//# sourceMappingURL=index-ClhbwSD8.d.mts.map
|
|
@@ -1,24 +1,8 @@
|
|
|
1
|
-
import { n as Theme } from "./
|
|
2
|
-
import { i as Input, o as Option, r as InferInput, s as Positional } from "./
|
|
1
|
+
import { n as Theme } from "./index-BtbthYjp.mjs";
|
|
2
|
+
import { i as Input, o as Option, r as InferInput, s as Positional } from "./index-ClhbwSD8.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/command/error.d.ts
|
|
3
5
|
|
|
4
|
-
//#region src/error.d.ts
|
|
5
|
-
declare namespace error_d_exports {
|
|
6
|
-
export { ConvokerError, HelpAskedError, InputValidationError, MissingRequiredArgumentError, MissingRequiredOptionError, TooManyArgumentsError, UnknownOptionError };
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Thrown when the command fails to validate an input.
|
|
10
|
-
*/
|
|
11
|
-
declare class InputValidationError extends Error {
|
|
12
|
-
/**
|
|
13
|
-
* A list of messages.
|
|
14
|
-
*/
|
|
15
|
-
messages: string[];
|
|
16
|
-
/**
|
|
17
|
-
* Creates a new input validation error.
|
|
18
|
-
* @param messages The messages.
|
|
19
|
-
*/
|
|
20
|
-
constructor(messages: string[]);
|
|
21
|
-
}
|
|
22
6
|
/**
|
|
23
7
|
* A Convoker-related error. These are usually handled by default.
|
|
24
8
|
*/
|
|
@@ -111,7 +95,7 @@ declare class MissingRequiredArgumentError extends ConvokerError {
|
|
|
111
95
|
constructor(command: Command<any>, key: string, entry: Positional<any, any, any>);
|
|
112
96
|
}
|
|
113
97
|
//#endregion
|
|
114
|
-
//#region src/command.d.ts
|
|
98
|
+
//#region src/command/index.d.ts
|
|
115
99
|
/**
|
|
116
100
|
* What the command is an alias for.
|
|
117
101
|
*/
|
|
@@ -268,11 +252,11 @@ declare class Command<T extends Input = Input> {
|
|
|
268
252
|
*/
|
|
269
253
|
error(fn: ErrorFn<T>): this;
|
|
270
254
|
/**
|
|
271
|
-
* Adds
|
|
272
|
-
* @param
|
|
255
|
+
* Adds existing commands to this.
|
|
256
|
+
* @param commands The commands.
|
|
273
257
|
* @returns this
|
|
274
258
|
*/
|
|
275
|
-
add(
|
|
259
|
+
add(...commands: Command<any>[]): this;
|
|
276
260
|
/**
|
|
277
261
|
* Creates a new subcommand and adds it.
|
|
278
262
|
* @param names The aliases of the subcommand.
|
|
@@ -328,4 +312,5 @@ declare class Command<T extends Input = Input> {
|
|
|
328
312
|
run(argv?: string[]): Promise<this>;
|
|
329
313
|
}
|
|
330
314
|
//#endregion
|
|
331
|
-
export { ErrorFn as a, ConvokerError as c,
|
|
315
|
+
export { ErrorFn as a, ConvokerError as c, MissingRequiredOptionError as d, TooManyArgumentsError as f, CommandAlias as i, HelpAskedError as l, Builder as n, MiddlewareFn as o, UnknownOptionError as p, Command as r, ParseResult as s, ActionFn as t, MissingRequiredArgumentError as u };
|
|
316
|
+
//# sourceMappingURL=index-Cnx4H4D-.d.mts.map
|
package/dist/index.d.mts
CHANGED
|
@@ -1,73 +1,37 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import "./standard-schema-
|
|
3
|
-
import { d as
|
|
4
|
-
import { a as ErrorFn,
|
|
5
|
-
import "./raw-
|
|
6
|
-
import { d as index_d_exports } from "./index-
|
|
1
|
+
import { J as DeepPartial, M as index_d_exports, n as Theme } from "./index-BtbthYjp.mjs";
|
|
2
|
+
import "./standard-schema-DXS-QnwX.mjs";
|
|
3
|
+
import { d as index_d_exports$1 } from "./index-ClhbwSD8.mjs";
|
|
4
|
+
import { a as ErrorFn, c as ConvokerError, d as MissingRequiredOptionError, f as TooManyArgumentsError, i as CommandAlias, l as HelpAskedError, n as Builder, o as MiddlewareFn, p as UnknownOptionError, r as Command, s as ParseResult, t as ActionFn, u as MissingRequiredArgumentError } from "./index-Cnx4H4D-.mjs";
|
|
5
|
+
import "./raw-cqTp2vds.mjs";
|
|
6
|
+
import { d as index_d_exports$3 } from "./index-BYLskLxk.mjs";
|
|
7
|
+
import Stream from "node:stream";
|
|
7
8
|
|
|
8
|
-
//#region src/log.d.ts
|
|
9
|
-
declare
|
|
10
|
-
|
|
9
|
+
//#region src/log/error.d.ts
|
|
10
|
+
declare class WriteError extends Error {
|
|
11
|
+
constructor(streamName: string);
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*/
|
|
15
|
-
interface LogConfig {
|
|
16
|
-
/**
|
|
17
|
-
* The format to print as.
|
|
18
|
-
*/
|
|
19
|
-
format: "text" | "json" | "xml" | "yaml" | "csv";
|
|
20
|
-
/**
|
|
21
|
-
* Standard output.
|
|
22
|
-
*/
|
|
23
|
-
stdout: WritableStream<string>;
|
|
24
|
-
/**
|
|
25
|
-
* Standard error.
|
|
26
|
-
*/
|
|
27
|
-
stderr: WritableStream<string>;
|
|
28
|
-
/**
|
|
29
|
-
* Standard input.
|
|
30
|
-
*/
|
|
31
|
-
stdin: ReadableStream<string>;
|
|
13
|
+
declare namespace index_d_exports$2 {
|
|
14
|
+
export { Config, DEFAULT_CONFIG, WriteError, error, fatal, info, setConfig, setTheme, trace, warn };
|
|
32
15
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
declare function
|
|
52
|
-
/**
|
|
53
|
-
* Prints information.
|
|
54
|
-
* @param msgs The messages to write.
|
|
55
|
-
*/
|
|
56
|
-
declare function info(...msgs: any[]): Promise<void>;
|
|
57
|
-
/**
|
|
58
|
-
* Prints warnings.
|
|
59
|
-
* @param msgs The messages to write.
|
|
60
|
-
*/
|
|
61
|
-
declare function warn(...msgs: any[]): Promise<void>;
|
|
62
|
-
/**
|
|
63
|
-
* Prints errors.
|
|
64
|
-
* @param msgs The messages to write.
|
|
65
|
-
*/
|
|
66
|
-
declare function error(...msgs: any[]): Promise<void>;
|
|
67
|
-
/**
|
|
68
|
-
* Prints errors and exits.
|
|
69
|
-
* @param msgs The messages to write.
|
|
70
|
-
*/
|
|
71
|
-
declare function fatal(...msgs: any[]): Promise<void>;
|
|
16
|
+
declare function setTheme(theme: Theme): void;
|
|
17
|
+
interface Config {
|
|
18
|
+
format: "text" | "json";
|
|
19
|
+
stdout: Stream.Writable;
|
|
20
|
+
stderr: Stream.Writable;
|
|
21
|
+
jsonSpace: number;
|
|
22
|
+
}
|
|
23
|
+
declare const DEFAULT_CONFIG: Config;
|
|
24
|
+
declare function setConfig({
|
|
25
|
+
theme,
|
|
26
|
+
...cfg
|
|
27
|
+
}: DeepPartial<Config> & {
|
|
28
|
+
theme?: Theme;
|
|
29
|
+
}): void;
|
|
30
|
+
declare function trace(...msgs: any[]): void;
|
|
31
|
+
declare function info(...msgs: any[]): void;
|
|
32
|
+
declare function warn(...msgs: any[]): void;
|
|
33
|
+
declare function error(...msgs: any[]): void;
|
|
34
|
+
declare function fatal(...msgs: any[]): void;
|
|
72
35
|
//#endregion
|
|
73
|
-
export { ActionFn, Builder, Command, CommandAlias, ErrorFn, MiddlewareFn,
|
|
36
|
+
export { ActionFn, Builder, Command, CommandAlias, ConvokerError, ErrorFn, HelpAskedError, MiddlewareFn, MissingRequiredArgumentError, MissingRequiredOptionError, ParseResult, TooManyArgumentsError, UnknownOptionError, index_d_exports as color, index_d_exports$1 as i, index_d_exports$2 as log, index_d_exports$3 as prompt };
|
|
37
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,92 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import { S as color_exports } from "./color-
|
|
3
|
-
import
|
|
4
|
-
import "./
|
|
5
|
-
import "./
|
|
6
|
-
import { a as
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
1
|
+
import { t as __export } from "./chunk-z5eko27R.mjs";
|
|
2
|
+
import { S as color_exports, q as merge, t as DEFAULT_THEME } from "./color-DiZvJ0Fc.mjs";
|
|
3
|
+
import "./standard-schema-DTuaYJjO.mjs";
|
|
4
|
+
import "./raw-DEtZFeMv.mjs";
|
|
5
|
+
import { a as prompt_exports } from "./prompt-a5Ix_Hyc.mjs";
|
|
6
|
+
import { a as MissingRequiredOptionError, i as MissingRequiredArgumentError, n as ConvokerError, o as TooManyArgumentsError, r as HelpAskedError, s as UnknownOptionError, t as Command } from "./command-8P8qXJ2o.mjs";
|
|
7
|
+
import { a as input_exports } from "./input-XUsy1LCQ.mjs";
|
|
8
|
+
import process from "node:process";
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
//#region src/log/error.ts
|
|
11
|
+
var WriteError = class extends Error {
|
|
12
|
+
constructor(streamName) {
|
|
13
|
+
super(`Could not write to \`${streamName}\`.`);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/log/index.ts
|
|
19
|
+
var log_exports = /* @__PURE__ */ __export({
|
|
20
|
+
DEFAULT_CONFIG: () => DEFAULT_CONFIG,
|
|
21
|
+
WriteError: () => WriteError,
|
|
22
|
+
error: () => error,
|
|
23
|
+
fatal: () => fatal,
|
|
24
|
+
info: () => info,
|
|
25
|
+
setConfig: () => setConfig,
|
|
26
|
+
setTheme: () => setTheme,
|
|
27
|
+
trace: () => trace,
|
|
28
|
+
warn: () => warn
|
|
29
|
+
});
|
|
30
|
+
let th = DEFAULT_THEME;
|
|
31
|
+
function setTheme(theme) {
|
|
32
|
+
th = theme;
|
|
33
|
+
}
|
|
34
|
+
const DEFAULT_CONFIG = {
|
|
35
|
+
format: "text",
|
|
36
|
+
stderr: process.stderr,
|
|
37
|
+
stdout: process.stdout,
|
|
38
|
+
jsonSpace: 2
|
|
39
|
+
};
|
|
40
|
+
let config = DEFAULT_CONFIG;
|
|
41
|
+
function setConfig({ theme,...cfg }) {
|
|
42
|
+
config = merge(DEFAULT_CONFIG, cfg);
|
|
43
|
+
th = theme ?? th;
|
|
44
|
+
}
|
|
45
|
+
function trace(...msgs) {
|
|
46
|
+
const str = format(msgs, "TRACE");
|
|
47
|
+
if (!config.stdout.write(str)) throw new WriteError("stdout");
|
|
48
|
+
}
|
|
49
|
+
function info(...msgs) {
|
|
50
|
+
const str = format(msgs, "INFO");
|
|
51
|
+
if (!config.stdout.write(str)) throw new WriteError("stdout");
|
|
52
|
+
}
|
|
53
|
+
function warn(...msgs) {
|
|
54
|
+
const str = format(msgs, "WARN");
|
|
55
|
+
if (!config.stderr.write(th.warning(str))) throw new WriteError("stderr");
|
|
56
|
+
}
|
|
57
|
+
function error(...msgs) {
|
|
58
|
+
const str = format(msgs, "ERROR");
|
|
59
|
+
if (!config.stderr.write(str)) throw new WriteError("stderr");
|
|
60
|
+
}
|
|
61
|
+
function fatal(...msgs) {
|
|
62
|
+
const str = format(msgs, "FATAL");
|
|
63
|
+
if (!config.stderr.write(str)) throw new WriteError("stderr");
|
|
64
|
+
process.exit(-1);
|
|
65
|
+
}
|
|
66
|
+
function format(msgs, level) {
|
|
67
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
68
|
+
const msg = msgs.map((m) => typeof m === "string" ? m : JSON.stringify(m, null, config.jsonSpace)).join(" ");
|
|
69
|
+
switch (config.format) {
|
|
70
|
+
case "json": return colorize(JSON.stringify({
|
|
71
|
+
timestamp,
|
|
72
|
+
level,
|
|
73
|
+
message: msg
|
|
74
|
+
}) + "\n", level);
|
|
75
|
+
case "text":
|
|
76
|
+
default: return colorize(`[${timestamp}] [${th.symbols[level] ?? level}] ${msg}\n`, level);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function colorize(str, level) {
|
|
80
|
+
switch (level) {
|
|
81
|
+
case "TRACE": return th.secondary(str);
|
|
82
|
+
case "WARN": return th.warning(str);
|
|
83
|
+
case "ERROR": return th.error(str);
|
|
84
|
+
case "FATAL": return th.styles?.bold ? th.styles.bold(th.error(str)) : th.error(str);
|
|
85
|
+
case "INFO":
|
|
86
|
+
default: return th.info?.(str) ?? str;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
//#endregion
|
|
91
|
+
export { Command, ConvokerError, HelpAskedError, MissingRequiredArgumentError, MissingRequiredOptionError, TooManyArgumentsError, UnknownOptionError, color_exports as color, input_exports as i, log_exports as log, prompt_exports as prompt };
|
|
92
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["th: Theme","DEFAULT_CONFIG: Config","config: Config"],"sources":["../src/log/error.ts","../src/log/index.ts"],"sourcesContent":["export class WriteError extends Error {\n constructor(streamName: string) {\n super(`Could not write to \\`${streamName}\\`.`);\n }\n}\n","import Stream from \"node:stream\";\nimport process from \"node:process\";\nimport { DEFAULT_THEME, type Theme } from \"@/color\";\nimport { DeepPartial, merge } from \"@/utils\";\nimport { WriteError } from \"./error\";\n\nlet th: Theme = DEFAULT_THEME;\n\nexport function setTheme(theme: Theme) {\n th = theme;\n}\n\nexport interface Config {\n format: \"text\" | \"json\";\n stdout: Stream.Writable;\n stderr: Stream.Writable;\n jsonSpace: number;\n}\n\nexport const DEFAULT_CONFIG: Config = {\n format: \"text\",\n stderr: process.stderr,\n stdout: process.stdout,\n jsonSpace: 2,\n};\n\nlet config: Config = DEFAULT_CONFIG;\n\nexport function setConfig({\n theme,\n ...cfg\n}: DeepPartial<Config> & { theme?: Theme }) {\n config = merge(DEFAULT_CONFIG, cfg);\n th = theme ?? th;\n}\n\nexport function trace(...msgs: any[]) {\n const str = format(msgs, \"TRACE\");\n if (!config.stdout.write(str)) {\n throw new WriteError(\"stdout\");\n }\n}\n\nexport function info(...msgs: any[]) {\n const str = format(msgs, \"INFO\");\n if (!config.stdout.write(str)) {\n throw new WriteError(\"stdout\");\n }\n}\n\nexport function warn(...msgs: any[]) {\n const str = format(msgs, \"WARN\");\n if (!config.stderr.write(th.warning(str))) {\n throw new WriteError(\"stderr\");\n }\n}\n\nexport function error(...msgs: any[]) {\n const str = format(msgs, \"ERROR\");\n if (!config.stderr.write(str)) {\n throw new WriteError(\"stderr\");\n }\n}\n\nexport function fatal(...msgs: any[]) {\n const str = format(msgs, \"FATAL\");\n if (!config.stderr.write(str)) {\n throw new WriteError(\"stderr\");\n }\n\n process.exit(-1);\n}\n\nexport * from \"./error\";\n\nfunction format(msgs: any[], level: string): string {\n const timestamp = new Date().toISOString();\n const msg = msgs\n .map((m) =>\n typeof m === \"string\" ? m : JSON.stringify(m, null, config.jsonSpace),\n )\n .join(\" \");\n switch (config.format) {\n case \"json\":\n return colorize(\n JSON.stringify({ timestamp, level, message: msg }) + \"\\n\",\n level,\n );\n case \"text\":\n default:\n return colorize(\n `[${timestamp}] [${(th.symbols as any)[level] ?? level}] ${msg}\\n`,\n level,\n );\n }\n}\n\nfunction colorize(str: string, level: string) {\n switch (level) {\n case \"TRACE\":\n return th.secondary(str);\n case \"WARN\":\n return th.warning(str);\n case \"ERROR\":\n return th.error(str);\n case \"FATAL\":\n return th.styles?.bold ? th.styles.bold(th.error(str)) : th.error(str);\n case \"INFO\":\n default:\n return th.info?.(str) ?? str;\n }\n}\n"],"mappings":";;;;;;;;;;AAAA,IAAa,aAAb,cAAgC,MAAM;CACpC,YAAY,YAAoB;AAC9B,QAAM,wBAAwB,WAAW,KAAK;;;;;;;;;;;;;;;;;ACIlD,IAAIA,KAAY;AAEhB,SAAgB,SAAS,OAAc;AACrC,MAAK;;AAUP,MAAaC,iBAAyB;CACpC,QAAQ;CACR,QAAQ,QAAQ;CAChB,QAAQ,QAAQ;CAChB,WAAW;CACZ;AAED,IAAIC,SAAiB;AAErB,SAAgB,UAAU,EACxB,MACA,GAAG,OACuC;AAC1C,UAAS,MAAM,gBAAgB,IAAI;AACnC,MAAK,SAAS;;AAGhB,SAAgB,MAAM,GAAG,MAAa;CACpC,MAAM,MAAM,OAAO,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O,GAAG,QAAQ,IAAI;EACxB,KAAK,QACH,QAAO,GAAG,MAAM,IAAI;EACtB,KAAK,QACH,QAAO,GAAG,QAAQ,OAAO,GAAG,OAAO,KAAK,GAAG,MAAM,IAAI,CAAC,GAAG,GAAG,MAAM,IAAI;EACxE,KAAK;EACL,QACE,QAAO,GAAG,OAAO,IAAI,IAAI"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import "../standard-schema-DXS-QnwX.mjs";
|
|
2
|
+
import { a as Kind, c as TypeOf, f as option, i as Input, l as argument, m as InputValidationError, n as InferEntry, o as Option, p as positional, r as InferInput, s as Positional, t as BasicKind, u as convert } from "../index-ClhbwSD8.mjs";
|
|
3
|
+
export { BasicKind, InferEntry, InferInput, Input, InputValidationError, Kind, Option, Positional, TypeOf, argument, convert, option, positional };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { n as InputValidationError } from "../standard-schema-DTuaYJjO.mjs";
|
|
2
|
+
import { i as convert, n as Positional, o as option, r as argument, s as positional, t as Option } from "../input-XUsy1LCQ.mjs";
|
|
3
|
+
|
|
4
|
+
export { InputValidationError, Option, Positional, argument, convert, option, positional };
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { t as __export } from "./chunk-z5eko27R.mjs";
|
|
2
|
-
import { t as validate } from "./standard-schema-
|
|
2
|
+
import { n as InputValidationError, t as validate } from "./standard-schema-DTuaYJjO.mjs";
|
|
3
3
|
|
|
4
|
-
//#region src/input.ts
|
|
4
|
+
//#region src/input/index.ts
|
|
5
5
|
var input_exports = /* @__PURE__ */ __export({
|
|
6
|
+
InputValidationError: () => InputValidationError,
|
|
6
7
|
Option: () => Option,
|
|
7
8
|
Positional: () => Positional,
|
|
8
9
|
argument: () => argument,
|
|
@@ -171,4 +172,5 @@ function argument(kind) {
|
|
|
171
172
|
}
|
|
172
173
|
|
|
173
174
|
//#endregion
|
|
174
|
-
export { input_exports as a, convert as i, Positional as n, option as o, argument as r, positional as s, Option as t };
|
|
175
|
+
export { input_exports as a, convert as i, Positional as n, option as o, argument as r, positional as s, Option as t };
|
|
176
|
+
//# sourceMappingURL=input-XUsy1LCQ.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"input-XUsy1LCQ.mjs","names":[],"sources":["../src/input/index.ts"],"sourcesContent":["import { validate, type StandardSchemaV1 } from \"./standard-schema\";\n\n/**\n * An input object.\n */\nexport interface Input {\n [x: string]: Option<any, any, any> | Positional<any, any, any>;\n}\n\n/**\n * A basic input type.\n */\nexport type BasicKind = \"boolean\" | \"string\" | \"number\" | \"bigint\";\n/**\n * An input type.\n */\nexport type Kind = BasicKind | StandardSchemaV1<any, any>;\n\n/**\n * Converts a Kind to a TypeScript type.\n */\nexport type TypeOf<T extends Kind> =\n T extends StandardSchemaV1<any, infer Out>\n ? Out\n : T extends \"boolean\"\n ? boolean\n : T extends \"string\"\n ? string\n : T extends \"number\"\n ? number\n : T extends \"bigint\"\n ? bigint\n : never;\n\n/**\n * Infers TypeScript types from an input object.\n */\nexport type InferInput<T extends Input> = {\n [K in keyof T]: InferEntry<T[K]>;\n};\n\n/**\n * Infers a TypeScript type from an option or positional.\n */\nexport type InferEntry<T> = T extends {\n $kind: infer TKind extends Kind;\n $required: infer Required;\n $list: infer List;\n}\n ? List extends true\n ? Required extends true\n ? TypeOf<TKind>[]\n : TypeOf<TKind>[] | undefined\n : Required extends true\n ? TypeOf<TKind>\n : TypeOf<TKind> | undefined\n : never;\n\n/**\n * Converts a value from a Kind to a TypeScript type.\n * @param kind The kind to convert to.\n * @param value The value to convert.\n * @returns The converted value.\n */\nexport async function convert<TKind extends Kind>(\n kind: TKind,\n value: string | string[],\n): Promise<TypeOf<TKind> | TypeOf<TKind>[]> {\n // Helper for single value conversion\n async function convertOne(val: string): Promise<TypeOf<TKind>> {\n if (typeof kind === \"string\") {\n switch (kind) {\n case \"boolean\":\n return (val === \"true\") as any;\n case \"bigint\":\n return BigInt(val) as any;\n case \"number\":\n return parseFloat(val) as any;\n case \"string\":\n return val as any;\n }\n }\n // Otherwise, Standard Schema\n return validate(kind, val);\n }\n\n // If list → map each item\n if (Array.isArray(value)) {\n const results = await Promise.all(value.map((v) => convertOne(v)));\n return results as any;\n }\n\n // Single value case\n return convertOne(value);\n}\n\n/**\n * An option.\n */\nexport class Option<\n TKind extends Kind,\n TRequired extends boolean = true,\n TList extends boolean = false,\n> {\n /**\n * The kind of this option.\n */\n $kind: TKind;\n /**\n * The aliases of this option.\n */\n $names: string[];\n /**\n * The description of this option.\n */\n $description: string | undefined;\n /**\n * The default value of this option.\n */\n $default: TypeOf<TKind> | undefined;\n /**\n * If this option is required.\n */\n $required: TRequired = true as TRequired;\n /**\n * If this option is a list.\n */\n $list: TList = false as TList;\n /**\n * A separator if this option is a list.\n */\n $separator: string | undefined;\n\n /**\n * Creates a new option.\n * @param kind The type of this option.\n * @param names The names of this option.\n */\n constructor(kind: TKind, names: string[]) {\n this.$kind = kind;\n this.$names = names.map((name) => name.replace(/^-+/, \"\"));\n }\n\n /**\n * Makes this option a list.\n * @returns this\n */\n list(separator?: string): Option<TKind, TRequired, true> {\n this.$list = true as TList;\n this.$separator = separator ?? this.$separator;\n return this as any;\n }\n\n /**\n * Makes this option required.\n * @returns this\n */\n required(): Option<TKind, true, TList> {\n this.$required = true as TRequired;\n return this as any;\n }\n\n /**\n * Makes this option optional.\n * @returns this\n */\n optional(): Option<TKind, false, TList> {\n this.$required = false as TRequired;\n return this as any;\n }\n\n /**\n * Sets a default value.\n * @param value The default value.\n * @returns this\n */\n default(value: TypeOf<TKind>): this {\n this.$default = value;\n return this;\n }\n\n /**\n * Sets a description.\n * @param desc The description.\n * @returns this\n */\n description(desc: string): this {\n this.$description = desc;\n return this;\n }\n}\n\n/**\n * A positional argument.\n */\nexport class Positional<\n TKind extends Kind,\n TRequired extends boolean = true,\n TList extends boolean = false,\n> {\n /**\n * The type of this argument.\n */\n $kind: TKind;\n /**\n * The default value of this argument.\n */\n $default: TypeOf<TKind> | undefined;\n /**\n * The description of this argument.\n */\n $description: string | undefined;\n /**\n * If this argument is required.\n */\n $required: TRequired = true as TRequired;\n /**\n * If this argument is a list.\n */\n $list: TList = false as TList;\n\n /**\n * Creates a new positional argument.\n * @param kind The positional argument.\n */\n constructor(kind: TKind) {\n this.$kind = kind;\n }\n\n /**\n * Makes this argument a list.\n * @returns this\n */\n list(): Positional<TKind, TRequired, true> {\n this.$list = true as TList;\n return this as any;\n }\n\n /**\n * Makes this argument required.\n * @returns this\n */\n required(): Positional<TKind, true, TList> {\n this.$required = true as TRequired;\n return this as any;\n }\n\n /**\n * Makes this argument optional.\n * @returns this\n */\n optional(): Positional<TKind, false, TList> {\n this.$required = false as TRequired;\n return this as any;\n }\n\n /**\n * Sets a default value.\n * @param value The default value.\n * @returns this\n */\n default(value: TypeOf<TKind>): this {\n this.$default = value;\n return this;\n }\n\n /**\n * Sets a description.\n * @param desc The description.\n * @returns this\n */\n description(desc: string): this {\n this.$description = desc;\n return this;\n }\n}\n\n/**\n * Creates a new option.\n * @param kind The kind of option.\n * @param names The names of the option.\n * @returns A new option.\n */\nexport function option<T extends Kind>(kind: T, ...names: string[]): Option<T> {\n return new Option(kind, names);\n}\n\n/**\n * Creates a new positional argument.\n * @param kind The kind of positional argument.\n * @returns A new positional argument.\n */\nexport function positional<T extends Kind>(kind: T): Positional<T> {\n return new Positional(kind);\n}\n\n/**\n * Creates a new positional argument.\n * @param kind The kind of positional argument.\n * @returns A new positional argument.\n */\nexport function argument<T extends Kind>(kind: T): Positional<T> {\n return new Positional(kind);\n}\n\nexport * from \"./error\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAgEA,eAAsB,QACpB,MACA,OAC0C;CAE1C,eAAe,WAAW,KAAqC;AAC7D,MAAI,OAAO,SAAS,SAClB,SAAQ,MAAR;GACE,KAAK,UACH,QAAQ,QAAQ;GAClB,KAAK,SACH,QAAO,OAAO,IAAI;GACpB,KAAK,SACH,QAAO,WAAW,IAAI;GACxB,KAAK,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mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import "../
|
|
2
|
-
import "../standard-schema-
|
|
3
|
-
import { i as raw_d_exports } from "../raw-
|
|
4
|
-
import { _ as text, a as SearchOpts, c as TextOpts, f as multiselect, g as setTheme, h as select, i as PasswordOpts, l as confirm, m as search, n as ConfirmOpts, o as SelectOption, p as password, r as EditorOpts, s as SelectOpts, t as BaseOpts, u as editor } from "../index-
|
|
1
|
+
import "../index-BtbthYjp.mjs";
|
|
2
|
+
import "../standard-schema-DXS-QnwX.mjs";
|
|
3
|
+
import { i as raw_d_exports } from "../raw-cqTp2vds.mjs";
|
|
4
|
+
import { _ as text, a as SearchOpts, c as TextOpts, f as multiselect, g as setTheme, h as select, i as PasswordOpts, l as confirm, m as search, n as ConfirmOpts, o as SelectOption, p as password, r as EditorOpts, s as SelectOpts, t as BaseOpts, u as editor } from "../index-BYLskLxk.mjs";
|
|
5
5
|
export { BaseOpts, ConfirmOpts, EditorOpts, PasswordOpts, SearchOpts, SelectOption, SelectOpts, TextOpts, confirm, editor, multiselect, password, raw_d_exports as raw, search, select, setTheme, text };
|
package/dist/prompt/index.mjs
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import "../
|
|
2
|
-
import "../
|
|
3
|
-
import "../
|
|
4
|
-
import "../
|
|
5
|
-
import { i as raw_exports } from "../raw--889icsd.mjs";
|
|
6
|
-
import { c as setTheme, i as password, l as text, n as editor, o as search, r as multiselect, s as select, t as confirm } from "../prompt-Cvufljin.mjs";
|
|
1
|
+
import "../color-DiZvJ0Fc.mjs";
|
|
2
|
+
import "../standard-schema-DTuaYJjO.mjs";
|
|
3
|
+
import { i as raw_exports } from "../raw-DEtZFeMv.mjs";
|
|
4
|
+
import { c as setTheme, i as password, l as text, n as editor, o as search, r as multiselect, s as select, t as confirm } from "../prompt-a5Ix_Hyc.mjs";
|
|
7
5
|
|
|
8
6
|
export { confirm, editor, multiselect, password, raw_exports as raw, search, select, setTheme, text };
|
package/dist/prompt/raw.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as readKey, n as cursorDown, o as readLine, r as cursorUp, t as clearLines } from "../raw-
|
|
1
|
+
import { a as readKey, n as cursorDown, o as readLine, r as cursorUp, t as clearLines } from "../raw-cqTp2vds.mjs";
|
|
2
2
|
export { clearLines, cursorDown, cursorUp, readKey, readLine };
|
package/dist/prompt/raw.mjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import "../
|
|
2
|
-
import { a as readKey, n as cursorDown, o as readLine, r as cursorUp, t as clearLines } from "../raw--889icsd.mjs";
|
|
1
|
+
import { a as readKey, n as cursorDown, o as readLine, r as cursorUp, t as clearLines } from "../raw-DEtZFeMv.mjs";
|
|
3
2
|
|
|
4
3
|
export { clearLines, cursorDown, cursorUp, readKey, readLine };
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { t as __export } from "./chunk-z5eko27R.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import { t as
|
|
4
|
-
import { r as
|
|
5
|
-
import
|
|
6
|
-
import
|
|
2
|
+
import { t as DEFAULT_THEME } from "./color-DiZvJ0Fc.mjs";
|
|
3
|
+
import { n as InputValidationError, t as validate } from "./standard-schema-DTuaYJjO.mjs";
|
|
4
|
+
import { a as readKey, i as raw_exports, o as readLine, r as cursorUp, t as clearLines } from "./raw-DEtZFeMv.mjs";
|
|
5
|
+
import childProcess from "node:child_process";
|
|
6
|
+
import fs from "node:fs/promises";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import os from "node:os";
|
|
7
9
|
|
|
8
10
|
//#region src/prompt/index.ts
|
|
9
11
|
var prompt_exports = /* @__PURE__ */ __export({
|
|
@@ -37,7 +39,7 @@ async function text(opts) {
|
|
|
37
39
|
if (opts.maxLength && answer.length > opts.maxLength) throw new InputValidationError([`Must be at most ${opts.maxLength} characters`]);
|
|
38
40
|
if (typeof opts.validate === "function") {
|
|
39
41
|
if (!opts.validate(answer)) throw new InputValidationError(["Validation function returned a falsy value"]);
|
|
40
|
-
} else if (opts.validate) validate(opts.validate, answer);
|
|
42
|
+
} else if (opts.validate) await validate(opts.validate, answer);
|
|
41
43
|
return answer;
|
|
42
44
|
}
|
|
43
45
|
/**
|
|
@@ -120,7 +122,7 @@ async function multiselect(opts) {
|
|
|
120
122
|
else if (key === "down" && index < options.length - 1) index++;
|
|
121
123
|
else if (key === "space") if (selected.has(index)) selected.delete(index);
|
|
122
124
|
else selected.add(index);
|
|
123
|
-
else if (key === "
|
|
125
|
+
else if (key === "enter") {
|
|
124
126
|
const chosen = Array.from(selected).map((i) => options[i].value);
|
|
125
127
|
clearLines(options.length + 1);
|
|
126
128
|
console.log(th.success(`${opts.message} ${chosen.length} selected`));
|
|
@@ -144,10 +146,10 @@ async function search(opts) {
|
|
|
144
146
|
console.log(th.primary(opts.message));
|
|
145
147
|
const matches = opts.options.filter((o) => filter(query, o));
|
|
146
148
|
matches.forEach((o) => console.log(" " + (th.foreground?.(o.label) ?? o.label)));
|
|
149
|
+
if (matches.length === 1) return matches[0].value;
|
|
147
150
|
const input = await readLine(th.secondary(`Search: ${query}`));
|
|
148
|
-
if (input
|
|
151
|
+
if (!input) continue;
|
|
149
152
|
query = input;
|
|
150
|
-
if (matches.length === 1) return matches[0].value;
|
|
151
153
|
}
|
|
152
154
|
}
|
|
153
155
|
/**
|
|
@@ -193,56 +195,26 @@ async function editor(opts) {
|
|
|
193
195
|
*/
|
|
194
196
|
async function openSystemEditor(initial) {
|
|
195
197
|
const tmpFile = `edit-${Date.now()}.txt`;
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
const { $ } = await import("bun");
|
|
213
|
-
const path = `/tmp/${tmpFile}`;
|
|
214
|
-
await Bun.write(path, initial ?? "");
|
|
215
|
-
await $`${process.env.EDITOR ?? "vi"} ${path}`;
|
|
216
|
-
const text$1 = await Bun.file(path).text();
|
|
217
|
-
await Bun.write(path, "");
|
|
218
|
-
return text$1;
|
|
219
|
-
}
|
|
220
|
-
if (isNode) {
|
|
221
|
-
const { tmpdir } = await import("node:os");
|
|
222
|
-
const { join } = await import("node:path");
|
|
223
|
-
const { promises: fs } = await import("node:fs");
|
|
224
|
-
const { spawn } = await import("node:child_process");
|
|
225
|
-
const path = join(tmpdir(), tmpFile);
|
|
226
|
-
await fs.writeFile(path, initial ?? "", "utf8");
|
|
227
|
-
const editor$1 = process.env.EDITOR || process.env.VISUAL || (process.platform === "win32" ? "notepad" : "vi");
|
|
228
|
-
return new Promise((resolve, reject) => {
|
|
229
|
-
spawn(editor$1, [path], { stdio: "inherit" }).on("exit", async (code) => {
|
|
230
|
-
if (code !== 0) {
|
|
231
|
-
reject(/* @__PURE__ */ new Error(`${editor$1} exited with code ${code}`));
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
try {
|
|
235
|
-
const data = await fs.readFile(path, "utf8");
|
|
236
|
-
await fs.unlink(path).catch(() => {});
|
|
237
|
-
resolve(data);
|
|
238
|
-
} catch (err) {
|
|
239
|
-
reject(err);
|
|
240
|
-
}
|
|
241
|
-
});
|
|
198
|
+
const filePath = path.join(os.tmpdir(), tmpFile);
|
|
199
|
+
await fs.writeFile(filePath, initial ?? "", "utf8");
|
|
200
|
+
const editor$1 = process.env.EDITOR || process.env.VISUAL || (process.platform === "win32" ? "notepad" : "vi");
|
|
201
|
+
return new Promise((resolve, reject) => {
|
|
202
|
+
childProcess.spawn(editor$1, [filePath], { stdio: "inherit" }).on("exit", async (code) => {
|
|
203
|
+
if (code !== 0) {
|
|
204
|
+
reject(/* @__PURE__ */ new Error(`${editor$1} exited with code ${code}`));
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
try {
|
|
208
|
+
const data = await fs.readFile(filePath, "utf8");
|
|
209
|
+
await fs.unlink(filePath).catch(() => {});
|
|
210
|
+
resolve(data);
|
|
211
|
+
} catch (err) {
|
|
212
|
+
reject(err);
|
|
213
|
+
}
|
|
242
214
|
});
|
|
243
|
-
}
|
|
244
|
-
throw new Error("Unsupported runtime for system editor.");
|
|
215
|
+
});
|
|
245
216
|
}
|
|
246
217
|
|
|
247
218
|
//#endregion
|
|
248
|
-
export { prompt_exports as a, setTheme as c, password as i, text as l, editor as n, search as o, multiselect as r, select as s, confirm as t };
|
|
219
|
+
export { prompt_exports as a, setTheme as c, password as i, text as l, editor as n, search as o, multiselect as r, select as s, confirm as t };
|
|
220
|
+
//# sourceMappingURL=prompt-a5Ix_Hyc.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt-a5Ix_Hyc.mjs","names":["theme: Theme","raw.readLine","raw.readKey","editor"],"sources":["../src/prompt/index.ts"],"sourcesContent":["import childProcess from \"node:child_process\";\nimport fs from \"node:fs/promises\";\nimport path from \"node:path\";\nimport os from \"node:os\";\n\nimport { DEFAULT_THEME, type Theme } from \"@/color\";\nimport { validate, type StandardSchemaV1 } from \"@/input/standard-schema\";\nimport { InputValidationError } from \"@/input/error\";\nimport * as raw from \"./raw\";\n\nlet theme: Theme = DEFAULT_THEME;\n\n/**\n * Sets the theme of the prompts.\n * @param t The new theme.\n */\nexport function setTheme(t: Theme) {\n theme = t;\n}\n\n/**\n * Base options for prompts.\n */\nexport interface BaseOpts<T> {\n /**\n * The message of the prompt.\n */\n message: string;\n /**\n * An `AbortSignal` to cancel the prompt.\n */\n signal?: AbortSignal;\n /**\n * The default value.\n */\n default?: T;\n /**\n * The theme of the prompt.\n */\n theme?: Theme;\n /**\n * A validator function, or a Standard Schema validator.\n */\n validate?: StandardSchemaV1<any, T> | ((value: T) => boolean | T);\n}\n\n// -- text -- //\n\n/**\n * Options for text input.\n */\nexport interface TextOpts extends BaseOpts<string> {\n /**\n * A placeholder, displayed when the user hasn't typed anything yet.\n */\n placeholder?: string;\n /**\n * Minimum length of the input.\n */\n minLength?: number;\n /**\n * Maximum length of the input.\n */\n maxLength?: number;\n}\n\n/**\n * Prompts the user for text input.\n * @param opts Options for text input.\n * @returns The text the user typed in, or the default.\n */\nexport async function text(opts: TextOpts): Promise<string> {\n const th = opts.theme ?? theme;\n const message = th.primary(opts.message) + \" \";\n const answer = await raw.readLine(message, opts.default);\n\n if (opts.minLength && answer.length < opts.minLength)\n throw new InputValidationError([\n `Must be at least ${opts.minLength} characters`,\n ]);\n if (opts.maxLength && answer.length > opts.maxLength)\n throw new InputValidationError([\n `Must be at most ${opts.maxLength} characters`,\n ]);\n\n if (typeof opts.validate === \"function\") {\n if (!opts.validate(answer))\n throw new InputValidationError([\n \"Validation function returned a falsy value\",\n ]);\n } else if (opts.validate) {\n await validate(opts.validate, answer);\n }\n return answer;\n}\n\n// -- password -- //\n\n/**\n * Options for password input.\n */\nexport interface PasswordOpts extends TextOpts {\n /**\n * The mask for the password input.\n */\n mask?: string;\n /**\n * If the user should be asked to confirm the password, by typing it again.\n */\n confirm?: boolean;\n}\n\n/**\n * Prompts the user for a password.\n * @param opts Options for password input.\n * @returns The password.\n */\nexport async function password(opts: PasswordOpts): Promise<string> {\n const th = opts.theme ?? theme;\n const first = await raw.readLine(th.primary(opts.message) + \" \", undefined, {\n masked: true,\n maskChar: opts.mask ?? \"*\",\n });\n if (opts.confirm) {\n const second = await raw.readLine(\n th.secondary(\"Confirm password: \"),\n undefined,\n {\n masked: true,\n maskChar: opts.mask ?? \"*\",\n },\n );\n if (first !== second) throw new Error(th.error(\"Passwords do not match\"));\n }\n return first;\n}\n\n// -- select -- //\n\n/**\n * An option for select input.\n */\nexport interface SelectOption<T> {\n /**\n * The label (what gets displayed) of the select option.\n */\n label: string;\n /**\n * The value (what gets returned) of the select option.\n */\n value: T;\n /**\n * A description of the option.\n */\n hint?: string;\n /**\n * If this option is disabled.\n */\n disabled?: boolean;\n}\n\n/**\n * Options for select input.\n */\nexport interface SelectOpts<T> extends BaseOpts<T> {\n /**\n * Every option the user can pick from.\n */\n options: SelectOption<T>[];\n /**\n * The initial option selected.\n */\n initialIndex?: number;\n}\n\n/**\n * Prompts the user to select a single option.\n * @param opts Options for select input.\n * @returns The selected option's value.\n */\nexport async function select<T>(opts: SelectOpts<T>): Promise<T> {\n const th = opts.theme ?? theme;\n const options = opts.options;\n let index = opts.initialIndex ?? 0;\n\n const render = () => {\n raw.clearLines(options.length + 1);\n console.log(th.primary(opts.message));\n for (let i = 0; i < options.length; i++) {\n const o = options[i];\n const prefix = i === index ? (th.accent?.(\"> \") ?? \"> \") : \" \";\n const label = o.disabled\n ? th.secondary(o.label)\n : (th.foreground?.(o.label) ?? o.label);\n console.log(prefix + label);\n }\n };\n\n console.log(th.primary(opts.message));\n options.forEach((o, i) =>\n console.log(`${i === index ? \"> \" : \" \"}${o.label}`),\n );\n\n while (true) {\n const key = await raw.readKey();\n if (key === \"up\" && index > 0) index--;\n else if (key === \"down\" && index < options.length - 1) index++;\n else if (key === \"enter\") {\n const choice = options[index];\n if (choice.disabled) continue;\n raw.clearLines(options.length + 1);\n console.log(th.success(`${th.symbols?.success ?? \"✔\"} ${choice.label}`));\n return choice.value;\n }\n render();\n }\n}\n\n/**\n * Prompts the user to select multiple options.\n * @param opts Options for select input.\n * @returns The selected options.\n */\nexport async function multiselect<T>(opts: SelectOpts<T>): Promise<T[]> {\n const th = opts.theme ?? theme;\n const options = opts.options;\n let index = opts.initialIndex ?? 0;\n const selected = new Set<number>();\n\n const render = () => {\n raw.clearLines();\n console.log(th.primary(opts.message));\n options.forEach((opt, i) => {\n const prefix = i === index ? (th.accent?.(\"> \") ?? \"> \") : \" \";\n const mark = selected.has(i) ? th.success(\"[x]\") : \"[ ]\";\n console.log(prefix + mark + \" \" + opt.label);\n });\n };\n\n render();\n while (true) {\n const key = await raw.readKey();\n if (key === \"up\" && index > 0) index--;\n else if (key === \"down\" && index < options.length - 1) index++;\n else if (key === \"space\") {\n if (selected.has(index)) selected.delete(index);\n else selected.add(index);\n } else if (key === \"enter\") {\n const chosen = Array.from(selected).map((i) => options[i].value);\n raw.clearLines(options.length + 1);\n console.log(th.success(`${opts.message} ${chosen.length} selected`));\n return chosen;\n }\n raw.cursorUp(options.length);\n render();\n }\n}\n\n// -- search -- //\n\n/**\n * Options for search input.\n */\nexport interface SearchOpts<T> extends BaseOpts<T> {\n /**\n * Every option the user can search through.\n */\n options: SelectOption<T>[];\n /**\n * Placeholder for the search input.\n */\n placeholder?: string;\n /**\n * Minimum length for a query string.\n */\n minQueryLength?: number;\n /**\n * Filters a single option.\n * @param query The search query.\n * @param option The option to filter.\n */\n filter?(query: string, option: SelectOption<T>): boolean;\n}\n\n/**\n * Prompts the user to search through a list of options.\n * @param opts Options for search input.\n * @returns The selected option.\n */\nexport async function search<T>(opts: SearchOpts<T>): Promise<T> {\n const th = opts.theme ?? theme;\n let query = \"\";\n const filter =\n opts.filter ?? ((q, o) => o.label.toLowerCase().includes(q.toLowerCase()));\n while (true) {\n raw.clearLines();\n console.log(th.primary(opts.message));\n\n const matches = opts.options.filter((o) => filter(query, o));\n matches.forEach((o) =>\n console.log(\" \" + (th.foreground?.(o.label) ?? o.label)),\n );\n\n if (matches.length === 1) {\n return matches[0].value;\n }\n\n const input = await raw.readLine(th.secondary(`Search: ${query}`));\n if (!input) continue;\n\n query = input;\n }\n}\n\n// -- confirm -- //\n\n/**\n * Options for confirm input.\n */\nexport interface ConfirmOpts extends BaseOpts<boolean> {\n /**\n * What gets displayed for the Yes option.\n */\n yesLabel?: string;\n /**\n * What gets displayed for the No option.\n */\n noLabel?: string;\n}\n\n/**\n * Prompts the user to confirm an action.\n * @param opts Options for confirm input.\n * @returns If the user picked Yes.\n */\nexport async function confirm(opts: ConfirmOpts): Promise<boolean> {\n const th = opts.theme ?? theme;\n const yes = opts.yesLabel ?? \"y\";\n const no = opts.noLabel ?? \"n\";\n const def = opts.default ? yes : no;\n const res = await raw.readLine(\n `${th.primary(opts.message)} ${th.secondary(`[${yes}/${no}] (default: ${def})`)} `,\n );\n if (!res) return !!opts.default;\n return /^y/i.test(res.trim());\n}\n\n// -- editor -- //\n\n/**\n * Options for opening the system editor.\n */\nexport interface EditorOpts extends BaseOpts<string> {\n /**\n * The initial value.\n */\n initial?: string;\n /**\n * The language of the value.\n */\n language?: string;\n /**\n * If the input is required for continuing or not.\n */\n required?: boolean;\n}\n\n/**\n * Opens the system editor, or asks for input in the terminal as fallback.\n * @param opts Options for opening the system editor.\n * @returns The result of the system editor.\n */\nexport async function editor(opts: EditorOpts): Promise<string> {\n const th = opts.theme ?? {\n primary: (s: string) => s,\n secondary: (s: string) => s,\n };\n\n console.log(th.primary(opts.message ?? \"Please enter text:\"));\n console.log(th.secondary(\"Press Ctrl+D (or save & close editor) when done.\"));\n\n try {\n const result = await openSystemEditor(opts.initial ?? \"\");\n if (opts.required && !result.trim()) throw new Error(\"Input required.\");\n return result;\n } catch {\n // fallback: cross-runtime multiline input\n const value = await raw.readLine(\"\", opts.initial, { multiline: true });\n if (opts.required && !value.trim()) throw new Error(\"Input required.\");\n return value;\n }\n}\n\n/**\n * Opens the system editor on a temporary file.\n * @param initial Initial contents of the file.\n * @returns The contents of the file after saving.\n */\nasync function openSystemEditor(initial: string): Promise<string> {\n const tmpFile = `edit-${Date.now()}.txt`;\n const filePath = path.join(os.tmpdir(), tmpFile);\n await fs.writeFile(filePath, initial ?? \"\", \"utf8\");\n\n const editor =\n process.env.EDITOR ||\n process.env.VISUAL ||\n (process.platform === \"win32\" ? \"notepad\" : \"vi\");\n\n return new Promise((resolve, reject) => {\n const child = childProcess.spawn(editor, [filePath], { stdio: \"inherit\" });\n child.on(\"exit\", async (code: number) => {\n if (code !== 0) {\n reject(new Error(`${editor} exited with code ${code}`));\n return;\n }\n try {\n const data = await fs.readFile(filePath, \"utf8\");\n await fs.unlink(filePath).catch(() => {});\n resolve(data);\n } catch (err) {\n reject(err);\n }\n });\n });\n}\n\nexport { raw };\n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,IAAI,MAAM,GAAGA,SAAO,oBAAoB,OAAO,CAAC;AACvD;;AAEF,OAAI;IACF,MAAM,OAAO,MAAM,GAAG,SAAS,UAAU,OAAO;AAChD,UAAM,GAAG,OAAO,SAAS,CAAC,YAAY,GAAG;AACzC,YAAQ,KAAK;YACN,KAAK;AACZ,WAAO,IAAI;;IAEb;GACF"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as __export } from "./chunk-z5eko27R.mjs";
|
|
2
|
-
import
|
|
2
|
+
import readline from "node:readline";
|
|
3
3
|
|
|
4
4
|
//#region src/prompt/raw.ts
|
|
5
5
|
var raw_exports = /* @__PURE__ */ __export({
|
|
@@ -16,25 +16,7 @@ var raw_exports = /* @__PURE__ */ __export({
|
|
|
16
16
|
* @param opts Options for reading a line.
|
|
17
17
|
* @returns The line that was read.
|
|
18
18
|
*/
|
|
19
|
-
|
|
20
|
-
if (isDeno) {
|
|
21
|
-
await Deno.stdout.write(new TextEncoder().encode(message));
|
|
22
|
-
const decoder = new TextDecoder();
|
|
23
|
-
const buf = new Uint8Array(1024);
|
|
24
|
-
let input = "";
|
|
25
|
-
while (true) {
|
|
26
|
-
const n = await Deno.stdin.read(buf);
|
|
27
|
-
if (!n) break;
|
|
28
|
-
const chunk = decoder.decode(buf.subarray(0, n));
|
|
29
|
-
if (chunk.includes("\n")) {
|
|
30
|
-
input += chunk.split("\n")[0];
|
|
31
|
-
break;
|
|
32
|
-
}
|
|
33
|
-
input += chunk;
|
|
34
|
-
}
|
|
35
|
-
return input.trim() || def || "";
|
|
36
|
-
}
|
|
37
|
-
const readline = await import("node:readline");
|
|
19
|
+
function readLine(message = "", def, opts) {
|
|
38
20
|
return new Promise((resolve) => {
|
|
39
21
|
const rl = readline.createInterface({
|
|
40
22
|
input: process.stdin,
|
|
@@ -102,4 +84,5 @@ function cursorDown(n = 1) {
|
|
|
102
84
|
}
|
|
103
85
|
|
|
104
86
|
//#endregion
|
|
105
|
-
export { readKey as a, raw_exports as i, cursorDown as n, readLine as o, cursorUp as r, clearLines as t };
|
|
87
|
+
export { readKey as a, raw_exports as i, cursorDown as n, readLine as o, cursorUp as r, clearLines as t };
|
|
88
|
+
//# sourceMappingURL=raw-DEtZFeMv.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"raw-DEtZFeMv.mjs","names":[],"sources":["../src/prompt/raw.ts"],"sourcesContent":["import readline from \"node:readline\";\n\n/**\n * Reads a line from standard input.\n * @param message The message.\n * @param def Default value.\n * @param opts Options for reading a line.\n * @returns The line that was read.\n */\nexport function readLine(\n message = \"\",\n def?: string,\n opts?: { masked?: boolean; maskChar?: string; multiline?: boolean },\n): Promise<string> {\n return new Promise((resolve) => {\n const rl = readline.createInterface({\n input: process.stdin,\n output: process.stdout,\n terminal: true,\n });\n\n if (opts?.masked) {\n const write = (rl as any)._writeToOutput.bind(rl);\n (rl as any)._writeToOutput = (str: string) => {\n if (str.match(/^\\x1b/)) return write(str);\n if (str.endsWith(\"\\n\") || str.endsWith(\"\\r\")) return write(str);\n const mask = opts.maskChar ?? \"*\";\n write(mask.repeat(str.length));\n };\n }\n\n rl.question(message, (answer: string) => {\n rl.close();\n resolve(answer || def || \"\");\n });\n });\n}\n\n/**\n * Reads a single key from stdin.\n * @returns The key that was read.\n */\nexport async function readKey(): Promise<string> {\n return new Promise((resolve) => {\n const stdin = process.stdin;\n stdin.setRawMode(true);\n stdin.resume();\n stdin.once(\"data\", (data: Buffer) => {\n const s = data.toString();\n stdin.setRawMode(false);\n stdin.pause();\n if (s === \"\\r\" || s === \"\\n\") return resolve(\"enter\");\n if (s === \" \") return resolve(\"space\");\n if (s === \"\\u001b[A\") return resolve(\"up\");\n if (s === \"\\u001b[B\") return resolve(\"down\");\n if (s === \"\\u001b[C\") return resolve(\"right\");\n if (s === \"\\u001b[D\") return resolve(\"left\");\n return resolve(s);\n });\n });\n}\n\n/**\n * Clears `lines` amount of lines.\n * @param lines Amount of lines to clear.\n */\nexport function clearLines(lines = 1) {\n for (let i = 0; i < lines; i++) process.stdout.write(\"\\x1b[2K\\x1b[1A\");\n process.stdout.write(\"\\x1b[2K\\r\");\n}\n\n/**\n * Moves the cursor up `n` times.\n * @param n The amount of steps to move.\n */\nexport function cursorUp(n = 1) {\n process.stdout.write(`\\x1b[${n}A`);\n}\n\n/**\n * Moves the cursor down `n` times.\n * @param n The amount of steps to move.\n */\nexport function cursorDown(n = 1) {\n process.stdout.write(`\\x1b[${n}B`);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AASA,SAAgB,SACd,UAAU,IACV,KACA,MACiB;AACjB,QAAO,IAAI,SAAS,YAAY;EAC9B,MAAM,KAAK,SAAS,gBAAgB;GAClC,OAAO,QAAQ;GACf,QAAQ,QAAQ;GAChB,UAAU;GACX,CAAC;AAEF,MAAI,MAAM,QAAQ;GAChB,MAAM,QAAS,GAAW,eAAe,KAAK,GAAG;AACjD,GAAC,GAAW,kBAAkB,QAAgB;AAC5C,QAAI,IAAI,MAAM,QAAQ,CAAE,QAAO,MAAM,IAAI;AACzC,QAAI,IAAI,SAAS,KAAK,IAAI,IAAI,SAAS,KAAK,CAAE,QAAO,MAAM,IAAI;AAE/D,WADa,KAAK,YAAY,KACnB,OAAO,IAAI,OAAO,CAAC;;;AAIlC,KAAG,SAAS,UAAU,WAAmB;AACvC,MAAG,OAAO;AACV,WAAQ,UAAU,OAAO,GAAG;IAC5B;GACF;;;;;;AAOJ,eAAsB,UAA2B;AAC/C,QAAO,IAAI,SAAS,YAAY;EAC9B,MAAM,QAAQ,QAAQ;AACtB,QAAM,WAAW,KAAK;AACtB,QAAM,QAAQ;AACd,QAAM,KAAK,SAAS,SAAiB;GACnC,MAAM,IAAI,KAAK,UAAU;AACzB,SAAM,WAAW,MAAM;AACvB,SAAM,OAAO;AACb,OAAI,MAAM,QAAQ,MAAM,KAAM,QAAO,QAAQ,QAAQ;AACrD,OAAI,MAAM,IAAK,QAAO,QAAQ,QAAQ;AACtC,OAAI,MAAM,SAAY,QAAO,QAAQ,KAAK;AAC1C,OAAI,MAAM,SAAY,QAAO,QAAQ,OAAO;AAC5C,OAAI,MAAM,SAAY,QAAO,QAAQ,QAAQ;AAC7C,OAAI,MAAM,SAAY,QAAO,QAAQ,OAAO;AAC5C,UAAO,QAAQ,EAAE;IACjB;GACF;;;;;;AAOJ,SAAgB,WAAW,QAAQ,GAAG;AACpC,MAAK,IAAI,IAAI,GAAG,IAAI,OAAO,IAAK,SAAQ,OAAO,MAAM,iBAAiB;AACtE,SAAQ,OAAO,MAAM,YAAY;;;;;;AAOnC,SAAgB,SAAS,IAAI,GAAG;AAC9B,SAAQ,OAAO,MAAM,QAAQ,EAAE,GAAG;;;;;;AAOpC,SAAgB,WAAW,IAAI,GAAG;AAChC,SAAQ,OAAO,MAAM,QAAQ,EAAE,GAAG"}
|
|
@@ -34,4 +34,5 @@ declare function cursorUp(n?: number): void;
|
|
|
34
34
|
*/
|
|
35
35
|
declare function cursorDown(n?: number): void;
|
|
36
36
|
//#endregion
|
|
37
|
-
export { readKey as a, raw_d_exports as i, cursorDown as n, readLine as o, cursorUp as r, clearLines as t };
|
|
37
|
+
export { readKey as a, raw_d_exports as i, cursorDown as n, readLine as o, cursorUp as r, clearLines as t };
|
|
38
|
+
//# sourceMappingURL=raw-cqTp2vds.d.mts.map
|