Permalink
Browse files

initial commit

  • Loading branch information...
chapel committed Feb 14, 2011
0 parents commit f38e34d566f438543c268511a40ca345d5c178a1
Showing with 411 additions and 0 deletions.
  1. +5 −0 .npmignore
  2. +22 −0 LICENSE
  3. +3 −0 README
  4. +185 −0 lib/ngist.js
  5. +25 −0 package.json
  6. +171 −0 src/ngist.coffee
@@ -0,0 +1,5 @@
+*.coffee
+*.html
+.DS_Store
+.git*
+src/
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2011 Jacob Chapel
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
3 README
@@ -0,0 +1,3 @@
+
+
+ngist - Gist CLI tool using node.js
@@ -0,0 +1,185 @@
+(function() {
+ var EventEmitter, anError, emitter, exec, fs, getClip, getCredentials, getFiles, https, opt, os, path, post, querystring, run, setClipboard, setCredentials, usage;
+ fs = require('fs');
+ os = require('os');
+ path = require('path');
+ https = require('https');
+ querystring = require('querystring');
+ exec = require('child_process').exec;
+ EventEmitter = require('events').EventEmitter;
+ opt = require('optimist').usage('Usage: ngist [options] [file.ext] [file...]').argv;
+ emitter = new EventEmitter;
+ run = function() {
+ if (opt.h || opt.help) {
+ return usage();
+ }
+ if ((opt.f || opt.file) && !(opt.c || opt.clip)) {
+ return getFiles();
+ }
+ if ((opt.c || opt.clip) && !(opt.f || opt.file)) {
+ return getClip();
+ }
+ if ((opt.c || opt.clip) && (opt.f || opt.file)) {
+ anError('You cannot use files and clipboard together.');
+ }
+ if (!((opt.f || opt.file) || (opt.c || opt.f))) {
+ return anError('File or clipboard not specified. For help: ngist -h');
+ }
+ };
+ getCredentials = function() {
+ if ((opt.u || opt.user) && (opt.t || opt.token)) {
+ return setCredentials();
+ } else {
+ return exec("git config --global github.user", function(err, stdout, stderr) {
+ var user;
+ if (!err) {
+ user = stdout.replace("\n", "");
+ return exec("git config --global github.token", function(err, stdout, stderr) {
+ var token;
+ if (!err) {
+ token = stdout.replace("\n", "");
+ return emitter.emit('credentials', user, token);
+ } else {
+ return anError('No token set with git.\nFor help: ngist -h');
+ }
+ });
+ } else {
+ return anError('No username or token set with git.\nFor help: ngist -h');
+ }
+ });
+ }
+ };
+ setCredentials = function() {
+ return exec("git config --global github.user " + (opt.u || opt.user), function(err, stdout, stderr) {
+ if (!err) {
+ return exec("git config --global github.token " + (opt.t || opt.token), function(err, stdout, stderr) {
+ return emitter.emit('credentials', opt.u || opt.user, opt.t || opt.token);
+ });
+ }
+ });
+ };
+ setClipboard = function(url) {
+ var clipboard;
+ switch (os.type()) {
+ case 'Darwin':
+ clipboard = "echo " + url + " | pbcopy";
+ break;
+ case 'Linux':
+ clipboard = "echo " + url + " | xclip";
+ }
+ return exec(clipboard, function(err, stdout) {
+ if (err) {
+ return anError('Problem sending url to clipboard');
+ }
+ });
+ };
+ post = function(gist) {
+ var gistData, options, req;
+ gistData = querystring.stringify(gist);
+ options = {
+ host: 'gist.github.com',
+ port: 443,
+ path: '/gists',
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/x-www-form-urlencoded',
+ 'Content-Length': gistData.length
+ }
+ };
+ req = https.request(options, function(res) {
+ if (res.statusCode === 401) {
+ anError("Github.com username or token are not valid.\n username: " + gist.login + " token: " + gist.token + "\n For help: gist -h");
+ process.exit(1);
+ }
+ return res.on('end', function() {
+ if (opt.o || opt.out) {
+ setClipboard(res.headers.location);
+ }
+ return console.log('Gist URL: ' + res.headers.location);
+ });
+ });
+ return req.end(gistData);
+ };
+ getFiles = function() {
+ var files, gist, i;
+ gist = {};
+ files = [];
+ files.push(opt.f || opt.file);
+ if (opt._) {
+ for (i in opt._) {
+ files.push(opt._[i]);
+ }
+ }
+ files.forEach(function(file, i, arr) {
+ return path.exists(file, function(exists) {
+ if (!exists) {
+ anError("File not found: " + file);
+ }
+ gist["file_ext[gistfile" + (i + 1) + "]"] = path.extname(file);
+ gist["file_name[gistfile" + (i + 1) + "]"] = path.basename(file);
+ gist["file_contents[gistfile" + (i + 1) + "]"] = fs.readFileSync(file, 'utf8');
+ if (opt.p || opt.private) {
+ gist['action_button'] = 'private';
+ }
+ if (i === arr.length - 1) {
+ return emitter.emit('filesDone');
+ }
+ });
+ });
+ return emitter.on('filesDone', function() {
+ if (opt.l || opt.login) {
+ getCredentials();
+ return emitter.on('credentials', function(user, token) {
+ gist['login'] = user;
+ gist['token'] = token;
+ return post(gist);
+ });
+ } else {
+ return post(gist);
+ }
+ });
+ };
+ getClip = function() {
+ var clipboard, gist;
+ gist = {};
+ switch (os.type()) {
+ case 'Darwin':
+ clipboard = 'pbpaste';
+ break;
+ case 'Linux':
+ clipboard = 'xclip -out -selection clipboard';
+ }
+ return exec(clipboard, function(err, stdout) {
+ if (stdout === '') {
+ anError('Nothing in clipboard');
+ }
+ if (err) {
+ anError('Problem getting contents of clipboard');
+ }
+ gist['file_ext[gistfile1]'] = path.extname(opt.c || opt.clip);
+ gist['file_name[gistfile1]'] = opt.c || opt.clip;
+ gist['file_contents[gistfile1]'] = stdout;
+ if (opt.p || opt.private) {
+ gist['action_button'] = 'private';
+ }
+ if (opt.l || opt.login) {
+ getCredentials();
+ return emitter.on('credentials', function(user, token) {
+ gist['login'] = user;
+ gist['token'] = token;
+ return post(gist);
+ });
+ } else {
+ return post(gist);
+ }
+ });
+ };
+ anError = function(text) {
+ console.error(text);
+ return process.exit(1);
+ };
+ usage = function() {
+ return console.log('\n Usage: ngist [options] [-f file.ext] [files...]\n \n -f, --file use file(s) for gist\n -c, --clip use clipboard for gist\n -p, --private make private gist\n -l, --login use github.com credentials\n -u, --username github.com username\n -t, --token github.com API token\n -o, --out copy new gist url to clipboard\n -h, --help display this help message\n \n \n Examples\n \n Single File\n ngist -f file.js\n \n Multiple Files\n ngist -f file.js file2.js file3.js\n \n Use clipboard\n ngist -c file.js\n \n Login and set user and token\n ngist -f file.js -l -u user -t fj920df\n \n Login after user and token are set\n ngist -f file.js -l\n \n Private gist\n ngist -p -f file.js\n ');
+ };
+ run();
+}).call(this);
@@ -0,0 +1,25 @@
+{
+ "name": "ngist",
+ "description": "Gist CLI tool using node.js",
+ "keywords": ["gist", "cli", "github", "tools"],
+ "author": "Jacob Chapel <jacob.chapel@gmail.com>",
+ "version": "0.1.0",
+ "licenses": [{
+ "type": "MIT",
+ "url": "http://github.com/chapel/ngist/raw/master/LICENSE"
+ }],
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/chapel/ngist.git"
+ },
+ "engines": {
+ "node": ">=0.4.0"
+ },
+ "dependencies": {
+ "optimist": ">= 0.1.5"
+ },
+ "main" : "./lib/ngist",
+ "bin": {
+ "ngist": "./lib/ngist.js"
+ }
+}
Oops, something went wrong.

0 comments on commit f38e34d

Please sign in to comment.