@topcli/prompts 1.4.0 → 1.5.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/README.md CHANGED
@@ -51,6 +51,7 @@ question(message: string, options?: PromptOptions): Promise<string>
51
51
  ```
52
52
 
53
53
  Simple prompt, similar to `rl.question()` with an improved UI.
54
+ Use `options.secure` if you need to hide both input and answer.
54
55
  Use `options.validators` to handle user input.
55
56
 
56
57
  **Example**
@@ -156,6 +157,7 @@ export interface Validator {
156
157
  export interface QuestionOptions extends SharedOptions {
157
158
  defaultValue?: string;
158
159
  validators?: Validator[];
160
+ secure?: boolean;
159
161
  }
160
162
 
161
163
  export interface Choice {
package/index.d.ts CHANGED
@@ -15,6 +15,7 @@ export interface Validator {
15
15
  export interface QuestionOptions extends SharedOptions {
16
16
  defaultValue?: string;
17
17
  validators?: Validator[];
18
+ secure?: boolean;
18
19
  }
19
20
 
20
21
  export interface Choice {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@topcli/prompts",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Node.js user input library for command-line interfaces.",
5
5
  "scripts": {
6
6
  "test": "node --no-warnings=ExperimentalWarning --loader=esmock --test test/",
@@ -24,10 +24,10 @@
24
24
  "type": "module",
25
25
  "devDependencies": {
26
26
  "@nodesecure/eslint-config": "^1.8.0",
27
- "@types/node": "^20.8.2",
27
+ "@types/node": "^20.8.5",
28
28
  "c8": "^8.0.1",
29
- "eslint": "^8.50.0",
30
- "esmock": "^2.5.1"
29
+ "eslint": "^8.51.0",
30
+ "esmock": "^2.5.2"
31
31
  },
32
32
  "dependencies": {
33
33
  "is-unicode-supported": "^1.3.0",
@@ -1,6 +1,7 @@
1
1
  // Import Node.js Dependencies
2
2
  import { EOL } from "node:os";
3
3
  import { createInterface } from "node:readline";
4
+ import { Writable } from "node:stream";
4
5
 
5
6
  // Import Third-party Dependencies
6
7
  import stripAnsi from "strip-ansi";
@@ -23,11 +24,23 @@ export class AbstractPrompt {
23
24
  this.message = message;
24
25
  this.history = [];
25
26
  this.agent = PromptAgent.agent();
27
+ this.mute = false;
26
28
 
27
29
  if (this.stdout.isTTY) {
28
30
  this.stdin.setRawMode(true);
29
31
  }
30
- this.rl = createInterface({ input, output });
32
+ this.rl = createInterface({
33
+ input,
34
+ output: new Writable({
35
+ write: (chunk, encoding, callback) => {
36
+ if (!this.mute) {
37
+ this.stdout.write(chunk, encoding);
38
+ }
39
+ callback();
40
+ }
41
+ }),
42
+ terminal: true
43
+ });
31
44
  }
32
45
 
33
46
  write(data) {
@@ -10,13 +10,15 @@ import { SYMBOLS } from "./constants.js";
10
10
 
11
11
  export class QuestionPrompt extends AbstractPrompt {
12
12
  #validators;
13
+ #secure;
13
14
 
14
15
  constructor(message, options = {}) {
15
16
  const {
16
17
  stdin = process.stdin,
17
18
  stdout = process.stdout,
18
19
  defaultValue,
19
- validators = []
20
+ validators = [],
21
+ secure = false
20
22
  } = options;
21
23
 
22
24
  super(message, stdin, stdout);
@@ -28,6 +30,7 @@ export class QuestionPrompt extends AbstractPrompt {
28
30
  this.defaultValue = defaultValue;
29
31
  this.tip = this.defaultValue ? ` (${this.defaultValue})` : "";
30
32
  this.#validators = validators;
33
+ this.#secure = Boolean(secure);
31
34
  this.questionSuffixError = "";
32
35
  }
33
36
 
@@ -37,9 +40,11 @@ export class QuestionPrompt extends AbstractPrompt {
37
40
 
38
41
  this.rl.question(questionQuery, (answer) => {
39
42
  this.history.push(questionQuery + answer);
43
+ this.mute = false;
40
44
 
41
45
  resolve(answer);
42
46
  });
47
+ this.mute = this.#secure;
43
48
  });
44
49
  }
45
50
 
@@ -54,8 +59,7 @@ export class QuestionPrompt extends AbstractPrompt {
54
59
 
55
60
  #writeAnswer() {
56
61
  const prefix = this.answer ? SYMBOLS.Tick : SYMBOLS.Cross;
57
- const answer = kleur.yellow(this.answer ?? "");
58
-
62
+ const answer = kleur.yellow(this.#secure ? "CONFIDENTIAL" : this.answer ?? "");
59
63
  this.write(`${prefix} ${kleur.bold(this.message)} ${SYMBOLS.Pointer} ${answer}${EOL}`);
60
64
  }
61
65