-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (63 loc) · 2.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const Interface = require('./Interface');
const FileHandler = require('./FileHandler');
const inquirer = require('inquirer');
const chalk = require('chalk');
// 1. Open file from args, store in a variable
// 2. Load homonym dictionary and ask user to pick homonym to checks, store in a variable
// 3. Scan the file: output x words at a time, show
// inquirer prompt, replace or don't replace based
// on user response
const app = {
runOnce: false,
start() {
if (!this.runOnce) {
this.runOnce = true;
this.runApp('input', process.argv[2]);
} else {
this.runApp('output', process.argv[2]);
}
},
runApp(folder, fileName) {
const IO = new Interface();
const UserFile = new FileHandler();
console.log(chalk `{green.bold ${'\n=================================='}}`);
console.log(chalk `{green.bold ${'=== Welcome to Homonym Helper! ==='}}`);
console.log(chalk `{green.bold ${'=================================='}}`);
UserFile.getFile(folder, fileName, 'currentFileText')
.then(data => console.log(chalk `{green ${'\nyour file output is below!\n\n\n\n'}}${data}`))
.then(() => UserFile.getFile('ref', 'homonyms.txt', 'currentHomonyms'))
.then(homonyms => IO.homonymList(homonyms))
.then((chosenHomonyms) => {
if (chosenHomonyms === null) {
// Just want to exit Promise chain, not throw a real error.
return Promise.reject('');
}
console.log('\n> Matches found. See below...\n');
const cleanTextArr = UserFile.cleanText(UserFile.currentData.currentFileText);
return IO.homonymReplacer(cleanTextArr);
})
// After showing new output to user (in homonymReplacer above), write to new file in output dir
.then((data) => {
UserFile.writeFile(process.argv[2], data);
return data;
})
.then(() => inquirer.prompt({
type: 'list',
name: 'status',
message: 'Do you want to run Homonym Helper on the same file again?',
choices: [
'Yes',
'No',
],
})
.then((answer) => {
if (answer.status === 'Yes') {
this.start();
} else {
console.log(chalk `{green ${'\n> You have exited Homonym Helper. Goodbye!'}}`);
}
}))
.catch(error => console.error(error));
},
};
app.start();