1 | semver = require "semver"
|
2 | Plugin = require "./Plugin"
|
3 | {join} = require "path"
|
4 | {readdirSync} = require "fs"
|
5 |
|
6 | plugins = {}
|
7 |
|
8 | status =
|
9 | plugins: -> plugins
|
10 | plugin: (name) -> plugins[name]
|
11 | remove: (name) -> delete plugins[name]
|
12 | load: (plugin) ->
|
13 |
|
14 | return "Invalid plugin: Plugin must be an object" unless typeof plugin is "object"
|
15 | return "Invalid plugin: Missing meta field" unless typeof plugin.meta is "object"
|
16 | {name, author, version} = plugin.meta
|
17 | return "Invalid plugin: Missing name field" unless typeof name is "string" and name.length > 0
|
18 | return "Invalid plugin: Missing author field" unless typeof author is "string" and author.length > 0
|
19 | return "Invalid plugin: Missing version field" unless typeof version is "string" and version.length > 0
|
20 | return "Invalid plugin: Name must be alphanumeric" unless name.match /^\w+$/
|
21 |
|
22 |
|
23 | version = semver.clean version
|
24 | author = author.trim()
|
25 | name = name.trim()
|
26 |
|
27 | return "Invalid plugin: Invalid version field" unless semver.valid version
|
28 | return "Plugin #{name} already exists" if plugins[name]?
|
29 |
|
30 |
|
31 | try
|
32 | plugins[name] = new Plugin plugin
|
33 | catch e
|
34 | return e.message or e
|
35 |
|
36 | return true
|
37 |
|
38 | pluginDir = join __dirname, "./plugins"
|
39 | for file in readdirSync pluginDir
|
40 | try
|
41 | plug = require join pluginDir, file
|
42 | msg = status.load plug
|
43 | console.log msg, file unless msg is true
|
44 |
|
45 | module.exports = status |
\ | No newline at end of file |