@trenskow/reader 0.1.75 → 0.1.76

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/lib/interview.js CHANGED
@@ -31,7 +31,8 @@ const isvalid = async () => {
31
31
  validatorsForType: () => ({
32
32
  label: ['string'],
33
33
  description: ['string'],
34
- silent: ['boolean']
34
+ silent: ['boolean'],
35
+ env: ['string']
35
36
  }),
36
37
  validate: (data) => data,
37
38
  formalize: (schema) => schema
@@ -44,7 +45,12 @@ const isvalid = async () => {
44
45
  });
45
46
  };
46
47
 
47
- const interview = async (schema, { spacing = 0, input = process.stdin, output = process.stdout, strings = {} } = {}) => {
48
+ const interview = async (schema, {
49
+ spacing = 0,
50
+ input = process.stdin,
51
+ output = process.stdout,
52
+ strings = {}
53
+ } = {}) => {
48
54
 
49
55
  schema = (await isvalid()).formalize(schema);
50
56
 
@@ -94,22 +100,31 @@ const interview = async (schema, { spacing = 0, input = process.stdin, output =
94
100
  }
95
101
 
96
102
  let attempts = 0;
103
+ let initial;
104
+
105
+ if (schema.env) {
106
+ initial = process.env[schema.env];
107
+ }
97
108
 
98
109
  while (true) {
99
110
  try {
100
111
 
101
- if (!attempts && schema.description) {
112
+ if (!initial && !attempts && schema.description) {
102
113
  print.tty.nn('\x1b[2m');
103
114
  print(schema.description);
104
115
  print.tty.nn('\x1b[0m');
105
116
  }
106
117
 
107
- return await (await isvalid())(await question(`${schema.label}${schema.required ? ` ${strings?.required || '(required)'}` : ''}: `, {
118
+ const answer = initial || await question(`${schema.label}${schema.required ? ` ${strings?.required || '(required)'}` : ''}: `, {
108
119
  defaultValue: schema.default,
109
120
  input,
110
121
  output,
111
122
  silent: schema.silent === true
112
- }) || undefined, schema);
123
+ });
124
+
125
+ initial = undefined;
126
+
127
+ return await (await isvalid())(answer, schema);
113
128
 
114
129
  } catch (error) {
115
130
  print.err.bold(error.message);
package/lib/question.js CHANGED
@@ -9,7 +9,12 @@
9
9
  import readline from 'readline';
10
10
  import { Writable } from 'stream';
11
11
 
12
- const question = async (question, { defaultValue, silent = false, input = process.stdin, output = process.stdout } = {}) => {
12
+ const question = async (question, {
13
+ defaultValue,
14
+ silent = false,
15
+ input = process.stdin,
16
+ output = process.stdout
17
+ } = {}) => {
13
18
 
14
19
  const mutableStdout = new Writable({
15
20
  write: function (chunk, encoding, callback) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/reader",
3
- "version": "0.1.75",
3
+ "version": "0.1.76",
4
4
  "description": "A simple package to ask questions of a stream.",
5
5
  "main": "index.js",
6
6
  "type": "module",