Permalink
Browse files

config saving implemented

  • Loading branch information...
0 parents commit 5bc1a9b0d98c9121bef29100c7a8423db557c329 @substack committed Apr 10, 2012
Showing with 112 additions and 0 deletions.
  1. +7 −0 bin/cmd.js
  2. +77 −0 lib/config.js
  3. +28 −0 package.json
@@ -0,0 +1,7 @@
+#!/usr/bin/env node
+var withConfig = require('../lib/config');
+
+withConfig(function (config) {
+ console.log('beep boop');
+ console.dir(config);
+});
@@ -0,0 +1,77 @@
+var fs = require('fs');
+var path = require('path');
+var mkdirp = require('mkdirp');
+
+var tty = require('tty');
+function echo (mode) {
+ tty.setRawMode(!mode);
+}
+
+module.exports = function (cb) {
+ var dir = process.env.HOME + '/.config';
+ mkdirp(dir, function (err) {
+ if (err) return console.error(err);
+
+ var file = dir + '/travisify.json';
+ function read () {
+ fs.readFile(file, function (err, body) {
+ if (err) console.error(err)
+ else cb(JSON.parse(body))
+ })
+ }
+
+ path.exists(file, function (ex) {
+ if (!ex) prompt(function (vars) {
+ fs.writeFile(file, JSON.stringify(vars), function (err) {
+ if (err) console.error(err)
+ else read()
+ });
+ })
+ else read()
+ });
+ });
+};
+
+function prompt (cb) {
+ var vars = {};
+ var queue = [
+ function (user) {
+ vars.user = user;
+ process.stdout.write('github password: ');
+ echo(false);
+ },
+ function (pass) {
+ vars.pass = pass;
+ process.stdout.write('\r\ntravis-ci api key: ');
+ echo(true);
+ },
+ function (token) {
+ vars.token = token;
+ cb(vars);
+ },
+ ];
+ process.stdout.write('github username: ');
+
+ var line = '';
+ process.stdin.on('data', function fn (buf) {
+ if (buf[0] === 0x03) process.exit();
+
+ line += buf;
+
+ function findBreak () {
+ var i = line.indexOf('\n');
+ if (i >= 0) return i;
+ return line.indexOf('\r');
+ }
+
+ var ix;
+ while ((ix = findBreak()) >= 0) {
+ var s = line.slice(0, ix);
+ line = line.slice(ix + 1);
+ if (queue.length === 0) process.stdin.removeListener('data', fn);
+ else queue.shift()(s);
+ }
+ });
+
+ process.stdin.resume();
+}
@@ -0,0 +1,28 @@
+{
+ "name" : "travisify",
+ "description" : "easily update github admin hooks for travis-ci",
+ "version" : "0.0.0",
+ "repository" : {
+ "type" : "git",
+ "url" : "git://github.com/substack/travisify.git"
+ },
+ "bin" : {
+ "travisify" : "bin/cmd.js"
+ },
+ "dependencies" : {
+ "mkdirp" : "~0.3.1"
+ },
+ "keywords" : [
+ "travis",
+ "ci"
+ ],
+ "engines" : {
+ "node" : ">=0.4.0"
+ },
+ "license" : "MIT",
+ "author" : {
+ "name" : "James Halliday",
+ "email" : "mail@substack.net",
+ "url" : "http://substack.net"
+ }
+}

0 comments on commit 5bc1a9b

Please sign in to comment.