Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env node
var program = require('commander');
var request = require('request');
var chalk = require('chalk');
program
.version('0.0.1')
.usage('[options] <keywords>')
.option('-o, --owner [name]', 'Filter by the repositories owner')
.option('-l, --language [language]', 'Filter by the repositories language')
.option('-f, --full', 'Full output without any styling')
.parse(process.argv);
if(!program.args.length) {
program.help();
} else {
var keywords = program.args;
var url = 'https://api.github.com/search/repositories?sort=stars&order=desc&q='+keywords;
if(program.owner) {
url = url + '+user:' + program.owner;
}
if(program.language) {
url = url + '+language:' + program.language;
}
request({
method: 'GET',
headers: {
'User-Agent': 'YourGitHubUsername'
},
url: url
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
var body = JSON.parse(body);
if(program.full) {
console.log(body);
} else {
for(var i = 0; i < body.items.length; i++) {
console.log(chalk.cyan.bold('Name: ' + body.items[i].name));
console.log(chalk.magenta.bold('Owner: ' + body.items[i].owner.login));
console.log(chalk.grey('Desc: ' + body.items[i].description + '\n'));
console.log(chalk.grey('Clone url: ' + body.items[i].clone_url + '\n'));
}
process.exit(0);
}
process.exit(0);
} else if (error) {
console.log(chalk.red('Error: ' + error));
process.exit(1);
}
});
}
{
"name": "gitsearch",
"version": "0.0.1",
"description": "A simple command-line tool for searching git repositories",
"author": "Glynn Phillips",
"engines": {
"node": ">=0.10"
},
"dependencies": {
"commander": "~2.2.0",
"request": "~2.36.0",
"chalk": "~0.4.0"
},
"bin": {
"gitsearch": "gitsearch.js"
}
}
@dimkir

This comment has been minimized.

dimkir commented May 17, 2018

It seems you won't produce any output in case statusCode is different from 200 (eg. 400)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment