-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.ts
41 lines (36 loc) · 935 Bytes
/
hook.ts
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
import lodash from 'lodash'
type HookItem = {
[key: string]: any
}
export class Hook {
private mappings: HookItem = {}
private scriptName = 'semo'
constructor(name: string | HookItem[], handler: any) {
if (lodash.isString(name)) {
name = this.convertName(name)
this.mappings[<string>name] = handler
} else if (lodash.isObject(name)) {
const newMappings = {}
Object.keys(name).forEach(n => {
const nn = this.convertName(n)
newMappings[nn] = name[n]
})
this.mappings = newMappings
} else {
throw new Error('Invalid hook')
}
}
public getHook(name: string) {
name = this.convertName(name)
return this.mappings[name] || {}
}
private convertName(name) {
if (
name !== this.scriptName &&
name.indexOf(`${this.scriptName}-plugin-`) === -1
) {
name = this.scriptName + '-plugin-' + name
}
return name
}
}