-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeItDance.js
95 lines (81 loc) · 2.46 KB
/
makeItDance.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
/*
most of this code is from:https://github.com/hugomd/parrot.live
i just added a little bit of spice 🌶️ to it
*/
const path = require("path");
const fs = require("fs").promises;
const http = require("http");
const colors = require("colors/safe");
const url = require("url");
const { Readable } = require("stream");
const randomColors = require("./utils/randomColor");
/**
*
* @param {Number} frameRate
* @param {Array} customColor
*/
async function makeItDance({ frameRate = 70, customColor = [] }) {
let original;
(async () => {
try {
const filePath = process.cwd() + "/ascii";
const files = await fs.readdir(filePath);
original = await Promise.all(
files.map(async (res) => {
const frame = await fs.readFile(path.join(filePath, res));
return frame.toString();
})
).then((res) => res);
} catch (err) {
console.log(err);
}
})().catch((err) => {
console.log("error loading frame");
console.log(err);
});
const colorsOptions = customColor.length === 0 ? randomColors : customColor;
const randomColorSelector = () => {
let randomNumber = Math.floor(Math.random() * colorsOptions.length);
return randomNumber;
};
const streamer = async (stream) => {
let index = 0;
return setInterval(() => {
// clear terminal
stream.push("\033[2J\033[3J\033[H");
const newColor = randomColorSelector();
stream.push(colors[colorsOptions[newColor]](original[index]));
// it goes from 0 to length of asciiFiles you have
// but here original is array
index = (index + 1) % original.length;
}, frameRate);
};
const server = http.createServer((req, res) => {
if (req.url === "/status") {
res.writeHead(200, { "Content-Type": "application/json" });
return res.end(JSON.stringify({ status: "ok" }) + "\n");
}
if (
!req.headers["user-agent"].includes("curl")
) {
res.writeHead(302, {
Location: "https://github.com/DanielCodex/asciidance",
});
return res.end();
}
const stream = new Readable();
stream._read = function noop() {};
stream.pipe(res);
const interval = streamer(stream);
req.on("close", () => {
stream.destroy();
clearInterval(interval);
});
});
const port = process.env.PARROT_PORT || 3000;
server.listen(port, (err) => {
if (err) throw err;
console.log(`Listening on localhost:${port}`);
});
}
module.exports = makeItDance;