Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Massive rewrite / now with module

  • Loading branch information...
commit 2e9e016773de6677cab9b242b89a17ea86a6837d 1 parent 70ebb49
Jacob Chapel authored
127 README.markdown
View
@@ -0,0 +1,127 @@
+ngist
+=====
+
+ngist is a node.js module and CLI tool for easy posting of [gists](http://gist.github.com).
+
+
+Installation
+------------
+
+With [npm](http://github.com/isaacs/npm):
+
+ npm install ngist
+
+Clone this project:
+
+ git clone http://github.com/chapel/ngist.git
+
+
+CLI
+---
+
+ Usage: ngist [file.js file2.js ...] [options]
+
+ [Options]
+ -h, --help Display this help page
+ -c, --clip Use clipboard content
+ ex: ngist [file.js] -c file.js
+ -d, --desc Set description of gist
+ ex: ngist file.js -d 'Description'
+ -u, --user Github username
+ -t, --token Github API token
+ ex: ngist file.js -u username -t token
+ -p, --private Make gist private
+ -o, --out Copy gist url to clipboard
+
+How to set user/token with git config
+-------------------------------------
+
+Once you get your Github API token from your [Github Account](https://github.com/account#admin_bucket)
+If you set your Github username and API token using these steps, ngist will
+automatically gather them and authenticate for you.
+
+Run these two commands:
+
+ git config --add github.user [github_username]
+ git config --add github.token [github_api_token]
+
+To verify that they have been set, use:
+
+ git config --get github.user
+ git config --get github.token
+
+
+Module
+------
+
+The ngist module is simple and easy to use.
+
+**Example**
+
+ngist_text.js
+
+ var ngist = require('ngist');
+
+ var files = [{name: 'file1.js', content: "function(){ console.log('x'); };"},
+ {name: 'file2.js', content: "function(){ test.x; };"}];
+ var options = {
+ user: chapel, // Your username!
+ token: asa09fjew0f, // [Github Account](https://github.com/account#admin_bucket)
+ description: 'This is an example gist', // Set for whole gist and is optional.
+ private: true; // Set to true if you want the gist to be private, otherwise omit.
+ };
+
+ ngist.send(files, options, function(err, url) {
+ console.log(url); // Example: https://gist.github.com/830022
+ });
+
+ngist_files.js
+
+ var ngist = require('ngist');
+
+ var files = ['file1.js', 'file2.js'];
+ var options = {
+ user: chapel,
+ token: asa09fjew0f,
+ description: 'This is an example gist',
+ private: true;
+ };
+
+ ngist.files(files, function(err, processed_files) {
+ ngist.send(processed_files, options, function(err, url) {
+ console.log(url);
+ });
+ });
+
+ngist_files.coffee
+
+ ngist = require 'ngist'
+
+ files = ['file1.js', 'file2.js']
+ options =
+ user: chapel
+ token: pajsdfjoafe
+ description: 'This is an example gist'
+ private: true;
+
+ ngist.files files, (err, processed_files) ->
+ ngist.send processed_files, options, (err, url) ->
+ console.log url
+
+
+Functions
+---------
+
+**ngist.files**
+
+Takes an array of file names, fetches the files, while sending an error if a file is not found. If all files are found, then it returns an array of objects that have the files extension, name, and contents ready to be sent.
+
+**ngist.send**
+
+Takes an array of objects, as well as an object that has optional parameters like user/token, description and private. If there are no errors, it will return a gist url.
+
+
+Inspired By
+-----------
+
+The original CLI tool was inspired by [defunkt's gist](https://github.com/defunkt/gist) as well as those on [#node.js](irc://freenode.net) that gave advice and criticism. It is always welcome.
165 lib/cli.js
View
@@ -0,0 +1,165 @@
+#!/usr/bin/env node
+
+var exec, finish, get_clip, get_creds, next, ngist, opt, os, run, serial, set_clip, spit_error;
+os = require('os');
+ngist = require('./ngist');
+exec = require('child_process').exec;
+opt = require('optimist').argv;
+run = function() {
+ if (opt._.length === 0) {
+ opt._ = null;
+ }
+ opt.help = opt.help || opt.h;
+ opt.clip = opt.clip || opt.c;
+ opt.desc = opt.desc || opt.d;
+ opt.private = opt.private || opt.p;
+ opt.user = opt.user || opt.u;
+ opt.token = opt.token || opt.t;
+ opt.out = opt.out || opt.o;
+ if (opt.help) {
+ return console.log(' \nUsage: ngist [file.js file2.js ...] [options]\n\n[Options]\n-h, --help Display this help page\n-c, --clip Use clipboard content\n ex: ngist [file.js] -c file.js\n-d, --desc Set description of gist\n ex: ngist file.js -d \'Description\'\n-u, --user Github username\n-t, --token Github API token | https://github.com/account#admin_bucket\n ex: ngist file.js -u username -t token\n-p, --private Make gist private\n-o, --out Copy gist url to clipboard\n\n[How to set user/token with git config]\nOnce you get your Github API token from https://github.com/account#admin_bucket\nIf you set your Github username and API token using these steps, ngist will\nautomatically gather them and authenticate for you.\n\nRun these two commands:\n git config --add github.user [github_username]\n git config --add github.token [github_api_token]\n\nTo verify that they have been set, use:\n git config --get github.[user/token]');
+ }
+ if (opt._ && opt.clip) {
+ return ngist.files(opt._, function(err, files) {
+ if (err) {
+ spit_error(err);
+ }
+ return get_clip(function(clip) {
+ files = files.concat(clip);
+ return next(files);
+ });
+ });
+ } else if (opt._) {
+ return ngist.files(opt._, function(err, files) {
+ if (err) {
+ spit_error(err);
+ }
+ return next(files);
+ });
+ } else if (opt.clip) {
+ return get_clip(function(clip) {
+ return next(clip);
+ });
+ } else {
+ return spit_error('You must set file(s) and/or a clip from the clipboard (-c)');
+ }
+};
+next = function(files) {
+ if (opt.user && opt.token) {
+ return finish(files, opt.user, opt.token);
+ } else if ((opt.user && !opt.token) || (opt.token && !opt.user)) {
+ return spit_error('Both user (-u) and token (-t) must be set if you wish to login');
+ } else {
+ return get_creds(files);
+ }
+};
+finish = function(files, user, token) {
+ var options;
+ if (opt.desc || opt.private || (user && token)) {
+ options = {};
+ } else {
+ options = null;
+ }
+ if (opt.private) {
+ options.private = true;
+ }
+ if (opt.desc) {
+ options.description = opt.desc;
+ }
+ if (user) {
+ options.user = user;
+ }
+ if (token) {
+ options.token = token;
+ }
+ return ngist.send(files, options, function(err, url) {
+ if (err) {
+ spit_error(err);
+ }
+ if (opt.out) {
+ set_clip(url);
+ }
+ return console.log("Gist: " + url);
+ });
+};
+get_creds = function(files) {
+ var keys;
+ keys = ['get', 'system', 'global', 'simple'];
+ return serial(keys, function(key, i, _next) {
+ return exec("git config --" + key + " github.user", function(err, stdout, stderr) {
+ var user;
+ if (!err) {
+ user = stdout.replace("\n", "");
+ return exec("git config --" + key + " github.token", function(err, stdout, stderr) {
+ var token;
+ if (!err) {
+ token = stdout.replace("\n", "");
+ return finish(files, user, token);
+ }
+ });
+ } else {
+ return _next();
+ }
+ });
+ }, function() {
+ console.log("For instructions on how to authenticate your gists: ngist -h");
+ return finish(files, null, null);
+ });
+};
+set_clip = 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 spit_error('Problem sending url to clipboard');
+ }
+ });
+};
+get_clip = function(callback) {
+ var clip, clipboard;
+ clip = [];
+ switch (os.type()) {
+ case 'Darwin':
+ clipboard = 'pbpaste';
+ break;
+ case 'Linux':
+ clipboard = 'xclip -out -selection clipboard';
+ }
+ return exec(clipboard, function(err, stdout) {
+ if (stdout === '') {
+ spit_error('Nothing in clipboard');
+ }
+ if (err) {
+ spit_error('Problem getting contents of clipboard');
+ }
+ clip.push({
+ name: opt.clip,
+ contents: stdout
+ });
+ return callback(clip);
+ });
+};
+spit_error = function(err) {
+ console.error("" + err + "\nFor more help see: ngist -h");
+ return process.exit(1);
+};
+serial = function(array, iterator, next) {
+ var cycle;
+ cycle = function(i) {
+ if (i < array.length) {
+ return iterator(array[i], i, function() {
+ return cycle(i + 1);
+ });
+ } else {
+ return next();
+ }
+ };
+ return cycle(0);
+};
+run();
260 lib/ngist.js 100755 → 100644
View
@@ -1,191 +1,95 @@
-(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();
+var fs, https, path, post, querystring, serial;
+fs = require('fs');
+path = require('path');
+https = require('https');
+querystring = require('querystring');
+exports.send = function(files, options, callback) {
+ var gist;
+ gist = {};
+ if (typeof options === 'function') {
+ callback = options;
+ options = null;
+ }
+ if (files.length > 10) {
+ return callback(new Error("" + files.length + " files is more than the maximum of 10 files"));
+ }
+ files.forEach(function(file, i) {
+ gist["file_ext[gistfile" + (i + 1) + "]"] = path.extname(file.name);
+ gist["file_name[gistfile" + (i + 1) + "]"] = file.name;
+ return gist["file_contents[gistfile" + (i + 1) + "]"] = file.contents;
+ });
+ if (options !== null) {
+ if ((options.user && !options.token) || (options.token && !options.user)) {
+ return callback(new Error('To login both options.user and options.token must be given'));
}
- if ((opt.f || opt.file) && !(opt.c || opt.clip)) {
- return getFiles();
+ if (options.user) {
+ gist['login'] = options.user;
}
- if ((opt.c || opt.clip) && !(opt.f || opt.file)) {
- return getClip();
+ if (options.token) {
+ gist['token'] = options.token;
}
- if ((opt.c || opt.clip) && (opt.f || opt.file)) {
- anError('You cannot use files and clipboard together.');
+ if (options.description) {
+ gist['description'] = options.description;
}
- if (!((opt.f || opt.file) || (opt.c || opt.f))) {
- return anError('File or clipboard not specified. For help: ngist -h');
+ if (options.private === true) {
+ gist['action_button'] = 'private';
}
- };
- 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);
- });
+ }
+ return post(gist, callback);
+};
+exports.files = function(files, callback) {
+ var tmp;
+ tmp = [];
+ return serial(files, function(file, i, next) {
+ return path.exists(file, function(exists) {
+ if (!exists) {
+ return callback(new Error("File not found: " + file));
}
- });
- };
- 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 fs.readFile(file, 'utf8', function(err, data) {
+ tmp.push({
+ 'name': path.basename(file),
+ 'contents': data
+ });
+ return next();
});
});
- 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]);
- }
+ }, function() {
+ return callback(null, tmp);
+ });
+};
+post = function(gist, callback) {
+ 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
}
- 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);
- if (opt.d || opt.description) {
- gist['description'] = opt.d || opt.description;
- }
- 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';
+ req = https.request(options, function(res) {
+ if (res.statusCode === 401) {
+ return callback(new Error('Username or token not authorized on Github.com'));
}
- 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;
- if (opt.d || opt.description) {
- gist['description'] = opt.d || opt.description;
- }
- 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);
- }
+ return res.on('end', function() {
+ return callback(null, res.headers.location);
});
+ });
+ return req.end(gistData);
+};
+serial = function(array, iterator, next) {
+ var cycle;
+ cycle = function(i) {
+ if (i < array.length) {
+ return iterator(array[i], i, function() {
+ return cycle(i + 1);
+ });
+ } else {
+ return next();
+ }
};
- 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 -d, --description set description 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 Set description\n ngist -f file.js -d \'This is a description\'\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);
+ return cycle(0);
+};
6 package.json
View
@@ -1,9 +1,9 @@
{
"name": "ngist",
- "description": "Gist CLI tool using node.js",
+ "description": "Gist module and CLI tool using node.js",
"keywords": ["gist", "cli", "github", "tools"],
"author": "Jacob Chapel <jacob.chapel@gmail.com>",
- "version": "0.1.1",
+ "version": "0.2.0",
"licenses": [{
"type": "MIT",
"url": "http://github.com/chapel/ngist/raw/master/LICENSE"
@@ -20,6 +20,6 @@
},
"main" : "./lib/ngist",
"bin": {
- "ngist": "./lib/ngist.js"
+ "ngist": "./lib/cli.js"
}
}
138 src/cli.coffee
View
@@ -0,0 +1,138 @@
+os = require 'os'
+ngist = require './ngist'
+{exec} = require 'child_process'
+opt = require('optimist').argv
+
+run = ->
+ # Set args to the appropriate values
+ opt._ = null if opt._.length is 0
+ opt.help = opt.help or opt.h
+ opt.clip = opt.clip or opt.c
+ opt.desc = opt.desc or opt.d
+ opt.private = opt.private or opt.p
+ opt.user = opt.user or opt.u
+ opt.token = opt.token or opt.t
+ opt.out = opt.out or opt.o
+
+ # Return help message if -h or --help is set
+ if opt.help
+ return console.log '''
+
+Usage: ngist [file.js file2.js ...] [options]
+
+[Options]
+-h, --help Display this help page
+-c, --clip Use clipboard content
+ ex: ngist [file.js] -c file.js
+-d, --desc Set description of gist
+ ex: ngist file.js -d 'Description'
+-u, --user Github username
+-t, --token Github API token | https://github.com/account#admin_bucket
+ ex: ngist file.js -u username -t token
+-p, --private Make gist private
+-o, --out Copy gist url to clipboard
+
+[How to set user/token with git config]
+Once you get your Github API token from https://github.com/account#admin_bucket
+If you set your Github username and API token using these steps, ngist will
+automatically gather them and authenticate for you.
+
+Run these two commands:
+ git config --add github.user [github_username]
+ git config --add github.token [github_api_token]
+
+To verify that they have been set, use:
+ git config --get github.[user/token]
+'''
+
+ if opt._ and opt.clip
+ ngist.files opt._, (err, files) ->
+ spit_error err if err
+ get_clip (clip) ->
+ files = files.concat clip
+ next(files)
+ else if opt._
+ ngist.files opt._, (err, files) ->
+ spit_error err if err
+ next(files)
+ else if opt.clip
+ get_clip (clip) ->
+ next clip
+ else
+ spit_error 'You must set file(s) and/or a clip from the clipboard (-c)'
+
+
+next = (files) ->
+ if opt.user and opt.token
+ finish files, opt.user, opt.token
+ else if (opt.user and not opt.token) or (opt.token and not opt.user)
+ spit_error 'Both user (-u) and token (-t) must be set if you wish to login'
+ else
+ get_creds files
+
+finish = (files, user, token) ->
+ if opt.desc or opt.private or (user and token)
+ options = {}
+ else
+ options = null
+ options.private = true if opt.private
+ options.description = opt.desc if opt.desc
+ options.user = user if user
+ options.token = token if token
+ ngist.send files, options, (err, url) ->
+ spit_error err if err
+ set_clip url if opt.out
+ console.log "Gist: #{url}"
+
+get_creds = (files) ->
+ keys = ['get', 'system', 'global', 'simple']
+ serial keys, (key, i, _next) ->
+ exec "git config --#{key} github.user", (err, stdout, stderr) ->
+ unless err
+ user = stdout.replace("\n", "")
+ exec "git config --#{key} github.token", (err, stdout, stderr) ->
+ unless err
+ token = stdout.replace("\n", "")
+ return finish files, user, token
+ else
+ _next()
+ , ->
+ console.log "For instructions on how to authenticate your gists: ngist -h"
+ return finish files, null, null
+
+set_clip = (url) ->
+ switch os.type()
+ when 'Darwin' then clipboard = "echo #{url} | pbcopy"
+ when 'Linux' then clipboard = "echo #{url} | xclip"
+
+ exec clipboard, (err, stdout) ->
+ spit_error 'Problem sending url to clipboard' if err
+
+get_clip = (callback) ->
+ clip = []
+ switch os.type()
+ when 'Darwin' then clipboard = 'pbpaste'
+ when 'Linux' then clipboard = 'xclip -out -selection clipboard'
+
+ exec clipboard, (err, stdout) ->
+ spit_error 'Nothing in clipboard' if stdout == ''
+ spit_error 'Problem getting contents of clipboard' if err
+ clip.push
+ name: opt.clip
+ contents: stdout
+ callback clip
+
+spit_error = (err) ->
+ console.error "#{err}\nFor more help see: ngist -h"
+ process.exit 1
+
+serial = (array, iterator, next) ->
+ cycle = (i) ->
+ if i < array.length
+ iterator array[i], i, ->
+ cycle i+1
+ else
+ next()
+ cycle 0
+
+run()
202 src/ngist.coffee
View
@@ -1,58 +1,42 @@
fs = require 'fs'
-os = require 'os'
path = require 'path'
https = require 'https'
querystring = require 'querystring'
-{exec} = require 'child_process'
-{EventEmitter} = require 'events'
-opt = require('optimist')
- .usage('Usage: ngist [options] [file.ext] [file...]')
- .argv
-
-
-emitter = new EventEmitter
-run = ->
- return usage() if opt.h or opt.help
- return getFiles() if (opt.f or opt.file) and not (opt.c or opt.clip)
- return getClip() if (opt.c or opt.clip) and not (opt.f or opt.file)
-
-
- anError 'You cannot use files and clipboard together.' if (opt.c or opt.clip) and (opt.f or opt.file)
- anError 'File or clipboard not specified. For help: ngist -h' unless (opt.f or opt.file) or (opt.c or opt.f)
-
-getCredentials = ->
- if (opt.u or opt.user) and (opt.t or opt.token)
- setCredentials()
- else
- exec "git config --global github.user", (err, stdout, stderr) ->
- unless err
- user = stdout.replace("\n", "")
- exec "git config --global github.token", (err, stdout, stderr) ->
- unless err
- token = stdout.replace("\n", "")
- emitter.emit 'credentials', user, token
- else
- anError 'No token set with git.\nFor help: ngist -h'
- else
- anError 'No username or token set with git.\nFor help: ngist -h'
-
-setCredentials = ->
- exec "git config --global github.user #{opt.u or opt.user}", (err, stdout, stderr) ->
- unless err
- exec "git config --global github.token #{opt.t or opt.token}", (err, stdout, stderr) ->
- emitter.emit 'credentials', (opt.u or opt.user), (opt.t or opt.token)
-
-setClipboard = (url) ->
- switch os.type()
- when 'Darwin' then clipboard = "echo #{url} | pbcopy"
- when 'Linux' then clipboard = "echo #{url} | xclip"
-
- exec clipboard, (err, stdout) ->
- anError 'Problem sending url to clipboard' if err
-
-post = (gist) ->
+exports.send = (files, options, callback) ->
+ gist = {}
+ if typeof options is 'function'
+ callback = options
+ options = null
+ return callback new Error "#{files.length} files is more than the maximum of 10 files" if files.length > 10
+ files.forEach (file, i) ->
+ gist["file_ext[gistfile#{i+1}]"] = path.extname file.name
+ gist["file_name[gistfile#{i+1}]"] = file.name
+ gist["file_contents[gistfile#{i+1}]"] = file.contents
+ unless options is null
+ if (options.user and not options.token) or (options.token and not options.user)
+ return callback new Error 'To login both options.user and options.token must be given'
+ gist['login'] = options.user if options.user
+ gist['token'] = options.token if options.token
+ gist['description'] = options.description if options.description
+ gist['action_button'] = 'private' if options.private is true
+ return post gist, callback
+
+exports.files = (files, callback) ->
+ tmp = []
+ serial files, (file, i, next) ->
+ path.exists file, (exists) ->
+ return callback new Error "File not found: #{file}" unless exists
+ fs.readFile file, 'utf8', (err, data) ->
+ tmp.push
+ 'name': path.basename file
+ 'contents': data
+ next()
+ , ->
+ callback null, tmp
+
+post = (gist, callback) ->
gistData = querystring.stringify gist
options =
host: 'gist.github.com'
@@ -62,117 +46,21 @@ post = (gist) ->
headers:
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Length': gistData.length
-
+
req = https.request options, (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 callback new Error 'Username or token not authorized on Github.com'
+
res.on 'end', ->
- setClipboard(res.headers.location) if opt.o or opt.out
- console.log 'Gist URL: ' + res.headers.location
-
+ callback null, res.headers.location
+
req.end gistData
-getFiles = ->
- gist = {}
- files = []
- files.push opt.f or opt.file
- if opt._
- for i of opt._
- files.push opt._[i]
-
- files.forEach (file, i, arr) ->
- path.exists file, (exists) ->
- unless 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['description'] = opt.d or opt.description if opt.d or opt.description
- gist["file_contents[gistfile#{i+1}]"] = fs.readFileSync file, 'utf8'
- gist['action_button'] = 'private' if (opt.p or opt.private)
- if i == arr.length - 1 then emitter.emit 'filesDone'
-
- emitter.on 'filesDone', ->
- if opt.l or opt.login
- getCredentials()
- emitter.on 'credentials', (user, token) ->
- gist['login'] = user
- gist['token'] = token
- post gist
+serial = (array, iterator, next) ->
+ cycle = (i) ->
+ if i < array.length
+ iterator array[i], i, ->
+ cycle i+1
else
- post gist
-
-getClip = ->
- gist = {}
- switch os.type()
- when 'Darwin' then clipboard = 'pbpaste'
- when 'Linux' then clipboard = 'xclip -out -selection clipboard'
-
- exec clipboard, (err, stdout) ->
- anError 'Nothing in clipboard' if stdout == ''
- anError 'Problem getting contents of clipboard' if err
- gist['file_ext[gistfile1]'] = path.extname opt.c or opt.clip
- gist['file_name[gistfile1]'] = opt.c or opt.clip
- gist['description'] = opt.d or opt.description if opt.d or opt.description
- gist['file_contents[gistfile1]'] = stdout
- gist['action_button'] = 'private' if (opt.p or opt.private)
- if opt.l or opt.login
- getCredentials()
- emitter.on 'credentials', (user, token) ->
- gist['login'] = user
- gist['token'] = token
- post gist
- else
- post gist
-
-anError = (text) ->
- console.error text
- process.exit 1
-
-
-usage = ->
- console.log '''
-
- Usage: ngist [options] [-f file.ext] [files...]
-
- -f, --file use file(s) for gist
- -c, --clip use clipboard for gist
- -d, --description set description for gist
- -p, --private make private gist
- -l, --login use github.com credentials
- -u, --username github.com username
- -t, --token github.com API token
- -o, --out copy new gist url to clipboard
- -h, --help display this help message
-
-
- Examples
-
- Single File
- ngist -f file.js
-
- Multiple Files
- ngist -f file.js file2.js file3.js
-
- Use clipboard
- ngist -c file.js
-
- Set description
- ngist -f file.js -d 'This is a description'
-
- Login and set user and token
- ngist -f file.js -l -u user -t fj920df
-
- Login after user and token are set
- ngist -f file.js -l
-
- Private gist
- ngist -p -f file.js
-
- '''
-
-run()
+ next()
+ cycle 0
Please sign in to comment.
Something went wrong with that request. Please try again.