-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir.js
61 lines (59 loc) · 2.58 KB
/
dir.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
57
58
59
60
61
import chalk from 'chalk';
const {yellow, red, blue, green} = chalk;
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import fs from 'fs';
import * as dotenv from 'dotenv';
dotenv.config();
const PATHTO_AHRIMODULES = process.env.PATHTO_AHRIMODULES || "//ahri_modules";
const folderPath = __dirname + PATHTO_AHRIMODULES;
const defaultJSON = process.env.DEFAULT_JSON || {"category":"unknown", "limit_time":3000};
const allowedFormats = process.env.ALLOWED_FORMATS || [".py",".js"];
var files = []
function dirFiles(folder) {
let filedir = [];
let file = [];
fs.readdirSync(folder).forEach( filename => {
if(path.extname(filename) == ""){
filedir.push(folder + "//" + filename);
} else if(allowedFormats.includes(path.extname(filename))){
let object = {};
object.src = folder + "//" + filename;
object.fullname = filename;
object.name = filename.substring(0, (filename.length - path.extname(filename).length));
object.ext = path.extname(filename);
console.log(object.src);
if(fs.existsSync(folder + "//" + object.name + ".json")){
let rawdata = fs.readFileSync(folder + "//" + object.name + ".json");
try {
object.json = JSON.parse(rawdata);
if(typeof object.json.limit_time !== 'number'){
object.json.limit_time = defaultJSON.limit_time;
console.log(yellow("Załadowano domyślne wartości. Wartość limit_time dla ") + red(object.src) + yellow(" nie jest prawidłową liczbą!"));
}
if(typeof object.json.category !== 'string'){
object.json.category = defaultJSON.category;
console.log(yellow("Załadowano domyślne wartości. Wartość category dla ") + red(object.src) + yellow(" nie jest tekstem!"));
}
} catch (e) {
console.log(red(e.stack.split("\n", 1).join("")));
console.log(yellow("Załadowano domyślne wartości. Plik json dla ") + red(object.src) + yellow(" Jest błędny!"));
object.json = defaultJSON;
}
} else {
console.log(yellow("Załadowano domyślne wartości. Plik json nie istnieje!"));
object.json = defaultJSON;
}
files.push(object);
} else if(path.extname(filename) != ".json"){
console.log(yellow("Plik ") + red(folder + "//" + filename) + yellow(" ma nieprawidłowy format i nie został załadowany!"));
};
});
filedir.forEach(subdir => { dirFiles(subdir); });
}
console.log(blue("Ładowanie modułów..."));
dirFiles(folderPath);
console.log(green("Załadowano!"));
export default files;