-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcodename-generator.js
173 lines (149 loc) · 5.51 KB
/
codename-generator.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env node
const debug = require('debug')('codename-generator');
debug('Entry: [%s]', __filename);
// command line options parser
var argv = require('yargs')
.help(false)
.argv;
// console colours
const chalk = require('chalk');
// Error formatting
const PrettyError = require('pretty-error');
const pe = new PrettyError();
// Define running modes
const runningMode = {
normal: 0,
nsfw: 1
};
var nsfwSelection = null;
const nsfwSelectionChoices = {
adjective: 0,
noun: 1,
both: 2
};
// set default running mode
var mode = runningMode.normal;
function randomRange(minimum, maximum) {
try {
var randomNumber = Math.floor(Math.random() * (maximum - minimum + 1) + minimum);
// debug('Random Number: %s (in range %s - %s)', randomNumber, minimum, maximum);
return(randomNumber);
} catch (error) {
console.error(pe.render(error));
return(minimum);
}
}
function getRandomWord(wordList) {
try {
let randomWordIndex = randomRange(0, wordList.length - 1);
let randomWord = wordList[randomWordIndex];
return(randomWord);
} catch (error) {
console.error(pe.render(error));
return('null');
}
}
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
// Main()
try {
// Check for 'help' command line parameters
if (argv.help) {
debug('--help detected. Showing help screen.');
// Show help screen
const help = require('./help');
help.helpScreen(argv.verbose);
process.exit();
}
// Check for --list-nouns or --list-adjectives
if (argv.listNouns || argv.listAdjectives) {
const fs = require('fs');
var words = '';
if (argv.listNouns) {
// Read list of nouns
debug('--list-nouns detected');
words = JSON.parse(fs.readFileSync(__dirname + '/nouns.json'));
} else {
// Read list of adjectives
debug('--list-adjectives detected');
words = JSON.parse(fs.readFileSync(__dirname + '/adjectives.json'));
}
// Iterate through word list
for (let i = 0; i < words.length; i++) {
let count = (i + 1).toString();
// Echo current word to the console
console.log('%s %s', chalk.dim(count.padEnd(4, ' ')), chalk.bold(words[i]));
}
// Exit
process.exit();
}
// Check for NSFW flag
if ((argv.nsw) || (argv.nsfw)) {
// Switch running mode to NSFW
mode = runningMode.nsfw;
}
// Default number of codewords to generate; The number of displayable rows in the console,
// minus some room at the end so the console prompt doesn't scroll the first code name
const consoleRows = process.stdout.rows;
var iterations = consoleRows - 3;
debug('Setting default iterations to (%s - 3) = %s', consoleRows, iterations);
// Check if a specific number was provided
if (Number.isInteger(Number(process.argv[2]))) {
// Update iterations
iterations = Number(process.argv[2]);
debug('Iterations overridden to %s', iterations);
console.log(chalk.grey('Generating %d code names...'), iterations);
}
const fs = require('fs');
// Read list of adjectives and nouns
debug('Reading file: %s', __dirname + '/adjectives.json');
const adjectives = JSON.parse(fs.readFileSync(__dirname + '/adjectives.json'));
debug('Reading file: %s', __dirname + '/nouns.json');
const nouns = JSON.parse(fs.readFileSync(__dirname + '/nouns.json'));
var adjectivesNSFW = {};
var nounsNSFW = {};
if (mode === runningMode.nsfw) {
// Also read in nsfw files
try {
debug('NSFW: Not Safe for Work mode enabled.');
debug('Reading file: %s', __dirname + '/adjectives.nsfw.json');
adjectivesNSFW = JSON.parse(fs.readFileSync(__dirname + '/adjectives.nsfw.json'));
debug('Reading file: %s', __dirname + '/nouns.nsfw.json');
nounsNSFW = JSON.parse(fs.readFileSync(__dirname + '/nouns.nsfw.json'));
} catch (error) {
debug('An error occurred trying to load nsfw content: %O', error);
// Switch running mode back to normal as nsfw content didn't load
mode = runningMode.normal;
}
}
for (let i = 0; i < iterations; i++) {
let adjective= '';
let noun = '';
debug('Iteration: %s of %s', i + 1, iterations);
if (runningMode.nsfw) {
// We're running in NSFW mode, so choose whether to generate a rude adjective or noun in this iteration
nsfwSelection = getRandomInt(2);
}
// Get random adjective
if ((mode === runningMode.nsfw) && (nsfwSelection === nsfwSelectionChoices.adjective)) {
// NSFW mode is enabled and a 50/50 chance of using a profanity came up true
adjective = getRandomWord(adjectivesNSFW);
} else {
adjective = getRandomWord(adjectives);
}
debug('Adjective: %s', adjective);
// Get random noun
if ((mode === runningMode.nsfw) && (nsfwSelection === nsfwSelectionChoices.noun)) {
// NSFW mode is enabled and a 50/50 chance of using a profanity came up true
noun = getRandomWord(nounsNSFW);
} else {
noun = getRandomWord(nouns);
}
debug('Noun: %s', noun);
// Output code name
console.log(chalk.bold(' %s %s'), adjective, noun);
}
} catch (error) {
console.log(pe.render(error));
}