Permalink
Browse files

halfway there

  • Loading branch information...
1 parent 0ad9d84 commit 1c710de108d17f1890c89f5d701ebdd03c5ed1a3 @goatslacker committed Dec 11, 2011
Showing with 69 additions and 166 deletions.
  1. +1 −1 .gitignore
  2. +2 −1 bin/git-stats
  3. +66 −0 git.js
  4. +0 −50 lib/index.js
  5. +0 −114 lib/xhr.js
View
@@ -1 +1 @@
-config.json
+node_modules
View
@@ -3,4 +3,5 @@ if [ $1 != "--conf" ]
then
git "$@"
fi
-nohup bin/gitstatsjs "$@" > /dev/null 2>&1 &
+bin/gitstatsjs "$@"
+#nohup bin/gitstatsjs "$@" > /dev/null 2>&1 &
View
66 git.js
@@ -0,0 +1,66 @@
+var fs = require('fs');
+var Gist = require("gister");
+var path = process.env.HOME + "/.gitstats";
+
+// Pull credentials from environment variables
+var username = process.env.GITHUB_USER;
+var token = process.env.GITHUB_TOKEN;
+
+// TODO need to pull config from another place
+
+var gist = new Gist({
+ username: username,
+ token: token,
+ gist_id: gist_id
+});
+
+gist.on('created', function (_, gist_id) {
+ console.log("Posted to GitHub.");
+ // TODO need to save gist_id somewhere
+});
+
+gist.on('updated', function () {
+ console.log("Updated gist.");
+});
+
+// If the gist doesn't exist, create a new one.
+gist.on('error:gist_id', function () {
+ gist.post(JSON.stringify([log]));
+});
+
+// Throws an error if credentials are missing.
+gist.on('error:credentials', function () {
+ throw new Error("Credentials are required: GitHub username and token.");
+});
+
+gist.on('get', function (data) {
+ data = JSON.parse(data);
+ data.push(log);
+
+ gist.edit(data);
+});
+
+var stats = function (args) {
+ var log = {
+ date: Date.now(),
+ command: args.shift(),
+ details: args
+ };
+
+ // sync to gist
+ gist.get();
+
+ // collect on disk
+ fs.readFile(path, "utf-8", function (err, data) {
+ if (err) throw err;
+
+ data = JSON.parse(data);
+ data.push(log);
+
+ fs.writeFile(path, JSON.stringify(data), "utf-8", function (err) {
+ if (err) throw err;
+ });
+ });
+};
+
+module.exports = stats;
View
@@ -1,50 +0,0 @@
-var config;
-var fs = require('fs');
-var path = process.env.HOME + "/.gitstats";
-
-// check for/add config file
-(function () {
- var url = require('url');
- var argv = process.argv.slice(2);
- var uri;
- if (argv[0] === "--conf") {
- config = url.parse(argv[1]);
- fs.writeFileSync("./config.json", JSON.stringify(config), "utf8");
- process.exit();
- }
-}());
-
-try {
- config = require('../config.json');
-} catch (e) {
- process.stdout.write("Run git --conf <url>\n");
- process.stdout.write("You must first create a config.json\n");
- process.exit();
-}
-
-var xhr = require('./xhr').load(config.hostname, config.port);
-
-var stats = function (args) {
- var data = {
- date: Date.now(),
- command: args.shift(),
- details: args
- };
-
- // collect on disk
- var all = [];
- try {
- all = require(path);
- } catch (e) {
- all = [];
- }
- all.push(data);
-
- // post to cloud
- xhr(config.path).post(data);
-
- // write to disk
- fs.writeFileSync(path, JSON.stringify(all), "utf8");
-};
-
-module.exports = stats;
View
@@ -1,114 +0,0 @@
-/*global require exports */
-/**
- Copyright (C) 2011 by Josh Perez <josh@goatslacker.com>
-
- 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.
- */
-
-/**
- @author Josh Perez <josh@goatslacker.com>
- @version 1.0
- */
-
-var http = require("http");
-
-/**
- Performs an http request
-
- @param {Object} options { host: <server_name>, port: <server_port>, method: <request_method>, path: <url> }
- @param {Function} callback runs after operation has completed
- */
-var doXHR = function (options, callback, data) {
- var req = http.request(options, function (res) {
- res.setEncoding('utf8');
- res.responseText = "";
-
- res.on('data', function (chunk) {
- res.responseText += chunk;
- });
-
- res.on('end', function () {
- callback && callback(res);
- });
- });
-
- if (data) {
- req.write(JSON.stringify(data));
- }
-
- req.end();
-};
-
-/**
- Loads pre-configured host/port
- Useful if you're making many XHR requests to a single place
-
- @param {string} host the domain name to query
- @param {integer} port
- @returns {Object} get, post, put
- */
-exports.load = function (host, port) {
- var options = {
- host: host,
- port: port || 80,
- headers: {
- "Accept": "application/json",
- "Content-Type": "application/json"
- }
- };
-
- return function (path, headers) {
- headers = headers || {};
-
- options.path = path;
-
- // override headers if they exist
- Object.keys(headers).forEach(function (header) {
- options.headers[header] = headers[header];
- });
-
- return {
- get: function (callback) {
- options.method = "GET";
- doXHR(options, callback);
- },
-
- post: function (data, callback) {
- options.method = "POST";
- doXHR(options, callback, data);
- },
-
- put: function (data, callback) {
- options.method = "PUT";
- doXHR(options, callback, data);
- },
-
- del: function (data, callback) {
- options.method = "DELETE";
- doXHR(options, callback, data);
- }
- };
- };
-
-};
-
-/**
- Returns direct access to XHR function
- */
-exports.xhr = doXHR;

0 comments on commit 1c710de

Please sign in to comment.