-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-json.js
45 lines (40 loc) · 1.17 KB
/
generate-json.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
const fs = require("fs");
const outputName = "sketches.json";
function getDirectories(path) {
let dirs = [];
const allReads = fs.readdirSync(path);
allReads.forEach((file) => {
const stat = fs.statSync(path + "/" + file);
if (stat.isDirectory() && file.charAt(0) != "." && file.charAt(0) != "_") {
const dirContents = fs.readdirSync(`${path}/${file}`);
let newestFile = 0;
let newestDate = "";
dirContents.forEach((innerFile) => {
const innerStat = fs.statSync(`${path}/${file}/${innerFile}`);
if (innerStat.mtimeMs > newestFile) {
newestFile = innerStat.mtimeMs;
newestDate = innerStat.mtime;
}
});
dirs.push({
sketch: file,
name: file,
createdDate: stat.birthtime,
lastModifiedDate: newestDate,
code: file,
});
}
});
return dirs;
}
const directories = getDirectories(".");
const sorted_directories = directories.sort(
(a, b) => b.lastModifiedDate - a.lastModifiedDate
);
const data = JSON.stringify(sorted_directories);
fs.writeFile(outputName, data, (err) => {
if (err) {
throw err;
}
console.log("JSON data is saved.");
});