-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhdapi.js
More file actions
68 lines (60 loc) · 2.12 KB
/
Copy pathhdapi.js
File metadata and controls
68 lines (60 loc) · 2.12 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
/* Herp Derp API in node.js
*
* This should eventually spin up as a proper server with API but for the
* time being we'll just feed text files into hdapi.js and this will
* herp-derp-ify them.
*
* TODO:
* - make herpDerpify better (see notes there)
* - break herpDerpify off into a module
* - mess with import vs require
* - error handling
* - testing!
*/
const Promise = require('bluebird');
// note - this turns fs.readFile into fs.readFileAsync
const fs = Promise.promisifyAll(require('fs'));
const commandLineArgs = require('command-line-args');
// Define our command line options
const optionDefinitions = [
{ name: 'input', type: String, multiple: true, defaultOption: true },
]
// Suck in our options, if we have any. You could also just use
// process.argv but this is a little easier.
const options = commandLineArgs(optionDefinitions);
// Run this as command line if we've got files as input
if (options.input) {
console.log("...Running as command line");
console.log("...Processing files " + options.input + "\n\n");
// Iterate over all of our text inputs
options.input.forEach((entry) => {
// Read in our file
fs.readFileAsync(entry, 'utf8')
// HerpDerpify the file
.then((data) => {
return herpDerpfiy(data);
})
// Spit back out the HerpDerpified version
.then((data) => {
console.log(data);
})
})
// Otherwise, run as a server
} else {
// TODO - change this to bunyan or winston logging
console.log("Running as server");
}
// Split up some text and add some herps and derps.
function herpDerpfiy(text) {
return new Promise(function(resolve, reject) {
// note - global replace requires a regular expression
// Change all periods not followed by a word.
const re_period = /\.\W/g;
text = text.replace(re_period, '. Herp derp. ');
// Change all commas to herp.
const re_comma = /, /g;
// TODO - you could change this to a function so that it's rotating 'herp' and 'derp'.
text = text.replace(re_comma, ', herp, ');
resolve(text);
})
}