15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
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
const is = require('is-type-of');
const fs = require('fs');
const path = require('path');
const UtilsCore = require('../core/lib/utils');
const Ps = require('../ps');
const Log = require('../log');
 
module.exports = {
 
  /**
   * 加载单个文件(如果是函数,将被执行)
   *
   * @param {String} filepath - fullpath
   * @param {Array} inject - pass rest arguments into the function when invoke
   * @return {Object} exports
   * @since 1.0.0
   */
  loadOneFile (filepath, ...inject) {
    const isAbsolute = path.isAbsolute(filepath);
    if (!isAbsolute) {
      filepath = path.join(Ps.getBaseDir(), filepath);
    }
 
    filepath = filepath && this.resolveModule(filepath);
    if (!fs.existsSync(filepath)) {
      let errorMsg = `[ee-core] [loader/index] loadOneFile ${filepath} does not exist`;
      Log.coreLogger.error(errorMsg);
      throw new Error(errorMsg);
    }
 
    const ret = UtilsCore.loadFile(filepath);
    if (is.function(ret) && !is.class(ret) && !UtilsCore.isBytecodeClass(ret)) {
      ret = ret(...inject);
    }
    return ret;
  },
 
  /**
   * 加载js文件
   *
   * @param {String} filepath - fullpath
   * @return {Any} exports
   * @since 1.0.0
   */
  loadJsFile (filepath) {
    if (!fs.existsSync(filepath)) {
      let errMsg = `[ee-core] [loader] loadJobFile ${filepath} does not exist`;
      Log.coreLogger.error(errMsg);
      return;
    }
 
    const ret = UtilsCore.loadFile(filepath);
    return ret;
  },  
 
  /**
   * 加载并运行js文件
   *
   * @param {String} filepath - fullpath
   * @param {Array} inject - pass rest arguments into the function when invoke
   * @return {Any}
   * @since 1.0.0
   */
  execJsFile (filepath, ...inject) {
    if (!fs.existsSync(filepath)) {
      let errMsg = `[ee-core] [loader] loadJobFile ${filepath} does not exist`;
      Log.coreLogger.error(errMsg);
      return;
    }
 
    let ret = UtilsCore.loadFile(filepath);
    if (is.class(ret) || UtilsCore.isBytecodeClass(ret)) {
      ret = new ret(inject);
    } else if (is.function(ret)) {
      ret = ret(inject);
    }
 
    return ret;
  }, 
 
  /**
   * 模块的绝对路径
   * @param {String} filepath - fullpath
   */
  resolveModule(filepath) {
    let fullpath;
    try {
      fullpath = require.resolve(filepath);
    } catch (e) {
 
      // 特殊后缀处理
      if (filepath && (filepath.endsWith('.defalut') || filepath.endsWith('.prod'))) {
        fullpath = filepath + '.jsc';
      } else if (filepath && filepath.endsWith('.js')) {
        fullpath = filepath + 'c';
      }
 
      if (!fs.existsSync(filepath) && !fs.existsSync(fullpath)) {
        let files = { filepath, fullpath }
        Log.coreLogger.warn(`[ee-core] [loader] resolveModule unknow filepath: ${files}`)
        return undefined;
      }
    }
 
    return fullpath;
  },
 
  /**
   * 加载模块(子进程中使用)
   *
   * @param {String} filepath - fullpath
   * @return {Object} exports
   * @since 1.0.0
   */
  requireModule (filepath, type = '') {
    let fullpath;
    const isAbsolute = path.isAbsolute(filepath);
    if (!isAbsolute) {
      filepath = path.join(Ps.getBaseDir(), type, filepath);
    }
 
    fullpath = this.resolveModule(filepath);
    if (!fs.existsSync(fullpath)) {
      let errorMsg = `[ee-core] [loader] requireModule filepath: ${filepath} does not exist`;
      Log.coreLogger.error(errorMsg);
    }
    const ret = UtilsCore.loadFile(fullpath);
 
    return ret;
  },   
 
  /**
   * 加载jobs模块(子进程中使用)
   *
   * @param {String} filepath - fullpath
   * @return {Object} exports
   * @since 1.0.0
   */
  requireJobsModule (filepath) {
    const ret = this.requireModule(filepath, 'jobs');
 
    return ret;
  },
  
  /**
   * 获取electron目录下文件的绝对路径
   * @param {String} filepath - fullpath
   */
  getFullpath(filepath) {
    let fullpath;
    const isAbsolute = path.isAbsolute(filepath);
    if (!isAbsolute) {
      filepath = path.join(Ps.getBaseDir(), filepath);
    }
 
    fullpath = this.resolveModule(filepath);
    if (!fs.existsSync(fullpath)) {
      throw new Error(`[ee-core] [loader] getFullpath filepath ${fullpath} not exists`);
    }
 
    return fullpath;
  }
  
}