Created
June 3, 2014 08:39
-
-
Save GlynnPhillips/7f3dcb2b990796f1856f to your computer and use it in GitHub Desktop.
Accompanying sources files for http://cruft.io/posts/node-command-line-utilities/
This file contains 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
#!/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); | |
} | |
}); | |
} |
This file contains 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
{ | |
"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" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems you won't produce any output in case
statusCode
is different from 200 (eg. 400)