Passed
Push — master ( 2315cd...33f9e9 )
by
unknown
19:10 queued 17:30
created

lib/cjs/index.js   A

Complexity

Total Complexity 14
Complexity/F 14

Size

Lines of Code 83
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 14
mnd 13
bc 13
fnc 1
bpm 13
cpm 14
noi 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
/**
36
 * Autoload modules
37
 */
38
if (packageJson.autoload.modules) {
39
	for (const [file, param] of Object.entries(packageJson.autoload.modules)) {
40
		const filePath = path.resolve(PROJECT_ROOT, file);
41
42
		if (!fs.statSync(filePath).isDirectory()) {
43
			console.error("autoload modules should be a folder");
44
			return;
45
		}
46
47
		if ("" === param) {
48
			load(filePath, {});
49
			continue;
50
		}
51
52
		global[PREFIX_LOAD_PARAM + param] = load(filePath, {});
53
	}
54
}
55
56
57
/**
58
 * Autoload packages
59
 */
60
if (packageJson.autoload.packages) {
61
	for (const [package, param] of Object.entries(packageJson.autoload.packages)) {
62
		global[PREFIX_LOAD_PARAM + param] = require(package);
63
	}
64
}
65
/**
66
 * Autoload files
67
 */
68
if (packageJson.autoload.files) {
69
	for (const [file, param] of Object.entries(packageJson.autoload.files)) {
70
		const filePath = path.resolve(PROJECT_ROOT, file);
71
		if (!file.endsWith('.js') && !file.endsWith('.json')) {
72
			console.error("Only autoload for json file and javascript file");
73
			return;
74
		}
75
76
		if ("" === param) {
77
			require(filePath);
78
			continue;
79
		}
80
81
		global[PREFIX_LOAD_PARAM + param] = require(filePath);
82
	}
83
}