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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
const path = require('path');
const eis = require('../utils/is');
 
/**
 * 初始化模式
 */
exports.initMode = function(mode) {
  if(process.env.EE_MODE !== undefined)return
  return process.env.EE_MODE = mode ? mode : 'framework';
}
 
/**
 * 当前模式 - framework | module
 */
exports.mode = function() {
  return process.env.EE_MODE;
}
 
/**
 * 校验模式
 */
exports.verifyMode = function(mode) {
  if (['framework', 'module'].includes(mode)) {
    return true;
  }
  return false;
}
 
/**
 * 是否为框架模式
 */
exports.isFrameworkMode = function() {
  return (process.env.EE_MODE === 'framework');
}
 
/**
 * 是否为模块模式
 */
exports.isModuleMode = function() {
  return (process.env.EE_MODE === 'module');
}
 
/**
 * 当前进程的所有env
 */
exports.allEnv = function() {
  return process.env;
}
 
/**
 * 当前环境 - local | prod
 */
exports.env = function() {
  return process.env.EE_SERVER_ENV;
}
 
/**
 * 获取 当前环境
 */
exports.getEnv = this.env
 
/**
 * 是否生产环境
 */
exports.isProd = function() {
  return (process.env.EE_SERVER_ENV === 'prod');
}
 
/**
 * 是否为开发环境
 */
exports.isDev = function() {
  if ( process.env.EE_SERVER_ENV === 'development' ||
    process.env.EE_SERVER_ENV === 'dev' ||
    process.env.EE_SERVER_ENV === 'local'
  ) {
    return true;
  }
  
  if ( process.env.NODE_ENV === 'development' ||
    process.env.NODE_ENV === 'dev' ||
    process.env.NODE_ENV === 'local'
  ) {
    return true;
  }
 
  return false;
};
 
/**
 * 是否为渲染进程
 */
exports.isRenderer = function() {
  return (typeof process === 'undefined' ||
    !process ||
    process.type === 'renderer');
};
 
/**
 * 是否为主进程
 */
exports.isMain = function() {
  return ( typeof process !== 'undefined' &&
    process.type === 'browser');
};
 
/**
 * 是否为node子进程
 */
exports.isForkedChild = function() {
  return (Number(process.env.ELECTRON_RUN_AS_NODE) === 1);
};
 
/**
 * 当前进程类型
 */
exports.processType = function() {
  let type = '';
  if (this.isMain()) {
    type = 'browser';
  } else if (this.isRenderer()) {
    type = 'renderer';
  } else if (this.isForkedChild()) {
    type = 'child';
  }
 
  return type;
};
 
/**
 * app name
 */
exports.appName = function() {
  return process.env.EE_APP_NAME;
}
 
/**
 * 获取home路径
 */
exports.getHomeDir = function () {
  return process.env.EE_HOME;
}
 
/**
 * 获取数据存储路径
 */
exports.getStorageDir = function () {
  const storageDir = path.join(this.getRootDir(), 'data');
  return storageDir;
}
 
/**
 * 获取日志存储路径 
 */
exports.getLogDir = function () {
  const dir = path.join(this.getRootDir(), 'logs');
  return dir;
}
 
/**
 * 获取加密文件路径
 */
exports.getEncryptDir = function (basePath) {
  const base = basePath || process.cwd();
  const dir = path.join(base, 'public', 'electron');
  return dir;
}
 
/**
 * 获取root目录  (dev-项目根目录,pro-app user data目录)
 */
exports.getRootDir = function () {
  const appDir = this.isDev() ? process.env.EE_HOME : process.env.EE_APP_USER_DATA;
  return appDir;
}
 
/**
 * 获取base目录
 */
exports.getBaseDir = function() {
  return process.env.EE_BASE_DIR;
}
 
/**
 * 获取electron目录
 */
exports.getElectronDir = function() {
  return process.env.EE_BASE_DIR;
}
 
/**
 * 获取public目录
 */
exports.getPublicDir = function() {
  const dir = path.join(process.env.EE_HOME, "public");
  return dir;
}
 
/**
 * 获取 额外资源目录
 */
exports.getExtraResourcesDir = function() {
  const execDir = this.getExecDir();
  const isPackaged = this.isPackaged();
 
 
  // 资源路径不同
  let dir = '';
  if (isPackaged) {
    // 打包后  execDir为 应用程序 exe\dmg\dep软件所在目录;打包前该值是项目根目录
    // windows和MacOs不一样
    dir = path.join(execDir, "resources", "extraResources");
    if (eis.macOS()) {
      dir = path.join(execDir, "..", "Resources", "extraResources");
    }
  } else {
    // 打包前
    dir = path.join(execDir, "build", "extraResources");
  }
  return dir;
}
 
/**
 * 获取 appUserData目录
 */
exports.getAppUserDataDir = function() {
  return process.env.EE_APP_USER_DATA;
}
 
/**
 * 获取 exec目录
 */
exports.getExecDir = function() {
  return process.env.EE_EXEC_DIR;
}
 
/**
 * 获取操作系统用户目录
 */
exports.getUserHomeDir = function() {
  return process.env.EE_USER_HOME;
}
 
/**
 * 获取用户配置数据目录
 */
exports.getUserHomeConfigDir = function() {
  // const filePath = path.join(this.getHomeDir(), 'package.json');
  // if (!fs.existsSync(filePath)) {
  //   throw new Error(filePath + ' is not found');
  // }
  // const pkg = JSON.parse(fs.readFileSync(filePath));
  // if (!pkg.name || pkg.name == "") {
  //   throw new Error(`name is required from ${filePath}`);
  // }
  const appname = this.appName();
  const cfgDir = path.join(this.getUserHomeDir(), ".config", appname);
  return cfgDir;
}
 
/**
 * 获取基础数据路径
 */
exports.getUserHomeAppFilePath = function() {
  const p = path.join(this.getUserHomeConfigDir(), "app.json");
  return p;
}
 
/**
 * 获取主进程端口
 */
exports.getMainPort = function () {
  return parseInt(process.env.EE_MAIN_PORT) || 0;
}
 
/**
 * 获取内置socket端口
 */
exports.getSocketPort = function () {
  return parseInt(process.env.EE_SOCKET_PORT) || 0;
}
 
/**
 * 获取内置http端口
 */
exports.getHttpPort = function () {
  return parseInt(process.env.EE_HTTP_PORT) || 0;
}
 
/**
 * 是否打包
 */
exports.isPackaged = function () {
  return process.env.EE_IS_PACKAGED === 'true';
}
 
/**
 * 是否加密
 */
exports.isEncrypted = function () {
  return process.env.EE_IS_ENCRYPTED === 'true';
}
 
/**
 * 是否热重启
 */
exports.isHotReload = function () {
  return process.env.HOT_RELOAD === 'true';
}
 
/**
 * 进程退出
 */
exports.exit = function(code = 0) {
  return process.exit(code);
}
 
/**
 * 格式化message
 */
exports.makeMessage = function(msg = {}) {
  let message = Object.assign({
    channel: '',
    event: '', 
    data: {}
  }, msg);
 
  return message;
}
 
/**
 * 退出ChildJob进程
 */
exports.exitChildJob = function(code = 0) {
  try {
    let args = JSON.parse(process.argv[2]);
    if (args.type == 'childJob') {
      process.exit(code);
    }
  } catch (e) {
    process.exit(code);
  }
}
 
 
/**
 * 任务类型 ChildJob
 */
exports.isChildJob = function() {
  try {
    let args = JSON.parse(process.argv[2]);
    if (args.type == 'childJob') {
      return true;
    }
  } catch (e) {
    return false;
  }
}
 
/**
 * 任务类型 ChildPoolJob
 */
exports.isChildPoolJob = function() {
  try {
    let args = JSON.parse(process.argv[2]);
    if (args.type == 'childPoolJob') {
      return true;
    }
  } catch (e) {
    return false;
  }
}