-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathinstall-plugin.ls
More file actions
82 lines (82 loc) · 2.93 KB
/
install-plugin.ls
File metadata and controls
82 lines (82 loc) · 2.93 KB
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
require! {
\localStorage
\prelude-ls : { any, map }
#react controls
\./modal.ls : { install, replace }
\superagent : { get }
\./json-parse.ls
}
required-fields = <[ type token enabled ]>
not-in = (arr, arr2)->
arr |> any -> arr2.index-of(it) is -1
verify-plugin = (plugin, cb)->
return cb "Expected Object" if typeof! plugin isnt \Object
return cb "Required fields: #{required-fields.join(', ')}" if required-fields `not-in` Object.keys(plugin)
cb null
get-registry = ->
registry-string = local-storage.get-item(\plugin-registry) ? "[]"
#console.log registry-string
JSON.parse registry-string
get-plugin = (name)->
item = local-storage.get-item name
return null if typeof! item isnt \String
JSON.parse item
export get-install-list = ->
get-registry! |> map get-plugin
save-registry = (registry)->
resulted-string = JSON.stringify registry
local-storage.set-item \plugin-registry, resulted-string
add-to-registry = (name)->
registry = get-registry!
return if registry.index-of(name) > -1
registry.push name
save-registry registry
remove-from-registry = (name)->
registry = get-registry!
index = registry.index-of(name) is -1
return if index is -1
registry.splice index, 1
save-registry registry
build-name = (token)-> "plugin-#{token}"
install-plugin = (plugin, cb)->
err <- verify-plugin plugin
return cb err if err?
plugin.can-uninstall = yes
name = build-name plugin.token
body = JSON.stringify plugin
local-storage.set-item name, body
add-to-registry name
cb null
uninstall-plugin = (token, cb)->
return cb "expected string argument" if typeof! name isnt \String
name = build-name token
local-storage.set-item name, ""
remove-from-registry name
cb null
ask-user = (cweb3, store, plugin, cb)->
registry = get-registry!
return cb "pluing is required" if typeof! plugin isnt \Object
return cb "callback is required" if typeof! cb isnt \Function
return install store, plugin, cb if registry.index-of(plugin.token) is -1
replace store, plugin, cb
export build-install = (cweb3, store)-> (plugin, cb)->
return cb "Please unlock the wallet" if store.current.page is \locked
err <- verify-plugin plugin
return cb err if err?
err <- ask-user cweb3, store, plugin
return cb err if err?
err <- install-plugin plugin
return cb err if err?
cweb3.refresh cb
export build-uninstall = (cweb3, store)-> (name, cb)->
uninstall-plugin name, cb
export build-install-by-name = (cweb3, store)-> (name, cb)->
err, resp <- get "https://raw.githubusercontent.com/web3space/plugin-registry/master/plugins/#{name}.json"
return cb err if err?
err, plugin <- json-parse resp.text
return cb err if err?
return cb "type is required" if not plugin.type?
return cb "enabled is true" if plugin.enabled isnt yes
err <- install-plugin plugin
return cb err if err?
cweb3.refresh cb