-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
executable file
·270 lines (228 loc) · 7.04 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env node
// The above comment declares the environment that should be used to run the file.
const fetch = require('node-fetch');
//In react the fetch function is available globally but to use it
// without react we need to install node-fetch.
const pjson = require('./package.json');
//To get version and package name.
const Configstore = require('configstore');
// This package is used to create a user configuration to save the user settings.
// Presaved anime titles for random user searches.
const list = require('./RA.json');
//Preconfigured or default anime-cli user configuration.
const config = new Configstore(pjson.name, {
setLimit: false,
limit: 100,
onlyMatches: false,
showScore: false,
showYear: false
});
let limvalue = 100;
//getting the values from the arguments passed by the user.
let query = process.argv;
let search = ' ';
// Handling random search
if (query.length <= 2) {
search = list[parseInt(Math.random() * list.length)];
} else {
search = query.slice(2, query.length).join('_');
}
//API returns error if search string is less than 3 characters long
let arg = query;
// Table module for displaying the results in a table.
const Table = require('cli-table3');
const colors = require('./colors');
// Configuring the columns of the table.
const tableHead = ['Title', 'Episodes', 'Type', 'Status'];
// showScore column.
if (config.get('showScore') == true) {
tableHead.splice(1, 0, 'Score');
}
// showYear column.
if (config.get('showYear') == true) {
tableHead.splice(1, 0, 'Year');
}
// Structuring the table.
const table = new Table({
head: tableHead,
colWidths: [60, 15, 15, 15]
});
// Applying some colours and predefined vim colours.
const chalk = require('chalk');
// if-else condition for the arguments meant for app config.
if (arg[2] === '-h' || arg[2] === '--help') {
console.log(`
NAME
${colors.cyan}anime-cli: command line application to fetch anime details${colors.reset}
USAGE
${colors.cyan}anime-cli [ <anime name> | arguments ]
anime-cli [ <anime name> | -help | --h ...]${colors.reset}
OPTIONS
<anime name>
The name of the anime whose information you would
like to fetch
${colors.green}eg: anime-cli Naruto${colors.reset}
--help, -h
view this information
${colors.green}eg: anime-cli --help${colors.reset}
${colors.green}eg: anime-cli -h${colors.reset}
--version, -v
view the version number of the anime-cli app
${colors.green}eg: anime-cli --version${colors.reset}
${colors.green}eg: anime-cli -v${colors.reset}
DESCRIPION
anime-cli is a command line application that can be used to
fetch information regarding the user-queried anime, like
number of episodes, type of anime and it's airing status.
`);
process.exit();
}
if (arg[2] === '--version' || arg[2] === '-v') {
console.log(
`${colors.green}anime-cli\nversion: ${pjson.version}\n${colors.reset}`
);
process.exit();
}
if (search.length < 3) {
console.log(
'Name has to be at least 3 characters long or you might have entered a two digit number!'
);
process.exit();
}
if (arg.includes('setLimit') && arg[arg.indexOf('setLimit') + 1] === 'true') {
config.set('setLimit', true);
if (arg[4] === undefined) {
console.log(
'The argument should be used as: setLimit <true/false> limit <number>'
);
} else {
config.set('limit', parseInt(arg[4]));
}
process.exit();
} else if (arg.includes('setLimit') && arg.includes('false')) {
config.set('setLimit', false);
process.exit();
}
if (arg.includes('onlyMatches')) {
if (arg.includes('onlyMatches') && arg.includes('true')) {
config.set('onlyMatches', true);
process.exit();
} else if (arg.includes('onlyMatches') && arg.includes('false')) {
config.set('onlyMatches', false);
process.exit();
} else {
console.log('Allowed value for onlyMatches: [true, false]');
}
process.exit();
}
if (arg.includes('showScore')) {
if (arg[arg.indexOf('showScore') + 1] === 'true') {
config.set('showScore', true);
} else if (arg[arg.indexOf('showScore') + 1] === 'false') {
config.set('showScore', false);
} else {
console.log('Allowed value for showScore: [true, false]');
}
process.exit();
}
if (arg.includes('showYear')) {
if (arg[arg.indexOf('showYear') + 1] === 'true') {
config.set('showYear', true);
} else if (arg[arg.indexOf('showYear') + 1] === 'false') {
config.set('showYear', false);
} else {
console.log('Allowed value for showYear: [true, false]');
}
process.exit();
}
fetch(`https://api.jikan.moe/v4/anime?q=${search}`)
.then((response) => response.json())
.then((data) => {
limvalue = config.get('setLimit') ? config.get('limit') : 100;
if (
arg[3] !== undefined &&
typeof arg[3] === 'string' &&
!isNaN(parseInt(arg[3]))
) {
if (parseInt(arg[3]) <= 100) {
limvalue = parseInt(arg[3]);
}
}
const bunch = data.data.slice(0, limvalue);
let status = '';
let PTitle = '';
//arg[2] = arg;
if (arg[2] !== undefined) {
arg[2] = arg[2].toLowerCase();
}
bunch.map((item) => {
// We need to check whether the user filter is set to true or false.
if (config.get('onlyMatches') == true) {
if (item.airing === true) {
status = `${colors.red}Ongoing${colors.reset}`;
} else if (item.airing === false) {
status = `${colors.blue}Finished${colors.reset}`;
}
// Converting the fetched title to lowercase for better matching.
if (item.title.toLowerCase().includes(arg[2])) {
PTitle = chalk.bold.green;
//new row for new features
const row = [
PTitle(item.title),
item.episodes,
chalk.yellow(item.type),
status
];
// To check whether the user config has this option set to true.
if (config.get('showScore') == true) {
row.splice(1, 0, item.score);
}
// To check whether the user config has this option set to true.
if (config.get('showYear') == true) {
row.splice(
1,
0,
item.start_date ? item.start_date.split('-')[0] : ''
);
}
table.push(row);
}
} else if (config.get('onlyMatches') == false) {
if (item.title.toLowerCase().includes(arg[2])) {
PTitle = chalk.bold.green;
} else {
PTitle = chalk.bold.white;
}
if (item.airing === true) {
status = `${colors.red}Ongoing${colors.reset}`;
} else if (item.airing === false) {
status = `${colors.blue}Finished${colors.reset}`;
}
const row = [
PTitle(item.title),
item.episodes,
`${colors.yellow}${item.type}${colors.reset}`,
status
];
// To check whether the user config has this option set to true.
if (config.get('showScore')) {
row.splice(1, 0, item.score);
}
// To check whether the user config has this option set to true.
if (config.get('showYear')) {
row.splice(
1,
0,
item.start_date ? item.start_date.split('-')[0] : ''
);
}
table.push(row);
}
});
// Printing table to terminal.
console.log(table.toString());
const link = chalk.blueBright('https://git.io/JDcSO');
console.log(`Having some issues with the app? Report here: ${link}`);
})
.catch((error) => console.log(error));
// add pre-commit hook to the project.