Skip to content

Instantly share code, notes, and snippets.

@ReemRashwan
Created August 26, 2020 20:22
Show Gist options
  • Save ReemRashwan/fb8b0e455606360c1220dbddfe14f484 to your computer and use it in GitHub Desktop.
Save ReemRashwan/fb8b0e455606360c1220dbddfe14f484 to your computer and use it in GitHub Desktop.
Practicing promises and yargs.
// 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