-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.js
82 lines (70 loc) · 2.17 KB
/
cli.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
#!/usr/bin/env node
const start = require('./index');
const argv = require('minimist')(process.argv.slice(2));
const https = require('https');
const { version } = require('./package');
const checkVersion = () => {
https.get("https://raw.githubusercontent.com/higgins/starting-condition/master/package.json", res => {
let data = ""
res.on("data", d => {
data += d
})
res.on("end", () => {
try {
const { version: originVersion } = JSON.parse(data);
const currentSourceMinor = version.split('.')[1];
const currentOriginMinor = originVersion.split('.')[1];
if (currentSourceMinor !== currentOriginMinor) {
console.log(
`
\x1b[33mPsst\x1b[0m:
There is a newer version published.
Update with "\x1b[33mnpm update -g starting-condition\x1b[0m"
`);
}
} catch(e) {
// It's not the end of the world
}
})
})
}
const LANGUAGES = {
en: ['English', 'Starting condition'],
es: ['Español', 'Condición inicial'],
fr: ['Français', 'Condition de départ'],
de: ['Deutsch', 'Ausgangsbedingung'],
};
if (argv.h || argv.help) {
console.log(`
options:
-h, --help Prints this help information
-l, --language Select language [${Object.keys(LANGUAGES)}]
-f, --fresh Only show recent conditions
-n, Do not check for latest version
-a View available languages
`)
process.exit(0);
}
if (!argv.n) {
// NOTE: check if there is a later version of the cli we can upgrade
checkVersion();
}
if (argv.a) {
const langFMT = Object.entries(LANGUAGES).map(l => `\x1b[33m${l[0]}\x1b[0m -- ${l[1][0]}`).join('\n');
console.log(`
${langFMT}
`)
process.exit(0);
}
let language = 'en';
if (argv.l || argv.language) {
const selectedLang = argv.l || argv.language;
language = Object.keys(LANGUAGES).includes(selectedLang) ? selectedLang : language;
}
const onlyLatest = !!(argv.f || argv.fresh);
const condition = start(language, onlyLatest);
const starting = `
\x1b[33m${LANGUAGES[language][1]}:\x1b[0m
${condition}
`
console.log(starting);