Created
August 26, 2020 20:22
-
-
Save ReemRashwan/fb8b0e455606360c1220dbddfe14f484 to your computer and use it in GitHub Desktop.
Practicing promises and yargs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usage: node index.js getUserRepos --username userName | |
const fetch = require("node-fetch"); | |
const yargs = require("yargs"); | |
const path = require("path"); | |
yargs.command({ | |
command: "getUserRepos", | |
builder: { | |
username: { | |
describe: "username", | |
demandOption: true, | |
type: "string", | |
}, | |
}, | |
handler(argv) { | |
console.log(argv.username); | |
displayUserRepos(argv.username); | |
}, | |
}); | |
yargs.parse(); | |
function displayUserRepos(username) { | |
fetch(`https://api.github.com/users/${username}/repos`) | |
.then((res) => { | |
if (res.status == 404) { | |
throw Error("Can't get data"); | |
} | |
res.json(); | |
}) | |
.then((json) => { | |
json.forEach((element) => { | |
let repoName = path.basename(element.html_url); | |
console.log(repoName); | |
}); | |
}) | |
.catch((error) => console.log(error)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment