-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.js
58 lines (48 loc) · 1.95 KB
/
lib.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
const exec = require('child_process').exec;
const getMajor = require('semver').major;
const fetchWords = require('datamuse').words;
const giphy = require('giphy-api')('dc6zaTOxFJmzC');
const { get, random, sample, startCase } = require('lodash');
function fetchWordsStartingWithLetter(letter) {
return fetchWords({ sp: letter + '*', max: 1000, md: 'p' });
}
function fetchRandomWordsOfType(type) {
return fetchWordsStartingWithLetter(sample('abcdefghijklmnopqrstuvwxyz'))
.then(data => data.filter(({ tags = [] }) => tags.includes(type)))
.then(data => data.map(item => item.word));
}
function fetchRandomWordOfType(type, fallback) {
return fetchRandomWordsOfType(type).then(words => sample(words) || fallback);
}
function fetchNpmModuleMajor(moduleId, timeout = 1000) {
return new Promise((resolve, reject) => exec(
'npm info ' + moduleId + ' version',
{ timeout },
(error, version) => error ? reject(error) : resolve(version)
)).then(getMajor);
}
function fetchTopicRelatedGifUrl(topic, fallback = 'No gif found...') {
return giphy.search({ q: topic, limit: 1 })
.then(results => get(results, 'data[0].url', fallback))
.catch(() => fallback);
}
function fetchReleaseName() {
return Promise.all([
fetchRandomWordOfType('adj', 'angular'),
fetchRandomWordOfType('n', 'next')
]).then(values => values.map(startCase).join(' '));
}
function fetchNextMajor(fallback = random(1, 999)) {
return fetchNpmModuleMajor('@angular/core')
.then(major => major ? major + 1 : fallback)
.catch(() => fallback);
}
function fetchRelatedData(name, version, gif = fetchTopicRelatedGifUrl(name)) {
return Promise.all([name, version, gif]);
}
module.exports = function releaseNameGenerator(major = fetchNextMajor(), gif) {
return fetchReleaseName()
.then(name => fetchRelatedData(name, major, gif))
.then(([name, version, gif]) => ({ name, version, gif }))
.catch(() => { throw new Error('Something went wrong, try again...'); });
};