-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy-class.js
207 lines (187 loc) · 7.58 KB
/
deploy-class.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
'use strict';
let fs = require('fs');
let path = require('path');
let sources = {};
let modules = {};
module.exports = class DeployClass {
/**
* @description
* Automatically load the package module.
* @param {string} prefix
* @param {string} sourceFolderName
*/
static automatic(prefix, sourceFolderName) {
if (sourceFolderName == null) sourceFolderName = 'sources';
let sourceFolderPath = '';
sourceFolderPath = path.join(process.argv[1], '../');
sourceFolderPath = path.join(sourceFolderPath, sourceFolderName);
if (prefix !== undefined) {
if (!fs.existsSync(sourceFolderPath)) return;
DeployClass.registerSourceFolder(prefix, sourceFolderPath);
DeployClass.treeLoader(sourceFolderPath, sourceFolderPath, prefix);
DeployClass.sourceLoader(sourceFolderPath, sourceFolderPath, prefix);
} else {
if (!fs.existsSync(sourceFolderPath)) return;
fs.readdirSync(sourceFolderPath).forEach(function(file) {
let prefixFolderPath = path.join(sourceFolderPath, file);
let stat = fs.statSync(prefixFolderPath);
try {
if (stat.isDirectory()) {
prefix = file;
DeployClass.registerSourceFolder(prefix, prefixFolderPath);
DeployClass.treeLoader(prefixFolderPath, prefixFolderPath, prefix);
DeployClass.sourceLoader(prefixFolderPath, prefixFolderPath, prefix);
}
} catch (e) {}
});
}
}
/**
* @description
* Load the global variable depending on the file hierarchy.
* @param {string} sourceFolderPath
* @param {string} originPath
* @param {string} prefix
*/
static treeLoader(sourceFolderPath, originPath, prefix) {
if (!fs.existsSync(sourceFolderPath)) return;
fs.readdirSync(sourceFolderPath).forEach(function(file) {
let filePath = path.join(sourceFolderPath, file);
let stat = fs.statSync(filePath);
try {
let tree = sourceFolderPath.split(originPath)[1];
tree = prefix + tree.replace(new RegExp("/", 'g'), '.');
tree = tree.replace(/\\/g, '.');
tree = tree.replace(/[&\/\\#,+()$~%;@$^!'":*?<>{}]/g, '');
if (eval("!" + tree))
eval(tree + " = {};");
if (stat.isDirectory())
DeployClass.treeLoader(filePath, originPath, prefix);
} catch (e) {}
});
}
/**
* @description
* Load the module in the string code.
* @param {string} tree
* @param {string} className
* @param {string} code
* @param {string} filePath
* @return {class}
*/
static requireFromString(tree, className, code, filePath) {
try {
if (modules[filePath] != null) return eval('(' + tree + ')');
code = 'module.exports={load:()=>{' + code + ';' + tree + '=' + className + ';}}';
let paths = module.constructor._nodeModulePaths(path.dirname(filePath));
let moduleBuild = new module.constructor(filePath, module.parent);
moduleBuild.filename = filePath;
moduleBuild.paths = [].concat(['prepend']).concat(paths).concat(['append']);
moduleBuild._compile(code, filePath);
let moduleInstance = moduleBuild.exports;
if (moduleInstance == null || typeof(moduleInstance.load) != 'function') return;
moduleInstance.load();
modules[filePath] = moduleInstance;
return eval('(' + tree + ')');
} catch (e) {
console.log('\r\n' + filePath + '\r\n\t' + e);
return null;
}
}
/**
* @description
* Load all the source files in the specified folder.
* @param {string} sourceFolderPath
*/
static sourceLoader(sourceFolderPath, originPath, prefix) {
if (!fs.existsSync(sourceFolderPath)) return;
fs.readdirSync(sourceFolderPath).forEach(function(file) {
let filePath = path.join(sourceFolderPath, file);
let stat = fs.statSync(filePath);
try {
if (stat.isFile()) {
let tree = prefix + filePath.split(originPath)[1];
tree = tree.replace(/\.js/gi, '');
tree = tree.replace(new RegExp("/", 'g'), '.');
tree = tree.replace(/\\/g, '.');
let extension = path.extname(file);
if (extension != '.js' && extension != '.jsx') return;
let targetClassName = file.replace(/\.js/gi, '');
DeployClass.requireFromString(tree, targetClassName,
fs.readFileSync(filePath, 'utf8'), filePath);
} else {
DeployClass.sourceLoader(filePath, originPath, prefix);
}
} catch (e) {}
});
}
/**
* @description
* This function is load the required source.
* Use when there are dependencies with sources.
* @param {string} requireSourcePath
*/
static require(requireSourcePath) {
let splitPath = requireSourcePath.split(".");
let prefix = null;
let prefixCount = 0;
for (let checkCount = 0; checkCount < splitPath.length; checkCount++) {
let checkPrefix = "";
for (let i = 0; i <= checkCount; i++) {
if (i != 0) checkPrefix += ".";
checkPrefix += splitPath[i];
}
if (sources[checkPrefix] != null) {
prefix = checkPrefix;
prefixCount = checkCount;
break;
}
}
if (!prefix) return null;
let requireAbsolutePath = "";
for (let key in splitPath)
if (key > prefixCount) requireAbsolutePath += ("/" + splitPath[key]);
let stats, path;
try {
stats = fs.statSync(sources[prefix] + requireAbsolutePath + '.js');
path = sources[prefix] + requireAbsolutePath + '.js';
} catch (err) {
try {
stats = fs.statSync(sources[prefix] + requireAbsolutePath + '.jsx');
path = sources[prefix] + requireAbsolutePath + '.jsx';
} catch (err) {
return;
}
}
/**
* @description
* If the source is already loaded, not loaded.
*/
if (modules[path] != null) return eval('(' + requireSourcePath + ')');
return DeployClass.requireFromString(requireSourcePath, splitPath[splitPath.length - 1],
fs.readFileSync(path, 'utf8'), path);
}
/**
* @description
* Register the source folder with the desired namespace.
* You can only sentence like minejs, develper_name.plugin_name
* like also possible to attach the name of the plugin developers behind the name.
* Must register the namespace with the source folder, you can use the function dependencies.
*
* @param {string} prefix
* @param {string} directory
*/
static registerSourceFolder(prefix, directory) {
let splitPrefix = prefix.split('.');
if (splitPrefix[1] == null) {
eval(`global.${prefix} = {}`);
} else {
let prefixBuild = 'global';
for (let key in splitPrefix) {
prefixBuild += `.${splitPrefix[key]}`;
eval(`${prefixBuild} = {}`);
}
}
sources[prefix] = directory;
}
};