-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcodename-generator.js
More file actions
198 lines (173 loc) · 6.93 KB
/
Copy pathcodename-generator.js
File metadata and controls
198 lines (173 loc) · 6.93 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env node
import debugModule from 'debug';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import chalk from 'chalk';
import PrettyError from 'pretty-error';
import fs from 'fs';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { helpScreen } from './help.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const debug = debugModule('codename-generator');
debug('Entry: [%s]', __filename);
// command line options parser
const argv = yargs(hideBin(process.argv))
.help(false)
.argv;
// Error formatting
const pe = new PrettyError();
// Constants
const DEFAULT_CONSOLE_ROWS = 25;
const CONSOLE_ROWS_MARGIN = 3;
const MAX_ITERATIONS = 1000;
const MIN_ITERATIONS = 1;
// Define running modes
const runningMode = {
normal: 0,
nsfw: 1
};
let nsfwSelection = null;
const nsfwSelectionChoices = {
adjective: 0,
noun: 1,
both: 2
};
// set default running mode
let mode = runningMode.normal;
function randomRange(minimum, maximum) {
try {
const randomNumber = Math.floor(Math.random() * (maximum - minimum + 1) + minimum);
// debug(`Random Number: ${randomNumber} (in range ${minimum} - ${maximum})`);
return randomNumber;
} catch (error) {
console.error(chalk.red('Error in randomRange:'), error.message);
debug('randomRange error details: %O', error);
return minimum;
}
}
function getRandomWord(wordList) {
try {
const randomWordIndex = randomRange(0, wordList.length - 1);
const randomWord = wordList[randomWordIndex];
return randomWord;
} catch (error) {
console.error(chalk.red('Error in getRandomWord:'), error.message);
debug('getRandomWord error details: %O', error);
return 'null';
}
}
// Main function
function main() {
try {
// Check for 'help' command line parameters
if (argv.help) {
debug('--help detected. Showing help screen.');
// Show help screen
helpScreen(argv.verbose);
return 0;
}
// Check for --list-nouns or --list-adjectives
if (argv.listNouns || argv.listAdjectives) {
let 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]));
}
return 0;
}
// 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 ?? DEFAULT_CONSOLE_ROWS;
let iterations = consoleRows - CONSOLE_ROWS_MARGIN;
debug(`Setting default iterations to (${consoleRows} - ${CONSOLE_ROWS_MARGIN}) = ${iterations}`);
// Check if a specific number was provided
if (Number.isInteger(Number(process.argv[2]))) {
const requestedIterations = Number(process.argv[2]);
// Validate the number is within acceptable range
if (requestedIterations < MIN_ITERATIONS) {
console.error(chalk.red(`Error: Number must be greater than or equal to ${MIN_ITERATIONS}`));
return 1;
}
if (requestedIterations > MAX_ITERATIONS) {
console.error(chalk.red(`Error: Number must be ${MAX_ITERATIONS} or less`));
return 1;
}
// Update iterations
iterations = requestedIterations;
debug(`Iterations overridden to ${iterations}`);
console.log(chalk.grey(`Generating ${iterations} code names...`));
}
// Read list of adjectives and nouns
debug(`Reading file: ${__dirname}/adjectives.json`);
const adjectives = JSON.parse(fs.readFileSync(`${__dirname}/adjectives.json`));
debug(`Reading file: ${__dirname}/nouns.json`);
const nouns = JSON.parse(fs.readFileSync(`${__dirname}/nouns.json`));
let adjectivesNSFW = {};
let nounsNSFW = {};
if (mode === runningMode.nsfw) {
// Also read in nsfw files
try {
debug('NSFW: Not Safe for Work mode enabled.');
debug(`Reading file: ${__dirname}/adjectives.nsfw.json`);
adjectivesNSFW = JSON.parse(fs.readFileSync(`${__dirname}/adjectives.nsfw.json`));
debug(`Reading file: ${__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: ${i + 1} of ${iterations}`);
if (runningMode.nsfw) {
// We're running in NSFW mode, so choose whether to generate a rude adjective or noun in this iteration
nsfwSelection = randomRange(0, 1);
}
// 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: ${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: ${noun}`);
// Output code name
console.log(chalk.bold(' %s %s'), adjective, noun);
}
return 0;
} catch (error) {
console.log(pe.render(error));
return 1;
}
}
// Execute main and exit with the return code
process.exit(main());