Browse files

Added cli-confim.

No tests, since just requiring readline seems to hang the test runner.
  • Loading branch information...
1 parent 462c746 commit 70ab1fc8d6436fff567f462dfab825bb47bd7fd1 @magnars committed Feb 11, 2012
Showing with 22 additions and 3 deletions.
  1. +1 −3 bin/autolint
  2. +21 −0 lib/cli-confirm.js
View
4 bin/autolint
@@ -2,16 +2,14 @@
var startAutolint = require('../lib/start-autolint');
var configuration = require('../lib/configuration');
-//var cliConfirm = require("../lib/cli-confirm");
+var cliConfirm = require("../lib/cli-confirm");
var util = require('util');
if (configuration.exists()) {
startAutolint(configuration.load());
} else {
- util.puts("");
util.puts("No configuration file (autolint.js) found in this directory.");
util.puts("Autolint can create a default config file for you.");
- util.puts("");
cliConfirm("Create default autolint.js in current directory?", function () {
configuration.createDefaultConfigFile();
util.puts("");
View
21 lib/cli-confirm.js
@@ -0,0 +1,21 @@
+function prompt(q, callback) {
+ var rl = require("readline");
+ var i = rl.createInterface(process.stdin, process.stdout, null);
+ i.question(q, function (a) {
+ i.close();
+ process.stdin.destroy();
+ callback(a);
+ });
+}
+
+function confirmation(answer) {
+ return answer.trim().substring(0, 1).toLowerCase() === "y";
+}
+
+module.exports = function (q, callback) {
+ prompt(q + " [y/N]: ", function (a) {
+ if (confirmation(a)) {
+ callback();
+ }
+ });
+};

0 comments on commit 70ab1fc

Please sign in to comment.