├── .npmignore ├── .gitignore ├── package.json ├── LICENSE ├── test └── cli.spec.js ├── index.js └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | test 2 | .gitignore 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/node 2 | 3 | ### Node ### 4 | # Logs 5 | logs 6 | *.log 7 | npm-debug.log* 8 | 9 | # Runtime data 10 | pids 11 | *.pid 12 | *.seed 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directory 30 | # https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git 31 | node_modules 32 | 33 | # IntelliJ configuration 34 | .idea 35 | *.iml 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chance-cli", 3 | "version": "1.0.7", 4 | "description": "Chance CLI - random generator for the command line", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": "chancejs/chancejs-cli", 10 | "bin": { 11 | "chance": "./index.js" 12 | }, 13 | "preferGlobal": true, 14 | "keywords": [ 15 | "random", 16 | "chance", 17 | "cli", 18 | "terminal", 19 | "generator" 20 | ], 21 | "author": "Victor Quinn ", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/chancejs/chancejs-cli/issues" 25 | }, 26 | "homepage": "https://github.com/chancejs/chancejs-cli#readme", 27 | "dependencies": { 28 | "chalk": "^1.1.1", 29 | "chance": "^1.0.10", 30 | "commander": "^2.8.1", 31 | "minimist": "^1.2.0", 32 | "ramda": "^0.17.1" 33 | }, 34 | "devDependencies": { 35 | "chai": "^3.5.0", 36 | "mocha": "^2.4.5" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Victor Quinn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /test/cli.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const expect = require('chai').expect; 4 | const spawn = require('child_process').spawn; 5 | 6 | const Chance = require('chance'); 7 | 8 | const configurations = [ 9 | makeConfiguration('23', 'string', { }), 10 | makeConfiguration('23', 'string', { length: 11 }), 11 | makeConfiguration('23', 'name', { }), 12 | makeConfiguration('23', 'name', { middle_initial: true }), 13 | makeConfiguration('23', 'name', { suffix: true }), 14 | makeConfiguration('23', 'age', { }), 15 | makeConfiguration('23', 'ip', { }), 16 | makeConfiguration('23', 'twitter', { }), 17 | makeConfiguration('23', 'guid', { }), 18 | makeConfiguration('23', 'country', { }) 19 | ]; 20 | 21 | describe('Output', () => { 22 | configurations.forEach((config) => { 23 | it(`matches Chance result for \`${config.commandLine}\``, (done) => { 24 | const expectedResult = chance(config.seed, config.name, config.options); 25 | executeWithExpectedOutput(config.args, String(expectedResult), done); 26 | }); 27 | }); 28 | }); 29 | 30 | function chance(seed, generator, options) { 31 | return Chance(seed)[generator](options); 32 | } 33 | 34 | function executeWithExpectedOutput(args, expectedOutput, done) { 35 | execute(args, (code, output) => { 36 | expect(code).to.equal(0); 37 | expect(output).to.equal(expectedOutput); 38 | done(); 39 | }); 40 | } 41 | 42 | function makeConfiguration(seed, name, options) { 43 | const argsWithoutSeed = [ name ].concat(makeCommandArguments(options)); 44 | const args = ['--seed', seed].concat(argsWithoutSeed); 45 | const commandLine = argsWithoutSeed.join(' '); 46 | 47 | return { 48 | seed: seed, 49 | name: name, 50 | options: options, 51 | args: args, 52 | commandLine: commandLine 53 | } 54 | } 55 | 56 | function makeCommandArguments(options) { 57 | const result = []; 58 | for (const propertyName in options) { 59 | if (options.hasOwnProperty(propertyName)) { 60 | result.push('--' + propertyName); 61 | result.push(String(options[propertyName])); 62 | } 63 | } 64 | return result; 65 | } 66 | 67 | function execute(args, onFinished) { 68 | const app = spawn("node", ['index.js'].concat(args)); 69 | 70 | let output = ''; 71 | 72 | app.stdout.on('data', (data) => { 73 | output += data; 74 | }); 75 | 76 | app.stderr.on('data', (data) => { 77 | output += data; 78 | }); 79 | 80 | app.on('close', (code) => { 81 | onFinished(code, output); 82 | }); 83 | } 84 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var Chance = require('chance'); 4 | var argv = require('minimist')(process.argv.slice(2), { string:'pool' }); 5 | var chalk = require('chalk'); 6 | var R = require('ramda'); 7 | 8 | var program = require('commander'); 9 | 10 | program 11 | .version('0.0.1') 12 | .option('-s, --seed [seed]', 'seed for the random number generator') 13 | .arguments(' [args...]') 14 | .action(function(generator, args, program) { 15 | var chance = program.seed ? new Chance(program.seed) : new Chance(); 16 | 17 | var result = null; 18 | var error = null; 19 | var options = require('minimist')(program.rawArgs.slice(3), { string: 'pool' }); 20 | 21 | // Note, this is potentially dangerous as it prevents users from having 22 | // option values that are "true" and "false" but likely does more harm 23 | // than good. Unless we want to parse each option explicitly, which would 24 | // require this CLI knowing more about the internals of Chance, this is 25 | // the best we can probably do for now. And since the majority of options 26 | // are boolean, seems better to get them right and disallow "true" and 27 | // "false" than to get them wrong and allow those as option values. 28 | 29 | // Remove this minimist quirk 30 | delete options["_"]; 31 | options = R.mapObj(function(item) { 32 | if (item === 'true') { 33 | return true; 34 | } else if (item === 'false') { 35 | return false; 36 | } else if (!isNaN(parseFloat(item, 10))) { 37 | return parseFloat(item); 38 | } else { 39 | return item; 40 | } 41 | }, options); 42 | 43 | if (generator && chance[generator]) { 44 | result = chance[generator](options); 45 | } else if (generator === undefined) { 46 | 47 | } else { 48 | error = 'Chance: unknown generator "' + generator + '"'; 49 | } 50 | 51 | if (error !== null) { 52 | if (process.stdout.istty) { 53 | error = chalk.red(error); 54 | } 55 | process.stderr.write(error); 56 | } else { 57 | // We can only print out a string, so cast if number 58 | if (!R.is(String, result)) { 59 | result = String(result); 60 | } 61 | process.stdout.write(result); 62 | } 63 | }) 64 | .parse(process.argv); 65 | 66 | if (program.args.length < 1) { 67 | process.stderr.write('Chance: you must supply a generator'); 68 | } 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Chance CLI 2 | 3 | [![Chance Logo](http://chancejs.com/logo.png)](http://chancejs.com) 4 | 5 | [![GitHub license](https://img.shields.io/github/license/chancejs/chancejs-cli.svg)](https://github.com/chancejs/chance-cli/blob/master/LICENSE) 6 | [![GitHub stars](https://img.shields.io/github/stars/chancejs/chancejs-cli.svg)](https://github.com/chancejs/chancejs-cli) 7 | [![npm](https://img.shields.io/npm/dm/chance-cli.svg)](https://npmjs.com/package/chance-cli) 8 | [![npm](https://img.shields.io/npm/v/chance-cli.svg)](https://npmjs.com/package/chance-cli) 9 | [![awesomeness](https://img.shields.io/badge/awesomeness-maximum-red.svg)](https://github.com/chancejs/chancejs) 10 | 11 | Chance CLI - random generator for the command line 12 | 13 | Chance documentation: [http://chancejs.com](http://chancejs.com) 14 | 15 | ## Installation 16 | 17 | Install globally so it can be used anywhere on your system: 18 | 19 | `npm install -g chance-cli` 20 | 21 | Then just use it! 22 | 23 | ```bash 24 | $ chance sentence 25 | Sikolnul risuz issi ah re gul unagacire mo fu nuuwazek wihnus tagit bolome. 26 | ``` 27 | 28 | ## Usage 29 | 30 | First invoke it with its keyword, `chance` 31 | 32 | Then follow it with the name of the generator you'd like from the [docs](http://chancejs.com) 33 | 34 | Then any options follow as flags. 35 | 36 | Can optionally pass a seed with the `--seed` flag for repeatable results 37 | 38 | ```bash 39 | $ chance --seed 23 name 40 | Edna Flores 41 | $ chance --seed 23 name 42 | Edna Flores 43 | ``` 44 | 45 | ## Examples 46 | 47 | ```bash 48 | # Just the generator 49 | $ chance name 50 | Steven Nguyen 51 | 52 | # Now add an option 53 | $ chance name --middle_initial true 54 | Ethel M. Barnett 55 | 56 | # Now 2 options 57 | $ chance name --middle_initial true --suffix true 58 | Maria C. Daniel Esq. 59 | 60 | # Miscellaneous random examples 61 | $ chance floating --min 0 --max 100 62 | 11.3463 63 | 64 | $ chance ip 65 | 35.108.104.238 66 | 67 | $ chance hammertime 68 | 1770301460334 69 | 70 | $ chance twitter 71 | @mekogza 72 | 73 | $ chance guid 74 | f76e0aa4-bd9f-5343-b7bf-66572ba5669b 75 | ``` 76 | 77 | ## Tips/Tricks 78 | 79 | On a Mac? Copy directly from the command line to your clipboard by piping 80 | to `pbcopy` the Mac clipboard utility 81 | 82 | ```bash 83 | $ chance hash | pbcopy 84 | # d1db9ef9ecee059f528d36c200534f34f474e144 is now your clipboard 85 | ``` 86 | 87 | ## Author 88 | 89 | ### Victor Quinn 90 | 91 | | [GitHub](https://github.com/chancejs) | [http://victorquinn.com](http://victorquinn.com) | [OneName](https://onename.com/victor) 92 | --------------------------------------------------------------------------------