diff options
Diffstat (limited to 'node_modules/prompt')
29 files changed, 3524 insertions, 0 deletions
diff --git a/node_modules/prompt/.jshintrc b/node_modules/prompt/.jshintrc new file mode 100644 index 0000000..0d97986 --- /dev/null +++ b/node_modules/prompt/.jshintrc @@ -0,0 +1,54 @@ +{ + "passfail": false, + "maxerr": 100, + + "browser": false, + "node": true, + "rhino": false, + "couch": true, + "wsh": true, + "jquery": true, + "prototypejs": false, + "mootools": false, + "dojo": false, + + "devel": false, + + "es5": true, + "strict": false, + "globalstrict": false, + + "asi": false, + "lastsemic": true, + "laxbreak": true, + "laxcomma": false, + "bitwise": false, + "boss": false, + "curly": true, + "eqeqeq": true, + "eqnull": false, + "evil": false, + "expr": false, + "forin": false, + "immed": false, + "latedef": false, + "loopfunc": true, + "noarg": true, + "regexp": true, + "regexdash": false, + "scripturl": true, + "shadow": true, + "supernew": true, + "undef": true, + + "newcap": true, + "noempty": true, + "nonew": true, + "nomen": false, + "onevar": true, + "plusplus": false, + "sub": true, + "trailing": true, + "white": false, + "indent": 2 +}
\ No newline at end of file diff --git a/node_modules/prompt/.npmignore b/node_modules/prompt/.npmignore new file mode 100644 index 0000000..e3bc275 --- /dev/null +++ b/node_modules/prompt/.npmignore @@ -0,0 +1,3 @@ +node_modules/ +node_modules/* +npm-debug.log
\ No newline at end of file diff --git a/node_modules/prompt/.travis.yml b/node_modules/prompt/.travis.yml new file mode 100644 index 0000000..9473f32 --- /dev/null +++ b/node_modules/prompt/.travis.yml @@ -0,0 +1,8 @@ +language: node_js +node_js: + - "node" + - "0.12" + - "4" + - "5" + +sudo: false diff --git a/node_modules/prompt/CHANGELOG.md b/node_modules/prompt/CHANGELOG.md new file mode 100644 index 0000000..b094c48 --- /dev/null +++ b/node_modules/prompt/CHANGELOG.md @@ -0,0 +1,13 @@ + +0.2.7 / 2012-08-30 +================== + + * Fixed handling of numeric inputs with parseFloat + * Fixed overwriting of non-string inputs + * Added support for boolean types + +0.2.6 / 2012-08-12 +================== + + * Added allowance of empty default values + diff --git a/node_modules/prompt/LICENSE b/node_modules/prompt/LICENSE new file mode 100644 index 0000000..56217ca --- /dev/null +++ b/node_modules/prompt/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2010 Nodejitsu Inc. + +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 newline at end of file diff --git a/node_modules/prompt/README.md b/node_modules/prompt/README.md new file mode 100644 index 0000000..2fb9ffb --- /dev/null +++ b/node_modules/prompt/README.md @@ -0,0 +1,444 @@ +# prompt [![Build Status](https://secure.travis-ci.org/flatiron/prompt.svg)](http://travis-ci.org/flatiron/prompt) + +A beautiful command-line prompt for node.js + +## Features + +* prompts the user for input +* supports validation and defaults +* hides passwords + +## Usage +Using prompt is relatively straight forward. There are two core methods you should be aware of: `prompt.get()` and `prompt.addProperties()`. Their methods take strings representing property names in addition to objects for complex property validation (and more). There are a number of [examples][0] that you should examine for detailed usage. + +### Getting Basic Prompt Information +Getting started with `prompt` is easy. Lets take a look at `examples/simple-prompt.js`: + +``` js + var prompt = require('prompt'); + + // + // Start the prompt + // + prompt.start(); + + // + // Get two properties from the user: username and email + // + prompt.get(['username', 'email'], function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' username: ' + result.username); + console.log(' email: ' + result.email); + }); +``` + +This will result in the following command-line output: + +``` + $ node examples/simple-prompt.js + prompt: username: some-user + prompt: email: some-user@some-place.org + Command-line input received: + username: some-user + email: some-user@some-place.org +``` + +### Prompting with Validation, Default Values, and More (Complex Properties) +In addition to prompting the user with simple string prompts, there is a robust API for getting and validating complex information from a command-line prompt. Here's a quick sample: + +``` js + var schema = { + properties: { + name: { + pattern: /^[a-zA-Z\s\-]+$/, + message: 'Name must be only letters, spaces, or dashes', + required: true + }, + password: { + hidden: true + } + } + }; + + // + // Start the prompt + // + prompt.start(); + + // + // Get two properties from the user: email, password + // + prompt.get(schema, function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' name: ' + result.name); + console.log(' password: ' + result.password); + }); +``` + +Pretty easy right? The output from the above script is: + +``` + $ node examples/property-prompt.js + prompt: name: nodejitsu000 + error: Invalid input for name + error: Name must be only letters, spaces, or dashes + prompt: name: Nodejitsu Inc + prompt: password: + Command-line input received: + name: Nodejitsu Inc + password: some-password +``` + +## Valid Property Settings +`prompt` understands JSON-schema with a few extra parameters and uses [revalidator](https://github.com/flatiron/revalidator) for validation. + +Here's an overview of the properties that may be used for validation and prompting controls: + +``` js + { + description: 'Enter your password', // Prompt displayed to the user. If not supplied name will be used. + type: 'string', // Specify the type of input to expect. + pattern: /^\w+$/, // Regular expression that input must be valid against. + message: 'Password must be letters', // Warning message to display if validation fails. + hidden: true, // If true, characters entered will either not be output to console or will be outputed using the `replace` string. + replace: '*', // If `hidden` is set it will replace each hidden character with the specified string. + default: 'lamepassword', // Default value to use if no value is entered. + required: true // If true, value entered must be non-empty. + before: function(value) { return 'v' + value; } // Runs before node-prompt callbacks. It modifies user's input + } +``` + +Alternatives to `pattern` include `format` and `conform`, as documented in [revalidator](https://github.com/flatiron/revalidator). + +Supported types are `string`, `boolean`, `number`, `integer`, `array` + +Using `type: 'boolean'` accepts case insensitive values 'true', 't', 'false', 'f' + +Using `type: 'array'` has some special cases. + +- `description` will not work in the schema if `type: 'array'` is defined. +- `maxItems` takes precedence over `minItems`. +- Arrays that do not have `maxItems` defined will require users to `SIGINT` (`^C`) before the array is ended. +- If `SIGINT` (`^C`) is triggered before `minItems` is met, a validation error will appear. This will require users to `SIGEOF` (`^D`) to end the input. + +For more information on things such as `maxItems` and `minItems`, refer to the [revalidator](https://github.com/flatiron/revalidator) repository. + +### Alternate Validation API: + +Prompt, in addition to iterating over JSON-Schema properties, will also happily iterate over an array of validation objects given an extra 'name' property: + +```js + var prompt = require('../lib/prompt'); + + // + // Start the prompt + // + prompt.start(); + + // + // Get two properties from the user: username and password + // + prompt.get([{ + name: 'username', + required: true + }, { + name: 'password', + hidden: true, + conform: function (value) { + return true; + } + }], function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' username: ' + result.username); + console.log(' password: ' + result.password); + }); +``` + +### Backward Compatibility + +Note that, while this structure is similar to that used by prompt 0.1.x, that the object properties use the same names as in JSON-Schema. prompt 0.2.x is backward compatible with prompt 0.1.x except for asynchronous validation. + +### Skipping Prompts + +Sometimes power users may wish to skip prompts and specify all data as command line options. +if a value is set as a property of `prompt.override` prompt will use that instead of +prompting the user. + +``` js + //prompt-override.js + + var prompt = require('prompt'), + optimist = require('optimist') + + // + // set the overrides + // + prompt.override = optimist.argv + + // + // Start the prompt + // + prompt.start(); + + // + // Get two properties from the user: username and email + // + prompt.get(['username', 'email'], function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' username: ' + result.username); + console.log(' email: ' + result.email); + }) + + //: node prompt-override.js --username USER --email EMAIL +``` + +It is also possible to skip prompts dynamically based on previous prompts. +If an `ask` method is added, prompt will use it to determine if the prompt should be displayed. +If `ask` returns true the prompt is displayed. otherwise, the default value or empty string are used. + +``` js + var schema = { + properties: { + proxy: { + description: 'Proxy url', + }, + proxyCredentials: { + description: 'Proxy credentials', + ask: function() { + // only ask for proxy credentials if a proxy was set + return prompt.history('proxy').value > 0; + } + } + } + }; + + // + // Start the prompt + // + prompt.start(); + + // + // Get one or two properties from the user, depending on + // what the user answered for proxy + // + prompt.get(schema, function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' proxy: ' + result.proxy); + console.log(' credentials: ' + result.proxyCredentials); + }); +``` + + +### Adding Properties to an Object +A common use-case for prompting users for data from the command-line is to extend or create a configuration object that is passed onto the entry-point method for your CLI tool. `prompt` exposes a convenience method for doing just this: + +``` js + var obj = { + password: 'lamepassword', + mindset: 'NY' + } + + // + // Log the initial object. + // + console.log('Initial object to be extended:'); + console.dir(obj); + + // + // Add two properties to the empty object: username and email + // + prompt.addProperties(obj, ['username', 'email'], function (err) { + // + // Log the results. + // + console.log('Updated object received:'); + console.dir(obj); + }); +``` + +### Prompt history +You can use the `prompt.history()` method to get access to previous prompt input. + +``` js + prompt.get([{ + name: 'name', + description: 'Your name', + type: 'string', + required: true + }, { + name: 'surname', + description: 'Your surname', + type: 'string', + required: true, + message: 'Please dont use the demo credentials', + conform: function(surname) { + var name = prompt.history('name').value; + return (name !== 'John' || surname !== 'Smith'); + } + }], function(err, results) { + console.log(results); + }); +``` + +## Customizing your prompt +Aside from changing `property.message`, you can also change `prompt.message` +and `prompt.delimiter` to change the appearance of your prompt. + +The basic structure of a prompt is this: + +``` js +prompt.message + prompt.delimiter + property.message + prompt.delimiter; +``` + +The default `prompt.message` is "prompt," the default `prompt.delimiter` is +": ", and the default `property.message` is `property.name`. +Changing these allows you to customize the appearance of your prompts! In +addition, prompt supports ANSI color codes via the +[colors module](https://github.com/Marak/colors.js) for custom colors. For a +very colorful example: + +``` js + var prompt = require("prompt"); + var colors = require("colors/safe"); + // + // Setting these properties customizes the prompt. + // + prompt.message = colors.rainbow("Question!"); + prompt.delimiter = colors.green("><"); + + prompt.start(); + + prompt.get({ + properties: { + name: { + description: colors.magenta("What is your name?") + } + } + }, function (err, result) { + console.log(colors.cyan("You said your name is: " + result.name)); + }); +``` + +If you don't want colors, you can set + +```js +var prompt = require('prompt'); + +prompt.colors = false; +``` + +## Integration with streamlinejs + +When integrating prompt with projects using streamlinejs such as the following + +``` +prompt.start(); +function test_prompt(_){ + console.log(prompt.get(loadDataValues(), _).output); +} +test_prompt(_); +``` + +This will work, however the process is then stuck with a stdin stream still open. If you setup the traditional way (with callback) such as this + + ``` +prompt.start(); +function test_prompt(){ + prompt.get(loadDataValues(), function(err, data){ + console.log(data.output); + }); +} +test_prompt(); +``` +This works and ends correctly. + +To resolve this we have added a new method to prompt, which will stop the stdin stream + +``` +// +// ### function stop () +// Stops input coming in from stdin +// +prompt.stop = function () { + if (prompt.stopped || !prompt.started) { + return; + } + + stdin.destroy(); + prompt.emit('stop'); + prompt.stopped = true; + prompt.started = false; + prompt.paused = false; + return prompt; +} +``` + +And you can find an example in the example folder `examples/prompt-streamline.js` + +``` +/* + * prompt-streamline._js: Example of how to use prompt with streamlinejs. + * + * calling syntax: _node prompt-streamline._js + * + */ +var prompt = require('../lib/prompt'); + +function getSampleData(){ + return [ + { + name: 'username', + message: 'Enter a username' + } + ]; +}; + +// +// Start the prompt +// +prompt.start(); + +function get_username_prompt(_){ + console.log(prompt.get(getSampleData(), _).username); +} + +get_username_prompt(_); + +// +// Clean the prompt +// +prompt.stop(); +``` + +## Installation + +``` bash + $ [sudo] npm install prompt +``` + +## Running tests + +``` bash + $ npm test +``` + +#### License: MIT +#### Author: [Charlie Robbins](http://github.com/indexzero) +#### Contributors: [Josh Holbrook](http://github.com/jesusabdullah), [Pavan Kumar Sunkara](http://github.com/pksunkara) + +[0]: https://github.com/flatiron/prompt/tree/master/examples diff --git a/node_modules/prompt/docs/docco.css b/node_modules/prompt/docs/docco.css new file mode 100644 index 0000000..bd54134 --- /dev/null +++ b/node_modules/prompt/docs/docco.css @@ -0,0 +1,194 @@ +/*--------------------- Layout and Typography ----------------------------*/ +body { + font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif; + font-size: 15px; + line-height: 22px; + color: #252519; + margin: 0; padding: 0; +} +a { + color: #261a3b; +} + a:visited { + color: #261a3b; + } +p { + margin: 0 0 15px 0; +} +h4, h5, h6 { + color: #333; + margin: 6px 0 6px 0; + font-size: 13px; +} + h2, h3 { + margin-bottom: 0; + color: #000; + } + h1 { + margin-top: 40px; + margin-bottom: 15px; + color: #000; + } +#container { + position: relative; +} +#background { + position: fixed; + top: 0; left: 525px; right: 0; bottom: 0; + background: #f5f5ff; + border-left: 1px solid #e5e5ee; + z-index: -1; +} +#jump_to, #jump_page { + background: white; + -webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777; + -webkit-border-bottom-left-radius: 5px; -moz-border-radius-bottomleft: 5px; + font: 10px Arial; + text-transform: uppercase; + cursor: pointer; + text-align: right; +} +#jump_to, #jump_wrapper { + position: fixed; + right: 0; top: 0; + padding: 5px 10px; +} + #jump_wrapper { + padding: 0; + display: none; + } + #jump_to:hover #jump_wrapper { + display: block; + } + #jump_page { + padding: 5px 0 3px; + margin: 0 0 25px 25px; + } + #jump_page .source { + display: block; + padding: 5px 10px; + text-decoration: none; + border-top: 1px solid #eee; + } + #jump_page .source:hover { + background: #f5f5ff; + } + #jump_page .source:first-child { + } +table td { + border: 0; + outline: 0; +} + td.docs, th.docs { + max-width: 450px; + min-width: 450px; + min-height: 5px; + padding: 10px 25px 1px 50px; + overflow-x: hidden; + vertical-align: top; + text-align: left; + } + .docs pre { + margin: 15px 0 15px; + padding-left: 15px; + } + .docs p tt, .docs p code { + background: #f8f8ff; + border: 1px solid #dedede; + font-size: 12px; + padding: 0 0.2em; + } + .pilwrap { + position: relative; + } + .pilcrow { + font: 12px Arial; + text-decoration: none; + color: #454545; + position: absolute; + top: 3px; left: -20px; + padding: 1px 2px; + opacity: 0; + -webkit-transition: opacity 0.2s linear; + } + td.docs:hover .pilcrow { + opacity: 1; + } + td.code, th.code { + padding: 14px 15px 16px 25px; + width: 100%; + vertical-align: top; + background: #f5f5ff; + border-left: 1px solid #e5e5ee; + } + pre, tt, code { + font-size: 12px; line-height: 18px; + font-family: Menlo, Monaco, Consolas, "Lucida Console", monospace; + margin: 0; padding: 0; + } + + +/*---------------------- Syntax Highlighting -----------------------------*/ +td.linenos { background-color: #f0f0f0; padding-right: 10px; } +span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; } +body .hll { background-color: #ffffcc } +body .c { color: #408080; font-style: italic } /* Comment */ +body .err { border: 1px solid #FF0000 } /* Error */ +body .k { color: #954121 } /* Keyword */ +body .o { color: #666666 } /* Operator */ +body .cm { color: #408080; font-style: italic } /* Comment.Multiline */ +body .cp { color: #BC7A00 } /* Comment.Preproc */ +body .c1 { color: #408080; font-style: italic } /* Comment.Single */ +body .cs { color: #408080; font-style: italic } /* Comment.Special */ +body .gd { color: #A00000 } /* Generic.Deleted */ +body .ge { font-style: italic } /* Generic.Emph */ +body .gr { color: #FF0000 } /* Generic.Error */ +body .gh { color: #000080; font-weight: bold } /* Generic.Heading */ +body .gi { color: #00A000 } /* Generic.Inserted */ +body .go { color: #808080 } /* Generic.Output */ +body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ +body .gs { font-weight: bold } /* Generic.Strong */ +body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ +body .gt { color: #0040D0 } /* Generic.Traceback */ +body .kc { color: #954121 } /* Keyword.Constant */ +body .kd { color: #954121; font-weight: bold } /* Keyword.Declaration */ +body .kn { color: #954121; font-weight: bold } /* Keyword.Namespace */ +body .kp { color: #954121 } /* Keyword.Pseudo */ +body .kr { color: #954121; font-weight: bold } /* Keyword.Reserved */ +body .kt { color: #B00040 } /* Keyword.Type */ +body .m { color: #666666 } /* Literal.Number */ +body .s { color: #219161 } /* Literal.String */ +body .na { color: #7D9029 } /* Name.Attribute */ +body .nb { color: #954121 } /* Name.Builtin */ +body .nc { color: #0000FF; font-weight: bold } /* Name.Class */ +body .no { color: #880000 } /* Name.Constant */ +body .nd { color: #AA22FF } /* Name.Decorator */ +body .ni { color: #999999; font-weight: bold } /* Name.Entity */ +body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ +body .nf { color: #0000FF } /* Name.Function */ +body .nl { color: #A0A000 } /* Name.Label */ +body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ +body .nt { color: #954121; font-weight: bold } /* Name.Tag */ +body .nv { color: #19469D } /* Name.Variable */ +body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ +body .w { color: #bbbbbb } /* Text.Whitespace */ +body .mf { color: #666666 } /* Literal.Number.Float */ +body .mh { color: #666666 } /* Literal.Number.Hex */ +body .mi { color: #666666 } /* Literal.Number.Integer */ +body .mo { color: #666666 } /* Literal.Number.Oct */ +body .sb { color: #219161 } /* Literal.String.Backtick */ +body .sc { color: #219161 } /* Literal.String.Char */ +body .sd { color: #219161; font-style: italic } /* Literal.String.Doc */ +body .s2 { color: #219161 } /* Literal.String.Double */ +body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ +body .sh { color: #219161 } /* Literal.String.Heredoc */ +body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ +body .sx { color: #954121 } /* Literal.String.Other */ +body .sr { color: #BB6688 } /* Literal.String.Regex */ +body .s1 { color: #219161 } /* Literal.String.Single */ +body .ss { color: #19469D } /* Literal.String.Symbol */ +body .bp { color: #954121 } /* Name.Builtin.Pseudo */ +body .vc { color: #19469D } /* Name.Variable.Class */ +body .vg { color: #19469D } /* Name.Variable.Global */ +body .vi { color: #19469D } /* Name.Variable.Instance */ +body .il { color: #666666 } /* Literal.Number.Integer.Long */
\ No newline at end of file diff --git a/node_modules/prompt/docs/prompt.html b/node_modules/prompt/docs/prompt.html new file mode 100644 index 0000000..7776f5b --- /dev/null +++ b/node_modules/prompt/docs/prompt.html @@ -0,0 +1,296 @@ +<!DOCTYPE html> <html> <head> <title>prompt.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> prompt.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="cm">/*</span> +<span class="cm"> * prompt.js: Simple prompt for prompting information from the command line </span> +<span class="cm"> *</span> +<span class="cm"> * (C) 2010, Nodejitsu Inc.</span> +<span class="cm"> *</span> +<span class="cm"> */</span> + +<span class="kd">var</span> <span class="nx">events</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'events'</span><span class="p">),</span> + <span class="nx">async</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'async'</span><span class="p">),</span> + <span class="nx">colors</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'colors'</span><span class="p">),</span> + <span class="nx">winston</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'winston'</span><span class="p">),</span> + <span class="nx">stdio</span> <span class="o">=</span> <span class="nx">process</span><span class="p">.</span><span class="nx">binding</span><span class="p">(</span><span class="s1">'stdio'</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <h3>@private function capitalize (str)</h3> + +<h4>str {string} String to capitalize</h4> + +<p>Capitalizes the string supplied.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">capitalize</span><span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">{</span> + <span class="k">return</span> <span class="nx">str</span><span class="p">.</span><span class="nx">charAt</span><span class="p">(</span><span class="mi">0</span><span class="p">).</span><span class="nx">toUpperCase</span><span class="p">()</span> <span class="o">+</span> <span class="nx">str</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> +<span class="p">}</span> + +<span class="kd">var</span> <span class="nx">prompt</span> <span class="o">=</span> <span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">create</span><span class="p">(</span><span class="nx">events</span><span class="p">.</span><span class="nx">EventEmitter</span><span class="p">.</span><span class="nx">prototype</span><span class="p">);</span> + +<span class="kd">var</span> <span class="nx">logger</span> <span class="o">=</span> <span class="nx">prompt</span><span class="p">.</span><span class="nx">logger</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">winston</span><span class="p">.</span><span class="nx">Logger</span><span class="p">({</span> + <span class="nx">transports</span><span class="o">:</span> <span class="p">[</span> + <span class="k">new</span> <span class="p">(</span><span class="nx">winston</span><span class="p">.</span><span class="nx">transports</span><span class="p">.</span><span class="nx">Console</span><span class="p">)()</span> + <span class="p">]</span> +<span class="p">});</span> + +<span class="nx">prompt</span><span class="p">.</span><span class="nx">started</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span> +<span class="nx">prompt</span><span class="p">.</span><span class="nx">paused</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span> +<span class="nx">prompt</span><span class="p">.</span><span class="nx">allowEmpty</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span> + +<span class="kd">var</span> <span class="nx">stdin</span><span class="p">,</span> <span class="nx">stdout</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <p>Create an empty object for the properties +known to <code>prompt</code></p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">prompt</span><span class="p">.</span><span class="nx">properties</span> <span class="o">=</span> <span class="p">{};</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <p>Setup the default winston logger to use +the <code>cli</code> levels and colors.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">logger</span><span class="p">.</span><span class="nx">cli</span><span class="p">();</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <h3>function start (options)</h3> + +<h4>@options {Object} <strong>Optional</strong> Options to consume by prompt</h4> + +<p>Starts the prompt by listening to the appropriate events on <code>options.stdin</code> +and <code>options.stdout</code>. If no streams are supplied, then <code>process.stdin</code> +and <code>process.stdout</code> are used, respectively.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">prompt</span><span class="p">.</span><span class="nx">start</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span> + <span class="k">if</span> <span class="p">(</span><span class="nx">prompt</span><span class="p">.</span><span class="nx">started</span><span class="p">)</span> <span class="p">{</span> + <span class="k">return</span><span class="p">;</span> + <span class="p">}</span> + + <span class="nx">options</span> <span class="o">=</span> <span class="nx">options</span> <span class="o">||</span> <span class="p">{};</span> + <span class="nx">stdin</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">stdin</span> <span class="o">||</span> <span class="nx">process</span><span class="p">.</span><span class="nx">openStdin</span><span class="p">();</span> + <span class="nx">stdout</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">stdout</span> <span class="o">||</span> <span class="nx">process</span><span class="p">.</span><span class="nx">stdout</span><span class="p">;</span> + + <span class="nx">prompt</span><span class="p">.</span><span class="nx">allowEmpty</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">allowEmpty</span> <span class="o">||</span> <span class="kc">false</span><span class="p">;</span> + + <span class="nx">process</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'SIGINT'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span> + <span class="nx">stdout</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="s1">'\n'</span><span class="p">);</span> + <span class="nx">process</span><span class="p">.</span><span class="nx">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> + <span class="p">})</span> + + <span class="nx">prompt</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'start'</span><span class="p">);</span> + <span class="nx">prompt</span><span class="p">.</span><span class="nx">started</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span> + <span class="k">return</span> <span class="nx">prompt</span><span class="p">;</span> +<span class="p">};</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <h3>function pause ()</h3> + +<p>Pauses input coming in from stdin</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">prompt</span><span class="p">.</span><span class="nx">pause</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span> + <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">prompt</span><span class="p">.</span><span class="nx">started</span> <span class="o">||</span> <span class="nx">prompt</span><span class="p">.</span><span class="nx">paused</span><span class="p">)</span> <span class="p">{</span> + <span class="k">return</span><span class="p">;</span> + <span class="p">}</span> + + <span class="nx">stdin</span><span class="p">.</span><span class="nx">pause</span><span class="p">();</span> + <span class="nx">prompt</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'pause'</span><span class="p">);</span> + <span class="nx">prompt</span><span class="p">.</span><span class="nx">paused</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span> + <span class="k">return</span> <span class="nx">prompt</span><span class="p">;</span> +<span class="p">};</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <h3>function resume ()</h3> + +<p>Resumes input coming in from stdin </p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">prompt</span><span class="p">.</span><span class="nx">resume</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span> + <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">prompt</span><span class="p">.</span><span class="nx">started</span> <span class="o">||</span> <span class="o">!</span><span class="nx">prompt</span><span class="p">.</span><span class="nx">paused</span><span class="p">)</span> <span class="p">{</span> + <span class="k">return</span><span class="p">;</span> + <span class="p">}</span> + + <span class="nx">stdin</span><span class="p">.</span><span class="nx">resume</span><span class="p">();</span> + <span class="nx">prompt</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'resume'</span><span class="p">);</span> + <span class="nx">prompt</span><span class="p">.</span><span class="nx">paused</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span> + <span class="k">return</span> <span class="nx">prompt</span><span class="p">;</span> +<span class="p">};</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <h3>function get (msg, [validator,] callback)</h3> + +<h4>@msg {Array|Object|string} Set of variables to get input for.</h4> + +<h4>@callback {function} Continuation to pass control to when complete.</h4> + +<p>Gets input from the user via stdin for the specified message(s) <code>msg</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">prompt</span><span class="p">.</span><span class="nx">get</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">msg</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span> + <span class="kd">var</span> <span class="nx">vars</span> <span class="o">=</span> <span class="o">!</span><span class="nb">Array</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">msg</span><span class="p">)</span> <span class="o">?</span> <span class="p">[</span><span class="nx">msg</span><span class="p">]</span> <span class="o">:</span> <span class="nx">msg</span><span class="p">,</span> + <span class="nx">result</span> <span class="o">=</span> <span class="p">{};</span> + + <span class="nx">vars</span> <span class="o">=</span> <span class="nx">vars</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">v</span><span class="p">)</span> <span class="p">{</span> + <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">v</span> <span class="o">===</span> <span class="s1">'string'</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">v</span> <span class="o">=</span> <span class="nx">v</span><span class="p">.</span><span class="nx">toLowerCase</span><span class="p">();</span> + <span class="p">}</span> + + <span class="k">return</span> <span class="nx">prompt</span><span class="p">.</span><span class="nx">properties</span><span class="p">[</span><span class="nx">v</span><span class="p">]</span> <span class="o">||</span> <span class="nx">v</span><span class="p">;</span> + <span class="p">});</span> + + <span class="kd">function</span> <span class="nx">get</span><span class="p">(</span><span class="nx">target</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">prompt</span><span class="p">.</span><span class="nx">getInput</span><span class="p">(</span><span class="nx">target</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">line</span><span class="p">)</span> <span class="p">{</span> + <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span> + <span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span> + <span class="p">}</span> + + <span class="kd">var</span> <span class="nx">name</span> <span class="o">=</span> <span class="nx">target</span><span class="p">.</span><span class="nx">name</span> <span class="o">||</span> <span class="nx">target</span><span class="p">;</span> + <span class="nx">result</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">line</span><span class="p">;</span> + <span class="nx">next</span><span class="p">();</span> + <span class="p">});</span> + <span class="p">}</span> + + <span class="nx">async</span><span class="p">.</span><span class="nx">forEachSeries</span><span class="p">(</span><span class="nx">vars</span><span class="p">,</span> <span class="nx">get</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span> + <span class="k">return</span> <span class="nx">err</span> <span class="o">?</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="o">:</span> <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">result</span><span class="p">);</span> + <span class="p">});</span> + + <span class="k">return</span> <span class="nx">prompt</span><span class="p">;</span> +<span class="p">};</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">¶</a> </div> <h3>function getInput (msg, validator, callback)</h3> + +<h4>@msg {Object|string} Variable to get input for.</h4> + +<h4>@callback {function} Continuation to pass control to when complete.</h4> + +<p>Gets input from the user via stdin for the specified message <code>msg</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">prompt</span><span class="p">.</span><span class="nx">getInput</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">prop</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span> + <span class="kd">var</span> <span class="nx">name</span> <span class="o">=</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">message</span> <span class="o">||</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">name</span> <span class="o">||</span> <span class="nx">prop</span><span class="p">,</span> + <span class="nx">raw</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'prompt'</span><span class="p">,</span> <span class="s1">': '</span> <span class="o">+</span> <span class="nx">name</span><span class="p">.</span><span class="nx">grey</span><span class="p">,</span> <span class="s1">': '</span><span class="p">.</span><span class="nx">grey</span><span class="p">],</span> + <span class="nx">read</span> <span class="o">=</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">hidden</span> <span class="o">?</span> <span class="nx">prompt</span><span class="p">.</span><span class="nx">readLineHidden</span> <span class="o">:</span> <span class="nx">prompt</span><span class="p">.</span><span class="nx">readLine</span><span class="p">,</span> + <span class="nx">length</span><span class="p">,</span> <span class="nx">msg</span><span class="p">;</span> + + <span class="k">if</span> <span class="p">(</span><span class="nx">prop</span><span class="p">.</span><span class="k">default</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">raw</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="s1">' ('</span> <span class="o">+</span> <span class="nx">prop</span><span class="p">.</span><span class="k">default</span> <span class="o">+</span> <span class="s1">')'</span><span class="p">);</span> + <span class="p">}</span> + </pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">¶</a> </div> <p>Calculate the raw length and colorize the prompt</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">length</span> <span class="o">=</span> <span class="nx">raw</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">''</span><span class="p">).</span><span class="nx">length</span><span class="p">;</span> + <span class="nx">raw</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="nx">raw</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> + <span class="nx">msg</span> <span class="o">=</span> <span class="nx">raw</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">''</span><span class="p">);</span> + + <span class="k">if</span> <span class="p">(</span><span class="nx">prop</span><span class="p">.</span><span class="nx">help</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">prop</span><span class="p">.</span><span class="nx">help</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">line</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">logger</span><span class="p">.</span><span class="nx">help</span><span class="p">(</span><span class="nx">line</span><span class="p">);</span> + <span class="p">});</span> + <span class="p">}</span> + + <span class="nx">stdout</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">msg</span><span class="p">);</span> + <span class="nx">prompt</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'prompt'</span><span class="p">,</span> <span class="nx">prop</span><span class="p">);</span> + + <span class="nx">read</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">line</span><span class="p">)</span> <span class="p">{</span> + <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span> + <span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span> + <span class="p">}</span> + + <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">line</span> <span class="o">||</span> <span class="nx">line</span> <span class="o">===</span> <span class="s1">''</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">line</span> <span class="o">=</span> <span class="nx">prop</span><span class="p">.</span><span class="k">default</span> <span class="o">||</span> <span class="nx">line</span><span class="p">;</span> + <span class="p">}</span> + + <span class="k">if</span> <span class="p">(</span><span class="nx">prop</span><span class="p">.</span><span class="nx">validator</span> <span class="o">||</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">empty</span> <span class="o">===</span> <span class="kc">false</span><span class="p">)</span> <span class="p">{</span> + <span class="kd">var</span> <span class="nx">valid</span><span class="p">;</span> + + <span class="k">if</span> <span class="p">(</span><span class="nx">prop</span><span class="p">.</span><span class="nx">validator</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">valid</span> <span class="o">=</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">validator</span><span class="p">.</span><span class="nx">test</span> + <span class="o">?</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">validator</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">line</span><span class="p">)</span> + <span class="o">:</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">validator</span><span class="p">(</span><span class="nx">line</span><span class="p">);</span> + <span class="p">}</span> + + <span class="k">if</span> <span class="p">(</span><span class="nx">prop</span><span class="p">.</span><span class="nx">empty</span> <span class="o">===</span> <span class="kc">false</span> <span class="o">&&</span> <span class="nx">valid</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">valid</span> <span class="o">=</span> <span class="nx">line</span><span class="p">.</span><span class="nx">length</span> <span class="o">></span> <span class="mi">0</span><span class="p">;</span> + <span class="nx">prop</span><span class="p">.</span><span class="nx">warning</span> <span class="o">=</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">warning</span> <span class="o">||</span> <span class="s1">'You must supply a value.'</span><span class="p">;</span> + <span class="p">}</span> + + <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">valid</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">logger</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="s1">'Invalid input for '</span> <span class="o">+</span> <span class="nx">name</span><span class="p">.</span><span class="nx">grey</span><span class="p">);</span> + <span class="k">if</span> <span class="p">(</span><span class="nx">prop</span><span class="p">.</span><span class="nx">warning</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">logger</span><span class="p">.</span><span class="nx">error</span><span class="p">(</span><span class="nx">prop</span><span class="p">.</span><span class="nx">warning</span><span class="p">);</span> + <span class="p">}</span> + + <span class="nx">prompt</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'invalid'</span><span class="p">,</span> <span class="nx">prop</span><span class="p">,</span> <span class="nx">line</span><span class="p">);</span> + <span class="k">return</span> <span class="nx">prompt</span><span class="p">.</span><span class="nx">getInput</span><span class="p">(</span><span class="nx">prop</span><span class="p">,</span> <span class="nx">callback</span><span class="p">);</span> + <span class="p">}</span> + <span class="p">}</span> + + <span class="nx">logger</span><span class="p">.</span><span class="nx">input</span><span class="p">(</span><span class="nx">line</span><span class="p">.</span><span class="nx">yellow</span><span class="p">);</span> + <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">line</span><span class="p">);</span> + <span class="p">});</span> + + <span class="k">return</span> <span class="nx">prompt</span><span class="p">;</span> +<span class="p">};</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div> <h3>function addProperties (obj, properties, callback)</h3> + +<h4>@obj {Object} Object to add properties to</h4> + +<h4>@properties {Array} List of properties to get values for</h4> + +<h4>@callback {function} Continuation to pass control to when complete.</h4> + +<p>Prompts the user for values each of the <code>properties</code> if <code>obj</code> does not already +have a value for the property. Responds with the modified object. </p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">prompt</span><span class="p">.</span><span class="nx">addProperties</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">properties</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">properties</span> <span class="o">=</span> <span class="nx">properties</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">prop</span><span class="p">)</span> <span class="p">{</span> + <span class="k">return</span> <span class="k">typeof</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">prop</span><span class="p">]</span> <span class="o">===</span> <span class="s1">'undefined'</span><span class="p">;</span> + <span class="p">});</span> + + <span class="k">if</span> <span class="p">(</span><span class="nx">properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> + <span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">obj</span><span class="p">);</span> + <span class="p">}</span> + + <span class="nx">prompt</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">properties</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">results</span><span class="p">)</span> <span class="p">{</span> + <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span> + <span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span> + <span class="p">}</span> + <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">results</span><span class="p">)</span> <span class="p">{</span> + <span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">obj</span><span class="p">);</span> + <span class="p">}</span> + + <span class="kd">function</span> <span class="nx">putNested</span> <span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">path</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> <span class="p">{</span> + <span class="kd">var</span> <span class="nx">last</span> <span class="o">=</span> <span class="nx">obj</span><span class="p">,</span> <span class="nx">key</span><span class="p">;</span> + + <span class="k">while</span> <span class="p">(</span><span class="nx">path</span><span class="p">.</span><span class="nx">length</span> <span class="o">></span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">key</span> <span class="o">=</span> <span class="nx">path</span><span class="p">.</span><span class="nx">shift</span><span class="p">();</span> + <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">last</span><span class="p">[</span><span class="nx">key</span><span class="p">])</span> <span class="p">{</span> + <span class="nx">last</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">{};</span> + <span class="p">}</span> + + <span class="nx">last</span> <span class="o">=</span> <span class="nx">last</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span> + <span class="p">}</span> + + <span class="nx">last</span><span class="p">[</span><span class="nx">path</span><span class="p">.</span><span class="nx">shift</span><span class="p">()]</span> <span class="o">=</span> <span class="nx">value</span><span class="p">;</span> + <span class="p">}</span> + + <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">results</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">putNested</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">key</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s1">'.'</span><span class="p">),</span> <span class="nx">results</span><span class="p">[</span><span class="nx">key</span><span class="p">]);</span> + <span class="p">});</span> + + <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">obj</span><span class="p">);</span> + <span class="p">});</span> + + <span class="k">return</span> <span class="nx">prompt</span><span class="p">;</span> +<span class="p">};</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div> <h3>function readLine (callback)</h3> + +<h4>@callback {function} Continuation to respond to when complete</h4> + +<p>Gets a single line of input from the user. </p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">prompt</span><span class="p">.</span><span class="nx">readLine</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span> + <span class="kd">var</span> <span class="nx">value</span> <span class="o">=</span> <span class="s1">''</span><span class="p">,</span> <span class="nx">buffer</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span> + <span class="nx">prompt</span><span class="p">.</span><span class="nx">resume</span><span class="p">();</span> + <span class="nx">stdin</span><span class="p">.</span><span class="nx">setEncoding</span><span class="p">(</span><span class="s1">'utf8'</span><span class="p">);</span> + <span class="nx">stdin</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'error'</span><span class="p">,</span> <span class="nx">callback</span><span class="p">);</span> + <span class="nx">stdin</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'data'</span><span class="p">,</span> <span class="kd">function</span> <span class="nx">data</span> <span class="p">(</span><span class="nx">chunk</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">value</span> <span class="o">+=</span> <span class="nx">buffer</span> <span class="o">+</span> <span class="nx">chunk</span><span class="p">;</span> + <span class="nx">buffer</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span> + <span class="nx">value</span> <span class="o">=</span> <span class="nx">value</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\r/g</span><span class="p">,</span> <span class="s1">''</span><span class="p">);</span> + <span class="k">if</span> <span class="p">(</span><span class="nx">value</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">'\n'</span><span class="p">)</span> <span class="o">!==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span> + <span class="k">if</span> <span class="p">(</span><span class="nx">value</span> <span class="o">!==</span> <span class="s1">'\n'</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">value</span> <span class="o">=</span> <span class="nx">value</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/^\n+/</span><span class="p">,</span> <span class="s1">''</span><span class="p">);</span> + <span class="p">}</span> + + <span class="nx">buffer</span> <span class="o">=</span> <span class="nx">value</span><span class="p">.</span><span class="nx">substr</span><span class="p">(</span><span class="nx">value</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">'\n'</span><span class="p">));</span> + <span class="nx">val</span> <span class="o">=</span> <span class="nx">value</span><span class="p">.</span><span class="nx">substr</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">value</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">'\n'</span><span class="p">));</span> + <span class="nx">prompt</span><span class="p">.</span><span class="nx">pause</span><span class="p">();</span> + <span class="nx">stdin</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">(</span><span class="s1">'data'</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span> + <span class="nx">stdin</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">(</span><span class="s1">'error'</span><span class="p">,</span> <span class="nx">callback</span><span class="p">);</span> + <span class="nx">value</span> <span class="o">=</span> <span class="nx">value</span><span class="p">.</span><span class="nx">trim</span><span class="p">();</span> + <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">value</span><span class="p">);</span> + <span class="p">}</span> + <span class="p">});</span> + + <span class="k">return</span> <span class="nx">prompt</span><span class="p">;</span> +<span class="p">};</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">¶</a> </div> <h3>function readLineHidden (callback)</h3> + +<h4>@callback {function} Continuation to respond to when complete</h4> + +<p>Gets a single line of hidden input (i.e. <code>rawMode = true</code>) from the user. </p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">prompt</span><span class="p">.</span><span class="nx">readLineHidden</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span> + <span class="kd">var</span> <span class="nx">value</span> <span class="o">=</span> <span class="s1">''</span><span class="p">,</span> <span class="nx">buffer</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span> + <span class="nx">stdio</span><span class="p">.</span><span class="nx">setRawMode</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span> + <span class="nx">prompt</span><span class="p">.</span><span class="nx">resume</span><span class="p">();</span> + <span class="nx">stdin</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'error'</span><span class="p">,</span> <span class="nx">callback</span><span class="p">);</span> + <span class="nx">stdin</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">'data'</span><span class="p">,</span> <span class="kd">function</span> <span class="nx">data</span> <span class="p">(</span><span class="nx">c</span><span class="p">)</span> <span class="p">{</span> + <span class="nx">c</span> <span class="o">=</span> <span class="s1">''</span> <span class="o">+</span> <span class="nx">c</span><span class="p">;</span> + <span class="k">switch</span> <span class="p">(</span><span class="nx">c</span><span class="p">)</span> <span class="p">{</span> + <span class="k">case</span> <span class="s1">'\n'</span><span class="o">:</span> <span class="k">case</span> <span class="s1">'\r'</span><span class="o">:</span> <span class="k">case</span> <span class="s1">'\r\n'</span><span class="o">:</span> <span class="k">case</span> <span class="s1">'\u0004'</span><span class="o">:</span> + <span class="nx">stdio</span><span class="p">.</span><span class="nx">setRawMode</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span> + <span class="nx">stdin</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">(</span><span class="s1">'data'</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span> + <span class="nx">stdin</span><span class="p">.</span><span class="nx">removeListener</span><span class="p">(</span><span class="s1">'error'</span><span class="p">,</span> <span class="nx">callback</span><span class="p">);</span> + <span class="nx">value</span> <span class="o">=</span> <span class="nx">value</span><span class="p">.</span><span class="nx">trim</span><span class="p">();</span> + <span class="nx">stdout</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="s1">'\n'</span><span class="p">);</span> + <span class="nx">stdout</span><span class="p">.</span><span class="nx">flush</span><span class="p">();</span> + <span class="nx">prompt</span><span class="p">.</span><span class="nx">pause</span><span class="p">();</span> + <span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> + <span class="k">case</span> <span class="s1">'\u0003'</span><span class="o">:</span> <span class="k">case</span> <span class="s1">'\0'</span><span class="o">:</span> + <span class="nx">stdout</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="s1">'\n'</span><span class="p">);</span> + <span class="nx">process</span><span class="p">.</span><span class="nx">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> + <span class="k">break</span><span class="p">;</span> + <span class="k">default</span><span class="o">:</span> + <span class="nx">value</span> <span class="o">+=</span> <span class="nx">buffer</span> <span class="o">+</span> <span class="nx">c</span> + <span class="nx">buffer</span> <span class="o">=</span> <span class="s1">''</span><span class="p">;</span> + <span class="k">break</span><span class="p">;</span> + <span class="p">}</span> + <span class="p">});</span> + + <span class="k">return</span> <span class="nx">prompt</span><span class="p">;</span> +<span class="p">};</span> + +</pre></div> </td> </tr> </tbody> </table> </div> </body> </html> diff --git a/node_modules/prompt/examples/add-properties.js b/node_modules/prompt/examples/add-properties.js new file mode 100644 index 0000000..1a56176 --- /dev/null +++ b/node_modules/prompt/examples/add-properties.js @@ -0,0 +1,35 @@ +/* + * add-properties.js: Example of how to add properties to an object using prompt. + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var prompt = require('../lib/prompt'); + +// +// Start the prompt +// +prompt.start(); + +var obj = { + password: 'lamepassword', + mindset: 'NY' +} + +// +// Log the initial object. +// +console.log('Initial object to be extended:'); +console.dir(obj); + +// +// Add two properties to the empty object: username and email +// +prompt.addProperties(obj, ['username', 'email'], function (err) { + // + // Log the results. + // + console.log('Updated object received:'); + console.dir(obj); +});
\ No newline at end of file diff --git a/node_modules/prompt/examples/color.js b/node_modules/prompt/examples/color.js new file mode 100644 index 0000000..7c63112 --- /dev/null +++ b/node_modules/prompt/examples/color.js @@ -0,0 +1,19 @@ +var prompt = require("../lib/prompt"); +var colors = require("colors/safe"); +// +// Setting these properties customizes the prompt. +// +prompt.message = colors.rainbow("Question!"); +prompt.delimiter = colors.green("><"); + +prompt.start(); + +prompt.get({ + properties: { + name: { + description: colors.magenta("What is your name?") + } + } +}, function (err, result) { + console.log(colors.cyan("You said your name is: " + result.name)); +}); diff --git a/node_modules/prompt/examples/dynamic-ask-prompt.js b/node_modules/prompt/examples/dynamic-ask-prompt.js new file mode 100644 index 0000000..194e1ef --- /dev/null +++ b/node_modules/prompt/examples/dynamic-ask-prompt.js @@ -0,0 +1,38 @@ +/* + * dynamic-ask-prompt.js: Dynamically decide whether to display prompt. + */ + +var prompt = require('../lib/prompt'); + +var schema = { + properties: { + proxy: { + description: 'Proxy url' + }, + proxyCredentials: { + description: 'Proxy credentials', + ask: function() { + // only ask for proxy credentials if a proxy was set + return prompt.history('proxy').value > 0; + } + } + } +}; + +// +// Start the prompt +// +prompt.start(); + +// +// Get one or two properties from the user, depending on +// what the user answered for proxy +// +prompt.get(schema, function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' proxy: ' + result.proxy); + console.log(' credentials: ' + result.proxyCredentials); +}); diff --git a/node_modules/prompt/examples/existing-properties.js b/node_modules/prompt/examples/existing-properties.js new file mode 100644 index 0000000..d87503b --- /dev/null +++ b/node_modules/prompt/examples/existing-properties.js @@ -0,0 +1,35 @@ +/* + * existing-properties.js: Example of using prompt with predefined properties. + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var prompt = require('../lib/prompt'); + +prompt.properties = { + email: { + format: 'email', + message: 'Must be a valid email address' + }, + password: { + hidden: true + } +}; + +// +// Start the prompt +// +prompt.start(); + +// +// Get two properties from the user: email, password +// +prompt.get(['email', 'password'], function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' email: ' + result.email); + console.log(' password: ' + result.password); +}); diff --git a/node_modules/prompt/examples/history.js b/node_modules/prompt/examples/history.js new file mode 100644 index 0000000..fd4369d --- /dev/null +++ b/node_modules/prompt/examples/history.js @@ -0,0 +1,44 @@ +/* + * history.js: Example of using the prompt history capabilities. + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var prompt = require('../lib/prompt'); + +// +// Start the prompt +// +prompt.start(); + +var properties = { + properties: { + animal: { + description: 'Enter an animal', + default: 'dog', + pattern: /dog|cat/ + }, + sound: { + description: 'What sound does this animal make?', + conform: function (value) { + var animal = prompt.history(0).value; + + return animal === 'dog' && value === 'woof' + || animal === 'cat' && value === 'meow'; + } + } + } +} + +// +// Get two properties from the user +// +prompt.get(properties, function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' animal: ' + result.animal); + console.log(' sound: ' + result.sound); +}); diff --git a/node_modules/prompt/examples/nested-properties-prompt.js b/node_modules/prompt/examples/nested-properties-prompt.js new file mode 100644 index 0000000..25106a2 --- /dev/null +++ b/node_modules/prompt/examples/nested-properties-prompt.js @@ -0,0 +1,37 @@ +/* + * property-prompt.js: Example of using prompt with complex properties. + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var prompt = require('../lib/prompt'); + +var schema = { + properties: { + url: { + required: true, + format: 'url' + }, + auth: { + properties: { + username: { + required: true + }, + password: { + required: true, + hidden: true + } + } + } + } +}; + +prompt.start(); + +prompt.get(schema, function (err, result) { + console.log('Command-line input received:'); + console.log(' url: ' + result.url); + console.log(' auth:username: ' + result.auth.username); + console.log(' auth:password: ' + result.auth.password); +}); diff --git a/node_modules/prompt/examples/old-schema.js b/node_modules/prompt/examples/old-schema.js new file mode 100644 index 0000000..631b7b5 --- /dev/null +++ b/node_modules/prompt/examples/old-schema.js @@ -0,0 +1,36 @@ +/* + * simple-prompt.js: Simple example of using prompt. + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var prompt = require('../lib/prompt'); + +// +// Start the prompt +// +prompt.start(); + +// +// Get two properties from the user: username and email +// +prompt.get([ + { + name: 'username', + validator: /^[a-z]+$/, + warning: 'Username should consist only lowercase alphabets', + empty: false + }, + { + name: 'email', + message: 'Email Address' + } +], function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' username: ' + result.username); + console.log(' email: ' + result.email); +}); diff --git a/node_modules/prompt/examples/override-validation.js b/node_modules/prompt/examples/override-validation.js new file mode 100644 index 0000000..0ca9ddd --- /dev/null +++ b/node_modules/prompt/examples/override-validation.js @@ -0,0 +1,52 @@ +/* + * override-validation.js: Example of using prompt with complex properties and command-line input. + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var prompt = require('../lib/prompt'), + optimist = require('optimist'); + +var schema = { + properties: { + name: { + pattern: /^[a-zA-Z\s-]+$/, + message: 'Name must be only letters, spaces, or dashes', + required: true + }, + email: { + name: 'email', + format: 'email', + message: 'Must be a valid email address' + } + } +}; + +// +// Set the overrides +// +prompt.override = optimist.argv + +// +// Start the prompt +// +prompt.start(); + +// +// Get two properties from the user: email, password +// +prompt.get(schema, function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' name: ' + result.name); + console.log(' email: ' + result.email); +}); + +// try running +// $ node ./override-validation.js --name USER --email EMAIL +// You will only be asked for email because it's invalid +// $ node ./override-validation.js --name h$acker --email me@example.com +// You will only be asked for name becasue it's invalid diff --git a/node_modules/prompt/examples/password.js b/node_modules/prompt/examples/password.js new file mode 100644 index 0000000..e8015e6 --- /dev/null +++ b/node_modules/prompt/examples/password.js @@ -0,0 +1,42 @@ +/* + * password.js: Simple example of using prompt. + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var prompt = require('../lib/prompt'); + +// +// Start the prompt +// +prompt.start(); + +// +// Get two properties from the user: username and password and password masked +// +prompt.get([{ + name: 'username', + required: true + }, { + name: 'password', + hidden: true, + conform: function (value) { + return true; + } + }, { + name: 'passwordMasked', + hidden: true, + replace: '*', + conform: function (value) { + return true; + } + }], function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' username: ' + result.username); + console.log(' password: ' + result.password); + console.log(' passwordMasked: ' + result.passwordMasked); +}); diff --git a/node_modules/prompt/examples/prompt-override.js b/node_modules/prompt/examples/prompt-override.js new file mode 100644 index 0000000..7f2848b --- /dev/null +++ b/node_modules/prompt/examples/prompt-override.js @@ -0,0 +1,36 @@ +var prompt = require('../lib/prompt'), + optimist; + +try { + optimist = require('optimist'); +} catch (err) { + throw new Error([ + 'You need to install optimist before this example will work!', + 'Try: `npm install optimist`.' + ].join('\n')); +} + +// +// Set the overrides +// +prompt.override = optimist.argv + +// +// Start the prompt +// +prompt.start(); + +// +// Get two properties from the user: username and email +// +prompt.get(['username', 'email'], function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' username: ' + result.username); + console.log(' email: ' + result.email); + prompt.pause(); +}) + +// $ node ./prompt-override.js --username USER --email EMAIL diff --git a/node_modules/prompt/examples/prompt-streamline._js b/node_modules/prompt/examples/prompt-streamline._js new file mode 100644 index 0000000..e7fef42 --- /dev/null +++ b/node_modules/prompt/examples/prompt-streamline._js @@ -0,0 +1,32 @@ +/* + * prompt-streamline._js: Example of how to use prompt with streamlinejs. + * + * calling syntax: _node prompt-streamline._js + * + */ +var prompt = require('../lib/prompt'); + +function getSampleData(){ + return [ + { + name: 'username', + message: 'Enter a username' + } + ]; +}; + +// +// Start the prompt +// +prompt.start(); + +function get_username_prompt(_){ + console.log(prompt.get(getSampleData(), _).username); +} + +get_username_prompt(_); + +// +// Clean the prompt +// +prompt.stop();
\ No newline at end of file diff --git a/node_modules/prompt/examples/property-prompt.js b/node_modules/prompt/examples/property-prompt.js new file mode 100644 index 0000000..c8b343b --- /dev/null +++ b/node_modules/prompt/examples/property-prompt.js @@ -0,0 +1,45 @@ +/* + * property-prompt.js: Example of using prompt with complex properties. + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var prompt = require('../lib/prompt'); + +var schema = { + properties: { + name: { + pattern: /^[a-zA-Z\s-]+$/, + message: 'Name must be only letters, spaces, or dashes', + required: true + }, + email: { + name: 'email', + format: 'email', + message: 'Must be a valid email address' + }, + password: { + required: true, + hidden: true + } + } +}; + +// +// Start the prompt +// +prompt.start(); + +// +// Get two properties from the user: email, password +// +prompt.get(schema, function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' name: ' + result.name); + console.log(' email: ' + result.email); + console.log(' password: ' + result.password); +}); diff --git a/node_modules/prompt/examples/simple-prompt.js b/node_modules/prompt/examples/simple-prompt.js new file mode 100644 index 0000000..062e529 --- /dev/null +++ b/node_modules/prompt/examples/simple-prompt.js @@ -0,0 +1,25 @@ +/* + * simple-prompt.js: Simple example of using prompt. + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var prompt = require('../lib/prompt'); + +// +// Start the prompt +// +prompt.start(); + +// +// Get two properties from the user: username and email +// +prompt.get(['username', 'email'], function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' username: ' + result.username); + console.log(' email: ' + result.email); +});
\ No newline at end of file diff --git a/node_modules/prompt/examples/types.js b/node_modules/prompt/examples/types.js new file mode 100644 index 0000000..83f45a6 --- /dev/null +++ b/node_modules/prompt/examples/types.js @@ -0,0 +1,20 @@ +var prompt = require('../lib/prompt'); + +prompt.start(); + +prompt.get([{ + name: 'integer', + type: 'integer', + required: true + }, { + name: 'number', + type: 'number', + required: true + }, { + name: 'boolean', + type: 'boolean', + required: true + }], function (err, result) { + console.log('Input received:'); + console.log(JSON.stringify(result, null, 2)); +}); diff --git a/node_modules/prompt/examples/yes-or-no-prompt.js b/node_modules/prompt/examples/yes-or-no-prompt.js new file mode 100644 index 0000000..512b556 --- /dev/null +++ b/node_modules/prompt/examples/yes-or-no-prompt.js @@ -0,0 +1,32 @@ +/* + * yes-or-no-prompt.js: Simple example of using prompt. + * + * (C) 2012, Nodejitsu Inc. + * + */ + +var prompt = require('../lib/prompt'); + +// +// Start the prompt +// +prompt.start(); + +var property = { + name: 'yesno', + message: 'are you sure?', + validator: /y[es]*|n[o]?/, + warning: 'Must respond yes or no', + default: 'no' +}; + +// +// Get the simple yes or no property +// +prompt.get(property, function (err, result) { + // + // Log the results. + // + console.log('Command-line input received:'); + console.log(' result: ' + result.yesno); +});
\ No newline at end of file diff --git a/node_modules/prompt/lib/prompt.js b/node_modules/prompt/lib/prompt.js new file mode 100644 index 0000000..78aeda2 --- /dev/null +++ b/node_modules/prompt/lib/prompt.js @@ -0,0 +1,783 @@ +/* + * prompt.js: Simple prompt for prompting information from the command line + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var events = require('events'), + readline = require('readline'), + utile = require('utile'), + async = utile.async, + read = require('read'), + validate = require('revalidator').validate, + winston = require('winston'), + colors = require('colors/safe'); + +// +// Monkey-punch readline.Interface to work-around +// https://github.com/joyent/node/issues/3860 +// +readline.Interface.prototype.setPrompt = function(prompt, length) { + this._prompt = prompt; + if (length) { + this._promptLength = length; + } else { + var lines = prompt.split(/[\r\n]/); + var lastLine = lines[lines.length - 1]; + this._promptLength = lastLine.replace(/\u001b\[(\d+(;\d+)*)?m/g, '').length; + } +}; + +// +// Expose version using `pkginfo` +// +require('pkginfo')(module, 'version'); + +var stdin, stdout, history = []; +var prompt = module.exports = Object.create(events.EventEmitter.prototype); +var logger = prompt.logger = new winston.Logger({ + transports: [new (winston.transports.Console)()] +}); + +prompt.started = false; +prompt.paused = false; +prompt.stopped = true; +prompt.allowEmpty = false; +prompt.message = 'prompt'; +prompt.delimiter = ': '; +prompt.colors = true; + +// +// Create an empty object for the properties +// known to `prompt` +// +prompt.properties = {}; + +// +// Setup the default winston logger to use +// the `cli` levels and colors. +// +logger.cli(); + +// +// ### function start (options) +// #### @options {Object} **Optional** Options to consume by prompt +// Starts the prompt by listening to the appropriate events on `options.stdin` +// and `options.stdout`. If no streams are supplied, then `process.stdin` +// and `process.stdout` are used, respectively. +// +prompt.start = function (options) { + if (prompt.started) { + return; + } + + options = options || {}; + stdin = options.stdin || process.stdin; + stdout = options.stdout || process.stdout; + + // + // By default: Remember the last `10` prompt property / + // answer pairs and don't allow empty responses globally. + // + prompt.memory = options.memory || 10; + prompt.allowEmpty = options.allowEmpty || false; + prompt.message = options.message || prompt.message; + prompt.delimiter = options.delimiter || prompt.delimiter; + prompt.colors = options.colors || prompt.colors; + + if (process.platform !== 'win32') { + // windows falls apart trying to deal with SIGINT + process.on('SIGINT', function () { + stdout.write('\n'); + process.exit(1); + }); + } + + prompt.emit('start'); + prompt.started = true; + prompt.stopped = false; + return prompt; +}; + +// +// ### function pause () +// Pauses input coming in from stdin +// +prompt.pause = function () { + if (!prompt.started || prompt.stopped || prompt.paused) { + return; + } + + stdin.pause(); + prompt.emit('pause'); + prompt.paused = true; + return prompt; +}; + +// +// ### function stop () +// Stops input coming in from stdin +// +prompt.stop = function () { + if (prompt.stopped || !prompt.started) { + return; + } + + stdin.destroy(); + prompt.emit('stop'); + prompt.stopped = true; + prompt.started = false; + prompt.paused = false; + return prompt; +} + +// +// ### function resume () +// Resumes input coming in from stdin +// +prompt.resume = function () { + if (!prompt.started || !prompt.paused) { + return; + } + + stdin.resume(); + prompt.emit('resume'); + prompt.paused = false; + return prompt; +}; + +// +// ### function history (search) +// #### @search {Number|string} Index or property name to find. +// Returns the `property:value` pair from within the prompts +// `history` array. +// +prompt.history = function (search) { + if (typeof search === 'number') { + return history[search] || {}; + } + + var names = history.map(function (pair) { + return typeof pair.property === 'string' + ? pair.property + : pair.property.name; + }); + + if (!~names.indexOf(search)) { + return null; + } + + return history.filter(function (pair) { + return typeof pair.property === 'string' + ? pair.property === search + : pair.property.name === search; + })[0]; +}; + +// +// ### function get (schema, callback) +// #### @schema {Array|Object|string} Set of variables to get input for. +// #### @callback {function} Continuation to pass control to when complete. +// Gets input from the user via stdin for the specified message(s) `msg`. +// +prompt.get = function (schema, callback) { + // + // Transforms a full JSON-schema into an array describing path and sub-schemas. + // Used for iteration purposes. + // + function untangle(schema, path) { + var results = []; + path = path || []; + + if (schema.properties) { + // + // Iterate over the properties in the schema and use recursion + // to process sub-properties. + // + Object.keys(schema.properties).forEach(function (key) { + var obj = {}; + obj[key] = schema.properties[key]; + + // + // Concat a sub-untangling to the results. + // + results = results.concat(untangle(obj[key], path.concat(key))); + }); + + // Return the results. + return results; + } + + // + // This is a schema "leaf". + // + return { + path: path, + schema: schema + }; + } + + // + // Iterate over the values in the schema, represented as + // a legit single-property object subschemas. Accepts `schema` + // of the forms: + // + // 'prop-name' + // + // ['string-name', { path: ['or-well-formed-subschema'], properties: ... }] + // + // { path: ['or-well-formed-subschema'], properties: ... ] } + // + // { properties: { 'schema-with-no-path' } } + // + // And transforms them all into + // + // { path: ['path', 'to', 'property'], properties: { path: { to: ...} } } + // + function iterate(schema, get, done) { + var iterator = [], + result = {}; + + if (typeof schema === 'string') { + // + // We can iterate over a single string. + // + iterator.push({ + path: [schema], + schema: prompt.properties[schema.toLowerCase()] || {} + }); + } + else if (Array.isArray(schema)) { + // + // An array of strings and/or single-prop schema and/or no-prop schema. + // + iterator = schema.map(function (element) { + if (typeof element === 'string') { + return { + path: [element], + schema: prompt.properties[element.toLowerCase()] || {} + }; + } + else if (element.properties) { + return { + path: [Object.keys(element.properties)[0]], + schema: element.properties[Object.keys(element.properties)[0]] + }; + } + else if (element.path && element.schema) { + return element; + } + else { + return { + path: [element.name || 'question'], + schema: element + }; + } + }); + } + else if (schema.properties) { + // + // Or a complete schema `untangle` it for use. + // + iterator = untangle(schema); + } + else { + // + // Or a partial schema and path. + // TODO: Evaluate need for this option. + // + iterator = [{ + schema: schema.schema ? schema.schema : schema, + path: schema.path || [schema.name || 'question'] + }]; + } + + // + // Now, iterate and assemble the result. + // + async.forEachSeries(iterator, function (branch, next) { + get(branch, function assembler(err, line) { + if (err) { + return next(err); + } + + function build(path, line) { + var obj = {}; + if (path.length) { + obj[path[0]] = build(path.slice(1), line); + return obj; + } + + return line; + } + + function attach(obj, attr) { + var keys; + if (typeof attr !== 'object' || attr instanceof Array) { + return attr; + } + + keys = Object.keys(attr); + if (keys.length) { + if (!obj[keys[0]]) { + obj[keys[0]] = {}; + } + obj[keys[0]] = attach(obj[keys[0]], attr[keys[0]]); + } + + return obj; + } + + result = attach(result, build(branch.path, line)); + next(); + }); + }, function (err) { + return err ? done(err) : done(null, result); + }); + } + + iterate(schema, function get(target, next) { + prompt.getInput(target, function (err, line) { + return err ? next(err) : next(null, line); + }); + }, callback); + + return prompt; +}; + +// +// ### function confirm (msg, callback) +// #### @msg {Array|Object|string} set of message to confirm +// #### @callback {function} Continuation to pass control to when complete. +// Confirms a single or series of messages by prompting the user for a Y/N response. +// Returns `true` if ALL messages are answered in the affirmative, otherwise `false` +// +// `msg` can be a string, or object (or array of strings/objects). +// An object may have the following properties: +// +// { +// description: 'yes/no' // message to prompt user +// pattern: /^[yntf]{1}/i // optional - regex defining acceptable responses +// yes: /^[yt]{1}/i // optional - regex defining `affirmative` responses +// message: 'yes/no' // optional - message to display for invalid responses +// } +// +prompt.confirm = function (/* msg, options, callback */) { + var args = Array.prototype.slice.call(arguments), + msg = args.shift(), + callback = args.pop(), + opts = args.shift(), + vars = !Array.isArray(msg) ? [msg] : msg, + RX_Y = /^[yt]{1}/i, + RX_YN = /^[yntf]{1}/i; + + function confirm(target, next) { + var yes = target.yes || RX_Y, + options = utile.mixin({ + description: typeof target === 'string' ? target : target.description||'yes/no', + pattern: target.pattern || RX_YN, + name: 'confirm', + message: target.message || 'yes/no' + }, opts || {}); + + + prompt.get([options], function (err, result) { + next(err ? false : yes.test(result[options.name])); + }); + } + + async.rejectSeries(vars, confirm, function(result) { + callback(null, result.length===0); + }); +}; + + +// Variables needed outside of getInput for multiline arrays. +var tmp = []; + + +// ### function getInput (prop, callback) +// #### @prop {Object|string} Variable to get input for. +// #### @callback {function} Continuation to pass control to when complete. +// Gets input from the user via stdin for the specified message `msg`. +// +prompt.getInput = function (prop, callback) { + var schema = prop.schema || prop, + propName = prop.path && prop.path.join(':') || prop, + storedSchema = prompt.properties[propName.toLowerCase()], + delim = prompt.delimiter, + defaultLine, + against, + hidden, + length, + valid, + name, + raw, + msg; + + // + // If there is a stored schema for `propName` in `propmpt.properties` + // then use it. + // + if (schema instanceof Object && !Object.keys(schema).length && + typeof storedSchema !== 'undefined') { + schema = storedSchema; + } + + // + // Build a proper validation schema if we just have a string + // and no `storedSchema`. + // + if (typeof prop === 'string' && !storedSchema) { + schema = {}; + } + + schema = convert(schema); + defaultLine = schema.default; + name = prop.description || schema.description || propName; + raw = prompt.colors + ? [colors.grey(name), colors.grey(delim)] + : [name, delim]; + + if (prompt.message) + raw.unshift(prompt.message, delim); + + prop = { + schema: schema, + path: propName.split(':') + }; + + // + // If the schema has no `properties` value then set + // it to an object containing the current schema + // for `propName`. + // + if (!schema.properties) { + schema = (function () { + var obj = { properties: {} }; + obj.properties[propName] = schema; + return obj; + })(); + } + + // + // Handle overrides here. + // TODO: Make overrides nestable + // + if (prompt.override && prompt.override[propName]) { + if (prompt._performValidation(name, prop, prompt.override, schema, -1, callback)) { + return callback(null, prompt.override[propName]); + } + + delete prompt.override[propName]; + } + + // + // Check if we should skip this prompt + // + if (typeof prop.schema.ask === 'function' && + !prop.schema.ask()) { + return callback(null, prop.schema.default || ''); + } + + var type = (schema.properties && schema.properties[propName] && + schema.properties[propName].type || '').toLowerCase().trim(), + wait = type === 'array'; + + if (type === 'array') { + length = prop.schema.maxItems; + if (length) { + msg = (tmp.length + 1).toString() + '/' + length.toString(); + } + else { + msg = (tmp.length + 1).toString(); + } + msg += delim; + raw.push(prompt.colors ? msg.grey : msg); + } + + // + // Calculate the raw length and colorize the prompt + // + length = raw.join('').length; + raw[0] = raw[0]; + msg = raw.join(''); + + if (schema.help) { + schema.help.forEach(function (line) { + logger.help(line); + }); + } + + // + // Emit a "prompting" event + // + prompt.emit('prompt', prop); + + // + // If there is no default line, set it to an empty string + // + if(typeof defaultLine === 'undefined') { + defaultLine = ''; + } + + // + // set to string for readline ( will not accept Numbers ) + // + defaultLine = defaultLine.toString(); + + // + // Make the actual read + // + read({ + prompt: msg, + silent: prop.schema && prop.schema.hidden, + replace: prop.schema && prop.schema.replace, + default: defaultLine, + input: stdin, + output: stdout + }, function (err, line) { + if (err && wait === false) { + return callback(err); + } + + var against = {}, + numericInput, + isValid; + + if (line !== '') { + + if (schema.properties[propName]) { + var type = (schema.properties[propName].type || '').toLowerCase().trim() || undefined; + + // + // If type is some sort of numeric create a Number object to pass to revalidator + // + if (type === 'number' || type === 'integer') { + line = Number(line); + } + + // + // Attempt to parse input as a boolean if the schema expects a boolean + // + if (type == 'boolean') { + if(line.toLowerCase() === "true" || line.toLowerCase() === 't') { + line = true; + } else if(line.toLowerCase() === "false" || line.toLowerCase() === 'f') { + line = false; + } + } + + // + // If the type is an array, wait for the end. Fixes #54 + // + if (type == 'array') { + var length = prop.schema.maxItems; + if (err) { + if (err.message == 'canceled') { + wait = false; + stdout.write('\n'); + } + } + else { + if (length) { + if (tmp.length + 1 < length) { + isValid = false; + wait = true; + } + else { + isValid = true; + wait = false; + } + } + else { + isValid = false; + wait = true; + } + tmp.push(line); + } + line = tmp; + } + } + + against[propName] = line; + } + + if (prop && prop.schema.before) { + line = prop.schema.before(line); + } + + // Validate + if (isValid === undefined) isValid = prompt._performValidation(name, prop, against, schema, line, callback); + + if (!isValid) { + return prompt.getInput(prop, callback); + } + + // + // Log the resulting line, append this `property:value` + // pair to the history for `prompt` and respond to + // the callback. + // + logger.input(line.yellow); + prompt._remember(propName, line); + callback(null, line); + + // Make sure `tmp` is emptied + tmp = []; + }); +}; + +// +// ### function performValidation (name, prop, against, schema, line, callback) +// #### @name {Object} Variable name +// #### @prop {Object|string} Variable to get input for. +// #### @against {Object} Input +// #### @schema {Object} Validation schema +// #### @line {String|Boolean} Input line +// #### @callback {function} Continuation to pass control to when complete. +// Perfoms user input validation, print errors if needed and returns value according to validation +// +prompt._performValidation = function (name, prop, against, schema, line, callback) { + var numericInput, valid, msg; + try { + valid = validate(against, schema); + } + catch (err) { + return (line !== -1) ? callback(err) : false; + } + + if (!valid.valid) { + if (prop.schema.message) { + logger.error(prop.schema.message); + } else { + msg = line !== -1 ? 'Invalid input for ' : 'Invalid command-line input for '; + + if (prompt.colors) { + logger.error(msg + name.grey); + } + else { + logger.error(msg + name); + } + } + + prompt.emit('invalid', prop, line); + } + + return valid.valid; +}; + +// +// ### function addProperties (obj, properties, callback) +// #### @obj {Object} Object to add properties to +// #### @properties {Array} List of properties to get values for +// #### @callback {function} Continuation to pass control to when complete. +// Prompts the user for values each of the `properties` if `obj` does not already +// have a value for the property. Responds with the modified object. +// +prompt.addProperties = function (obj, properties, callback) { + properties = properties.filter(function (prop) { + return typeof obj[prop] === 'undefined'; + }); + + if (properties.length === 0) { + return callback(obj); + } + + prompt.get(properties, function (err, results) { + if (err) { + return callback(err); + } + else if (!results) { + return callback(null, obj); + } + + function putNested (obj, path, value) { + var last = obj, key; + + while (path.length > 1) { + key = path.shift(); + if (!last[key]) { + last[key] = {}; + } + + last = last[key]; + } + + last[path.shift()] = value; + } + + Object.keys(results).forEach(function (key) { + putNested(obj, key.split('.'), results[key]); + }); + + callback(null, obj); + }); + + return prompt; +}; + +// +// ### @private function _remember (property, value) +// #### @property {Object|string} Property that the value is in response to. +// #### @value {string} User input captured by `prompt`. +// Prepends the `property:value` pair into the private `history` Array +// for `prompt` so that it can be accessed later. +// +prompt._remember = function (property, value) { + history.unshift({ + property: property, + value: value + }); + + // + // If the length of the `history` Array + // has exceeded the specified length to remember, + // `prompt.memory`, truncate it. + // + if (history.length > prompt.memory) { + history.splice(prompt.memory, history.length - prompt.memory); + } +}; + +// +// ### @private function convert (schema) +// #### @schema {Object} Schema for a property +// Converts the schema into new format if it is in old format +// +function convert(schema) { + var newProps = Object.keys(validate.messages), + newSchema = false, + key; + + newProps = newProps.concat(['description', 'dependencies']); + + for (key in schema) { + if (newProps.indexOf(key) > 0) { + newSchema = true; + break; + } + } + + if (!newSchema || schema.validator || schema.warning || typeof schema.empty !== 'undefined') { + schema.description = schema.message; + schema.message = schema.warning; + + if (typeof schema.validator === 'function') { + schema.conform = schema.validator; + } else { + schema.pattern = schema.validator; + } + + if (typeof schema.empty !== 'undefined') { + schema.required = !(schema.empty); + } + + delete schema.warning; + delete schema.validator; + delete schema.empty; + } + + return schema; +} diff --git a/node_modules/prompt/package.json b/node_modules/prompt/package.json new file mode 100644 index 0000000..ce79183 --- /dev/null +++ b/node_modules/prompt/package.json @@ -0,0 +1,73 @@ +{ + "_from": "prompt", + "_id": "prompt@1.0.0", + "_inBundle": false, + "_integrity": "sha1-jlcSPDlquYiJf7Mn/Trtw+c15P4=", + "_location": "/prompt", + "_phantomChildren": {}, + "_requested": { + "type": "tag", + "registry": true, + "raw": "prompt", + "name": "prompt", + "escapedName": "prompt", + "rawSpec": "", + "saveSpec": null, + "fetchSpec": "latest" + }, + "_requiredBy": [ + "#USER", + "/" + ], + "_resolved": "https://registry.npmjs.org/prompt/-/prompt-1.0.0.tgz", + "_shasum": "8e57123c396ab988897fb327fd3aedc3e735e4fe", + "_spec": "prompt", + "_where": "/data/dev/Projets/Bingoloto Remote", + "author": { + "name": "Nodejitsu Inc.", + "email": "info@nodejitsu.com" + }, + "bugs": { + "url": "https://github.com/flatiron/prompt/issues" + }, + "bundleDependencies": false, + "dependencies": { + "colors": "^1.1.2", + "pkginfo": "0.x.x", + "read": "1.0.x", + "revalidator": "0.1.x", + "utile": "0.3.x", + "winston": "2.1.x" + }, + "deprecated": false, + "description": "A beautiful command-line prompt for node.js", + "devDependencies": { + "vows": "0.7.0" + }, + "engines": { + "node": ">= 0.6.6" + }, + "homepage": "https://github.com/flatiron/prompt#readme", + "license": "MIT", + "main": "./lib/prompt", + "maintainers": [ + { + "name": "indexzero", + "email": "charlie@nodejitsu.com" + }, + { + "name": "jesusabdullah", + "email": "josh@nodejitsu.com" + } + ], + "name": "prompt", + "repository": { + "type": "git", + "url": "git+ssh://git@github.com/flatiron/prompt.git" + }, + "scripts": { + "test": "vows test/prompt-test.js --spec", + "test-all": "vows --spec" + }, + "version": "1.0.0" +} diff --git a/node_modules/prompt/test/helpers.js b/node_modules/prompt/test/helpers.js new file mode 100644 index 0000000..b21021f --- /dev/null +++ b/node_modules/prompt/test/helpers.js @@ -0,0 +1,161 @@ +/* + * helpers.js: test helpers for the prompt tests. + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var stream = require('stream'), + util = require('util'), + prompt = require('../lib/prompt'); + +var helpers = exports; + +var MockReadWriteStream = helpers.MockReadWriteStream = function () { + // + // No need to do anything here, it's just a mock. + // + var self = this; + this.on('pipe', function (src) { + var _emit = src.emit; + src.emit = function () { + //console.dir(arguments); + _emit.apply(src, arguments); + }; + + src.on('data', function (d) { + self.emit('data', d + ''); + }) + }) +}; + +util.inherits(MockReadWriteStream, stream.Stream); + +['resume', 'pause', 'setEncoding', 'flush', 'end'].forEach(function (method) { + MockReadWriteStream.prototype[method] = function () { /* Mock */ }; +}); + +MockReadWriteStream.prototype.write = function (msg) { + this.emit('data', msg); + return true; +}; + +MockReadWriteStream.prototype.writeNextTick = function (msg) { + var self = this + process.nextTick(function () { + self.write(msg); + }); +}; + +// +// Create some mock streams for asserting against +// in our prompt teSts. +// +helpers.stdin = new MockReadWriteStream(); +helpers.stdout = new MockReadWriteStream(); +helpers.stderr = new MockReadWriteStream(); + +// +// Because `read` uses a `process.nextTick` for reading from +// stdin, it is necessary to write sequences of input with extra +// `process.nextTick` calls +// +helpers.stdin.writeSequence = function (lines) { + if (!lines || !lines.length) { + return; + } + + helpers.stdin.writeNextTick(lines.shift()); + prompt.once('prompt', function () { + process.nextTick(function () { + helpers.stdin.writeSequence(lines); + }); + }); +} + +// +// Monkey punch `util.error` to silence console output +// and redirect to helpers.stderr for testing. +// +process.stderr.write = function () { + helpers.stderr.write.apply(helpers.stderr, arguments); +} + +// 1) .properties +// 2) warning --> message +// 3) Name --> description || key +// 4) validator --> conform (fxns), pattern (regexp), format (built-in) +// 5) empty --> required +helpers.schema = { + properties: { + oldschema: { + message: 'Enter your username', + validator: /^[\w|\-]+$/, + warning: 'username can only be letters, numbers, and dashes', + empty: false + }, + riffwabbles: { + pattern: /^[\w|\-]+$/, + message: 'riffwabbles can only be letters, numbers, and dashes', + default: 'foobizzles' + }, + number: { + type: 'number', + message: 'pick a number, any number', + default: 10 + }, + integer: { + type: 'integer' + }, + boolean: { + type: 'boolean' + }, + username: { + pattern: /^[\w|\-]+$/, + message: 'Username can only be letters, numbers, and dashes' + }, + notblank: { + required: true + }, + password: { + hidden: true, + required: true + }, + badValidator: { + pattern: ['cant', 'use', 'array'] + }, + animal: { + description: 'Enter an animal', + default: 'dog', + pattern: /dog|cat/ + }, + sound: { + description: 'What sound does this animal make?', + conform: function (value) { + var animal = prompt.history(0).value; + + return animal === 'dog' && value === 'woof' + || animal === 'cat' && value === 'meow'; + } + }, + fnvalidator: { + name: 'fnvalidator', + validator: function (line) { + return line.slice(0,2) == 'fn'; + }, + message: 'fnvalidator must start with "fn"' + }, + fnconform: { + conform: function (line) { + return line.slice(0,2) == 'fn'; + }, + message: 'fnconform must start with "fn"' + }/*, + cbvalidator: { + conform: function (line, next) { + next(line.slice(0,2) == 'cb'); + }, + message: 'cbvalidator must start with "cb"' + }*/ + } +}; diff --git a/node_modules/prompt/test/interactive-prompt-test.js b/node_modules/prompt/test/interactive-prompt-test.js new file mode 100644 index 0000000..a3032e3 --- /dev/null +++ b/node_modules/prompt/test/interactive-prompt-test.js @@ -0,0 +1,49 @@ +/* + * prompt-test.js: Tests for prompt. + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var assert = require('assert'), + vows = require('vows'), + prompt = require('../lib/prompt'), + winston = require('winston').cli(), + helpers = require('./helpers'); + +vows.describe('prompt/interactive').addBatch({ + "When using prompt": { + topic: function () { + // + // Reset the prompt for interactive testing + // + prompt.started = false; + prompt.start(); + winston.info('These prompt tests are interactive'); + winston.info('Not following instructions will result in test failure'); + return null; + }, + "the getInput() method": { + "when passed a complex property with `hidden: true`": { + topic: function () { + winston.info('When prompted, enter: 12345 [backspace] [backspace] [enter]'); + prompt.getInput({ path: ['password'], schema: helpers.schema.properties.password }, this.callback); + }, + "should respond with `123`": function (err, result) { + assert.isNull(err); + assert.equal(result, '123'); + }, + "and then when passed a complex property expecting a number": { + topic: function () { + winston.info('When prompted, enter: 123 [enter]'); + prompt.getInput({ path: ['number'], schema: helpers.schema.properties.number }, this.callback); + }, + "should respond with `123` (as a number)": function (err, result) { + assert.isNull(err); + assert.equal(result, 123); + } + } + } + } + } +}).export(module); diff --git a/node_modules/prompt/test/macros.js b/node_modules/prompt/test/macros.js new file mode 100644 index 0000000..9f58f08 --- /dev/null +++ b/node_modules/prompt/test/macros.js @@ -0,0 +1,82 @@ +/* + * macros.js: Test macros for prompt. + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var assert = require('assert'), + helpers = require('./helpers'), + prompt = require('../lib/prompt'); + +exports.shouldConfirm = function (options, mixin) { + var message = options.response.toString().replace(/\n/g, ''), + messages = ["When using prompt", "the confirm() method"], + context = {}, + last = context; + + messages = messages.concat(options.messages || []); + + while (messages.length) { + var text = messages.shift(); + last[text] = {}; + last = last[text]; + } + + last['responding with ' + message] = { + topic: function () { + if(!mixin) + prompt.confirm(options.prop, this.callback); + else + prompt.confirm(options.prop, mixin, this.callback); + + if (!Array.isArray(options.response)) { + helpers.stdin.writeNextTick(options.response + '\n'); + } + else { + helpers.stdin.writeSequence(options.response); + } + }, + "should respond with true" : function(err, result) { + assert.isNull(err); + assert.isTrue(result); + } + } + + return context; +}; + +exports.shouldNotConfirm = function (options) { + var message = options.response.toString().replace(/\n/g, ''), + messages = ["When using prompt", "the confirm() method"], + context = {}, + last = context; + + messages = messages.concat(options.messages || []); + + while (messages.length) { + var text = messages.shift(); + last[text] = {}; + last = last[text]; + } + + last['responding with ' + message] = { + topic: function () { + prompt.confirm(options.prop, this.callback); + + if (!Array.isArray(options.response)) { + helpers.stdin.writeNextTick(options.response + '\n'); + } + else { + helpers.stdin.writeSequence(options.response); + } + }, + "should respond with false" : function(err, result) { + assert.isNull(err); + assert.isFalse(result); + } + }; + + return context; +}; + diff --git a/node_modules/prompt/test/prompt-test.js b/node_modules/prompt/test/prompt-test.js new file mode 100644 index 0000000..2be73bc --- /dev/null +++ b/node_modules/prompt/test/prompt-test.js @@ -0,0 +1,817 @@ +/* + * prompt-test.js: Tests for prompt. + * + * (C) 2010, Nodejitsu Inc. + * + */ + +var assert = require('assert'), + vows = require('vows'), + prompt = require('../lib/prompt'), + helpers = require('./helpers'), + macros = require('./macros'), + schema = helpers.schema; + +// A helper to pass fragments of our schema into prompt as full schemas. +function grab () { + var names = [].slice.call(arguments), + complete = { schema: {} }; + + names.forEach(function (name) { + complete.path = [name], + complete.schema = schema.properties[name]; + }); + return complete; +}; + +// +// Reset the prompt for mock testing +// +prompt.started = false; +prompt.start({ + stdin: helpers.stdin, + stdout: helpers.stdout +}); + +vows.describe('prompt').addBatch({ + "When using prompt": { + "the getInput() method": { + "with a simple string prompt": { + topic: function () { + var that = this; + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }) + + prompt.getInput('test input', this.callback); + helpers.stdin.writeNextTick('test value\n'); + }, + "should prompt to stdout and respond with data": function (err, input) { + assert.isNull(err); + assert.equal(input, 'test value'); + assert.isTrue(this.msg.indexOf('test input') !== -1); + } + }, + } + } +}).addBatch({ + "When using prompt": { + "the getInput() method": { + "with any field that is not supposed to be empty": { + "and we don't provide any input": { + topic: function () { + var that = this; + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + helpers.stderr.once('data', function (msg) { + that.errmsg = msg; + }); + + prompt.getInput(grab('notblank'), function () {}); + prompt.once('invalid', this.callback.bind(null, null)) + helpers.stdin.writeNextTick('\n'); + }, + "should prompt with an error": function (_, prop, input) { + assert.isObject(prop); + assert.equal(input, ''); + assert.isTrue(this.errmsg.indexOf('Invalid input') !== -1); + assert.isTrue(this.msg.indexOf('notblank') !== -1); + } + } + } + } + } +}).addBatch({ + "When using prompt": { + "the getInput() method": { + "with a hidden field that is not supposed to be empty": { + "and we provide valid input": { + topic: function () { + var that = this; + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + prompt.getInput('password', this.callback); + helpers.stdin.writeNextTick('trustno1\n'); + }, + + "should prompt to stdout and respond with data": function (err, input) { + assert.isNull(err); + assert.equal(input, 'trustno1'); + assert.isTrue(this.msg.indexOf('password') !== -1); + } + }, + } + } + } +}).addBatch({ + "When using prompt": { + "the getInput() method": { + "with a hidden field that is not supposed to be empty": { + "and we don't provide an input": { + topic: function () { + var that = this; + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + helpers.stderr.once('data', function (msg) { + that.errmsg = msg; + }); + + prompt.getInput(grab('password'), function () {} ); + prompt.once('invalid', this.callback.bind(null, null)) + helpers.stdin.writeNextTick('\n'); + }, + "should prompt with an error": function (ign, prop, input) { + assert.isObject(prop); + assert.equal(input, ''); + assert.isTrue(this.errmsg.indexOf('Invalid input') !== -1); + assert.isTrue(this.msg.indexOf('password') !== -1); + } + } + } + } + } +}).addBatch({ + "When using prompt": { + "the getInput() method": { + "with an integer field": { + "and we provide valid input": { + topic: function () { + var that = this; + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + prompt.getInput(grab('integer'), this.callback); + helpers.stdin.writeNextTick('42\n'); + }, + "should prompt to stdout and respond with data": function (err, input) { + assert.isNull(err); + assert.equal(input, '42'); + assert.isTrue(this.msg.indexOf('integer') !== -1); + } + }, + } + } + } +}).addBatch({ + "When using prompt": { + "the getInput() method": { + "with an integer field": { + "and we don't provide an integer": { + topic: function () { + var that = this; + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + helpers.stderr.once('data', function (msg) { + that.errmsg = msg; + }) + + prompt.getInput(grab('integer'), this.callback); + + prompt.once('invalid', function () { + prompt.once('prompt', function () { + process.nextTick(function () { + helpers.stdin.writeNextTick('42\n'); + }) + }) + }); + + helpers.stdin.writeNextTick('4.2\n'); + }, + "should prompt with an error before completing the operation": function (err, input) { + assert.isNull(err); + assert.equal(input, '42'); + assert.isTrue(this.errmsg.indexOf('Invalid input') !== -1); + assert.isTrue(this.msg.indexOf('integer') !== -1); + } + } + } + } + } +}).addBatch({ + "When using prompt": { + "the getInput() method": { + "with a boolean field": { + "and we provide valid input": { + topic: function () { + var that = this; + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + prompt.getInput(grab('boolean'), this.callback); + helpers.stdin.writeNextTick('true\n'); + }, + "should prompt to stdout and respond with data": function (err, input) { + assert.isNull(err); + assert.equal(input, true); + assert.isTrue(this.msg.indexOf('boolean') !== -1); + } + }, + } + } + } +}).addBatch({ + "When using prompt": { + "the getInput() method": { + "with a boolean field": { + "and we don't provide an bool": { + topic: function () { + var that = this; + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + helpers.stderr.once('data', function (msg) { + that.errmsg = msg; + }) + + prompt.getInput(grab('boolean'), this.callback); + + prompt.once('invalid', function () { + prompt.once('prompt', function () { + process.nextTick(function () { + helpers.stdin.writeNextTick('F\n'); + }) + }) + }); + + helpers.stdin.writeNextTick('4.2\n'); + }, + "should prompt with an error before completing the operation": function (err, input) { + assert.isNull(err); + assert.equal(input, false); + assert.isTrue(this.errmsg.indexOf('Invalid input') !== -1); + assert.isTrue(this.msg.indexOf('boolean') !== -1); + } + } + } + } + } +}).addBatch({ + "When using prompt": { + "the getInput() method": { + "with a complex property prompt": { + "and a valid input": { + topic: function () { + var that = this; + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + prompt.getInput(grab('username'), this.callback); + helpers.stdin.writeNextTick('some-user\n'); + }, + "should prompt to stdout and respond with data": function (err, input) { + assert.isNull(err); + assert.equal(input, 'some-user'); + assert.isTrue(this.msg.indexOf('username') !== -1); + } + } + } + } + } +}).addBatch({ + "When using prompt": { + "the getInput() method": { + "with a complex property prompt": { + "and an invalid input": { + topic: function () { + var that = this; + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + helpers.stderr.once('data', function (msg) { + that.errmsg = msg; + }) + + prompt.getInput(grab('username'), this.callback); + + prompt.once('invalid', function () { + prompt.once('prompt', function () { + process.nextTick(function () { + helpers.stdin.writeNextTick('some-user\n'); + }) + }) + }); + + helpers.stdin.writeNextTick('some -user\n'); + }, + "should prompt with an error before completing the operation": function (err, input) { + assert.isNull(err); + assert.equal(input, 'some-user'); + assert.isTrue(this.errmsg.indexOf('Username can only be letters, numbers, and dashes') !== -1); + assert.isTrue(this.msg.indexOf('username') !== -1); + } + } + } + } + } +}).addBatch({ + "When using prompt": { + "the getInput() method": { + "with a complex property prompt": { + "with an invalid validator (array)": { + topic: function () { + var that = this, + called; + + prompt.getInput(grab('badValidator'), function (err) { + if (!called) { + called = true; + that.callback(err); + } + }); + helpers.stdin.writeNextTick('some-user\n'); + }, + "should respond with an error": function (err, ign) { + assert.isTrue(!!err); + } + } + } + } + } +}).addBatch({ + "When using prompt": { + "the get() method": { + "with a simple string prompt": { + "that is not a property in prompt.properties": { + topic: function () { + var that = this; + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }) + + prompt.get('test input', this.callback); + helpers.stdin.writeNextTick('test value\n'); + }, + "should prompt to stdout and respond with the value": function (err, result) { + assert.isNull(err); + assert.include(result, 'test input'); + assert.equal(result['test input'], 'test value'); + assert.isTrue(this.msg.indexOf('test input') !== -1); + } + }, + } + } + } +}).addBatch({ + "When using prompt": { + "the get() method": { + "with a simple string prompt": { + "that is a property name in prompt.properties": { + "with a default value": { + topic: function () { + var that = this; + + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + prompt.properties.riffwabbles = schema.properties.riffwabbles; + prompt.get('riffwabbles', this.callback); + helpers.stdin.writeNextTick('\n'); + }, + "should prompt to stdout and respond with the default value": function (err, result) { + assert.isNull(err); + assert.isTrue(this.msg.indexOf('riffwabbles') !== -1); + assert.isTrue(this.msg.indexOf('(foobizzles)') !== -1); + assert.include(result, 'riffwabbles'); + assert.equal(result['riffwabbles'], schema.properties['riffwabbles'].default); + } + }, + } + } + } + } +}).addBatch({ + "When using prompt": { + "the get() method": { + "with a simple string prompt": { + "that is a property name in prompt.properties": { + "that expects a numeric value": { + "and gets valid input": { + topic: function () { + var that = this; + + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + prompt.properties.number = schema.properties.number; + prompt.get('number', this.callback); + helpers.stdin.writeNextTick('15\n'); + }, + "should prompt to stdout and respond with a numeric value": function (err, result) { + assert.isNull(err); + assert.include(result, 'number'); + assert.equal(result['number'], 15); + } + } + } + } + } + } + } +}).addBatch({ + "When using prompt": { + "the get() method": { + "with a simple string prompt": { + "that is a property name in prompt.properties": { + "with a sync function validator (.validator)": { + topic: function () { + var that = this; + + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + prompt.get(helpers.schema.properties.fnvalidator, this.callback); + helpers.stdin.writeNextTick('fn123\n'); + }, + "should accept a value that is checked": function (err, result) { + assert.isNull(err); + assert.equal(result['fnvalidator'],'fn123'); + } + } + } + } + } + } +}).addBatch({ + "When using prompt": { + "the get() method": { + "with a simple string prompt": { + "that is a property name in prompt.properties": { + "with a sync function validator (.conform)": { + topic: function () { + var that = this; + + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + prompt.get(grab('fnconform'), this.callback); + helpers.stdin.writeNextTick('fn123\n'); + }, + "should accept a value that is checked": function (err, result) { + assert.isNull(err); + assert.equal(result['fnconform'],'fn123'); + } + } + // + // Remark Does not work with revalidator + // + // "with a callback validator": { + // topic: function () { + // var that = this; + // + // helpers.stdout.once('data', function (msg) { + // that.msg = msg; + // }); + // + // prompt.get(grab('cbvalidator'), this.callback); + // helpers.stdin.writeNextTick('cb123\n'); + // }, + // "should not accept a value that is correct": function (err, result) { + // assert.isNull(err); + // assert.equal(result['cbvalidator'],'cb123'); + // } + // } + } + }, + } + } +}).addBatch({ + "When using prompt": { + "the get() method": { + "with a simple string prompt": { + "that is a property name in prompt.properties": { + "with a sync function before (.before)": { + topic: function() { + var that = this; + + helpers.stdout.once('data', function(msg) { + that.msg = msg; + }); + + prompt.get({ + properties: { + fnbefore: { + before: function(v) { + return 'v' + v; + } + } + } + }, this.callback); + helpers.stdin.writeNextTick('fn456\n'); + }, + "should modify user's input": function(e, result) { + assert.equal(result.fnbefore, 'vfn456'); + } + } + } + } + } + } +}).addBatch({ + "When using prompt": { + "the get() method": { + "skip prompt with prompt.overide": { + topic: function () { + prompt.override = { coconihet: 'whatever' } + prompt.get('coconihet', this.callback); + }, + "skips prompt and uses overide": function (err, results) { + assert.equal(results.coconihet, 'whatever') + } + } + } + } +}).addBatch({ + "When using prompt": { + "the addProperties() method": { + topic: function () { + prompt.addProperties({}, ['foo', 'bar'], this.callback); + helpers.stdin.writeSequence(['foo\n', 'bar\n']); + }, + "should add the properties to the object": function (err, obj) { + assert.isNull(err); + assert.isObject(obj); + assert.equal(obj.foo, 'foo'); + assert.equal(obj.bar, 'bar'); + } + } + } +}).addBatch({ + "When using prompt": { + "the get() method": { + "with old schema": { + topic: function () { + var that = this; + + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + prompt.properties.username = schema.properties.oldschema; + prompt.get('username', this.callback); + + helpers.stdin.writeSequence(['\n', 'hell$\n', 'hello\n']); + }, + "should prompt to stdout and respond with the default value": function (err, result) { + assert.isNull(err); + assert.isTrue(this.msg.indexOf('username') !== -1); + assert.include(result, 'username'); + assert.equal(result.username, 'hello'); + } + } + } + } +}).addBatch({ + "When using prompt": { + "the history() method": { + "when used inside of a complex property": { + "with correct value(s)": { + topic: function () { + prompt.get([grab('animal'), grab('sound')], this.callback); + helpers.stdin.writeSequence(['dog\n', 'woof\n']); + }, + "should respond with the values entered": function (err, result) { + assert.isTrue(!err); + assert.equal(result.animal, 'dog'); + assert.equal(result.sound, 'woof'); + assert.equal(prompt.history('nothing'), null); + assert.deepEqual(prompt.history('animal'), { property: 'animal', value: 'dog' }); + } + }, + } + } + } +}).addBatch({ + "When using prompt": { + "the history() method": { + "when used inside of a complex property": { + "with an incorrect value": { + topic: function () { + prompt.get([grab('animal'), grab('sound')], function () {}); + prompt.once('invalid', this.callback.bind(null, null)); + helpers.stdin.writeSequence(['dog\n', 'meow\n']); + }, + "should prompt for the error": function (ign, property, line) { + assert.equal(property.path.join(''), 'sound'); + assert.equal(line, 'meow'); + } + } + } + } + } +}).addBatch({ + "when using prompt": { + "the get() method": { + topic: function () { + prompt.override = { xyz: 468, abc: 123 } + prompt.get(['xyz', 'abc'], this.callback); + }, + "should respond with overrides": function (err, results) { + assert.isNull(err); + assert.deepEqual(results, { xyz: 468, abc: 123 }); + } + } + } +}).addBatch({ + "When using prompt": { + "with fancy properties": { + "the get() method": { + topic: function () { + prompt.override = { UVW: 5423, DEF: 64235 } + prompt.get({ + properties: { + 'UVW': { + description: 'a custom message', + default: 6 + }, + 'DEF': { + description: 'a custom message', + default: 6 + } + } + }, this.callback); + }, + "should respond with overrides": function (err, results) { + assert.isNull(err); + assert.deepEqual(results, { UVW: 5423, DEF: 64235 }); + } + } + } + } +}).addBatch({ + "When using prompt": { + "with a type and a description property": { + "the get() method": { + topic: function () { + var that = this; + helpers.stdout.once('data', function (msg) { + that.msg = msg; + }); + + prompt.get({ + name: 'test', + type: 'number', + description: 'Please input a number' + }, this.callback); + helpers.stdin.writeNextTick('42\n'); + }, + "should prompt to stdout and respond with the value": function (err, result) { + assert.isNull(err); + assert.include(result, 'test'); + assert.equal(result['test'], '42'); + assert.isTrue(this.msg.indexOf('Please input a number') !== -1); + } + }, + } + } + }) +.addBatch( + macros.shouldConfirm({ + messages: ['with a string message'], + prop: 'test', + response: 'Y' + }) +).addBatch( + macros.shouldConfirm({ + messages: ['with an empty string and default yes'], + prop: 'test', + response: '' + }, { + default: 'yes' + }) +).addBatch( + macros.shouldNotConfirm({ + messages: ['with a string message'], + prop: 'test', + response: 'N' + }) +).addBatch( + macros.shouldConfirm({ + messages: ['with a string message'], + prop: 'test', + response: 'YES' + }) +).addBatch( + macros.shouldNotConfirm({ + messages: ['with a string message'], + prop: 'test', + response: 'NO' + }) +).addBatch( + macros.shouldConfirm({ + messages: ['with a string message'], + prop: 'test', + response: 'T' + }) +).addBatch( + macros.shouldNotConfirm({ + messages: ['with a string message'], + prop: 'test', + response: 'F' + }) +).addBatch( + macros.shouldConfirm({ + messages: ['with a string message'], + prop: 'test', + response: 'TRUE' + }) +).addBatch( + macros.shouldNotConfirm({ + messages: ['with a string message'], + prop: 'test', + response: 'FALSE' + }) +).addBatch( + macros.shouldConfirm({ + messages: ['with an object', 'and description set'], + prop: { description: 'a custom message' }, + response: 'Y' + }) +).addBatch( + macros.shouldConfirm({ + messages: ['with an object', 'and they forgot the description'], + prop: { description: 'a custom message' }, + response: 'Y' + }) +).addBatch( + macros.shouldConfirm({ + messages: ['with an object', 'and custom validators'], + prop: { + description: 'node or jitsu?', + pattern: /^(node|jitsu)/i, + yes: /^node/i + }, + response: 'node' + }) +).addBatch( + macros.shouldNotConfirm({ + messages: ['with an object', 'and custom validators'], + prop: { + description: 'node or jitsu?', + pattern: /^(node|jitsu)/i, + yes: /^node/i + }, + response: 'jitsu' + }) +).addBatch( + macros.shouldConfirm({ + messages: ['with multiple strings'], + prop: ["test", "test2", "test3"], + response: ['Y\n', 'y\n', 'YES\n'] + }) +).addBatch( + macros.shouldNotConfirm({ + messages: ['with multiple strings'], + prop: ["test", "test2", "test3"], + response: ['Y\n', 'N\n', 'YES\n'] + }) +).addBatch( + macros.shouldNotConfirm({ + messages: ['with multiple strings'], + prop: ["test", "test2", "test3"], + response: ['n\n', 'NO\n', 'N\n'] + }) +).addBatch( + macros.shouldConfirm({ + messages: ['with multiple objects'], + prop: [ + { message: "test" }, + { message: "test2" } + ], + response: ['y\n', 'y\n'] + }) +).addBatch( + macros.shouldNotConfirm({ + messages: ['with multiple objects'], + prop: [ + { message: "test" }, + { message: "test2" } + ], + response: ['n\n', 'n\n'] + }) +).addBatch( + macros.shouldNotConfirm({ + messages: ['with multiple objects'], + prop: [ + { message: "test" }, + { message: "test2" } + ], + response: ['n\n', 'y\n'] + }) +).export(module); |