/
Plugin.js
58 lines (45 loc) · 1.38 KB
/
Plugin.js
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
const semver = require('semver')
const version = require('./package.json').version
class Plugin {
constructor(userPlugin, pluggageVersion) {
pluggageVersion = pluggageVersion || version
this._userPlugin = userPlugin
if (!userPlugin) {
throw new TypeError('missing plugin implementation')
}
if (typeof(userPlugin.init) !== 'function') {
throw new TypeError('missing init() method in plugin implementation')
}
if (typeof(userPlugin.shutdown) !== 'function') {
throw new TypeError('missing shutdown() method in plugin implementation')
}
if (!isValidSemver(pluggageVersion)) {
throw new TypeError(`version "${pluggageVersion}" is not a valid semver version`)
}
this._pluggageVersion = pluggageVersion
}
/**
* create a plugin interface
*
* @param {object} plugin - an object with an init() and shutdown() methods
* @param {string} _versionOverride - a semver version, this is used for testing purposes
* mostly.
*
*/
static create(userPlugin, overrideVersion) {
return new Plugin(userPlugin, overrideVersion)
}
get version() {
return this._pluggageVersion
}
init(hostApi) {
return this._userPlugin.init(hostApi)
}
shutdown() {
return this._userPlugin.shutdown()
}
}
function isValidSemver(version) {
return semver.valid(version) !== null
}
module.exports = Plugin