-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.js
More file actions
87 lines (74 loc) · 1.68 KB
/
seed.js
File metadata and controls
87 lines (74 loc) · 1.68 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
const fs = require("fs");
const { exec } = require("child_process");
const art = require("ascii-art");
const canvas = require("canvas");
const { Photon } = require("@generated/photon");
const renderImage = async path => {
return new Promise((resolve, reject) => {
const image = new art.Image({
filepath: path,
alphabet: "variant1"
});
image.write(function(err, rendered) {
if (err) {
return reject(err);
}
return resolve(rendered);
});
});
};
const readdir = path =>
new Promise((res, rej) => {
return fs.readdir(path, (err, files) => {
if (err) {
console.error("Error in readdir", err);
return rej(files);
}
return res(files);
});
});
const mkdir = path => {
return new Promise((res, rej) => {
exec(`rm -r ${path}`, e => {
if (e) {
return rej(e);
}
return exec(`mkdir ${path}`, e => {
if (e) {
return rej(e);
}
return res(e);
});
});
});
};
const seed = async framesDir => {
const photon = new Photon();
try {
// Clear all older frames
await photon.frames.deleteMany({
where: {
content: {
not: "1"
}
}
});
const files = await readdir(framesDir);
for (let i = 0; i < files.length; i++) {
if (!files[i].endsWith(".jpg")) {
continue;
}
const rendered = await renderImage(`${framesDir}/${files[i]}`);
await photon.frames.create({
data: {
content: rendered
}
});
}
} catch (e) {
console.error("Error seeding:", e);
} finally {
await photon.disconnect();
}
};
module.exports = seed;