1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const fs = require("fs");
|
4 | const utils = require("./utils");
|
5 | function getGlobalModuleByPath(appName, loaderItem) {
|
6 | const res = {};
|
7 | const appPath = '/app/' + appName;
|
8 | const keypath = '/app/' + appName;
|
9 | let { path, ignore, isNecessary } = loaderItem;
|
10 | ignore = ignore || [];
|
11 | isNecessary = isNecessary || false;
|
12 | path = appPath + path;
|
13 | exports.loader({ path, keypath, context: res, ignore, isNecessary, isGetMap: true });
|
14 | return res;
|
15 | }
|
16 | exports.getGlobalModule = (appName, loadList) => {
|
17 | let map = {};
|
18 | loadList.forEach((item) => {
|
19 | map = Object.assign({}, map, getGlobalModuleByPath(appName, item));
|
20 | });
|
21 | bun.globalModule[appName] = map;
|
22 | };
|
23 | exports.loader = (obj) => {
|
24 | let { keypath, path, context, type, ignore, isNecessary, isGetMap } = obj;
|
25 | keypath = keypath || '';
|
26 | context = context || global;
|
27 | type = type || "sync";
|
28 | ignore = ignore || [];
|
29 | isNecessary = isNecessary || false;
|
30 | isGetMap = isGetMap || false;
|
31 | path = bun.globalPath.ROOT_PATH + path;
|
32 | let key;
|
33 | if (!utils.fsExistsSync(path)) {
|
34 | if (isNecessary) {
|
35 | bun.Logger.bunwarn("bun-loader: Loader not found " + path);
|
36 | }
|
37 | return;
|
38 | }
|
39 | const fstat = fs.lstatSync(path);
|
40 | if (fstat.isFile()) {
|
41 | key = exports.getFuncName(path, keypath);
|
42 | if (isGetMap) {
|
43 | context[key] = path;
|
44 | }
|
45 | else {
|
46 | initModule(context, key, path, type);
|
47 | }
|
48 | return;
|
49 | }
|
50 | const files = fs.readdirSync(path);
|
51 | files.forEach((filename) => {
|
52 | const stat = fs.lstatSync(path + "/" + filename);
|
53 | if (stat.isDirectory()) {
|
54 | if (ignore.indexOf(filename + "/") !== -1) {
|
55 | return;
|
56 | }
|
57 | exports.loader({
|
58 | keypath,
|
59 | path: path.replace(bun.globalPath.ROOT_PATH, "") + "/" + filename,
|
60 | context,
|
61 | type,
|
62 | ignore,
|
63 | isNecessary,
|
64 | isGetMap
|
65 | });
|
66 | return;
|
67 | }
|
68 | if (ignore.indexOf(filename) !== -1) {
|
69 | return;
|
70 | }
|
71 | if (!utils.isjs(filename)) {
|
72 | return;
|
73 | }
|
74 | key = exports.getFuncName(path + '/' + filename, keypath);
|
75 | if (isGetMap) {
|
76 | context[key] = path + "/" + filename;
|
77 | }
|
78 | else {
|
79 | initModule(context, key, path + "/" + filename, type);
|
80 | }
|
81 | });
|
82 | };
|
83 | function initModule(context, key, path, type) {
|
84 | let mod;
|
85 | if (type === "async") {
|
86 | if (context[key]) {
|
87 | bun.Logger.bunwarn("bun-loader: Repeated method name: " + key + " in file: " + path);
|
88 | }
|
89 | Object.defineProperty(context, key, {
|
90 | get: () => {
|
91 | mod = loadModule(path);
|
92 | if (mod) {
|
93 | return mod;
|
94 | }
|
95 | bun.Logger.bunwarn("bun-loader: module cannot find path is :" + path);
|
96 | },
|
97 | enumerable: true,
|
98 | configurable: false,
|
99 | });
|
100 | return;
|
101 | }
|
102 | mod = loadModule(path);
|
103 | if (mod) {
|
104 | context[key] = (() => {
|
105 | return mod;
|
106 | })();
|
107 | }
|
108 | else {
|
109 | bun.Logger.bunwarn("bun-loader: module cannot find path is :" + path);
|
110 | }
|
111 | }
|
112 | function loadModule(path) {
|
113 | let mod;
|
114 | try {
|
115 | mod = require(path);
|
116 | }
|
117 | catch (e) {
|
118 | bun.Logger.bunerr("bun-loader: " + e);
|
119 | }
|
120 | return mod;
|
121 | }
|
122 | exports.getFuncName = (path, keypath) => {
|
123 | let newpath = path.replace(bun.globalPath.ROOT_PATH, "");
|
124 | newpath = newpath.replace('.js', "");
|
125 | if (keypath === newpath) {
|
126 | newpath = "";
|
127 | }
|
128 | else {
|
129 | newpath = newpath.replace(keypath + "/", "");
|
130 | }
|
131 | const patharr = newpath.split("/");
|
132 | const arr = [];
|
133 | for (const item of patharr) {
|
134 | if (!item) {
|
135 | continue;
|
136 | }
|
137 | arr.push(item.replace(/^\S/g, (s) => {
|
138 | return s.toUpperCase();
|
139 | }));
|
140 | }
|
141 | return arr.join("_");
|
142 | };
|
143 |
|
\ | No newline at end of file |