-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcompile-datasets.ts
101 lines (76 loc) · 2.65 KB
/
compile-datasets.ts
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
96
97
98
99
100
101
import fs from 'fs';
interface FileOutput{
files: string[];
lastUpdated: Date | null;
createdAt: Date | null;
}
function recurseFiles(path: string, datasetId: string): FileOutput {
const directoryStructure = fs.readdirSync(path, { withFileTypes: true });
let files: string[] = [];
let lastUpdated = null;
let createdAt = null;
for (let child of directoryStructure) {
if (child.isDirectory()) {
const tmp = recurseFiles(`${path}/${child.name}`, datasetId);
files = [...files, ...tmp.files];
lastUpdated = tmp.lastUpdated;
createdAt = tmp.createdAt;
}
if (child.isFile()) {
const stats = fs.statSync(`${path}/${child.name}`);
if (lastUpdated === null || stats.mtime > lastUpdated) {
lastUpdated = stats.mtime;
}
if (createdAt === null || stats.birthtime < createdAt) {
createdAt = stats.birthtime;
}
}
if (child.isFile() && child.name !== 'meta.json') {
let subdirectory = path.replace(`./datasets/${datasetId}`, "").replace("/", "");
files.push(`${subdirectory ? `${subdirectory}/` : ""}${child.name}`);
}
}
return { files, lastUpdated, createdAt };
}
function processDatasetDirectory(path: string, datasetId: string) {
const directoryStructure = fs.readdirSync(path, { withFileTypes: true });
let meta = null;
for (let child of directoryStructure) {
if (child.isFile() && child.name === 'meta.json') {
try {
meta = JSON.parse(fs.readFileSync(`${path}/${child.name}`, 'utf8'));
} catch (e) {
console.log(datasetId);
console.log(e);
}
}
}
if (meta) {
const { files, lastUpdated, createdAt } = recurseFiles(path, datasetId);
meta.files = files;
meta.id = datasetId;
meta.lastUpdated = lastUpdated;
meta.createdAt = createdAt;
}
return meta;
}
const processDatasets = () => {
const directoryStructure = fs.readdirSync('./datasets', { withFileTypes: true });
const datasets = [];
for (let child of directoryStructure) {
if (child.isDirectory()) {
const meta = processDatasetDirectory(`./datasets/${child.name}`, child.name);
if (meta) datasets.push(meta);
}
}
return datasets;
}
function main(){
const datasets = processDatasets();
if (!fs.existsSync("json")) {
fs.mkdirSync("json");
}
fs.writeFileSync('./json/datasets.json', JSON.stringify(datasets, null, 2));
}
main();
export {};