lib/cjs/index.js   A
last analyzed

Complexity

Total Complexity 15
Complexity/F 15

Size

Lines of Code 85
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 42
mnd 14
bc 14
fnc 1
dl 0
loc 85
rs 10
bpm 14
cpm 15
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ load 0 19 4
1
const fs = require('fs'); // Import the 'fs' module for file system operations
2
const path = require('path'); // Import the 'path' module for path manipulation
3
4
const PROJECT_ROOT = path.resolve(__dirname, './../../../../../'); // Determine the project's root directory
5
const PREFIX_LOAD_PARAM = '$_'; // Prefix used for automatically loaded variables
6
7
const packageJson = require(path.join(PROJECT_ROOT, "./package.json")); // Read the package.json file
8
9
/**
10
 * The load function to automatically load files and directories
11
 * @param {string} filePath - The path to the directory or file to load
12
 * @param {object} baseObject - The base object to store autoloaded data
13
 * @returns {object} - An object containing the autoloaded data
14
 */
15
function load(filePath, baseObject = null) {
16
	const files = fs.readdirSync(filePath);
17
18
	for (const file of files) {
19
		const updateFilePath = path.join(filePath, file);
20
		const stats = fs.statSync(updateFilePath);
21
		if (stats.isDirectory()) {
22
			baseObject[file] = load(updateFilePath, baseObject);
23
		} else {
24
			if (!file.endsWith('.js') && !file.endsWith('.json')) {
25
				throw Error("Only autoload for json file and javascript file");
26
			}
27
			const fileName = path.basename(updateFilePath, path.extname(updateFilePath));
28
			baseObject[fileName] = require(updateFilePath);
29
		}
30
	}
31
32
	return baseObject;
33
}
34
35
if (packageJson && packageJson.autoload) {
36
	/**
37
	 * Autoload modules
38
	 */
39
	if (packageJson.autoload.modules) {
40
		for (const [file, param] of Object.entries(packageJson.autoload.modules)) {
41
			const filePath = path.resolve(PROJECT_ROOT, file);
42
43
			if (!fs.statSync(filePath).isDirectory()) {
44
				console.error("autoload modules should be a folder");
45
				return;
46
			}
47
48
			if ("" === param) {
49
				load(filePath, {});
50
				continue;
51
			}
52
53
			global[PREFIX_LOAD_PARAM + param] = load(filePath, {});
54
		}
55
	}
56
57
58
	/**
59
	 * Autoload packages
60
	 */
61
	if (packageJson.autoload.packages) {
62
		for (const [package, param] of Object.entries(packageJson.autoload.packages)) {
63
			global[PREFIX_LOAD_PARAM + param] = require(package);
64
		}
65
	}
66
	/**
67
	 * Autoload files
68
	 */
69
	if (packageJson.autoload.files) {
70
		for (const [file, param] of Object.entries(packageJson.autoload.files)) {
71
			const filePath = path.resolve(PROJECT_ROOT, file);
72
			if (!file.endsWith('.js') && !file.endsWith('.json')) {
73
				console.error("Only autoload for json file and javascript file");
74
				return;
75
			}
76
77
			if ("" === param) {
78
				require(filePath);
79
				continue;
80
			}
81
82
			global[PREFIX_LOAD_PARAM + param] = require(filePath);
83
		}
84
	}
85
}
86