forked from StudentTraineeCenter/zpevnicek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-operations.js
56 lines (51 loc) · 1.43 KB
/
json-operations.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
const fs = require("fs");
/**
* The structure contains only the song's metadata. It is used to improve
* the loading experience by reducing size.
*/
class MetaSong {
constructor(name, slug, author) {
this.name = name;
this.slug = slug;
this.author = author;
}
}
/**
* The function generates the JSON file, which contains only the song's
* metadata without page content to reduce the loading performance
* of the index page.
*/
function generateMetaJson() {
console.log(
"~~~~~\nThe function for generating JSON file with song's metadata has been started!"
);
const files = fs.readdirSync("_songs");
let out = new Array();
files.map((file) => {
const song = JSON.parse(fs.readFileSync(`_songs/${file}`));
out.push(new MetaSong(song.name, song.slug, song.author));
});
fs.writeFileSync("_songs-metadata.json", JSON.stringify(out));
console.log(
"The function for generating JSON file with song's metadata succesful end!\n~~~~~"
);
}
function printManual() {
console.log(`
MANUAL FOR JSON OPERATIONS
-m run the function generates the JSON file, which contains only the song's
metadata without page content to reduce the loading performance
of the index page
-h print manual
`);
}
if (process.argv[2] === "-m") {
generateMetaJson();
process.exit(0);
} else if (process.argv[2] === "-h") {
printManual();
process.exit(0);
} else {
printManual();
process.exit(1);
}