forked from NickMarinade/cowsaydex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (91 loc) · 2.5 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
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
// ----- require dependencies -----
const cowsay = require('cowsay')
const fs = require('fs')
// ----- declare constants -----
const COWSAID_DIRNAME = 'cowsaid'
const SUPPORTED_VERBS = [
'say',
'think'
]
const DEFAULT_CONFIG = {
text: 'I cow hard',
e: 'oo',
T: ' ',
f: 'default',
file: (new Date()).toJSON() + '.txt',
verb: 'say'
}
const DOC_STRING = `CONFG OPTIONS
text:"lorem ipsum" -- what the cow should say
default -- I cow hard
e:"oo" -- the cow's eyes.
not all cowfiles have customizable eyes
2 characters only is recommended
default -- "oo"
T:" " -- the cow's tongue
not all cowfiles have customizable tongues
2 characters only is recommended
default -- " "
f:"bees" -- the cowfile to render
default -- default
file:fileName.txt -- the file name to render to
default -- (new Date()).toJSON() + ".txt"
verb:"say" -- does the cow say or think?
only "say" and "think" are supported
default -- "say"
NOTE: any extra options are ignored
FLAGS
-h -- prints this helpful message
-l -- prints all possible cows
if either flag is passed as an argument, no cowsay will be generated`
// ----- begin main program script -----
// --- process user input ---
// create an array containing only the user-provided CLI arguments
const userArgs = _
// --- handle tech support ---
if (userArgs.includes('-h')) {
console.log(DOC_STRING)
// like an early return, quit the program
process.exit(0)
}
if (userArgs.includes('-l')) {
cowsay.list((err, cows) => {
console.log(cows.join('\n'))
})
// like an early return, quit the program
process.exit(0)
}
// --- generate cowsay ---
// parse the CLI arguments from the user into a config object
const userConfig = _
/* example:
user arguments ...
[ "text=cows be like : moooooo", "file=readme.txt" ]
become ...
{
text: "cows be like : moooooo",
file: "readme.txt"
}
*/
if (!SUPPORTED_VERBS.includes(userConfig.verb)) {
console.log(userConfig.verb + ' is not a supported verb, the cow will "say"');
delete userConfig.verb
}
const finalConfig = Object.assign({}, DEFAULT_CONFIG, userConfig)
const thisCow = cowsay[finalConfig.verb]({
text: _,
e: _,
T: _,
f: _,
})
console.log(thisCow)
// --- preserve cowsay for future generations ---
try {
fs.accessSync('./' + COWSAID_DIRNAME)
} catch (err) {
console.log('--- creating ./' + COWSAID_DIRNAME + ' directory ---')
fs.mkdirSync('./' + COWSAID_DIRNAME)
}
// write thisCow to a new file using COWSAID_DIRNAME and finalConfig.file
fs._
// ----- end main program script -----