This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathPluginManager.ts
83 lines (61 loc) · 2.38 KB
/
PluginManager.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
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
import { EventEmitter } from "events"
import * as fs from "fs"
import * as path from "path"
import * as Oni from "oni-api"
import { configuration } from "./../Services/Configuration"
import { AnonymousPlugin } from "./AnonymousPlugin"
import { Plugin } from "./Plugin"
const corePluginsRoot = path.join(__dirname, "vim", "core")
const defaultPluginsRoot = path.join(__dirname, "vim", "default")
const extensionsRoot = path.join(__dirname, "extensions")
export class PluginManager extends EventEmitter {
private _config = configuration
private _rootPluginPaths: string[] = []
private _plugins: Plugin[] = []
private _anonymousPlugin: AnonymousPlugin
public get plugins(): Plugin[] {
return this._plugins
}
public discoverPlugins(): void {
this._rootPluginPaths.push(corePluginsRoot)
this._rootPluginPaths.push(extensionsRoot)
if (this._config.getValue("oni.useDefaultConfig")) {
this._rootPluginPaths.push(defaultPluginsRoot)
this._rootPluginPaths.push(path.join(defaultPluginsRoot, "bundle"))
}
this._rootPluginPaths.push(path.join(this._config.getUserFolder(), "plugins"))
const allPluginPaths = this._getAllPluginPaths()
this._plugins = allPluginPaths.map((pluginRootDirectory) => this._createPlugin(pluginRootDirectory))
this._anonymousPlugin = new AnonymousPlugin()
}
public startApi(): Oni.Plugin.Api {
this._plugins.forEach((plugin) => {
plugin.activate()
})
return this._anonymousPlugin.oni
}
public getAllRuntimePaths(): string[] {
const pluginPaths = this._getAllPluginPaths()
return pluginPaths.concat(this._rootPluginPaths)
}
private _createPlugin(pluginRootDirectory: string): Plugin {
return new Plugin(pluginRootDirectory)
}
private _getAllPluginPaths(): string[] {
const paths: string[] = []
this._rootPluginPaths.forEach((rp) => {
const subPaths = getDirectories(rp)
paths.push(...subPaths)
})
return paths
}
}
export const pluginManager = new PluginManager()
function getDirectories(rootPath: string): string[] {
if (!fs.existsSync(rootPath)) {
return []
}
return fs.readdirSync(rootPath)
.map((f) => path.join(rootPath.toString(), f))
.filter((f) => fs.statSync(f).isDirectory())
}