@trenskow/reader 0.1.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 +11 -0
- package/README.md +87 -0
- package/eslint.config.js +54 -0
- package/index.js +17 -0
- package/lib/index.js +15 -0
- package/lib/interview.js +105 -0
- package/lib/question.js +51 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Copyright 2024 Kristian Trenskow
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
+
|
|
11
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# @trenskow/reader
|
|
2
|
+
|
|
3
|
+
A simple package to ask questions of a stream.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
To ask a question use the example below.
|
|
8
|
+
|
|
9
|
+
````javascript
|
|
10
|
+
import { question } from '@trenskow/reader';
|
|
11
|
+
|
|
12
|
+
const answer = await question('What is your name?', {
|
|
13
|
+
// options
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
console.info(answer);
|
|
17
|
+
````
|
|
18
|
+
|
|
19
|
+
### Options
|
|
20
|
+
|
|
21
|
+
These are the options available for `question`.
|
|
22
|
+
|
|
23
|
+
| Name | Description | Default value |
|
|
24
|
+
| -------------- | -------------------------------------------- | ---------------- |
|
|
25
|
+
| `defaultValue` | The default value for the question. | |
|
|
26
|
+
| `silent` | Do not echo input (useful for private data). | `false` |
|
|
27
|
+
| `input` | The input stream to read from. | `process.stdin` |
|
|
28
|
+
| `output` | The output stream to write to. | `process.stdout` |
|
|
29
|
+
|
|
30
|
+
## Interviews
|
|
31
|
+
|
|
32
|
+
There is also an interview mechanism for filling an entire data structure. The data is validated using [isvalid](https://npmjs.com/package/isvalid).
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
### Usage
|
|
36
|
+
|
|
37
|
+
````javascript
|
|
38
|
+
import { interview } from '@trenskow/reader';
|
|
39
|
+
|
|
40
|
+
const data = await interview({
|
|
41
|
+
// schema
|
|
42
|
+
'username': {
|
|
43
|
+
type: String,
|
|
44
|
+
name: 'Username',
|
|
45
|
+
description: 'Enter your username.',
|
|
46
|
+
type: String,
|
|
47
|
+
required: true,
|
|
48
|
+
len: '3-'
|
|
49
|
+
},
|
|
50
|
+
'password': {
|
|
51
|
+
type: String,
|
|
52
|
+
name: 'Password',
|
|
53
|
+
description: 'Enter your password.',
|
|
54
|
+
type: String,
|
|
55
|
+
required: true,
|
|
56
|
+
silent: true // Tells the interviewer to not echo input.
|
|
57
|
+
}
|
|
58
|
+
}, {
|
|
59
|
+
// options
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
console.info(data); // Will output `{ username: 'my-username', password: 'my-password' }`.
|
|
63
|
+
````
|
|
64
|
+
|
|
65
|
+
### Options
|
|
66
|
+
|
|
67
|
+
These are the options available for `interview`.
|
|
68
|
+
|
|
69
|
+
| Name | Description | Default value |
|
|
70
|
+
| --------- | --------------------------------------------------------- | ---------------- |
|
|
71
|
+
| `spacing` | The number of vertical spacing (lines) between questions. | `0` |
|
|
72
|
+
| `input` | The input stream to read from. | `process.stdin` |
|
|
73
|
+
| `output` | The output stream to write to. | `process.stdout` |
|
|
74
|
+
|
|
75
|
+
### Extensions to isvalid schemas
|
|
76
|
+
|
|
77
|
+
In addition to the build-in validators of isvalid, the following is also supported (as in the example above).
|
|
78
|
+
|
|
79
|
+
| Name | Description | Type | Default value |
|
|
80
|
+
| ------------- | ----------------------------------------------------------- | :-------: | :-----------: |
|
|
81
|
+
| `name` | The string to print before the user input. | `String` | |
|
|
82
|
+
| `description` | The string to print above the question. | `String` | |
|
|
83
|
+
| `silent` | Indicates if the property is private (does not echo input). | `Boolean` | `false` |
|
|
84
|
+
|
|
85
|
+
# License
|
|
86
|
+
|
|
87
|
+
See license in LICENSE.
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import globals from 'globals';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import js from '@eslint/js';
|
|
5
|
+
import { FlatCompat } from '@eslint/eslintrc';
|
|
6
|
+
|
|
7
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
8
|
+
const __dirname = path.dirname(__filename);
|
|
9
|
+
const compat = new FlatCompat({
|
|
10
|
+
baseDirectory: __dirname,
|
|
11
|
+
recommendedConfig: js.configs.recommended,
|
|
12
|
+
allConfig: js.configs.all
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export default [...compat.extends('eslint:recommended'), {
|
|
16
|
+
languageOptions: {
|
|
17
|
+
globals: {
|
|
18
|
+
...globals.node,
|
|
19
|
+
...globals.mocha,
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
ecmaVersion: 'latest',
|
|
23
|
+
sourceType: 'module',
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
rules: {
|
|
27
|
+
indent: ['error', 'tab', {
|
|
28
|
+
SwitchCase: 1,
|
|
29
|
+
}],
|
|
30
|
+
|
|
31
|
+
'linebreak-style': ['error', 'unix'],
|
|
32
|
+
quotes: ['error', 'single'],
|
|
33
|
+
semi: ['error', 'always'],
|
|
34
|
+
|
|
35
|
+
'no-console': ['error', {
|
|
36
|
+
allow: ['warn', 'error', 'info'],
|
|
37
|
+
}],
|
|
38
|
+
|
|
39
|
+
'no-unused-vars': ['error', {
|
|
40
|
+
argsIgnorePattern: '^_',
|
|
41
|
+
}],
|
|
42
|
+
|
|
43
|
+
'no-empty': ['error', {
|
|
44
|
+
allowEmptyCatch: true,
|
|
45
|
+
}],
|
|
46
|
+
|
|
47
|
+
'no-trailing-spaces': ['error', {
|
|
48
|
+
ignoreComments: true,
|
|
49
|
+
}],
|
|
50
|
+
|
|
51
|
+
'require-atomic-updates': 'off',
|
|
52
|
+
'eol-last': ['error', 'always'],
|
|
53
|
+
},
|
|
54
|
+
}];
|
package/index.js
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//
|
|
2
|
+
// interview.js
|
|
3
|
+
// 0-bit-games-cli
|
|
4
|
+
//
|
|
5
|
+
// Created by Kristian Trenskow on 2025/12/25
|
|
6
|
+
// See license in LICENSE.
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
import interview from './interview.js';
|
|
10
|
+
import question from './question.js';
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
interview,
|
|
14
|
+
question
|
|
15
|
+
};
|
package/lib/interview.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
//
|
|
2
|
+
// interview.js
|
|
3
|
+
// 0-bit-games-cli
|
|
4
|
+
//
|
|
5
|
+
// Created by Kristian Trenskow on 2025/12/23
|
|
6
|
+
// See license in LICENSE.
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
import { default as isvalid, plugins, formalize, keyPaths } from 'isvalid';
|
|
10
|
+
import keyd from 'keyd';
|
|
11
|
+
import print from '@trenskow/print';
|
|
12
|
+
|
|
13
|
+
import question from './question.js';
|
|
14
|
+
|
|
15
|
+
plugins.use(() => ({
|
|
16
|
+
phase: 'pre',
|
|
17
|
+
supportsType: () => true,
|
|
18
|
+
validatorsForType: () => ({
|
|
19
|
+
label: ['string'],
|
|
20
|
+
description: ['string'],
|
|
21
|
+
silent: ['boolean']
|
|
22
|
+
}),
|
|
23
|
+
validate: (data) => data,
|
|
24
|
+
formalize: (schema) => schema
|
|
25
|
+
}));
|
|
26
|
+
|
|
27
|
+
const interview = async (schema, { spacing = 0, input = process.stdin, output = process.stdout } = {}) => {
|
|
28
|
+
|
|
29
|
+
schema = formalize(schema);
|
|
30
|
+
|
|
31
|
+
if (schema.type === Object) {
|
|
32
|
+
|
|
33
|
+
const allKeyPaths = keyPaths(schema)
|
|
34
|
+
.all({ maxDepth: 1 })
|
|
35
|
+
.filter((keyPath) => keyPath);
|
|
36
|
+
|
|
37
|
+
let result = {};
|
|
38
|
+
|
|
39
|
+
for (let idx in allKeyPaths) {
|
|
40
|
+
|
|
41
|
+
const keyPath = allKeyPaths[idx];
|
|
42
|
+
|
|
43
|
+
keyd(result).set(keyPath, await interview(keyPaths(schema).get(keyPath), { spacing }));
|
|
44
|
+
|
|
45
|
+
if (idx < allKeyPaths.length - 1) {
|
|
46
|
+
for (let i = 0; i < spacing; i++) {
|
|
47
|
+
print();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return await isvalid(result, schema);
|
|
54
|
+
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (schema.type === Array) {
|
|
58
|
+
|
|
59
|
+
let result = [];
|
|
60
|
+
|
|
61
|
+
while (true) {
|
|
62
|
+
|
|
63
|
+
const item = await interview(schema.items);
|
|
64
|
+
|
|
65
|
+
if (!item)
|
|
66
|
+
break;
|
|
67
|
+
|
|
68
|
+
result.push(item);
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return result;
|
|
73
|
+
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let attempts = 0;
|
|
77
|
+
|
|
78
|
+
while (true) {
|
|
79
|
+
try {
|
|
80
|
+
|
|
81
|
+
if (!attempts && schema.description) {
|
|
82
|
+
print.tty.nn('\x1b[2m');
|
|
83
|
+
print(schema.description);
|
|
84
|
+
print.tty.nn('\x1b[0m');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return await isvalid(await question(`${schema.label}${schema.required ? ' (required)' : ''}: `, {
|
|
88
|
+
defaultValue: schema.default,
|
|
89
|
+
input,
|
|
90
|
+
output,
|
|
91
|
+
silent: schema.silent === true
|
|
92
|
+
}) || undefined, schema);
|
|
93
|
+
|
|
94
|
+
} catch (error) {
|
|
95
|
+
print.tty.err.nn('\x1b[31m');
|
|
96
|
+
print.err(error.message);
|
|
97
|
+
print.tty.err.nn('\x1b[0m');
|
|
98
|
+
print.err();
|
|
99
|
+
attempts++;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export default interview;
|
package/lib/question.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//
|
|
2
|
+
// question.js
|
|
3
|
+
// 0-bit-games-cli
|
|
4
|
+
//
|
|
5
|
+
// Created by Kristian Trenskow on 2024/12/20
|
|
6
|
+
// See license in LICENSE.
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
import readline from 'readline';
|
|
10
|
+
import { Writable } from 'stream';
|
|
11
|
+
|
|
12
|
+
const question = async (question, { defaultValue, silent = false, input = process.stdin, output = process.stdout } = {}) => {
|
|
13
|
+
|
|
14
|
+
const mutableStdout = new Writable({
|
|
15
|
+
write: function (chunk, encoding, callback) {
|
|
16
|
+
if (!this.muted) {
|
|
17
|
+
output.write(chunk, encoding);
|
|
18
|
+
}
|
|
19
|
+
callback();
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
mutableStdout.muted = false;
|
|
24
|
+
|
|
25
|
+
const rl = readline.createInterface({
|
|
26
|
+
input: input,
|
|
27
|
+
output: mutableStdout,
|
|
28
|
+
terminal: output.isTTY
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const ask = (question) => new Promise((resolve) => {
|
|
32
|
+
|
|
33
|
+
rl.question(question, (answer) => {
|
|
34
|
+
if (silent) output.write('\n');
|
|
35
|
+
resolve(answer);
|
|
36
|
+
rl.close();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
if (defaultValue) {
|
|
40
|
+
rl.write(defaultValue);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
mutableStdout.muted = silent;
|
|
44
|
+
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return await ask(question);
|
|
48
|
+
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export default question;
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@trenskow/reader",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A simple package to ask questions of a stream.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/trenskow/reader.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"reader",
|
|
16
|
+
"readline",
|
|
17
|
+
"question",
|
|
18
|
+
"stdin",
|
|
19
|
+
"stream"
|
|
20
|
+
],
|
|
21
|
+
"author": "Kristian Trenskow <this.is@kristians.email>",
|
|
22
|
+
"license": "BSD-3-Clause",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/trenskow/reader/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/trenskow/reader#readme",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@eslint/eslintrc": "^3.2.0",
|
|
29
|
+
"@eslint/js": "^9.17.0",
|
|
30
|
+
"eslint": "^9.17.0",
|
|
31
|
+
"globals": "^15.14.0"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@trenskow/print": "^0.1.2",
|
|
35
|
+
"isvalid": "^4.1.27",
|
|
36
|
+
"keyd": "^2.1.17"
|
|
37
|
+
}
|
|
38
|
+
}
|