ee-core 1.5.2-beta.2 → 2.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config/config.default.js +8 -1
- package/core/lib/ee.js +1 -1
- package/core/lib/loader/ee_loader.js +7 -3
- package/core/lib/loader/mixin/controller.js +3 -2
- package/core/lib/utils/function.js +30 -0
- package/index.js +2 -2
- package/lib/appLoader.js +0 -5
- package/lib/application.js +7 -6
- package/lib/baseApp.js +9 -26
- package/lib/eeApp.js +18 -52
- package/{lib/constant.js → module/const/index.js} +3 -0
- package/{lib/httpclient.js → module/httpclient/index.js} +45 -11
- package/module/jobs/child/forkProcess.js +99 -0
- package/module/jobs/child/index.js +33 -0
- package/module/jobs/index.js +55 -0
- package/module/jobs/renderer/index.js +140 -0
- package/module/jobs/renderer/loadView.js +40 -0
- package/module/loader/index.js +78 -0
- package/module/log/index.js +53 -0
- package/module/log/logger.js +61 -0
- package/module/message/index.js +13 -0
- package/module/message/ipcMain.js +160 -0
- package/module/message/ipcRender.js +0 -0
- package/{lib → module}/socket/httpServer.js +0 -0
- package/{lib → module}/socket/io.js +0 -0
- package/{lib → module}/socket/ipcServer.js +6 -8
- package/{lib → module}/socket/socketClient.js +4 -3
- package/{lib → module}/socket/socketServer.js +4 -3
- package/module/socket/start.js +22 -0
- package/{lib → module}/storage/index.js +13 -12
- package/module/storage/jsondb/adapters/Base.js +14 -0
- package/module/storage/jsondb/adapters/FileSync.js +32 -0
- package/{lib/storage/lowdb → module/storage/jsondb}/main.js +6 -10
- package/{lib/storage/lowdbStorage.js → module/storage/jsondbStorage.js} +13 -14
- package/{lib → module}/storage/sqliteStorage.js +7 -11
- package/module/utils/copyto.js +161 -0
- package/module/utils/helper.js +117 -0
- package/module/utils/index.js +120 -0
- package/module/utils/json.js +72 -0
- package/module/utils/ps.js +175 -0
- package/{utils → module/utils}/wrap.js +0 -2
- package/package.json +3 -7
- package/tools/encrypt.js +2 -2
- package/utils/index.js +17 -135
- package/lib/logger.js +0 -47
- package/lib/socket/start.js +0 -22
- package/lib/storage/lowdb/adapters/Base.js +0 -15
- package/lib/storage/lowdb/adapters/FileAsync.js +0 -41
- package/lib/storage/lowdb/adapters/FileSync.js +0 -39
- package/lib/storage/lowdb/adapters/LocalStorage.js +0 -20
- package/lib/storage/lowdb/adapters/Memory.js +0 -8
- package/lib/storage/lowdb/adapters/_stringify.js +0 -4
- package/lib/storage/lowdb/common.js +0 -33
- package/lib/storage/lowdb/fp.js +0 -23
- package/lib/storage/lowdb/isPromise.js +0 -6
- package/lib/storage/lowdb/nano.js +0 -5
- package/utils/common.js +0 -91
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
const assert = require('assert');
|
|
4
4
|
const { Server } = require('socket.io');
|
|
5
|
-
const constant = require('../constant');
|
|
6
5
|
const EggConsoleLogger = require('egg-logger').EggConsoleLogger;
|
|
7
6
|
const is = require('is-type-of');
|
|
7
|
+
const Storage = require('../storage');
|
|
8
|
+
const Constants = require('../const');
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* socket server
|
|
@@ -33,7 +34,7 @@ class SocketServer {
|
|
|
33
34
|
const self = this;
|
|
34
35
|
this.consoleLogger.info('[ee-core:socket:server] connection .....');
|
|
35
36
|
this.io.on('connection', (socket) => {
|
|
36
|
-
const channel =
|
|
37
|
+
const channel = Constants.socketIo.channel.partySoftware;
|
|
37
38
|
socket.on(channel, async (message, callback) => {
|
|
38
39
|
self.consoleLogger.info('[ee-core:socket:server] socket id:' + socket.id + ' message cmd: ' + message.cmd);
|
|
39
40
|
|
|
@@ -63,7 +64,7 @@ class SocketServer {
|
|
|
63
64
|
}
|
|
64
65
|
|
|
65
66
|
getCoreDB () {
|
|
66
|
-
const coreDB =
|
|
67
|
+
const coreDB = Storage.connection('system');
|
|
67
68
|
return coreDB;
|
|
68
69
|
}
|
|
69
70
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const SocketServer = require('./socketServer');
|
|
4
|
+
const IpcServer = require('./ipcServer');
|
|
5
|
+
const HttpServer = require('./httpServer');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* server
|
|
9
|
+
*/
|
|
10
|
+
module.exports = (app) => {
|
|
11
|
+
|
|
12
|
+
// 启动 socket server
|
|
13
|
+
new SocketServer(app);
|
|
14
|
+
|
|
15
|
+
// 启动 http server
|
|
16
|
+
new HttpServer(app);
|
|
17
|
+
|
|
18
|
+
// 启动 electron ipc server
|
|
19
|
+
new IpcServer(app);
|
|
20
|
+
|
|
21
|
+
}
|
|
22
|
+
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
const assert = require('assert');
|
|
4
2
|
const _ = require('lodash');
|
|
5
|
-
const
|
|
3
|
+
const DB = {};
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
let driver = options.driver || '
|
|
5
|
+
DB.connection = function (database, options = {}) {
|
|
6
|
+
let driver = options.driver || 'jsondb';
|
|
9
7
|
let opt = options.default || {};
|
|
10
|
-
if (!_.includes(['lowdb', 'sqlite'], driver)) {
|
|
8
|
+
if (!_.includes(['jsondb', 'lowdb', 'sqlite'], driver)) {
|
|
11
9
|
assert(database, `db driver ${driver} is not supported`);
|
|
12
10
|
}
|
|
13
11
|
if (_.isEmpty(database)) {
|
|
@@ -15,20 +13,23 @@ JsonDB.connection = function (database, options = {}) {
|
|
|
15
13
|
}
|
|
16
14
|
let storage;
|
|
17
15
|
switch (driver) {
|
|
18
|
-
case '
|
|
19
|
-
const
|
|
20
|
-
storage = new
|
|
16
|
+
case 'jsondb':
|
|
17
|
+
const JsondbStorage = require('./jsondbStorage');
|
|
18
|
+
storage = new JsondbStorage(database);
|
|
21
19
|
break;
|
|
22
20
|
case 'sqlite':
|
|
23
21
|
const SqliteStorage = require('./sqliteStorage');
|
|
24
22
|
storage = new SqliteStorage(database, opt);
|
|
25
23
|
break;
|
|
26
24
|
default:
|
|
25
|
+
const JsondbStorage = require('./jsondbStorage');
|
|
26
|
+
storage = new JsondbStorage(database);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
return storage;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
// 兼容之前的api
|
|
33
|
+
DB.JsonDB = DB;
|
|
34
|
+
|
|
35
|
+
module.exports = DB;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class Base {
|
|
2
|
+
constructor(source) {
|
|
3
|
+
this.source = source
|
|
4
|
+
this.defaultValue = {}
|
|
5
|
+
this.serialize = this._stringify
|
|
6
|
+
this.deserialize = JSON.parse
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
_stringify(obj) {
|
|
10
|
+
return JSON.stringify(obj, null, 2)
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = Base
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const Base = require('./Base')
|
|
2
|
+
const fs = require('fs')
|
|
3
|
+
|
|
4
|
+
class FileSync extends Base {
|
|
5
|
+
|
|
6
|
+
read() {
|
|
7
|
+
if (fs.existsSync(this.source)) {
|
|
8
|
+
// Read database
|
|
9
|
+
try {
|
|
10
|
+
const data = fs.readFileSync(this.source, {encoding: 'utf8'})
|
|
11
|
+
|
|
12
|
+
// Handle blank file
|
|
13
|
+
return data ? this.deserialize(data) : this.defaultValue
|
|
14
|
+
} catch (e) {
|
|
15
|
+
if (e instanceof SyntaxError) {
|
|
16
|
+
e.message = `Malformed JSON in file: ${this.source}\n${e.message}`
|
|
17
|
+
}
|
|
18
|
+
throw e
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
// Initialize
|
|
22
|
+
fs.writeFileSync(this.source, this.serialize(this.defaultValue))
|
|
23
|
+
return this.defaultValue
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
write(data) {
|
|
28
|
+
return fs.writeFileSync(this.source, this.serialize(data))
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = FileSync
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
const lodash = require('lodash')
|
|
2
|
-
const
|
|
1
|
+
const lodash = require('lodash');
|
|
2
|
+
const assert = require('assert');
|
|
3
|
+
const is = require('is-type-of');
|
|
3
4
|
|
|
4
5
|
module.exports = function(adapter) {
|
|
5
|
-
|
|
6
|
-
throw new Error(
|
|
7
|
-
'An adapter must be provided, see https://github.com/typicode/lowdb/#usage'
|
|
8
|
-
)
|
|
9
|
-
}
|
|
6
|
+
assert(typeof adapter === 'object', 'An adapter must be provided');
|
|
10
7
|
|
|
11
8
|
// Create a fresh copy of lodash
|
|
12
9
|
const _ = lodash.runInContext()
|
|
@@ -24,18 +21,17 @@ module.exports = function(adapter) {
|
|
|
24
21
|
return db
|
|
25
22
|
}
|
|
26
23
|
|
|
27
|
-
// Lowdb API
|
|
28
24
|
// Expose _ for mixins
|
|
29
25
|
db._ = _
|
|
30
26
|
|
|
31
27
|
db.read = () => {
|
|
32
28
|
const r = adapter.read()
|
|
33
|
-
return
|
|
29
|
+
return is.promise(r) ? r.then(plant) : plant(r)
|
|
34
30
|
}
|
|
35
31
|
|
|
36
32
|
db.write = returnValue => {
|
|
37
33
|
const w = adapter.write(db.getState())
|
|
38
|
-
return
|
|
34
|
+
return is.promise(w) ? w.then(() => returnValue) : returnValue
|
|
39
35
|
}
|
|
40
36
|
|
|
41
37
|
db.getState = () => db.__wrapped__
|
|
@@ -1,27 +1,26 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
const assert = require('assert');
|
|
4
2
|
const fs = require('fs');
|
|
5
3
|
const path = require('path');
|
|
6
|
-
const
|
|
7
|
-
const FileSync = require('./
|
|
4
|
+
const Jsondb = require('./jsondb/main');
|
|
5
|
+
const FileSync = require('./jsondb/adapters/FileSync');
|
|
8
6
|
const _ = require('lodash');
|
|
9
|
-
const
|
|
10
|
-
const
|
|
7
|
+
const Constants = require('../const');
|
|
8
|
+
const Helper = require('../utils/helper');
|
|
9
|
+
const Ps = require('../utils/ps');
|
|
11
10
|
|
|
12
|
-
class
|
|
11
|
+
class JsondbStorage {
|
|
13
12
|
constructor (name, opt = {}) {
|
|
14
13
|
assert(name, `db name ${name} Cannot be empty`);
|
|
15
14
|
|
|
16
15
|
this.name = name;
|
|
17
16
|
|
|
18
17
|
// 数据库key列表
|
|
19
|
-
this.storageKey =
|
|
18
|
+
this.storageKey = Constants.storageKey;
|
|
20
19
|
|
|
21
|
-
const storageDir =
|
|
20
|
+
const storageDir = Ps.getStorageDir();
|
|
22
21
|
if (!fs.existsSync(storageDir)) {
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
Helper.mkdir(storageDir);
|
|
23
|
+
Helper.chmodPath(storageDir, '777');
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
this.db = this.table(name);
|
|
@@ -35,7 +34,7 @@ class LowdbStorage {
|
|
|
35
34
|
|
|
36
35
|
const dbFile = this.getFilePath(name);
|
|
37
36
|
const adapter = new FileSync(dbFile);
|
|
38
|
-
const db =
|
|
37
|
+
const db = Jsondb(adapter);
|
|
39
38
|
|
|
40
39
|
assert(fs.existsSync(dbFile), `error: storage ${dbFile} not exists`);
|
|
41
40
|
|
|
@@ -53,7 +52,7 @@ class LowdbStorage {
|
|
|
53
52
|
* 获取文件绝对路径
|
|
54
53
|
*/
|
|
55
54
|
getFilePath (name) {
|
|
56
|
-
const storageDir =
|
|
55
|
+
const storageDir = Ps.getStorageDir();
|
|
57
56
|
const dbFile = path.join(storageDir, this.getFileName(name));
|
|
58
57
|
return dbFile;
|
|
59
58
|
}
|
|
@@ -96,4 +95,4 @@ class LowdbStorage {
|
|
|
96
95
|
}
|
|
97
96
|
}
|
|
98
97
|
|
|
99
|
-
module.exports =
|
|
98
|
+
module.exports = JsondbStorage;
|
|
@@ -4,7 +4,8 @@ const assert = require('assert');
|
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const path = require('path');
|
|
6
6
|
const Database = require('better-sqlite3');
|
|
7
|
-
const
|
|
7
|
+
const Helper = require('../utils/helper');
|
|
8
|
+
const Ps = require('../utils/ps');
|
|
8
9
|
|
|
9
10
|
class SqliteStorage {
|
|
10
11
|
constructor (name, opt = {}) {
|
|
@@ -12,13 +13,8 @@ class SqliteStorage {
|
|
|
12
13
|
|
|
13
14
|
this.name = name;
|
|
14
15
|
this.mode = this.getMode(name);
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
this.storageDir = storageDir;
|
|
18
|
-
|
|
19
|
-
const fileName = this._formatFileName(name);
|
|
20
|
-
this.fileName = fileName;
|
|
21
|
-
|
|
16
|
+
this.storageDir = this._createStorageDir();
|
|
17
|
+
this.fileName = this._formatFileName(name);
|
|
22
18
|
this.db = this._initDB(opt);
|
|
23
19
|
}
|
|
24
20
|
|
|
@@ -62,15 +58,15 @@ class SqliteStorage {
|
|
|
62
58
|
* 创建storage目录
|
|
63
59
|
*/
|
|
64
60
|
_createStorageDir () {
|
|
65
|
-
let storageDir =
|
|
61
|
+
let storageDir = Ps.getStorageDir();
|
|
66
62
|
|
|
67
63
|
if (this.mode == 'absolute') {
|
|
68
64
|
storageDir = path.dirname(this.name);
|
|
69
65
|
}
|
|
70
66
|
|
|
71
67
|
if (!fs.existsSync(storageDir)) {
|
|
72
|
-
|
|
73
|
-
|
|
68
|
+
Helper.mkdir(storageDir);
|
|
69
|
+
Helper.chmodPath(storageDir, '777');
|
|
74
70
|
}
|
|
75
71
|
|
|
76
72
|
return storageDir;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* copy-to - index.js
|
|
3
|
+
* Copyright(c) 2014 dead_horse <dead_horse@qq.com>
|
|
4
|
+
* MIT Licensed
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* slice() reference.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
var slice = Array.prototype.slice;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Expose copy
|
|
17
|
+
*
|
|
18
|
+
* ```
|
|
19
|
+
* copy({foo: 'nar', hello: 'copy'}).to({hello: 'world'});
|
|
20
|
+
* copy({foo: 'nar', hello: 'copy'}).toCover({hello: 'world'});
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @param {Object} src
|
|
24
|
+
* @return {Copy}
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
module.exports = Copy;
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Copy
|
|
32
|
+
* @param {Object} src
|
|
33
|
+
* @param {Boolean} withAccess
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
function Copy(src, withAccess) {
|
|
37
|
+
if (!(this instanceof Copy)) return new Copy(src, withAccess);
|
|
38
|
+
this.src = src;
|
|
39
|
+
this._withAccess = withAccess;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* copy properties include getter and setter
|
|
44
|
+
* @param {[type]} val [description]
|
|
45
|
+
* @return {[type]} [description]
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
Copy.prototype.withAccess = function (w) {
|
|
49
|
+
this._withAccess = w !== false;
|
|
50
|
+
return this;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* pick keys in src
|
|
55
|
+
*
|
|
56
|
+
* @api: public
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
Copy.prototype.pick = function(keys) {
|
|
60
|
+
if (!Array.isArray(keys)) {
|
|
61
|
+
keys = slice.call(arguments);
|
|
62
|
+
}
|
|
63
|
+
if (keys.length) {
|
|
64
|
+
this.keys = keys;
|
|
65
|
+
}
|
|
66
|
+
return this;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* copy src to target,
|
|
71
|
+
* do not cover any property target has
|
|
72
|
+
* @param {Object} to
|
|
73
|
+
*
|
|
74
|
+
* @api: public
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
Copy.prototype.to = function(to) {
|
|
78
|
+
to = to || {};
|
|
79
|
+
|
|
80
|
+
if (!this.src) return to;
|
|
81
|
+
var keys = this.keys || Object.keys(this.src);
|
|
82
|
+
|
|
83
|
+
if (!this._withAccess) {
|
|
84
|
+
for (var i = 0; i < keys.length; i++) {
|
|
85
|
+
var key = keys[i];
|
|
86
|
+
if (to[key] !== undefined) continue;
|
|
87
|
+
to[key] = this.src[key];
|
|
88
|
+
}
|
|
89
|
+
return to;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
for (var i = 0; i < keys.length; i++) {
|
|
93
|
+
var key = keys[i];
|
|
94
|
+
if (!notDefined(to, key)) continue;
|
|
95
|
+
var getter = this.src.__lookupGetter__(key);
|
|
96
|
+
var setter = this.src.__lookupSetter__(key);
|
|
97
|
+
if (getter) to.__defineGetter__(key, getter);
|
|
98
|
+
if (setter) to.__defineSetter__(key, setter);
|
|
99
|
+
|
|
100
|
+
if (!getter && !setter) {
|
|
101
|
+
to[key] = this.src[key];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return to;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* copy src to target,
|
|
109
|
+
* override any property target has
|
|
110
|
+
* @param {Object} to
|
|
111
|
+
*
|
|
112
|
+
* @api: public
|
|
113
|
+
*/
|
|
114
|
+
|
|
115
|
+
Copy.prototype.toCover = function(to) {
|
|
116
|
+
var keys = this.keys || Object.keys(this.src);
|
|
117
|
+
|
|
118
|
+
for (var i = 0; i < keys.length; i++) {
|
|
119
|
+
var key = keys[i];
|
|
120
|
+
delete to[key];
|
|
121
|
+
var getter = this.src.__lookupGetter__(key);
|
|
122
|
+
var setter = this.src.__lookupSetter__(key);
|
|
123
|
+
if (getter) to.__defineGetter__(key, getter);
|
|
124
|
+
if (setter) to.__defineSetter__(key, setter);
|
|
125
|
+
|
|
126
|
+
if (!getter && !setter) {
|
|
127
|
+
to[key] = this.src[key];
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
Copy.prototype.override = Copy.prototype.toCover;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* append another object to src
|
|
136
|
+
* @param {Obj} obj
|
|
137
|
+
* @return {Copy}
|
|
138
|
+
*/
|
|
139
|
+
|
|
140
|
+
Copy.prototype.and = function (obj) {
|
|
141
|
+
var src = {};
|
|
142
|
+
this.to(src);
|
|
143
|
+
this.src = obj;
|
|
144
|
+
this.to(src);
|
|
145
|
+
this.src = src;
|
|
146
|
+
|
|
147
|
+
return this;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* check obj[key] if not defiend
|
|
152
|
+
* @param {Object} obj
|
|
153
|
+
* @param {String} key
|
|
154
|
+
* @return {Boolean}
|
|
155
|
+
*/
|
|
156
|
+
|
|
157
|
+
function notDefined(obj, key) {
|
|
158
|
+
return obj[key] === undefined
|
|
159
|
+
&& obj.__lookupGetter__(key) === undefined
|
|
160
|
+
&& obj.__lookupSetter__(key) === undefined;
|
|
161
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const mkdirp = require('mkdirp');
|
|
4
|
+
const convert = require('koa-convert');
|
|
5
|
+
const is = require('is-type-of');
|
|
6
|
+
const co = require('co');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* fnDebounce
|
|
10
|
+
*
|
|
11
|
+
* @param {Function} fn - 回调函数
|
|
12
|
+
* @param {Time} delayTime - 延迟时间(ms)
|
|
13
|
+
* @param {Boolean} isImediate - 是否需要立即调用
|
|
14
|
+
* @param {type} args - 回调函数传入参数
|
|
15
|
+
*/
|
|
16
|
+
exports.fnDebounce = function() {
|
|
17
|
+
const fnObject = {};
|
|
18
|
+
let timer;
|
|
19
|
+
|
|
20
|
+
return (fn, delayTime, isImediate, args) => {
|
|
21
|
+
const setTimer = () => {
|
|
22
|
+
timer = setTimeout(() => {
|
|
23
|
+
fn(args);
|
|
24
|
+
clearTimeout(timer);
|
|
25
|
+
delete fnObject[fn];
|
|
26
|
+
}, delayTime);
|
|
27
|
+
|
|
28
|
+
fnObject[fn] = { delayTime, timer };
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
if (!delayTime || isImediate) return fn(args);
|
|
32
|
+
|
|
33
|
+
if (fnObject[fn]) {
|
|
34
|
+
clearTimeout(timer);
|
|
35
|
+
setTimer(fn, delayTime, args);
|
|
36
|
+
} else {
|
|
37
|
+
setTimer(fn, delayTime, args);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 随机10位字符串
|
|
44
|
+
*/
|
|
45
|
+
exports.getRandomString = function() {
|
|
46
|
+
return Math.random().toString(36).substring(2);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 创建文件夹
|
|
51
|
+
*/
|
|
52
|
+
exports.mkdir = function(filepath) {
|
|
53
|
+
mkdirp.sync(path.dirname(filepath));
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 修改文件权限
|
|
59
|
+
*/
|
|
60
|
+
exports.chmodPath = function(path, mode) {
|
|
61
|
+
let files = [];
|
|
62
|
+
if (fs.existsSync(path)) {
|
|
63
|
+
files = fs.readdirSync(path);
|
|
64
|
+
files.forEach((file, index) => {
|
|
65
|
+
const curPath = path + '/' + file;
|
|
66
|
+
if (fs.statSync(curPath).isDirectory()) {
|
|
67
|
+
this.chmodPath(curPath, mode); // 递归删除文件夹
|
|
68
|
+
} else {
|
|
69
|
+
fs.chmodSync(curPath, mode);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
fs.chmodSync(path, mode);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 版本号比较
|
|
78
|
+
*/
|
|
79
|
+
exports.compareVersion = function (v1, v2) {
|
|
80
|
+
v1 = v1.split('.')
|
|
81
|
+
v2 = v2.split('.')
|
|
82
|
+
const len = Math.max(v1.length, v2.length)
|
|
83
|
+
|
|
84
|
+
while (v1.length < len) {
|
|
85
|
+
v1.push('0')
|
|
86
|
+
}
|
|
87
|
+
while (v2.length < len) {
|
|
88
|
+
v2.push('0')
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
for (let i = 0; i < len; i++) {
|
|
92
|
+
const num1 = parseInt(v1[i])
|
|
93
|
+
const num2 = parseInt(v2[i])
|
|
94
|
+
|
|
95
|
+
if (num1 > num2) {
|
|
96
|
+
return 1
|
|
97
|
+
} else if (num1 < num2) {
|
|
98
|
+
return -1
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return 0
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 执行一个函数
|
|
107
|
+
*/
|
|
108
|
+
exports.callFn = async function (fn, args, ctx) {
|
|
109
|
+
args = args || [];
|
|
110
|
+
if (!is.function(fn)) return;
|
|
111
|
+
if (is.generatorFunction(fn)) fn = co.wrap(fn);
|
|
112
|
+
return ctx ? fn.call(ctx, ...args) : fn(...args);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
exports.middleware = function (fn) {
|
|
116
|
+
return is.generatorFunction(fn) ? convert(fn) : fn;
|
|
117
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const eis = require('electron-is');
|
|
3
|
+
const Storage = require('../storage');
|
|
4
|
+
const Constants = require('../const');
|
|
5
|
+
const Ps = require('./ps');
|
|
6
|
+
const Helper = require('./helper');
|
|
7
|
+
const UtilsJson = require('./json');
|
|
8
|
+
const Copy = require('./copyto');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* other module
|
|
12
|
+
*/
|
|
13
|
+
Copy(Ps)
|
|
14
|
+
.and(Helper)
|
|
15
|
+
.to(exports);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 获取项目根目录package.json
|
|
19
|
+
*/
|
|
20
|
+
exports.getPackage = function() {
|
|
21
|
+
const json = UtilsJson.readSync(path.join(this.getHomeDir(), 'package.json'));
|
|
22
|
+
|
|
23
|
+
return json;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 获取 coredb
|
|
28
|
+
*/
|
|
29
|
+
exports.getCoreDB = function() {
|
|
30
|
+
const coreDB = Storage.connection('system');
|
|
31
|
+
return coreDB;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 获取 ee配置
|
|
36
|
+
*/
|
|
37
|
+
exports.getEeConfig = function() {
|
|
38
|
+
const cdb = this.getCoreDB();
|
|
39
|
+
const config = cdb.getItem('config');
|
|
40
|
+
|
|
41
|
+
return config;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 获取 app version
|
|
46
|
+
*/
|
|
47
|
+
exports.getAppVersion = function() {
|
|
48
|
+
const cdb = this.getCoreDB();
|
|
49
|
+
const v = cdb.getItem('config').appVersion;
|
|
50
|
+
return v;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 获取 插件配置
|
|
55
|
+
*/
|
|
56
|
+
exports.getAddonConfig = function() {
|
|
57
|
+
const cdb = this.getCoreDB();
|
|
58
|
+
const cfg = cdb.getItem('config').addons;
|
|
59
|
+
return cfg;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 获取 mainServer配置
|
|
64
|
+
*/
|
|
65
|
+
exports.getMainServerConfig = function() {
|
|
66
|
+
const cdb = this.getCoreDB();
|
|
67
|
+
const cfg = cdb.getItem('config').mainServer;
|
|
68
|
+
return cfg;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 获取 httpServer配置
|
|
73
|
+
*/
|
|
74
|
+
exports.getHttpServerConfig = function() {
|
|
75
|
+
const cdb = this.getCoreDB();
|
|
76
|
+
const cfg = cdb.getItem('config').httpServer;
|
|
77
|
+
return cfg;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 获取 socketServer配置
|
|
82
|
+
*/
|
|
83
|
+
exports.getSocketServerConfig = function() {
|
|
84
|
+
const cdb = this.getCoreDB();
|
|
85
|
+
const cfg = cdb.getItem('config').socketServer;
|
|
86
|
+
return cfg;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 获取 socket channel
|
|
91
|
+
*/
|
|
92
|
+
exports.getSocketChannel = function() {
|
|
93
|
+
return Constants.socketIo.channel;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 获取 额外资源目录
|
|
98
|
+
*/
|
|
99
|
+
exports.getExtraResourcesDir = function() {
|
|
100
|
+
const execDir = this.getExecDir();
|
|
101
|
+
const isPackaged = this.isPackaged();
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
// 资源路径不同
|
|
105
|
+
let dir = '';
|
|
106
|
+
if (isPackaged) {
|
|
107
|
+
// 打包后 execDir为 应用程序 exe\dmg\dep软件所在目录;打包前该值是项目根目录
|
|
108
|
+
// windows和MacOs不一样
|
|
109
|
+
dir = path.join(execDir, "resources", "extraResources");
|
|
110
|
+
if (eis.macOS()) {
|
|
111
|
+
dir = path.join(execDir, "..", "Resources", "extraResources");
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
// 打包前
|
|
115
|
+
dir = path.join(execDir, "build", "extraResources");
|
|
116
|
+
}
|
|
117
|
+
return dir;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|