-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
68 lines (58 loc) · 2.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env node
var Chance = require('chance');
var argv = require('minimist')(process.argv.slice(2), { string:'pool' });
var chalk = require('chalk');
var R = require('ramda');
var program = require('commander');
program
.version('0.0.1')
.option('-s, --seed [seed]', 'seed for the random number generator')
.arguments('<generator> [args...]')
.action(function(generator, args, program) {
var chance = program.seed ? new Chance(program.seed) : new Chance();
var result = null;
var error = null;
var options = require('minimist')(program.rawArgs.slice(3), { string: 'pool' });
// Note, this is potentially dangerous as it prevents users from having
// option values that are "true" and "false" but likely does more harm
// than good. Unless we want to parse each option explicitly, which would
// require this CLI knowing more about the internals of Chance, this is
// the best we can probably do for now. And since the majority of options
// are boolean, seems better to get them right and disallow "true" and
// "false" than to get them wrong and allow those as option values.
// Remove this minimist quirk
delete options["_"];
options = R.mapObj(function(item) {
if (item === 'true') {
return true;
} else if (item === 'false') {
return false;
} else if (!isNaN(parseFloat(item, 10))) {
return parseFloat(item);
} else {
return item;
}
}, options);
if (generator && chance[generator]) {
result = chance[generator](options);
} else if (generator === undefined) {
} else {
error = 'Chance: unknown generator "' + generator + '"';
}
if (error !== null) {
if (process.stdout.istty) {
error = chalk.red(error);
}
process.stderr.write(error);
} else {
// We can only print out a string, so cast if number
if (!R.is(String, result)) {
result = String(result);
}
process.stdout.write(result);
}
})
.parse(process.argv);
if (program.args.length < 1) {
process.stderr.write('Chance: you must supply a generator');
}