-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.js
49 lines (43 loc) · 1.56 KB
/
config.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
'use strict';
const metavm = require('metavm');
const path = require('node:path');
const fsp = require('node:fs').promises;
class Config {
constructor(dirPath, options = {}) {
const { names, mode, context } = options;
this.sections = {};
this.path = dirPath;
this.names = names || null;
this.mode = mode || '';
this.context = context || metavm.createContext();
return this.load();
}
async load() {
const files = await fsp.readdir(this.path);
const mode = '.' + this.mode;
const names = [];
for (const file of files) {
const fileExt = path.extname(file);
if (fileExt !== '.js') continue;
const fileName = path.basename(file, fileExt);
const fileMode = path.extname(fileName);
const sectionName = path.basename(fileName, fileMode);
if (this.names && !this.names.includes(sectionName)) continue;
if (!this.mode && fileName.includes('.')) continue;
if (fileMode && fileMode !== mode) continue;
const defaultName = `${fileName}${mode}.js`;
if (!files.includes(defaultName)) names.push(this.loadFile(file));
}
await Promise.all(names);
return this.sections;
}
async loadFile(file) {
const configFile = path.join(this.path, file);
const sectionName = file.substring(0, file.indexOf('.'));
const options = { context: this.context };
const { exports } = await metavm.readScript(configFile, options);
this.sections[sectionName] = exports;
}
}
const readConfig = (dirPath, options) => new Config(dirPath, options);
module.exports = { Config, readConfig };