ee-core 2.9.2 → 2.10.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/LICENSE +21 -21
- package/README.md +65 -65
- package/addon/index.js +34 -34
- package/addon/window/index.js +98 -98
- package/bin/tools.js +8 -8
- package/config/cache.js +41 -38
- package/config/config.default.js +331 -330
- package/config/index.js +86 -73
- package/const/channel.js +17 -17
- package/const/index.js +8 -8
- package/controller/baseContextClass.js +34 -34
- package/controller/index.js +34 -34
- package/core/index.js +10 -10
- package/core/lib/ee.js +216 -216
- package/core/lib/loader/context_loader.js +106 -106
- package/core/lib/loader/ee_loader.js +435 -435
- package/core/lib/loader/file_loader.js +326 -326
- package/core/lib/loader/mixin/addon.js +32 -32
- package/core/lib/loader/mixin/config.js +130 -130
- package/core/lib/loader/mixin/controller.js +125 -125
- package/core/lib/loader/mixin/service.js +28 -28
- package/core/lib/utils/base_context_class.js +34 -34
- package/core/lib/utils/function.js +30 -30
- package/core/lib/utils/index.js +133 -133
- package/core/lib/utils/sequencify.js +59 -59
- package/core/lib/utils/timing.js +77 -77
- package/cross/index.js +183 -183
- package/cross/spawnProcess.js +183 -183
- package/ee/appLoader.js +48 -48
- package/ee/application.js +99 -99
- package/ee/baseApp.js +103 -102
- package/ee/eeApp.js +408 -408
- package/ee/index.js +57 -57
- package/electron/app/index.js +64 -58
- package/electron/index.js +19 -19
- package/electron/window/index.js +73 -73
- package/electron/window/winState.js +186 -186
- package/exception/index.js +112 -112
- package/html/boot.html +98 -98
- package/html/cross-failure.html +28 -28
- package/html/failure.html +28 -28
- package/html/index.js +13 -13
- package/httpclient/index.js +161 -161
- package/index.js +54 -54
- package/jobs/baseJobClass.js +16 -16
- package/jobs/child/app.js +58 -65
- package/jobs/child/forkProcess.js +145 -145
- package/jobs/child/index.js +82 -82
- package/jobs/child-pool/index.js +213 -213
- package/jobs/index.js +8 -8
- package/jobs/load-balancer/algorithm/index.js +11 -11
- package/jobs/load-balancer/algorithm/minimumConnection.js +18 -18
- package/jobs/load-balancer/algorithm/polling.js +11 -11
- package/jobs/load-balancer/algorithm/random.js +9 -9
- package/jobs/load-balancer/algorithm/specify.js +14 -14
- package/jobs/load-balancer/algorithm/weights.js +21 -21
- package/jobs/load-balancer/algorithm/weightsMinimumConnection.js +29 -29
- package/jobs/load-balancer/algorithm/weightsPolling.js +22 -22
- package/jobs/load-balancer/algorithm/weightsRandom.js +16 -16
- package/jobs/load-balancer/consts.js +9 -9
- package/jobs/load-balancer/index.js +201 -201
- package/jobs/load-balancer/scheduler.js +31 -31
- package/jobs/renderer/index.js +141 -141
- package/jobs/renderer/loadView.js +40 -40
- package/jobs/unification.js +63 -63
- package/loader/index.js +172 -172
- package/log/index.js +68 -68
- package/log/logger.js +86 -80
- package/main/index.js +56 -56
- package/message/childMessage.js +54 -54
- package/message/index.js +18 -18
- package/old-utils/index.js +91 -91
- package/package.json +38 -38
- package/ps/index.js +371 -371
- package/services/baseContextClass.js +34 -34
- package/services/index.js +40 -40
- package/socket/httpServer.js +147 -147
- package/socket/index.js +81 -81
- package/socket/io.js +27 -27
- package/socket/ipcServer.js +112 -112
- package/socket/socketServer.js +69 -67
- package/storage/index.js +38 -38
- package/storage/jsondb/adapters/Base.js +23 -23
- package/storage/jsondb/adapters/FileSync.js +64 -52
- package/storage/jsondb/main.js +55 -42
- package/storage/jsondbStorage.js +195 -195
- package/storage/sqliteStorage.js +123 -123
- package/utils/co.js +237 -237
- package/utils/copyto.js +160 -160
- package/utils/depd/index.js +538 -538
- package/utils/depd/lib/browser/index.js +77 -77
- package/utils/extend.js +73 -73
- package/utils/get-port/index.d.ts +64 -64
- package/utils/get-port/index.js +148 -148
- package/utils/helper.js +220 -220
- package/utils/index.js +160 -160
- package/utils/ip.js +261 -261
- package/utils/is.js +145 -145
- package/utils/json.js +72 -72
- package/utils/pargv.js +263 -263
- package/utils/time/index.js +19 -19
- package/utils/time/ms.js +162 -162
- package/utils/wrap.js +35 -35
package/utils/is.js
CHANGED
|
@@ -1,146 +1,146 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const semver = require('semver')
|
|
4
|
-
const gt = semver.gt
|
|
5
|
-
const lt = semver.lt
|
|
6
|
-
const release = require('os').release
|
|
7
|
-
|
|
8
|
-
const IS = {
|
|
9
|
-
// Checks if we are in renderer process
|
|
10
|
-
renderer() {
|
|
11
|
-
return process.type === 'renderer'
|
|
12
|
-
},
|
|
13
|
-
// Checks if we are in main process
|
|
14
|
-
main() {
|
|
15
|
-
return process.type === 'browser'
|
|
16
|
-
},
|
|
17
|
-
// Checks if we are under Mac OS
|
|
18
|
-
osx() {
|
|
19
|
-
return process.platform === 'darwin'
|
|
20
|
-
},
|
|
21
|
-
// Checks if we are under Mac OS
|
|
22
|
-
macOS() {
|
|
23
|
-
return this.osx()
|
|
24
|
-
},
|
|
25
|
-
// Checks if we are under Windows OS
|
|
26
|
-
windows() {
|
|
27
|
-
return process.platform === 'win32'
|
|
28
|
-
},
|
|
29
|
-
// Checks if we are under Linux OS
|
|
30
|
-
linux() {
|
|
31
|
-
return process.platform === 'linux'
|
|
32
|
-
},
|
|
33
|
-
// Checks if we are the processor's arch is x86
|
|
34
|
-
x86() {
|
|
35
|
-
return process.arch === 'ia32'
|
|
36
|
-
},
|
|
37
|
-
// Checks if we are the processor's arch is x64
|
|
38
|
-
x64() {
|
|
39
|
-
return process.arch === 'x64'
|
|
40
|
-
},
|
|
41
|
-
// Checks if the app is running in a sandbox on macOS
|
|
42
|
-
sandbox() {
|
|
43
|
-
return 'APP_SANDBOX_CONTAINER_ID' in process.env
|
|
44
|
-
},
|
|
45
|
-
// Checks if the app is running as a Mac App Store build
|
|
46
|
-
mas() {
|
|
47
|
-
return process.mas === true
|
|
48
|
-
},
|
|
49
|
-
// Checks if the app is running as a Windows Store (appx) build
|
|
50
|
-
windowsStore() {
|
|
51
|
-
return process.windowsStore === true
|
|
52
|
-
},
|
|
53
|
-
// checks if all the 'is functions' passed as arguments are true
|
|
54
|
-
all() {
|
|
55
|
-
const isFunctions = new Array(arguments.length)
|
|
56
|
-
for (var i = 0; i < isFunctions.length; i++) {
|
|
57
|
-
isFunctions[i] = arguments[i]
|
|
58
|
-
}
|
|
59
|
-
if (!isFunctions.length) return
|
|
60
|
-
for (i = 0; i < isFunctions.length; i++) {
|
|
61
|
-
if (!isFunctions[i]()) return false
|
|
62
|
-
}
|
|
63
|
-
return true
|
|
64
|
-
},
|
|
65
|
-
// checks if all the 'is functions' passed as arguments are false
|
|
66
|
-
none() {
|
|
67
|
-
const isFunctions = new Array(arguments.length)
|
|
68
|
-
for (var i = 0; i < isFunctions.length; i++) {
|
|
69
|
-
isFunctions[i] = arguments[i]
|
|
70
|
-
}
|
|
71
|
-
if (!isFunctions.length) return
|
|
72
|
-
for (i = 0; i < isFunctions.length; i++) {
|
|
73
|
-
if (isFunctions[i]()) return false
|
|
74
|
-
}
|
|
75
|
-
return true
|
|
76
|
-
},
|
|
77
|
-
// returns true if one of the 'is functions' passed as argument is true
|
|
78
|
-
one() {
|
|
79
|
-
const isFunctions = new Array(arguments.length)
|
|
80
|
-
for (var i = 0; i < isFunctions.length; i++) {
|
|
81
|
-
isFunctions[i] = arguments[i]
|
|
82
|
-
}
|
|
83
|
-
if (!isFunctions.length) return
|
|
84
|
-
for (i = 0; i < isFunctions.length; i++) {
|
|
85
|
-
if (isFunctions[i]()) return true
|
|
86
|
-
}
|
|
87
|
-
return false
|
|
88
|
-
},
|
|
89
|
-
// checks the if the given release is the same of the OS
|
|
90
|
-
release(requested) {
|
|
91
|
-
if (this.osx()) {
|
|
92
|
-
return requested === this._osxRelease()
|
|
93
|
-
} else if (this.windows()) {
|
|
94
|
-
requested = requested.split('.')
|
|
95
|
-
const actual = release().split('.')
|
|
96
|
-
if (requested.length === 2) {
|
|
97
|
-
return `${actual[0]}.${actual[1]}` === `${requested[0]}.${requested[1]}`
|
|
98
|
-
}
|
|
99
|
-
return `${actual[0]}.${actual[1]}.${actual[2]}` === `${requested[0]}.${requested[1]}.${requested[2]}`
|
|
100
|
-
} else {
|
|
101
|
-
// Not implemented for Linux yet
|
|
102
|
-
return null
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
// checks if the given release is greater than the current OS release
|
|
106
|
-
gtRelease(requested) {
|
|
107
|
-
if (this.osx()) {
|
|
108
|
-
return gt(requested, this._osxRelease())
|
|
109
|
-
} else if (this.windows()) {
|
|
110
|
-
requested = requested.split('.')
|
|
111
|
-
const actual = release().split('.')
|
|
112
|
-
if (requested.length === 2) {
|
|
113
|
-
return gt(`${requested[0]}.${requested[1]}.0`, `${actual[0]}.${actual[1]}.0`)
|
|
114
|
-
}
|
|
115
|
-
return gt(`${requested[0]}.${requested[1]}.${requested[2]}`, `${actual[0]}.${actual[1]}.${actual[2]}`)
|
|
116
|
-
} else {
|
|
117
|
-
// Not implemented for Linux yet
|
|
118
|
-
return null
|
|
119
|
-
}
|
|
120
|
-
},
|
|
121
|
-
// checks if the given release is less than the current OS release
|
|
122
|
-
ltRelease(requested) {
|
|
123
|
-
if (this.osx()) {
|
|
124
|
-
return lt(requested, this._osxRelease())
|
|
125
|
-
} else if (this.windows()) {
|
|
126
|
-
requested = requested.split('.')
|
|
127
|
-
const actual = release().split('.')
|
|
128
|
-
if (requested.length === 2) {
|
|
129
|
-
return lt(`${requested[0]}.${requested[1]}.0`, `${actual[0]}.${actual[1]}.0`)
|
|
130
|
-
}
|
|
131
|
-
return lt(`${requested[0]}.${requested[1]}.${requested[2]}`, `${actual[0]}.${actual[1]}.${actual[2]}`)
|
|
132
|
-
} else {
|
|
133
|
-
// Not implemented for Linux yet
|
|
134
|
-
return null
|
|
135
|
-
}
|
|
136
|
-
},
|
|
137
|
-
|
|
138
|
-
// returns the current osx release
|
|
139
|
-
_osxRelease () {
|
|
140
|
-
const actual = release().split('.')
|
|
141
|
-
return `10.${actual[0] - 4}.${actual[1]}`
|
|
142
|
-
},
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const semver = require('semver')
|
|
4
|
+
const gt = semver.gt
|
|
5
|
+
const lt = semver.lt
|
|
6
|
+
const release = require('os').release
|
|
7
|
+
|
|
8
|
+
const IS = {
|
|
9
|
+
// Checks if we are in renderer process
|
|
10
|
+
renderer() {
|
|
11
|
+
return process.type === 'renderer'
|
|
12
|
+
},
|
|
13
|
+
// Checks if we are in main process
|
|
14
|
+
main() {
|
|
15
|
+
return process.type === 'browser'
|
|
16
|
+
},
|
|
17
|
+
// Checks if we are under Mac OS
|
|
18
|
+
osx() {
|
|
19
|
+
return process.platform === 'darwin'
|
|
20
|
+
},
|
|
21
|
+
// Checks if we are under Mac OS
|
|
22
|
+
macOS() {
|
|
23
|
+
return this.osx()
|
|
24
|
+
},
|
|
25
|
+
// Checks if we are under Windows OS
|
|
26
|
+
windows() {
|
|
27
|
+
return process.platform === 'win32'
|
|
28
|
+
},
|
|
29
|
+
// Checks if we are under Linux OS
|
|
30
|
+
linux() {
|
|
31
|
+
return process.platform === 'linux'
|
|
32
|
+
},
|
|
33
|
+
// Checks if we are the processor's arch is x86
|
|
34
|
+
x86() {
|
|
35
|
+
return process.arch === 'ia32'
|
|
36
|
+
},
|
|
37
|
+
// Checks if we are the processor's arch is x64
|
|
38
|
+
x64() {
|
|
39
|
+
return process.arch === 'x64'
|
|
40
|
+
},
|
|
41
|
+
// Checks if the app is running in a sandbox on macOS
|
|
42
|
+
sandbox() {
|
|
43
|
+
return 'APP_SANDBOX_CONTAINER_ID' in process.env
|
|
44
|
+
},
|
|
45
|
+
// Checks if the app is running as a Mac App Store build
|
|
46
|
+
mas() {
|
|
47
|
+
return process.mas === true
|
|
48
|
+
},
|
|
49
|
+
// Checks if the app is running as a Windows Store (appx) build
|
|
50
|
+
windowsStore() {
|
|
51
|
+
return process.windowsStore === true
|
|
52
|
+
},
|
|
53
|
+
// checks if all the 'is functions' passed as arguments are true
|
|
54
|
+
all() {
|
|
55
|
+
const isFunctions = new Array(arguments.length)
|
|
56
|
+
for (var i = 0; i < isFunctions.length; i++) {
|
|
57
|
+
isFunctions[i] = arguments[i]
|
|
58
|
+
}
|
|
59
|
+
if (!isFunctions.length) return
|
|
60
|
+
for (i = 0; i < isFunctions.length; i++) {
|
|
61
|
+
if (!isFunctions[i]()) return false
|
|
62
|
+
}
|
|
63
|
+
return true
|
|
64
|
+
},
|
|
65
|
+
// checks if all the 'is functions' passed as arguments are false
|
|
66
|
+
none() {
|
|
67
|
+
const isFunctions = new Array(arguments.length)
|
|
68
|
+
for (var i = 0; i < isFunctions.length; i++) {
|
|
69
|
+
isFunctions[i] = arguments[i]
|
|
70
|
+
}
|
|
71
|
+
if (!isFunctions.length) return
|
|
72
|
+
for (i = 0; i < isFunctions.length; i++) {
|
|
73
|
+
if (isFunctions[i]()) return false
|
|
74
|
+
}
|
|
75
|
+
return true
|
|
76
|
+
},
|
|
77
|
+
// returns true if one of the 'is functions' passed as argument is true
|
|
78
|
+
one() {
|
|
79
|
+
const isFunctions = new Array(arguments.length)
|
|
80
|
+
for (var i = 0; i < isFunctions.length; i++) {
|
|
81
|
+
isFunctions[i] = arguments[i]
|
|
82
|
+
}
|
|
83
|
+
if (!isFunctions.length) return
|
|
84
|
+
for (i = 0; i < isFunctions.length; i++) {
|
|
85
|
+
if (isFunctions[i]()) return true
|
|
86
|
+
}
|
|
87
|
+
return false
|
|
88
|
+
},
|
|
89
|
+
// checks the if the given release is the same of the OS
|
|
90
|
+
release(requested) {
|
|
91
|
+
if (this.osx()) {
|
|
92
|
+
return requested === this._osxRelease()
|
|
93
|
+
} else if (this.windows()) {
|
|
94
|
+
requested = requested.split('.')
|
|
95
|
+
const actual = release().split('.')
|
|
96
|
+
if (requested.length === 2) {
|
|
97
|
+
return `${actual[0]}.${actual[1]}` === `${requested[0]}.${requested[1]}`
|
|
98
|
+
}
|
|
99
|
+
return `${actual[0]}.${actual[1]}.${actual[2]}` === `${requested[0]}.${requested[1]}.${requested[2]}`
|
|
100
|
+
} else {
|
|
101
|
+
// Not implemented for Linux yet
|
|
102
|
+
return null
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
// checks if the given release is greater than the current OS release
|
|
106
|
+
gtRelease(requested) {
|
|
107
|
+
if (this.osx()) {
|
|
108
|
+
return gt(requested, this._osxRelease())
|
|
109
|
+
} else if (this.windows()) {
|
|
110
|
+
requested = requested.split('.')
|
|
111
|
+
const actual = release().split('.')
|
|
112
|
+
if (requested.length === 2) {
|
|
113
|
+
return gt(`${requested[0]}.${requested[1]}.0`, `${actual[0]}.${actual[1]}.0`)
|
|
114
|
+
}
|
|
115
|
+
return gt(`${requested[0]}.${requested[1]}.${requested[2]}`, `${actual[0]}.${actual[1]}.${actual[2]}`)
|
|
116
|
+
} else {
|
|
117
|
+
// Not implemented for Linux yet
|
|
118
|
+
return null
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
// checks if the given release is less than the current OS release
|
|
122
|
+
ltRelease(requested) {
|
|
123
|
+
if (this.osx()) {
|
|
124
|
+
return lt(requested, this._osxRelease())
|
|
125
|
+
} else if (this.windows()) {
|
|
126
|
+
requested = requested.split('.')
|
|
127
|
+
const actual = release().split('.')
|
|
128
|
+
if (requested.length === 2) {
|
|
129
|
+
return lt(`${requested[0]}.${requested[1]}.0`, `${actual[0]}.${actual[1]}.0`)
|
|
130
|
+
}
|
|
131
|
+
return lt(`${requested[0]}.${requested[1]}.${requested[2]}`, `${actual[0]}.${actual[1]}.${actual[2]}`)
|
|
132
|
+
} else {
|
|
133
|
+
// Not implemented for Linux yet
|
|
134
|
+
return null
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
// returns the current osx release
|
|
139
|
+
_osxRelease () {
|
|
140
|
+
const actual = release().split('.')
|
|
141
|
+
return `10.${actual[0] - 4}.${actual[1]}`
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
}
|
|
145
|
+
|
|
146
146
|
module.exports = IS;
|
package/utils/json.js
CHANGED
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const mkdirp = require('mkdirp');
|
|
4
|
-
|
|
5
|
-
exports.strictParse = function (str) {
|
|
6
|
-
const obj = JSON.parse(str);
|
|
7
|
-
if (!obj || typeof obj !== 'object') {
|
|
8
|
-
throw new Error('JSON string is not object');
|
|
9
|
-
}
|
|
10
|
-
return obj;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
exports.readSync = function(filepath) {
|
|
14
|
-
if (!fs.existsSync(filepath)) {
|
|
15
|
-
throw new Error(filepath + ' is not found');
|
|
16
|
-
}
|
|
17
|
-
return JSON.parse(fs.readFileSync(filepath));
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
exports.writeSync = function(filepath, str, options) {
|
|
21
|
-
options = options || {};
|
|
22
|
-
if (!('space' in options)) {
|
|
23
|
-
options.space = 2;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
mkdirp.sync(path.dirname(filepath));
|
|
27
|
-
if (typeof str === 'object') {
|
|
28
|
-
str = JSON.stringify(str, options.replacer, options.space) + '\n';
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
fs.writeFileSync(filepath, str);
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
exports.read = function(filepath) {
|
|
35
|
-
return fs.stat(filepath)
|
|
36
|
-
.then(function(stats) {
|
|
37
|
-
if (!stats.isFile()) {
|
|
38
|
-
throw new Error(filepath + ' is not found');
|
|
39
|
-
}
|
|
40
|
-
return fs.readFile(filepath);
|
|
41
|
-
})
|
|
42
|
-
.then(function(buf) {
|
|
43
|
-
return JSON.parse(buf);
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
exports.write = function(filepath, str, options) {
|
|
48
|
-
options = options || {};
|
|
49
|
-
if (!('space' in options)) {
|
|
50
|
-
options.space = 2;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (typeof str === 'object') {
|
|
54
|
-
str = JSON.stringify(str, options.replacer, options.space) + '\n';
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return mkdir(path.dirname(filepath))
|
|
58
|
-
.then(function() {
|
|
59
|
-
return fs.writeFile(filepath, str);
|
|
60
|
-
});
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
function mkdir(dir) {
|
|
64
|
-
return new Promise(function(resolve, reject) {
|
|
65
|
-
mkdirp(dir, function(err) {
|
|
66
|
-
if (err) {
|
|
67
|
-
return reject(err);
|
|
68
|
-
}
|
|
69
|
-
resolve();
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
}
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const mkdirp = require('mkdirp');
|
|
4
|
+
|
|
5
|
+
exports.strictParse = function (str) {
|
|
6
|
+
const obj = JSON.parse(str);
|
|
7
|
+
if (!obj || typeof obj !== 'object') {
|
|
8
|
+
throw new Error('JSON string is not object');
|
|
9
|
+
}
|
|
10
|
+
return obj;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
exports.readSync = function(filepath) {
|
|
14
|
+
if (!fs.existsSync(filepath)) {
|
|
15
|
+
throw new Error(filepath + ' is not found');
|
|
16
|
+
}
|
|
17
|
+
return JSON.parse(fs.readFileSync(filepath));
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
exports.writeSync = function(filepath, str, options) {
|
|
21
|
+
options = options || {};
|
|
22
|
+
if (!('space' in options)) {
|
|
23
|
+
options.space = 2;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
mkdirp.sync(path.dirname(filepath));
|
|
27
|
+
if (typeof str === 'object') {
|
|
28
|
+
str = JSON.stringify(str, options.replacer, options.space) + '\n';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
fs.writeFileSync(filepath, str);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
exports.read = function(filepath) {
|
|
35
|
+
return fs.stat(filepath)
|
|
36
|
+
.then(function(stats) {
|
|
37
|
+
if (!stats.isFile()) {
|
|
38
|
+
throw new Error(filepath + ' is not found');
|
|
39
|
+
}
|
|
40
|
+
return fs.readFile(filepath);
|
|
41
|
+
})
|
|
42
|
+
.then(function(buf) {
|
|
43
|
+
return JSON.parse(buf);
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
exports.write = function(filepath, str, options) {
|
|
48
|
+
options = options || {};
|
|
49
|
+
if (!('space' in options)) {
|
|
50
|
+
options.space = 2;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (typeof str === 'object') {
|
|
54
|
+
str = JSON.stringify(str, options.replacer, options.space) + '\n';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return mkdir(path.dirname(filepath))
|
|
58
|
+
.then(function() {
|
|
59
|
+
return fs.writeFile(filepath, str);
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
function mkdir(dir) {
|
|
64
|
+
return new Promise(function(resolve, reject) {
|
|
65
|
+
mkdirp(dir, function(err) {
|
|
66
|
+
if (err) {
|
|
67
|
+
return reject(err);
|
|
68
|
+
}
|
|
69
|
+
resolve();
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|