-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
executable file
·147 lines (137 loc) · 4.43 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env node
import { promises as fs } from 'fs';
import path from 'path';
import chalk from 'chalk';
import show from './lib/banner.js';
import projector from './lib/engine.js';
const fileRead = async (fileName) => {
const fileData = await fs.readFile(fileName, 'utf8');
return fileData;
};
// Function to return the words of the data provided.
const fileWords = async (data) => {
const words = data.split(' ').length;
console.log(
`${chalk.yellowBright.bold('Number of words: ')}` + `${chalk.whiteBright(words)}`
);
};
// Function to return the sentences of the data provided.
const fileSentences = async (data) => {
const sentences = data.match(/[\w|\)][.?!](\s|$)/g).length;
console.log(
`${chalk.yellowBright.bold('Number of sentences: ')}` +
`${chalk.whiteBright(sentences)}`
);
};
(async () => {
let [command, filename, delay] = process.argv.slice(2);
const usage = `${chalk.yellowBright.bold('Commands: ')}
${chalk.yellowBright('start')} ${chalk.yellowBright(
'<file_path> [<delay>]'
)} : "Show text from file in ${chalk.whiteBright(
'<file_path>'
)} with delay between words in ${chalk.whiteBright('<delay>')}" ${chalk.whiteBright(
'(default 250WPM)'
)}
${chalk.yellowBright('stats')} ${chalk.yellowBright(
'<file_path>'
)} : "Show number of words, sentences, and absolute path of file in ${chalk.whiteBright(
'<file_path>'
)}"
${chalk.yellowBright('read')} ${chalk.yellowBright(
'<text> [<delay>]'
)} : "Show text from text wrapped with '' or "" in ${chalk.whiteBright(
'<text>'
)}, with delay between words in ${chalk.whiteBright('<delay>')}" ${chalk.whiteBright(
'(default 250WPM)'
)}
${chalk.yellowBright.bold('Argument Details: ')}
${chalk.yellowBright(
'file_path'
)} : full or absolute path of file (only .txt supported) [mandatory*]
${chalk.yellowBright('delay')} : delay between two words in WPM
${chalk.yellowBright.bold('Options: ')}
${chalk.yellowBright('-h, --help')} : show help
${chalk.yellowBright('-v, --version')} : show version
`;
switch (command) {
default:
show('METAXA', usage);
break;
case '--help':
case '-h':
console.log(usage);
break;
case '--version':
case '-v':
let rawdata = await fileRead(new URL('./package.json', import.meta.url));
let pjson = JSON.parse(rawdata);
console.log(pjson.version);
break;
case 'start':
if (filename && path.extname(filename) == '.txt') {
try {
const data = await fileRead(filename);
projector(
data,
delay != undefined && Number.isInteger(Number(delay))
? 60000 / delay
: 60000 / 250
);
} catch (err) {
console.log(chalk.whiteBright.bgRed.bold('\nSomething went wrong...'));
console.log(
`\n${chalk.yellowBright.bold('Error')}: ${chalk.red(err.message)}`
);
}
} else
console.log(
`\n${chalk.yellowBright.bold('Error')}: ${chalk.red(
'You need to specify proper path'
)}`
);
break;
case 'read':
try {
const data = filename;
projector(
data,
delay != undefined && Number.isInteger(Number(delay))
? 60000 / delay
: 60000 / 250
);
} catch (err) {
console.log(chalk.whiteBright.bgRed.bold('\nSomething went wrong...'));
console.log(
`\n${chalk.yellowBright.bold('Error')}: ${chalk.red(err.message)}`
);
}
break;
case 'stats':
if (filename && path.extname(filename) == '.txt') {
try {
const data = await fileRead(filename);
// Call functions to read number of words and sentences.
fileWords(data);
fileSentences(data);
// Get and display the absolute path of file.
console.log(
`${chalk.yellowBright.bold(
'The absolute path is: ')} ${chalk.whiteBright(path.resolve(filename)
)}`
);
} catch (err) {
console.log(chalk.whiteBright.bgRed.bold('\nSomething went wrong...'));
console.log(
`\n${chalk.yellowBright.bold('Error')}: ${chalk.red(err.message)}`
);
}
} else
console.log(
`\n${chalk.yellowBright.bold('Error')}: ${chalk.red(
'You need to specify proper path'
)}`
);
break;
}
})();