@little-nebulae/clack 0.2.0 → 0.3.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/dist/index.d.ts CHANGED
@@ -1,19 +1,35 @@
1
- import { CANCEL_SYMBOL, ConfirmOptions, MultiLineOptions, PathOptions, SelectOptions, TextOptions } from "@clack/prompts";
2
- import { UnexpectedError } from "@little-nebulae/error";
3
- type ClackIsCancelledWrapper = {
4
- isCancelled: true;
5
- message: string;
6
- };
7
- type ClackNotCancelledWrapper<T> = {
8
- isCancelled: false;
9
- value: T;
10
- };
11
- type ClackReturnWrapper<T> = ClackIsCancelledWrapper | ClackNotCancelledWrapper<T>;
1
+ import { BaseError, UnexpectedError } from "@little-nebulae/error";
2
+ import { ConfirmOptions, MultiLineOptions, PathOptions, SelectOptions, TextOptions } from "@clack/prompts";
3
+ import { AsyncResult } from "@little-nebulae/result";
12
4
  export declare const DEFAULT_CANCEL_MESSAGE = "Operation cancelled.";
13
- export declare function wrapClackReturn<T>(value: T | typeof CANCEL_SYMBOL, message?: string): ClackReturnWrapper<T>;
14
- export declare function clackText(opts: TextOptions): Promise<import("@little-nebulae/result").Result<ClackReturnWrapper<string>, UnexpectedError<unknown, null>>>;
15
- export declare function clackMultiline(opts: MultiLineOptions): Promise<import("@little-nebulae/result").Result<ClackReturnWrapper<string>, UnexpectedError<unknown, null>>>;
16
- export declare function clackSelect<Value>(opts: SelectOptions<Value>): Promise<import("@little-nebulae/result").Result<ClackReturnWrapper<Awaited<Value>>, UnexpectedError<unknown, null>>>;
17
- export declare function clackPath(opts: PathOptions): Promise<import("@little-nebulae/result").Result<ClackReturnWrapper<string>, UnexpectedError<unknown, null>>>;
18
- export declare function clackConfirm(opts: ConfirmOptions): Promise<import("@little-nebulae/result").Result<ClackReturnWrapper<boolean>, UnexpectedError<unknown, null>>>;
19
- export type { ClackIsCancelledWrapper, ClackNotCancelledWrapper, ClackReturnWrapper };
5
+ export declare class ClackNonInteractiveError extends BaseError<"CLACK_NON_INTERACTIVE_ERROR", null> {
6
+ readonly name = "ClackNonInteractiveError";
7
+ readonly code = "CLACK_NON_INTERACTIVE_ERROR";
8
+ constructor(message?: string);
9
+ }
10
+ export declare class ClackIsCancelledError extends BaseError<"CLACK_IS_CANCELLED_ERROR", string> {
11
+ readonly name = "ClackIsCancelledError";
12
+ readonly code = "CLACK_IS_CANCELLED_ERROR";
13
+ constructor(message?: string);
14
+ }
15
+ interface ClackTextParams extends TextOptions {
16
+ isTTY: boolean;
17
+ }
18
+ export declare function clackText({ isTTY, ...opts }: ClackTextParams): AsyncResult<string, ClackNonInteractiveError | ClackIsCancelledError | UnexpectedError>;
19
+ interface ClackMultilineParams extends MultiLineOptions {
20
+ isTTY: boolean;
21
+ }
22
+ export declare function clackMultiline({ isTTY, ...opts }: ClackMultilineParams): AsyncResult<string, ClackNonInteractiveError | ClackIsCancelledError | UnexpectedError>;
23
+ interface ClackSelectParams<Value> extends SelectOptions<Value> {
24
+ isTTY: boolean;
25
+ }
26
+ export declare function clackSelect<Value>({ isTTY, ...opts }: ClackSelectParams<Value>): AsyncResult<Value, ClackNonInteractiveError | ClackIsCancelledError | UnexpectedError>;
27
+ interface ClackPathParams extends PathOptions {
28
+ isTTY: boolean;
29
+ }
30
+ export declare function clackPath({ isTTY, ...opts }: ClackPathParams): AsyncResult<string, ClackNonInteractiveError | ClackIsCancelledError | UnexpectedError>;
31
+ interface ClackConfirmParams extends ConfirmOptions {
32
+ isTTY: boolean;
33
+ }
34
+ export declare function clackConfirm({ isTTY, ...opts }: ClackConfirmParams): AsyncResult<boolean, ClackNonInteractiveError | ClackIsCancelledError | UnexpectedError>;
35
+ export {};
package/dist/index.js CHANGED
@@ -1,72 +1,105 @@
1
- import { confirm, isCancel, multiline, path, select, text } from "@clack/prompts";
2
- import { UnexpectedError, composeErrorMessage } from "@little-nebulae/error";
3
- import { attemptAsync } from "@little-nebulae/result";
1
+ import { BaseError, UnexpectedError, composeErrorMessage } from "@little-nebulae/error";
2
+ import { CANCEL_SYMBOL, confirm, isCancel, multiline, path, select, text } from "@clack/prompts";
3
+ import { fail, succeed } from "@little-nebulae/result";
4
4
  // src/constants.ts
5
5
  const DEFAULT_CANCEL_MESSAGE = "Operation cancelled.";
6
- // src/utils.ts
7
- function wrapClackReturn(value, message = DEFAULT_CANCEL_MESSAGE) {
8
- if (isCancel(value)) return {
9
- isCancelled: true,
10
- message
11
- };
12
- return {
13
- isCancelled: false,
14
- value
15
- };
16
- }
6
+ // src/errors/non-interactive.ts
7
+ var ClackNonInteractiveError = class extends BaseError {
8
+ name = "ClackNonInteractiveError";
9
+ code = "CLACK_NON_INTERACTIVE_ERROR";
10
+ constructor(message = "A Clack prompt can only run in an interactive terminal context.") {
11
+ super({
12
+ message,
13
+ cause: null,
14
+ meta: null
15
+ });
16
+ }
17
+ };
18
+ // src/errors/is-cancelled.ts
19
+ var ClackIsCancelledError = class extends BaseError {
20
+ name = "ClackIsCancelledError";
21
+ code = "CLACK_IS_CANCELLED_ERROR";
22
+ constructor(message = DEFAULT_CANCEL_MESSAGE) {
23
+ super({
24
+ message,
25
+ cause: CANCEL_SYMBOL.toString(),
26
+ meta: null
27
+ });
28
+ }
29
+ };
17
30
  // src/prompts/text.ts
18
- async function clackText(opts) {
19
- return attemptAsync({
20
- tryFn: async () => wrapClackReturn(await text(opts)),
21
- catchFn: (error) => new UnexpectedError({
31
+ async function clackText({ isTTY, ...opts }) {
32
+ if (!isTTY) return fail(new ClackNonInteractiveError());
33
+ try {
34
+ const answer = await text(opts);
35
+ if (isCancel(answer)) return fail(new ClackIsCancelledError());
36
+ return succeed(answer);
37
+ } catch (error) {
38
+ return fail(new UnexpectedError({
22
39
  message: composeErrorMessage({ operation: "prompt for text input" }),
23
40
  cause: error,
24
41
  meta: null
25
- })
26
- });
42
+ }));
43
+ }
27
44
  }
28
45
  // src/prompts/multiline.ts
29
- async function clackMultiline(opts) {
30
- return attemptAsync({
31
- tryFn: async () => wrapClackReturn(await multiline(opts)),
32
- catchFn: (error) => new UnexpectedError({
46
+ async function clackMultiline({ isTTY, ...opts }) {
47
+ if (!isTTY) return fail(new ClackNonInteractiveError());
48
+ try {
49
+ const answer = await multiline(opts);
50
+ if (isCancel(answer)) return fail(new ClackIsCancelledError());
51
+ return succeed(answer);
52
+ } catch (error) {
53
+ return fail(new UnexpectedError({
33
54
  message: composeErrorMessage({ operation: "prompt for multi-line text input" }),
34
55
  cause: error,
35
56
  meta: null
36
- })
37
- });
57
+ }));
58
+ }
38
59
  }
39
60
  // src/prompts/select.ts
40
- async function clackSelect(opts) {
41
- return attemptAsync({
42
- tryFn: async () => wrapClackReturn(await select(opts)),
43
- catchFn: (error) => new UnexpectedError({
61
+ async function clackSelect({ isTTY, ...opts }) {
62
+ if (!isTTY) return fail(new ClackNonInteractiveError());
63
+ try {
64
+ const answer = await select(opts);
65
+ if (isCancel(answer)) return fail(new ClackIsCancelledError());
66
+ return succeed(answer);
67
+ } catch (error) {
68
+ return fail(new UnexpectedError({
44
69
  message: composeErrorMessage({ operation: "prompt for a selection" }),
45
70
  cause: error,
46
71
  meta: null
47
- })
48
- });
72
+ }));
73
+ }
49
74
  }
50
75
  // src/prompts/path.ts
51
- async function clackPath(opts) {
52
- return attemptAsync({
53
- tryFn: async () => wrapClackReturn(await path(opts)),
54
- catchFn: (error) => new UnexpectedError({
76
+ async function clackPath({ isTTY, ...opts }) {
77
+ if (!isTTY) return fail(new ClackNonInteractiveError());
78
+ try {
79
+ const answer = await path(opts);
80
+ if (isCancel(answer)) return fail(new ClackIsCancelledError());
81
+ return succeed(answer);
82
+ } catch (error) {
83
+ return fail(new UnexpectedError({
55
84
  message: composeErrorMessage({ operation: "prompt for path selection" }),
56
85
  cause: error,
57
86
  meta: null
58
- })
59
- });
87
+ }));
88
+ }
60
89
  }
61
90
  // src/prompts/confirm.ts
62
- async function clackConfirm(opts) {
63
- return attemptAsync({
64
- tryFn: async () => wrapClackReturn(await confirm(opts)),
65
- catchFn: (error) => new UnexpectedError({
91
+ async function clackConfirm({ isTTY, ...opts }) {
92
+ if (!isTTY) return fail(new ClackNonInteractiveError());
93
+ try {
94
+ const answer = await confirm(opts);
95
+ if (isCancel(answer)) return fail(new ClackIsCancelledError());
96
+ return succeed(answer);
97
+ } catch (error) {
98
+ return fail(new UnexpectedError({
66
99
  message: composeErrorMessage({ operation: "prompt for confirmation" }),
67
100
  cause: error,
68
101
  meta: null
69
- })
70
- });
102
+ }));
103
+ }
71
104
  }
72
- export { DEFAULT_CANCEL_MESSAGE, clackConfirm, clackMultiline, clackPath, clackSelect, clackText, wrapClackReturn };
105
+ export { ClackIsCancelledError, ClackNonInteractiveError, DEFAULT_CANCEL_MESSAGE, clackConfirm, clackMultiline, clackPath, clackSelect, clackText };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@little-nebulae/clack",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist"