Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit b4927ad4daefbd655ebd2401aee3fb7d24a1ff10 @satazor satazor committed Jan 26, 2013
Showing with 559 additions and 0 deletions.
  1. +54 −0 .gitignore
  2. +63 −0 .jshintrc
  3. +19 −0 LICENSE
  4. 0 README.md
  5. +149 −0 index.js
  6. +28 −0 package.json
  7. +246 −0 test/test.js
@@ -0,0 +1,54 @@
+# Compiled source #
+###################
+*.com
+*.class
+*.dll
+*.exe
+*.o
+*.so
+
+# OS generated files #
+######################
+.DS_Store*
+ehthumbs.db
+Icon?
+Thumbs.db
+Desktop.ini
+.Spotlight-V100
+.Trashes
+._*
+
+# NetBeans #
+############
+nbsettings/
+nbproject/
+nbbuild/
+nbdist/
+nbactions.xml
+nb-configuration.xml
+
+# Textmate #
+############
+*.tmproj
+*.tmproject
+tmtags
+
+# VIM #
+#######
+.*.sw[a-z]
+*.un~
+Session.vim
+
+# Sublime TextEditor #
+######################
+*.sublime-project
+
+# Eclipse #
+######################
+/.project
+/.settings
+
+# Node #
+######################
+node_modules
+npm-debug.*
@@ -0,0 +1,63 @@
+{
+ "predef": [
+ "console",
+ "require",
+ "define",
+ "describe",
+ "it",
+ "before",
+ "beforEach",
+ "after",
+ "afterEach"
+ ],
+
+ "node": true,
+ "devel": true,
+
+ "bitwise": true,
+ "curly": true,
+ "eqeqeq": true,
+ "forin": false,
+ "immed": true,
+ "latedef": false,
+ "newcap": true,
+ "noarg": true,
+ "noempty": false,
+ "nonew": true,
+ "plusplus": false,
+ "regexp": true,
+ "undef": true,
+ "unused": true,
+ "quotmark": "single",
+ "strict": true,
+ "trailing": true,
+
+ "asi": false,
+ "boss": false,
+ "debug": false,
+ "eqnull": true,
+ "es5": false,
+ "esnext": false,
+ "evil": false,
+ "expr": false,
+ "funcscope": false,
+ "globalstrict": false,
+ "iterator": false,
+ "lastsemic": false,
+ "laxbreak": false,
+ "laxcomma": false,
+ "loopfunc": true,
+ "multistr": false,
+ "onecase": true,
+ "regexdash": false,
+ "scripturl": false,
+ "smarttabs": false,
+ "shadow": false,
+ "sub": false,
+ "supernew": false,
+ "validthis": false,
+
+ "nomen": false,
+ "onevar": false,
+ "white": true
+}
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2012 IndigoUnited
+
+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.
No changes.
149 index.js
@@ -0,0 +1,149 @@
+'use strict';
+
+var readline = require('readline');
+var promptly = module.exports;
+
+promptly.prompt = function (message, opts, fn) {
+ // Arguments parsing
+ if (typeof opts === 'function') {
+ fn = opts;
+ opts = {};
+ } else {
+ opts = opts || {};
+ }
+
+ if (opts.trim === undefined) {
+ opts.trim = true;
+ }
+
+ // Instantiate node's readline
+ var rl = opts.rl;
+ if (!rl) {
+ rl = opts.rl = readline.createInterface({
+ input: opts.input || process.stdin,
+ output: opts.output || process.stdout
+ });
+ }
+
+ // Use readline question
+ rl.question(message, function (response) {
+ // Trim?
+ if (opts.trim) {
+ response = response.trim();
+ }
+
+ // Mandatory?
+ if (opts['default'] === undefined && !response) {
+ return promptly.prompt(message, opts, fn);
+ } else {
+ response = response || opts['default'];
+ }
+
+ // Validator verification
+ if (opts.validator) {
+ if (!Array.isArray(opts.validator)) {
+ opts.validator = [opts.validator];
+ }
+
+ var x;
+ var length = opts.validator.length;
+
+ for (x = 0; x < length; x += 1) {
+ try {
+ response = opts.validator[x](response);
+ } catch (e) {
+ // Retry automatically if the retry option is enabled
+ if (opts.retry) {
+ return promptly.prompt(message, opts, fn);
+ }
+
+ rl.close();
+ delete opts.rl;
+ e.retry = promptly.prompt.bind(promptly, message, opts, fn);
+
+ return fn(e);
+ }
+ }
+ }
+
+ // Everything ok
+ rl.close();
+ fn(null, response);
+ });
+};
+
+promptly.password = function () {
+ // TODO:
+};
+
+promptly.confirm = function (message, opts, fn) {
+ // Arguments parsing
+ if (typeof opts === 'function') {
+ fn = opts;
+ opts = {};
+ } else {
+ opts = opts || {};
+ }
+
+ opts.validator = opts.validator || [];
+ if (!Array.isArray(opts.validator)) {
+ opts.validator = [opts.validator];
+ }
+
+ // Push the validator that will coerse boolean values
+ var validator = function (value) {
+ if (typeof value === 'string') {
+ value = value.toLowerCase();
+ }
+
+ switch (value) {
+ case 'y':
+ case 'yes':
+ case '1':
+ case true:
+ return true;
+ case 'n':
+ case 'no':
+ case '0':
+ case false:
+ return false;
+ }
+
+ return value;
+ };
+ opts.validator.push(validator);
+
+ // Use choose with true, false
+ promptly.choose(message, [true, false], opts, fn);
+};
+
+promptly.choose = function (message, choices, opts, fn) {
+ // Arguments parsing
+ if (typeof opts === 'function') {
+ fn = opts;
+ opts = {};
+ } else {
+ opts = opts || {};
+ }
+
+ if (opts.retry === undefined) {
+ opts.retry = true;
+ }
+ opts.validator = opts.validator || [];
+ if (!Array.isArray(opts.validator)) {
+ opts.validator = [opts.validator];
+ }
+
+ // Push the choice validator
+ var validator = function (value) {
+ if (choices.indexOf(value) === -1) {
+ throw new Error('Invalid choice: ' + value);
+ }
+
+ return value;
+ };
+ opts.validator.push(validator);
+
+ // Use prompt()
+ promptly.prompt(message, opts, fn);
+};
@@ -0,0 +1,28 @@
+{
+ "name": "promptly",
+ "version": "0.0.0",
+ "description": "Simple command line prompting utility",
+ "main": "index.js",
+ "devDependencies": {
+ "mocha": "~1.8.1",
+ "async": "~0.1.22",
+ "expect.js": "~0.2.0"
+ },
+ "scripts": {
+ "test": "mocha -R spec"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/IndigoUnited/node-promptly"
+ },
+ "keywords": [
+ "prompt",
+ "choose",
+ "choice",
+ "cli",
+ "command",
+ "line"
+ ],
+ "author": "IndigoUnited <hello@indigounited.com> (http://indigounited.com)",
+ "license": "MIT"
+}
Oops, something went wrong.

0 comments on commit b4927ad

Please sign in to comment.