-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·50 lines (43 loc) · 1.28 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
#!/usr/bin/env node
const sym = require('log-symbols');
const cli = require('./utils/cli.js');
const init = require('./utils/init.js');
const print = require('./utils/print.js');
const cities = require('./utils/cities.js');
const theEnd = require('./utils/theEnd.js');
const fs = require('fs');
// CLI.
const [input] = cli.input;
const all = cli.flags.all;
const next = cli.flags.next;
const dft = cli.flags.default;
const notFound = `${sym.error} ${input}: not found!`;
// Let's do it.
(async () => {
init();
input === 'help' && (await cli.showHelp(0));
var city = input ? input.toLowerCase().replace(/\s+/g, '-') : null; // : `beijing`;
// if null read default city name
if (!city) {
city = await require('./data/defaultCity.json').name;
}
// set default city
if (dft) {
var defaultCity = {name: city};
// convert JSON object to a string
const data = JSON.stringify(defaultCity);
// write file to disk
fs.writeFile('./data/defaultCity.json', data, 'utf8', err => {
if (err) {
console.log(`Error writing file: ${err}`);
} else {
console.log(`Default city set successfully!`);
}
});
}
const noData = cities.indexOf(city) === -1;
// console.log(noData, city);
noData && console.log(notFound);
!noData && print({all, next, city});
theEnd({city, noData});
})();