Skip to content

Commit 7c11a16

Browse files
committed
更新[env.js]: 支持 Node.js 环境, 可对接由 BoxJs 导出的数据
1 parent afbe0bb commit 7c11a16

4 files changed

Lines changed: 368 additions & 3 deletions

File tree

‎Env.js‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
function Env(name) {
22
this.name = name
3+
this.data = null
34
this.logs = []
45
this.isSurge = () => 'undefined' !== typeof $httpClient
56
this.isQuanX = () => 'undefined' !== typeof $task
7+
this.isNode = () => 'undefined' !== typeof module && !!module.exports
68
this.log = (...log) => {
79
this.logs = [...this.logs, ...log]
810
if (log) console.log(log.join('\n'))
@@ -20,13 +22,35 @@ function Env(name) {
2022
this.getdata = (key) => {
2123
if (this.isSurge()) return $persistentStore.read(key)
2224
if (this.isQuanX()) return $prefs.valueForKey(key)
25+
if (this.isNode()) {
26+
const datafile = 'box.dat'
27+
this.fs = this.fs ? this.fs : require('fs')
28+
if (this.fs.existsSync(datafile)) {
29+
this.data = JSON.parse(this.fs.readFileSync(datafile))
30+
return this.data[key]
31+
} else {
32+
return null
33+
}
34+
}
2335
}
2436
this.setdata = (val, key) => {
2537
if (this.isSurge()) return $persistentStore.write(val, key)
2638
if (this.isQuanX()) return $prefs.setValueForKey(val, key)
39+
if (this.isNode()) {
40+
const datafile = 'box.dat'
41+
this.fs = this.fs ? this.fs : require('fs')
42+
if (this.fs.existsSync(datafile)) {
43+
this.data = JSON.parse(this.fs.readFileSync(datafile))
44+
this.data[key] = val
45+
this.fs.writeFileSync(datafile, JSON.stringify(this.data))
46+
return true
47+
} else {
48+
return false
49+
}
50+
}
2751
}
28-
this.get = (url, callback) => this.send(url, 'GET', callback)
2952
this.wait = (min, max = min) => (resove) => setTimeout(() => resove(), Math.floor(Math.random() * (max - min + 1) + min))
53+
this.get = (url, callback) => this.send(url, 'GET', callback)
3054
this.post = (url, callback) => this.send(url, 'POST', callback)
3155
this.send = (url, method, callback) => {
3256
if (this.isSurge()) {
@@ -49,6 +73,15 @@ function Env(name) {
4973
(reason) => callback(reason.error, reason, reason)
5074
)
5175
}
76+
if (this.isNode()) {
77+
this.request = this.request ? this.request : require('request')
78+
url.method = method
79+
url.gzip = true
80+
this.request(url, (err, response, body) => {
81+
if (response) response.status = response.statusCode
82+
callback(null, response, body)
83+
})
84+
}
5285
}
53-
this.done = (value = {}) => $done(value)
86+
this.done = (value = {}) => (!this.isNode() ? $done(value) : null)
5487
}

‎Env.min.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "chavy_scripts",
3+
"version": "1.0.0",
4+
"main": "Env.js",
5+
"repository": "https://github.com/chavyleung/scripts.git",
6+
"author": "Chavy <chavyleung@gmail.com>",
7+
"license": "MIT",
8+
"dependencies": {
9+
"request": "^2.88.2"
10+
}
11+
}

0 commit comments

Comments
 (0)