-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetValidModules.ts
69 lines (62 loc) · 1.95 KB
/
getValidModules.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
const { readDirSync } = Deno;
import validateModuleJson from "./validate/validateModuleJson.ts";
import validateModuleData from "./validate/validateModuleData.ts";
const directoryExists = (path: string) => {
try {
Deno.statSync(path).isDirectory;
return true;
} catch (_) {
return false;
}
};
export default (path: string): ValidModules[] => {
// Ensure that path is a directory
if (!directoryExists(path)) {
console.error("Error: path is not a directory");
Deno.exit(1);
}
const potentialModules = readDirSync(path);
const modules = [];
for (const potentialModule of potentialModules) {
if (!potentialModule.isDirectory) {
// Gotta be a directory to be a module
continue;
}
const modulePath = `${path}/${potentialModule.name}/output`;
if (!directoryExists(modulePath)) {
// Gotta have an output folder to be a module
continue;
}
const moduleFiles = Array.from(readDirSync(modulePath))
.filter((f) => f.isFile)
.map((f) => f.name);
if (!moduleFiles.includes("data.sqlite")) {
// Gotta have a data.sqlite to be a module
continue;
}
if (!moduleFiles.includes("module.json")) {
// Gotta have a module.json to be a module
continue;
}
const jsonPath = `${modulePath}/module.json`;
const [moduleJson, jsonError] = validateModuleJson(jsonPath);
if (jsonError) {
// Module.json is invalid
console.error("Module.json is invalid for module at path: ", modulePath);
continue;
}
const dataPath = `${modulePath}/data.sqlite`;
const [wordFeatures, dataError] = validateModuleData(dataPath);
if (dataError || wordFeatures === null) {
// Database is invalid
console.error("Database is invalid for module at path: ", modulePath);
continue;
}
modules.push({
pathToData: `${modulePath}/data.sqlite`,
wordFeatures,
...moduleJson as ModuleJson,
});
}
return modules;
};