Skip to content

Commit 075c8b5

Browse files
committed
refactor: change readline-sync to inquirer library
1 parent f13910b commit 075c8b5

File tree

5 files changed

+37
-83
lines changed

5 files changed

+37
-83
lines changed

package-lock.json

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
"chalk": "^5.4.1",
1212
"cli-table3": "^0.6.5",
1313
"csv-parser": "^3.1.0",
14-
"inquirer": "^12.3.2",
15-
"readline-sync": "^1.4.10"
14+
"inquirer": "^12.3.2"
1615
},
1716
"devDependencies": {
1817
"@eslint/js": "^9.18.0",

src/cli/main.js

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import path from 'path'
33
import { fileURLToPath } from 'url'
44
import { exit } from 'process'
55
import chalk from 'chalk'
6-
import inquirer from 'inquirer'
6+
import { confirm, select } from '@inquirer/prompts'
77
import csv from 'csv-parser'
88
import Table from 'cli-table3'
9-
import readlineSync from 'readline-sync'
109

1110
import singleFeatureProcess from './single-feature.js'
1211
import multiFeatureProcess from './multi-feature.js'
@@ -49,24 +48,21 @@ const DATA_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '
4948
SIMPLE LINEAR REGRESSION DEMO
5049
`)
5150

52-
const answers = await inquirer.prompt([
53-
{
54-
type: 'list',
55-
name: 'menu',
56-
message: 'Choose menu',
57-
choices: [
58-
'1. Guide',
59-
'2. Run Demo',
60-
'3. Exit'
61-
]
62-
}
63-
])
51+
const answers = await select({
52+
message: 'Choose menu',
53+
choices: [
54+
'1. Guide',
55+
'2. Run Demo',
56+
'3. Exit'
57+
]
58+
})
6459

6560
console.clear()
66-
67-
switch (answers.menu) {
61+
62+
63+
switch (answers) {
6864
case '1. Guide':
69-
showGuide()
65+
await showGuide()
7066
break;
7167
case '2. Run Demo':
7268
await runDemo()
@@ -79,22 +75,22 @@ SIMPLE LINEAR REGRESSION DEMO
7975
print('Invalid input!')
8076
break;
8177
}
82-
} while (readlineSync.keyInYNStrict(chalk.green('\nContinue program? ')))
78+
} while (await confirm({ message: '\nContinue program? ' }))
8379
})();
8480

8581

8682

8783

8884

8985

90-
function showGuide() {
86+
async function showGuide() {
9187
print('This is a guide for this program!');
9288
for (let i = 1; i <= 5; i++) {
9389
print('Data: ', i)
9490
}
9591

9692

97-
if (!readlineSync.keyInYNStrict(chalk.green('\nBack'))) {
93+
if (!await confirm({ message: 'Back?'})) {
9894
exit(0)
9995
}
10096
}
@@ -114,24 +110,18 @@ async function runDemo() {
114110
}
115111

116112

117-
const answers = await inquirer.prompt([
118-
{
119-
type: 'list',
120-
name: 'data',
121-
message: 'Choose data',
122-
choices: files
123-
}
124-
])
113+
const answers = await select({
114+
message: 'Choose data',
115+
choices: files
116+
})
125117

126-
if (readlineSync.keyInYNStrict(chalk.green('\nPreview data'))) {
127-
await previewData(answers.data)
118+
if (await confirm({ message: 'Preview data?'})) {
119+
await previewData(answers)
128120
}
129121
}
130122

131123

132124

133-
134-
135125
function readFileAsync(filePath) {
136126
return new Promise((resolve, reject) => {
137127
const data = []
@@ -145,8 +135,6 @@ function readFileAsync(filePath) {
145135

146136

147137

148-
149-
150138
async function previewData(filePath) {
151139
print('\n-----------------------------------------------------\n')
152140
print('Preview data: ', filePath)
@@ -163,32 +151,27 @@ async function previewData(filePath) {
163151

164152
print(table.toString())
165153

166-
if (readlineSync.keyInYNStrict(chalk.green('\nContinue next process'))) {
154+
if (await confirm({ message: '\nContinue next process' })) {
167155
await dataIdentification(data)
168156
}
169157
}
170158

171159

172160

173-
174161
async function dataIdentification(data) {
175162
print('\n-----------------------------------------------------\n')
176163
print('Data identification')
177164

178165

179-
const answers = await inquirer.prompt([
180-
{
181-
type: 'list',
182-
name: 'type',
183-
message: 'Choose model type',
184-
choices: [
185-
'1. Single feature',
186-
'2. Multi feature'
187-
]
188-
}
189-
])
190-
191-
switch (answers.type) {
166+
const answers = await select({
167+
message: 'Choose model type',
168+
choices: [
169+
'1. Single feature',
170+
'2. Multi feature'
171+
]
172+
})
173+
174+
switch (answers) {
192175
case '1. Single feature':
193176
await singleFeatureProcess(data)
194177
break;

src/cli/multi-feature.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import chalk from 'chalk'
2-
import readlineSync from 'readline-sync'
1+
import { confirm } from '@inquirer/prompts'
32

43

54
const print = console.log;
65

76

8-
export default function multiFeatureProcess() {
7+
export default async function multiFeatureProcess() {
98
print('Multi feature process')
109

11-
if (readlineSync.keyInYNStrict(chalk.green('\nShow visualization'))) {
10+
if (await confirm({ message: 'Show visualization?'})) {
1211
visualization()
1312
}
1413
}

src/cli/single-feature.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import chalk from 'chalk'
2-
import readlineSync from 'readline-sync'
32
import inquirer from 'inquirer'
43
import { input, confirm } from '@inquirer/prompts'
54

@@ -71,10 +70,6 @@ export default async function singleFeatureProcess(data) {
7170

7271

7372
evaluation()
74-
75-
if (readlineSync.keyInYNStrict(chalk.green('\nShow visualization'))) {
76-
visualization()
77-
}
7873
}
7974

8075

@@ -88,15 +83,3 @@ function evaluation() {
8883
print('Evaluation!')
8984
print('MSE, MAE, RMSE')
9085
}
91-
92-
93-
94-
function visualization() {
95-
print('\n-----------------------------------------------------\n')
96-
print('Visualization here!')
97-
print('Graph, chart, or whatever...')
98-
99-
100-
// 7
101-
print('Show the prove of linear equation is actually match with user data')
102-
}

0 commit comments

Comments
 (0)