/
getEmojis.js
45 lines (33 loc) 路 1.05 KB
/
getEmojis.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
// @flow
import chalk from 'chalk'
import fetch from 'node-fetch'
import ora from 'ora'
import cache from './emojisCache'
import buildFetchOptions from './buildFetchOptions'
import configurationVault from './configurationVault'
const getEmojis = async (
skipCache: boolean = false
): Promise<Array<Object>> => {
const emojisFromCache = cache.getEmojis()
if (cache.isAvailable() && !skipCache) return emojisFromCache
const spinner = ora('Fetching gitmojis').start()
try {
const response = await fetch(
configurationVault.getGitmojisUrl(),
buildFetchOptions()
)
const data = await response.json()
const emojis = data.gitmojis
cache.createEmojis(emojis)
if (emojis.length === emojisFromCache.length) {
spinner.info('Gitmojis already up to date')
return []
}
spinner.succeed('Gitmojis fetched successfully, these are the new emojis:')
return emojis.filter((emoji) => !emojisFromCache.includes(emoji))
} catch (error) {
spinner.fail(`Error: ${error}`)
return []
}
}
export default getEmojis