From 1fbb65b18aa6ad92850101c7eafe72765769feab Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Sun, 2 Mar 2014 20:07:33 -0800 Subject: [PATCH] Add --interactive. Fixes #5. Also fix getListUntrackedFiles, which I somehow broke previously. --- bin/gistup | 39 ++++++++++++++++++++++++++++++++++++--- package.json | 2 +- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/bin/gistup b/bin/gistup index 05256e6..7a46526 100755 --- a/bin/gistup +++ b/bin/gistup @@ -28,6 +28,11 @@ var argv = require("optimist").usage("Usage: \033[1mgistup\033[0m [options] -- [ default: "", describe: "an optional description for your gist" }) + .options("interactive", { + alias: "i", + default: false, + describe: "request confirmation of every file before adding" + }) .options("open", { default: true, describe: "open the created gist in your web browser" @@ -54,10 +59,11 @@ queue(1) .defer(gitListUntrackedFiles) .await(function(error, settings, _, _, files) { unless(error) + .defer(confirmFiles, files) .defer(gitAdd, files) .defer(gitCommit) .defer(createGist, settings.token) - .await(function(error, _, _, id) { + .await(function(error, _, _, _, id) { unless(error) .defer(gitRemoteAdd, id) .defer(gitPush) @@ -148,10 +154,37 @@ function gitRemoteOriginDoesNotExist(callback) { function gitListUntrackedFiles(callback) { if (argv._.length) return void callback(null, argv._); - child.exec("git ls-files ls-files --others --exclude-standard --directory -x '*/'", function(error, stdout, stderr) { + child.exec("git ls-files --others --exclude-standard --directory -x '*/'", function(error, stdout, stderr) { if (!error && stderr) process.stderr.write(stderr), error = new Error("git ls-files failed."); - callback(error, error ? null : stdout.split(os.EOL)); + callback(error, error ? null : stdout.trim().split(os.EOL)); + }); +} + +function confirmFiles(files, callback) { + if (!argv.interactive) return void callback(null); + var readin = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + + var q = queue(1); + + files.forEach(function(file, index) { + q.defer(confirmFile, file, index); + }); + + q.awaitAll(function(error) { + readin.close(); + callback(error); }); + + function confirmFile(file, index, callback) { + readin.question("add " + file + "? ", function(answer) { + if (/^y|yes$/i.test(answer)) return void callback(null); + if (/^n|no$/i.test(answer)) return files.splice(index, 1), void callback(null); + confirmFile(file, index, callback); + }); + } } function gitAdd(files, callback) { diff --git a/package.json b/package.json index 28022e7..e7e9ddf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gistup", - "version": "0.0.6", + "version": "0.0.7", "description": "Initialize a gist from the command-line.", "keywords": [ "gist",