Skip to content

Commit 4354220

Browse files
committed
Update To use Node-Fetch and Upgrade Dependencies
1 parent d36e065 commit 4354220

File tree

6 files changed

+5069
-3293
lines changed

6 files changed

+5069
-3293
lines changed

cli.js

100644100755
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#!/usr/bin/env node
22

33
'use strict'
4-
var meow = require('meow')
5-
var hacktoberFest = require('./main.js')
4+
import meow from 'meow'
5+
import { getHacktoberfestStats } from './main.js'
66

7-
const cli = meow(`
7+
const cli = meow(
8+
`
89
Usage:
910
$ npx hacktoberfeststats <username from GitHub>
1011
@@ -15,26 +16,28 @@ const cli = meow(`
1516
Examples
1617
$ npx hacktoberfeststats MatejMecka --year 2019
1718
18-
`,{
19-
flags: {
20-
year: {
21-
type: 'number',
22-
alias: 'y',
23-
default: 0,
19+
`,
20+
{
21+
flags: {
22+
year: {
23+
type: 'number',
24+
shortFlag: 'y',
25+
default: 0
26+
},
27+
help: {
28+
type: 'boolean',
29+
shortFlag: 'h'
30+
}
2431
},
25-
help: {
26-
type: 'boolean',
27-
alias: 'h',
28-
},
29-
},
30-
})
32+
importMeta: import.meta
33+
}
34+
)
3135

3236
if (cli.flags.help) {
3337
cli.showHelp(0)
3438
}
3539

36-
hacktoberFest
37-
.getHacktoberfestStats(cli.input[0], cli.flags.year)
40+
getHacktoberfestStats(cli.input[0], cli.flags.year)
3841
.then((stats) => {
3942
console.log(stats)
4043
})

example.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
var Hacktoberfest = require('./main.js')
1+
import {getHacktoberfestStats} from './main.js';
22

33
// Get a specific year
4-
Hacktoberfest('MatejMecka', '2017', function(hacktoberfestStats, callback, error) {
5-
console.log(hacktoberfestStats.mainStats)
4+
getHacktoberfestStats('MatejMecka', '2017', function (hacktoberfestStats, callback, error) {
5+
console.log(hacktoberfestStats)
66
})
77

88
// Get the actual year
9-
Hacktoberfest('MatejMecka', null, function(hacktoberfestStats, callback, error) {
10-
console.log(hacktoberfestStats.mainStats)
9+
getHacktoberfestStats('MatejMecka', null, function (hacktoberfestStats, callback, error) {
10+
console.log(hacktoberfestStats)
1111
})

main.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict'
2-
var request = require('request')
2+
import fetch from 'node-fetch'
33

44
const gitHubAPIURLs = {
55
getUser: 'https://api.github.com/users/%username%',
66
getPullRequests:
77
'https://api.github.com/search/issues?per_page=1000&q=-label:invalid+created:%year%-09-30T00:00:00-12:00..%year%-10-31T23:59:59-12:00+type:pr+is:public+author:%username%'
88
}
99

10-
const getMinPullRequests = year => {
10+
const getMinPullRequests = (year) => {
1111
switch (year) {
1212
case 2018:
1313
return 5
@@ -16,7 +16,7 @@ const getMinPullRequests = year => {
1616
}
1717
}
1818

19-
const _checkForValidYear = year => {
19+
const _checkForValidYear = (year) => {
2020
let currentYear = new Date().getFullYear()
2121
if (year > currentYear) {
2222
throw new Error('Invalid year provided. The year must be less than or equal to the current year')
@@ -26,39 +26,38 @@ const _checkForValidYear = year => {
2626
}
2727
}
2828

29-
const _query = url =>
30-
new Promise((resolve, reject) => {
31-
request.get(
32-
{
33-
url: url,
34-
headers: { 'User-Agent': 'request' }
35-
},
36-
(err, res, data) => {
37-
if (!err && res.statusCode == 200) resolve(data)
38-
reject(err)
39-
}
40-
)
29+
const _query = async (url) => {
30+
const response = await fetch(url, {
31+
headers: { 'User-Agent': 'request' }
4132
})
33+
34+
if (!response.ok) {
35+
throw new Error(`HTTP error! status: ${response.status}`)
36+
}
37+
38+
return response.text()
39+
}
4240

4341
const _processResult = (url, callback, transform) => {
4442
if (callback) {
4543
_query(url)
4644
.then(jsonPipe)
47-
.then(data => (transform ? transform(data) : data))
48-
.then(result => callback(result))
49-
.catch(err => {
45+
.then((data) => (transform ? transform(data) : data))
46+
.then((result) => callback(result))
47+
.catch((err) => {
5048
throw new Error(
5149
'There was a problem retrieving information for this account. Error Message: ' + (err ? err.message : '')
5250
)
5351
})
5452
} else {
5553
return _query(url)
5654
.then(jsonPipe)
57-
.then(data => (transform ? transform(data) : data))
55+
.then((data) => (transform ? transform(data) : data))
5856
}
5957
}
6058

61-
const jsonPipe = body => JSON.parse(body)
59+
const jsonPipe = (body) => JSON.parse(body)
60+
6261

6362
/**
6463
* Returns user information
@@ -71,12 +70,12 @@ const getUserInfo = (username, callback) => {
7170
return _processResult(url, callback)
7271
}
7372

74-
const _transformHacktoberfestResult = minPullRequest => statsInfo => ({
73+
const _transformHacktoberfestResult = (minPullRequest) => (statsInfo) => ({
7574
completed: !!(statsInfo.total_count >= minPullRequest),
7675
current: statsInfo.total_count,
7776
required: minPullRequest,
7877
progress: statsInfo.total_count + '/' + minPullRequest,
79-
contributions: statsInfo.items.map(repo => repo.repository_url)
78+
contributions: statsInfo.items.map((repo) => repo.repository_url)
8079
})
8180

8281
/**
@@ -106,5 +105,4 @@ const getHacktoberfestStats = (username, year, callback) => {
106105
return _processResult(url, callback, _transformHacktoberfestResult(minPullRequest))
107106
}
108107

109-
exports.getHacktoberfestStats = getHacktoberfestStats
110-
exports.getUserInfo = getUserInfo
108+
export { getUserInfo, getHacktoberfestStats }

0 commit comments

Comments
 (0)