UNPKG

1.43 kBJavaScriptView Raw
1
2import Emittery from 'emittery'
3import path from 'path'
4
5/**
6 * Plugin class
7 * @class
8 * @abstract
9 */
10class Plugin extends Emittery {
11 constructor (mid, options) {
12 super()
13
14 this.mid = mid
15
16 if (!options.name) throw new Error('The plugin have no name')
17
18 /**
19 * Plugin name
20 * @var {String}
21 */
22 this.name = options.name
23
24 if (!options.path) throw new Error('The plugin as no path')
25
26 /**
27 * Plugin path
28 * @var {String}
29 */
30 this.path = options.path
31
32 /**
33 * Plugin config from plugin-config.js
34 * @var {Object}
35 */
36 this.config = options.config || {}
37
38 /**
39 * Plugin dirs from plugin-config.js
40 * @var {Object}
41 */
42 this.dirs = this.config.dirs || {}
43
44 /**
45 * Plugin Manager
46 * @var {PluginManager}
47 */
48 this.pm = mid.pm
49
50 /**
51 * package.json
52 * @var {Object}
53 */
54 this.package = options.package
55 }
56
57 /**
58 * Init method
59 */
60 async init () {}
61
62 /**
63 * Return a dir path by is name
64 * @param {String} name
65 * @return {String}
66 */
67 getDir (name) {
68 if (!this.pm.pluginDirs[name] && !this.dirs[name]) {
69 this.mid.warn('Unknow plugin dir ' + name)
70 return null
71 }
72
73 return this.dirs[name] ? this.dirs[name] : this.pm.pluginDirs[name]
74 }
75
76 getDirPath (name) {
77 const dir = this.getDir(name)
78 return dir ? path.join(this.path, dir) : null
79 }
80}
81
82export default Plugin