Skip to content

Commit 20af457

Browse files
author
keindev
committed
feat: add unknown type to error param for error & fail methods
1 parent e0692a3 commit 20af457

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

src/Task.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,19 @@ export class Task {
128128
return this;
129129
}
130130

131-
error(error?: string | Error, fail?: boolean): Task {
131+
error(error?: string | Error | unknown, fail?: boolean): Task {
132132
if (typeof error === 'string') this.#errors.push(error);
133133
if (error instanceof Error && error.stack) this.#errors.push(error.stack);
134134
if (fail) this.fail(error);
135135

136136
return this;
137137
}
138138

139-
fail(error?: string | Error, clear = this.#autoClear): never {
140-
const text = error instanceof Error ? error.name : error;
139+
fail(error?: string | Error | unknown, clear = this.#autoClear): never {
140+
let text: string | undefined;
141+
142+
if (error instanceof Error) text = error.name;
143+
if (typeof error === 'string') text = error;
141144

142145
this.setStatus(TaskStatus.Failed, text, clear);
143146

src/TaskTree.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class TaskTree {
4141
}
4242

4343
/** Fail active task or adds a new subtask and call fail on it */
44-
static fail(error: string | Error, active = true): never {
44+
static fail(error: string | Error | unknown, active = true): never {
4545
return TaskTree.tree().fail(error, active);
4646
}
4747

@@ -50,7 +50,7 @@ export class TaskTree {
5050
*
5151
* @param theme - Theme properties. The field name is a modifier the value is options
5252
* @example
53-
* ```javascript
53+
* ```typescript
5454
* const theme = {
5555
* default: '#ffffff',
5656
* success: ['#008000', '✔'],
@@ -60,21 +60,26 @@ export class TaskTree {
6060
* error: ['#ff0000', '✖', '[error]'],
6161
* ...
6262
* };
63+
*
64+
* const tree = TaskTree.tree();
65+
*
66+
* // start task tree log update in terminal
67+
* tree.start();
6368
* ```
6469
* @description
65-
* | option | color | symbol | badge | description |
66-
* | ----------- | ----------------- | ------ | ----- | -------------------------------------------- |
67-
* | **default** | text | ✖ | ✖ | default color |
68-
* | **active** | symbol | ✔ | ✖ | spinner, progress bar color |
69-
* | **success** | symbol, text, bar | | ✖ | task symbol, progress bar color |
70-
* | **skip** | symbol, text, bar | | ✔ | task symbol, progress bar color |
71-
* | **error** | symbol, text, bar | | ✔ | task symbol, error title, progress bar color |
72-
* | **message** | symbol | ✔ | ✖ | dim pointer to task information |
73-
* | **info** | symbol | ✔ | ✖ | information message symbol |
74-
* | **warning** | symbol | ✔ | ✖ | warning message symbol |
75-
* | **subtask** | symbol, text | | ✖ | dim pointer to subtask |
76-
* | **list** | symbol | ✔ | ✖ | list symbol |
77-
* | **dim** | symbol, bar | | ✖ | dim color |
70+
* | Type | `badge` | `color` | `symbol` | Description |
71+
* | :------ | :------: | :---------------------------- | :---------: | :------------------------------------------- |
72+
* | default | | `[default]` - text | `-` | default color |
73+
* | active | | `#4285f4` - symbol | `frame (⠧)` | spinner, progress bar color |
74+
* | success | ✖ | `#00c851` - symbol, text, bar | | task symbol, progress bar color |
75+
* | skip | `[skip]` | `#ff8800` - symbol, text, bar | | task symbol, progress bar color |
76+
* | error | `[fail]` | `#ff4444` - symbol, text, bar | | task symbol, error title, progress bar color |
77+
* | message | | `#2e2e2e` - symbol | ─ | dim pointer to task information |
78+
* | info | | `#33b5e5` - symbol | ℹ | information message symbol |
79+
* | warning | | `#ffbb33` - symbol | ⚠ | warning message symbol |
80+
* | subtask | ✖ | `#2e2e2e` - symbol, text | | dim pointer to subtask |
81+
* | list | | `#4285f4` - symbol | ❯ | list symbol |
82+
* | dim | | `#838584` - symbol, bar | `-` | dim color |
7883
*
7984
* > If you use a gradient fill for the progress bar - the color will change from `active` to `success`
8085
*/
@@ -102,18 +107,18 @@ export class TaskTree {
102107
}
103108

104109
/** Force the process to exit (see process.exit). Do nothing in "silent mode" */
105-
exit(code: ExitCode = ExitCode.Success, error?: string | Error): void | never {
110+
exit(code: ExitCode = ExitCode.Success, error?: string | Error | unknown): void | never {
106111
if (this.#started) {
107112
this.stop();
108113

109114
if (this.#silent) {
110-
if (code === ExitCode.Error) throw error instanceof Error ? error : new Error(error);
115+
if (code === ExitCode.Error) throw this.getError(error);
111116
} else {
112117
// eslint-disable-next-line no-process-exit
113118
process.exit(code);
114119
}
115120
} else if (code === ExitCode.Error) {
116-
throw error instanceof Error ? error : new Error(error);
121+
throw this.getError(error);
117122
}
118123
}
119124

@@ -122,8 +127,8 @@ export class TaskTree {
122127
* @param error - Text or Error object for display
123128
* @param active - If `true` - call failed for active task, else create new task and call fail on it
124129
*/
125-
fail(error: string | Error, active = true): never {
126-
const errorObject = error instanceof Error ? error : new Error(error);
130+
fail(error: string | Error | unknown, active = true): never {
131+
const errorObject = this.getError(error);
127132

128133
if (!this.#started || this.#silent) {
129134
throw errorObject;
@@ -192,6 +197,12 @@ export class TaskTree {
192197
return this;
193198
}
194199

200+
private getError(error: string | Error | unknown): Error {
201+
const obj = error instanceof Error ? error : new Error(typeof error === 'string' ? error : '');
202+
203+
return obj;
204+
}
205+
195206
private log(): void {
196207
const offset = this.#offset;
197208

0 commit comments

Comments
 (0)