-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
118 lines (106 loc) · 2.89 KB
/
index.js
File metadata and controls
118 lines (106 loc) · 2.89 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
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
const path = require("path")
const Plasma = require("organic-plasma")
const Reactor = require("./lib/reactor")
const esm = require('esm')
const resolve = require('resolve/sync')
module.exports = class Angel {
constructor() {
this.plasma = new Plasma()
this.reactor = new Reactor()
this.env = {
cwd: process.cwd()
}
}
async start () {
const packagejson = require(path.join(this.env.cwd, 'package.json'))
const deps = Object.assign({}, packagejson.dependencies, packagejson.devDependencies)
await this.loadDepModules(deps, 'angelabilit', this)
await this.loadDepModules(deps, 'angelscript', this.clone())
this.plasma.emit({ type: 'Ready' })
}
async loadDepModules (deps, startingWith, context = this) {
for (let moduleName in deps) {
if (moduleName.startsWith(startingWith)) {
await this.loadScript(moduleName, context)
}
}
}
async loadScripts(scripts, context = this) {
for(let i = 0; i<scripts.length; i++) {
await this.loadScript(scripts[i], context)
}
}
async loadScript(script, context = this) {
const scriptPath = resolve(script, {basedir: this.env.cwd})
const m = esm(module)(scriptPath)
if (m.length === 2) {
return new Promise((resolve, reject) => {
m(context, function (err) {
if (err) return reject(err)
resolve()
})
})
} else {
await m(context)
}
}
clone () {
const cloned = {}
for (const key in this) {
cloned[key] = this[key]
}
let methods = ['on', 'once', 'clone', 'loadScript', 'loadScripts', 'off', 'addDefaultHandler', 'do']
for (const m of methods) {
cloned[m] = this[m]
}
return cloned
}
on (pattern, handler) {
return this.reactor.on(pattern, async (cmdData) => {
const state = this.clone()
state.cmdData = cmdData
if (handler.length === 2) {
return new Promise((resolve, reject) => {
handler(state, function (err, data) {
if (err) return reject(err)
resolve(data)
})
})
} else {
return handler(state)
}
})
}
once (pattern, handler) {
return this.reactor.once(pattern, async (cmdData) => {
const state = this.clone()
state.cmdData = cmdData
if (handler.length === 2) {
return new Promise((resolve, reject) => {
handler(state, function (err, data) {
if (err) return reject(err)
resolve(data)
})
})
} else {
return handler(state)
}
})
}
addDefaultHandler (handler) {
return this.reactor.$defaultHandlers.push(handler)
}
off (handlerMicroApi) {
return this.reactor.off(handlerMicroApi)
}
async do (input, next) {
try {
let r = await this.reactor.do(input)
next && next(null, r)
return r
} catch (e) {
next && next(e)
throw e
}
}
}