input-shell 1.0.1 → 1.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/README.md +113 -9
- package/demos/demos.input.async.js +28 -0
- package/demos/demos.input.js +1 -1
- package/demos/demos.input.select.checkbox.async.js +30 -0
- package/demos/demos.input.select.checkbox.js +5 -9
- package/demos/demos.input.select.multiple.questions.async.js +42 -0
- package/demos/demos.input.select.multiple.questions.js +27 -3
- package/demos/demos.input.select.radio.async.js +29 -0
- package/demos/demos.input.select.radio.js +6 -9
- package/index.js +389 -36
- package/package.json +1 -1
- package/test/test.input.js +18 -0
- package/test/test.input.multiple.async.js +175 -0
- package/test/test.input.multiple.js +93 -74
- package/test/test.input.select.js +18 -0
- package/test/test.input.select.multiple.js +84 -0
@@ -1,19 +1,32 @@
|
|
1
|
+
/**
|
2
|
+
*
|
3
|
+
* Package:
|
4
|
+
* Author: Ganesh B
|
5
|
+
* Description:
|
6
|
+
* Install: npm i input-shell --save
|
7
|
+
* Github: https://github.com/ganeshkbhat/
|
8
|
+
* npmjs Link: https://www.npmjs.com/package/
|
9
|
+
* File: index.js
|
10
|
+
* File Description:
|
11
|
+
*
|
12
|
+
*
|
13
|
+
*/
|
14
|
+
|
15
|
+
/* eslint no-console: 0 */
|
16
|
+
|
17
|
+
'use strict';
|
18
|
+
|
1
19
|
const assert = require('chai').assert;
|
2
20
|
const sinon = require('sinon');
|
3
21
|
const { inputSelectMultiple } = require('../index'); // Replace 'your-file-name'
|
4
22
|
|
5
|
-
// describe('inputSelectMultiple
|
6
|
-
// let stdinStub,
|
23
|
+
// describe('inputSelectMultiple', () => {
|
24
|
+
// let stdinStub, stdoutStub, processExitStub;
|
7
25
|
// let originalRawMode;
|
8
26
|
|
9
|
-
// before(() => {
|
10
|
-
// // stdoutSpy = sinon.spy(process.stdout, 'write');
|
11
|
-
// })
|
12
|
-
|
13
27
|
// beforeEach(() => {
|
14
28
|
// stdinStub = sinon.stub(process.stdin);
|
15
|
-
// stdoutStub = sinon.
|
16
|
-
|
29
|
+
// stdoutStub = sinon.spy(process.stdout, 'write');
|
17
30
|
// processExitStub = sinon.stub(process, 'exit');
|
18
31
|
// originalRawMode = process.stdin.isRaw;
|
19
32
|
// });
|
@@ -23,49 +36,7 @@ const { inputSelectMultiple } = require('../index'); // Replace 'your-file-name'
|
|
23
36
|
// process.stdin.setRawMode(originalRawMode);
|
24
37
|
// });
|
25
38
|
|
26
|
-
//
|
27
|
-
// // const questions = [
|
28
|
-
// // { text: 'What is your name?', type: 'text' },
|
29
|
-
// // { text: 'What is your favorite color?', type: 'radio', options: ['Red', 'Blue'] },
|
30
|
-
// // { text: 'Which fruits do you like?', type: 'checkbox', options: ['Apple', 'Banana'] },
|
31
|
-
// // ];
|
32
|
-
|
33
|
-
// // const expectedResults = [
|
34
|
-
// // { text: 'What is your name?', answer: 'Test Name' },
|
35
|
-
// // { text: 'What is your favorite color?', answer: 'Blue' },
|
36
|
-
// // { text: 'Which fruits do you like?', answer: ['Apple'] },
|
37
|
-
// // ];
|
38
|
-
|
39
|
-
// // const callback = (results) => {
|
40
|
-
// // assert.deepStrictEqual(results, expectedResults);
|
41
|
-
// // assert.ok(stdoutSpy.calledWith(sinon.match('What is your name?: Test Name')));
|
42
|
-
// // assert.ok(stdoutSpy.calledWith(sinon.match('What is your favorite color?: Blue')));
|
43
|
-
// // done();
|
44
|
-
// // };
|
45
|
-
|
46
|
-
// // inputSelectMultiple(questions, callback);
|
47
|
-
|
48
|
-
// // let keyPressCount = 0;
|
49
|
-
|
50
|
-
// // stdinStub.on('data', (data) => {
|
51
|
-
// // if (keyPressCount === 0) {
|
52
|
-
// // stdinStub.emit('keypress', '\r', { name: 'return' });
|
53
|
-
// // } else if (keyPressCount === 1) {
|
54
|
-
// // stdinStub.emit('keypress', '\r', { name: 'return' });
|
55
|
-
// // } else if (keyPressCount === 2) {
|
56
|
-
// // stdinStub.emit('keypress', '\r', { name: 'return' });
|
57
|
-
// // }
|
58
|
-
// // keyPressCount++;
|
59
|
-
// // });
|
60
|
-
|
61
|
-
// // stdinStub.emit('data', 'Test Name\n');
|
62
|
-
// // stdinStub.emit('keypress', null, { name: 'down' });
|
63
|
-
// // stdinStub.emit('keypress', null, { name: 'return' });
|
64
|
-
// // stdinStub.emit('keypress', null, { name: 'space' });
|
65
|
-
// // stdinStub.emit('keypress', null, { name: 'return' });
|
66
|
-
// // });
|
67
|
-
|
68
|
-
// it('should handle a sequence of text, radio, and checkbox questions with history', (done) => {
|
39
|
+
// it('should handle a sequence of text, radio, and checkbox questions', (done) => {
|
69
40
|
// const questions = [
|
70
41
|
// { text: 'What is your name?', type: 'text' },
|
71
42
|
// { text: 'What is your favorite color?', type: 'radio', options: ['Red', 'Blue'] },
|
@@ -78,12 +49,12 @@ const { inputSelectMultiple } = require('../index'); // Replace 'your-file-name'
|
|
78
49
|
// { text: 'Which fruits do you like?', answer: ['Apple'] },
|
79
50
|
// ];
|
80
51
|
|
81
|
-
//
|
82
|
-
//
|
83
|
-
//
|
84
|
-
//
|
85
|
-
|
86
|
-
//
|
52
|
+
// inputSelectMultiple(questions)
|
53
|
+
// .then((results) => {
|
54
|
+
// assert.deepStrictEqual(results, expectedResults);
|
55
|
+
// done();
|
56
|
+
// })
|
57
|
+
// .catch(done);
|
87
58
|
|
88
59
|
// let keyPressCount = 0;
|
89
60
|
|
@@ -103,15 +74,9 @@ const { inputSelectMultiple } = require('../index'); // Replace 'your-file-name'
|
|
103
74
|
// stdinStub.emit('keypress', null, { name: 'return' });
|
104
75
|
// stdinStub.emit('keypress', null, { name: 'space' });
|
105
76
|
// stdinStub.emit('keypress', null, { name: 'return' });
|
106
|
-
|
107
|
-
// // Check if previous answers are displayed.
|
108
|
-
// // console.log();
|
109
|
-
// assert.ok(stdoutSpy.calledWith(sinon.match('What is your name? Test Name')));
|
110
|
-
// // assert.ok(stdoutSpy.calledWith(sinon.match('What is your name? Test Name')));
|
111
|
-
// // assert.ok(stdoutSpy.calledWith(sinon.match('What is your favorite color? Blue')));
|
112
77
|
// });
|
113
78
|
|
114
|
-
// it('should handle only text questions
|
79
|
+
// it('should handle only text questions', (done) => {
|
115
80
|
// const questions = [
|
116
81
|
// { text: 'What is your name?', type: 'text' },
|
117
82
|
// { text: 'What is your age?', type: 'text' },
|
@@ -122,12 +87,12 @@ const { inputSelectMultiple } = require('../index'); // Replace 'your-file-name'
|
|
122
87
|
// { text: 'What is your age?', answer: '30' },
|
123
88
|
// ];
|
124
89
|
|
125
|
-
//
|
126
|
-
//
|
127
|
-
//
|
128
|
-
//
|
129
|
-
|
130
|
-
//
|
90
|
+
// inputSelectMultiple(questions)
|
91
|
+
// .then((results) => {
|
92
|
+
// assert.deepStrictEqual(results, expectedResults);
|
93
|
+
// done();
|
94
|
+
// })
|
95
|
+
// .catch(done);
|
131
96
|
|
132
97
|
// let count = 0;
|
133
98
|
|
@@ -142,16 +107,70 @@ const { inputSelectMultiple } = require('../index'); // Replace 'your-file-name'
|
|
142
107
|
|
143
108
|
// stdinStub.emit('data', 'Test Name\n');
|
144
109
|
// stdinStub.emit('data', '30\n');
|
110
|
+
// });
|
145
111
|
|
146
|
-
//
|
112
|
+
// it('should handle only radio questions', (done) => {
|
113
|
+
// const questions = [
|
114
|
+
// { text: 'What color?', type: 'radio', options: ['Red', 'Blue'] },
|
115
|
+
// { text: 'What second color?', type: 'radio', options: ['Green', 'Yellow'] },
|
116
|
+
// ];
|
147
117
|
|
118
|
+
// const expectedResults = [
|
119
|
+
// { text: 'What color?', answer: 'Blue' },
|
120
|
+
// { text: 'What second color?', answer: 'Yellow' },
|
121
|
+
// ];
|
122
|
+
|
123
|
+
// inputSelectMultiple(questions)
|
124
|
+
// .then((results) => {
|
125
|
+
// assert.deepStrictEqual(results, expectedResults);
|
126
|
+
// done();
|
127
|
+
// })
|
128
|
+
// .catch(done);
|
129
|
+
|
130
|
+
// stdinStub.emit('keypress', null, { name: 'down' });
|
131
|
+
// stdinStub.emit('keypress', null, { name: 'return' });
|
132
|
+
// stdinStub.emit('keypress', null, { name: 'down' });
|
133
|
+
// stdinStub.emit('keypress', null, { name: 'return' });
|
148
134
|
// });
|
149
135
|
|
150
|
-
// it('should handle
|
136
|
+
// it('should handle only checkbox questions', (done) => {
|
137
|
+
// const questions = [
|
138
|
+
// { text: 'Fruits?', type: 'checkbox', options: ['Apple', 'Banana'] },
|
139
|
+
// { text: 'Veggies?', type: 'checkbox', options: ['Carrot', 'Broccoli'] },
|
140
|
+
// ];
|
141
|
+
|
142
|
+
// const expectedResults = [
|
143
|
+
// { text: 'Fruits?', answer: ['Apple'] },
|
144
|
+
// { text: 'Veggies?', answer: ['Broccoli'] },
|
145
|
+
// ];
|
146
|
+
|
147
|
+
// inputSelectMultiple(questions)
|
148
|
+
// .then((results) => {
|
149
|
+
// assert.deepStrictEqual(results, expectedResults);
|
150
|
+
// done();
|
151
|
+
// })
|
152
|
+
// .catch(done);
|
153
|
+
|
154
|
+
// stdinStub.emit('keypress', null, { name: 'space' });
|
155
|
+
// stdinStub.emit('keypress', null, { name: 'return' });
|
156
|
+
// stdinStub.emit('keypress', null, { name: 'down' });
|
157
|
+
// stdinStub.emit('keypress', null, { name: 'space' });
|
158
|
+
// stdinStub.emit('keypress', null, { name: 'return' });
|
159
|
+
// });
|
160
|
+
|
161
|
+
// it('should handle ctrl+c exit during questions', (done) => {
|
151
162
|
// const questions = [{ text: 'What is your name?', type: 'text' }];
|
152
163
|
|
153
|
-
// inputSelectMultiple(questions
|
164
|
+
// inputSelectMultiple(questions)
|
165
|
+
// .then(() => {
|
166
|
+
// done(new Error('Promise should not resolve'));
|
167
|
+
// })
|
168
|
+
// .catch(() => {
|
169
|
+
// assert.ok(processExitStub.called);
|
170
|
+
// done();
|
171
|
+
// });
|
172
|
+
|
154
173
|
// stdinStub.emit('keypress', null, { ctrl: true, name: 'c' });
|
155
|
-
// assert.ok(!processExitStub.called);
|
156
174
|
// });
|
157
175
|
// });
|
176
|
+
|
@@ -1,3 +1,21 @@
|
|
1
|
+
/**
|
2
|
+
*
|
3
|
+
* Package:
|
4
|
+
* Author: Ganesh B
|
5
|
+
* Description:
|
6
|
+
* Install: npm i input-shell --save
|
7
|
+
* Github: https://github.com/ganeshkbhat/
|
8
|
+
* npmjs Link: https://www.npmjs.com/package/
|
9
|
+
* File: index.js
|
10
|
+
* File Description:
|
11
|
+
*
|
12
|
+
*
|
13
|
+
*/
|
14
|
+
|
15
|
+
/* eslint no-console: 0 */
|
16
|
+
|
17
|
+
'use strict';
|
18
|
+
|
1
19
|
const assert = require('chai').assert;
|
2
20
|
const sinon = require('sinon');
|
3
21
|
const { inputSelect } = require('../index'); // Replace 'your-file-name'
|
@@ -0,0 +1,84 @@
|
|
1
|
+
|
2
|
+
const assert = require('assert');
|
3
|
+
const sinon = require('sinon');
|
4
|
+
const { execSync } = require('child_process'); // For testing the callback version
|
5
|
+
|
6
|
+
// // Promise-based version (assuming it's in a file called 'promise-main.js')
|
7
|
+
// const promiseMain = require('./promise-main'); // Adjust the path
|
8
|
+
|
9
|
+
// // Callback-based version (assuming it's in a file called 'callback-main.js')
|
10
|
+
// const callbackMain = require('./callback-main'); // Adjust the path
|
11
|
+
|
12
|
+
// describe('Promise-based main function', () => {
|
13
|
+
// it('should resolve with the correct results', async () => {
|
14
|
+
// const results = await promiseMain();
|
15
|
+
// assert.ok(Array.isArray(results));
|
16
|
+
// // Add more specific assertions based on your expected results
|
17
|
+
// assert.strictEqual(results[0].question, 'What is your name?');
|
18
|
+
// assert.strictEqual(results[1].question, 'Select your favorite colors:');
|
19
|
+
// assert.strictEqual(results[2].question, 'Choose your operating system:');
|
20
|
+
// assert.strictEqual(results[3].question, 'Enter your city?');
|
21
|
+
// });
|
22
|
+
|
23
|
+
// it('should handle errors correctly', async () => {
|
24
|
+
// // Simulate an error condition (e.g., by modifying the questions array)
|
25
|
+
// const originalQuestions = [
|
26
|
+
// { type: 'text', text: 'What is your name?' },
|
27
|
+
// { type: 'checkbox', text: 'Select your favorite colors:', options: ['Red', 'Green', 'Blue', 'Yellow'] },
|
28
|
+
// { type: 'radio', text: 'Choose your operating system:', options: ['Windows', 'macOS', 'Linux'] },
|
29
|
+
// { type: 'text', text: 'Enter your city?' },
|
30
|
+
// ];
|
31
|
+
// //simulates a question type that does not exist.
|
32
|
+
// const badQuestions = [{type: 'badType', text: 'bad question'}];
|
33
|
+
|
34
|
+
// promiseMain.questions = badQuestions;
|
35
|
+
|
36
|
+
// try {
|
37
|
+
// await promiseMain();
|
38
|
+
// assert.fail('Should have thrown an error');
|
39
|
+
// } catch (error) {
|
40
|
+
// assert.strictEqual(error.message, 'Unknown question type: badType');
|
41
|
+
// }
|
42
|
+
// promiseMain.questions = originalQuestions; //reset questions array.
|
43
|
+
// });
|
44
|
+
|
45
|
+
// // Add more tests as needed (e.g., for different input scenarios)
|
46
|
+
// });
|
47
|
+
|
48
|
+
// describe('Callback-based main function', () => {
|
49
|
+
// it('should call the callback with the correct results', (done) => {
|
50
|
+
// callbackMain((err, results) => {
|
51
|
+
// assert.ifError(err);
|
52
|
+
// assert.ok(Array.isArray(results));
|
53
|
+
// // Add more specific assertions based on your expected results
|
54
|
+
// assert.strictEqual(results[0].question, 'What is your name?');
|
55
|
+
// assert.strictEqual(results[1].question, 'Select your favorite colors:');
|
56
|
+
// assert.strictEqual(results[2].question, 'Choose your operating system:');
|
57
|
+
// assert.strictEqual(results[3].question, 'Enter your city?');
|
58
|
+
// done();
|
59
|
+
// });
|
60
|
+
// });
|
61
|
+
|
62
|
+
// it('should call the callback with an error on failure', (done) => {
|
63
|
+
// const originalQuestions = [
|
64
|
+
// { type: 'text', text: 'What is your name?' },
|
65
|
+
// { type: 'checkbox', text: 'Select your favorite colors:', options: ['Red', 'Green', 'Blue', 'Yellow'] },
|
66
|
+
// { type: 'radio', text: 'Choose your operating system:', options: ['Windows', 'macOS', 'Linux'] },
|
67
|
+
// { type: 'text', text: 'Enter your city?' },
|
68
|
+
// ];
|
69
|
+
// //simulates a question type that does not exist.
|
70
|
+
// const badQuestions = [{type: 'badType', text: 'bad question'}];
|
71
|
+
|
72
|
+
// callbackMain.questions = badQuestions;
|
73
|
+
|
74
|
+
// callbackMain((err, results) => {
|
75
|
+
// assert.ok(err);
|
76
|
+
// assert.strictEqual(err.message, 'Unknown question type: badType');
|
77
|
+
// assert.strictEqual(results, null);
|
78
|
+
// callbackMain.questions = originalQuestions; // reset questions array.
|
79
|
+
// done();
|
80
|
+
// });
|
81
|
+
// });
|
82
|
+
|
83
|
+
// // Add more tests as needed (e.g., for different input scenarios)
|
84
|
+
// });
|