-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.js
More file actions
65 lines (57 loc) · 1.37 KB
/
data.js
File metadata and controls
65 lines (57 loc) · 1.37 KB
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
const FS = require('fs')
const Glob = require('glob')
const Path = require('path')
const YamlJS = require('js-yaml')
class Data {
constructor (options) {
this.type = this.constructor.name
}
get response () {
return this._response
}
set response (response) {
this._response = response
}
static instances () {
const data = []
// Load All Data Classes
Glob.sync('./src/data/**/*.js').forEach(file => {
data.push(require(Path.resolve(file)))
})
return data
}
static scoped (name = null) {
const data = []
this.instances().forEach(item => {
if (item.scope === name) {
data.push(item)
} else {
data.push(item)
}
})
return data
}
static run (scoped = null) {
this.scoped(scoped).forEach(data => {
return data.request()
.then(result => {
data.store(result)
})
})
}
static getStorage (filePath, extension) {
if (FS.existsSync(filePath + extension)) {
if (extension === 'js') {
return JSON.parse(FS.readFileSync(filePath + '.' + extension, 'utf8'))
} else if (extension === 'yaml') {
return YamlJS.safeLoad(FS.readFileSync(filePath + '.' + extension, 'utf8'))
} else {
return false
}
} else {
FS.openSync(filePath + extension, 'a')
return false
}
}
}
module.exports = Data