-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeText.js
More file actions
55 lines (45 loc) · 1.57 KB
/
Copy pathmakeText.js
File metadata and controls
55 lines (45 loc) · 1.57 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
/** Command-line tool to generate Markov text. */
const fs = require("fs");
const axios = require("axios");
const argv = process.argv;
const fileToRead = argv[2] // first argument passed from command line
const fileToWrite = argv[3] // second argument passed from command line
// import markovmachine
const markov = require("./markov");
let MarkovMachine = markov.MarkovMachine;
function readAndWrite(fileToRead, fileToWrite) {
fs.readFile(fileToRead, 'utf8', function(err, textRead) {
if (err) {
console.log("Yikes! Error reading data from file")
process.exit(1);
} else {
let mm = new MarkovMachine(textRead);
textToWrite = mm.makeText(200);
fs.writeFile(fileToWrite, textToWrite, "utf8", function(err) {
if (err) {
console.log("Error writing data on new file")
}
})
}
})
}
async function urlAndWrite(urlToRead, fileToWrite) {
try {
let response = await axios.get(urlToRead);
let mm = new MarkovMachine(response.data);
textToWrite = mm.makeText(200);
fs.writeFile(fileToWrite, textToWrite, "utf8", function(err) {
if (err) {
console.log("Error writing file with fetched data from specidied URL");
}
})
} catch(error) {
console.log("Error fetching data from specified URL :(");
process.exit(1);
}
}
if (fileToRead.includes("http")) {
urlAndWrite(fileToRead, fileToWrite)
} else {
readAndWrite(fileToRead, fileToWrite)
}