-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathinstall-plugin.ls
More file actions
109 lines (109 loc) · 3.85 KB
/
Copy pathinstall-plugin.ls
File metadata and controls
109 lines (109 loc) · 3.85 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require! {
\localStorage
\prelude-ls : { any, map, filter }
#react controls
\./modal.ls : { install, replace }
\../web3t/providers/superagent.ls : { get }
\./json-parse.ls
\./providers.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 = (cb)->
registry-string = local-storage.get-item(\plugin-registry) ? "[]"
#console.log registry-string
json-parse registry-string, cb
get-plugin = (name, cb)->
item = local-storage.get-item name
return cb null if typeof! item isnt \String
json-parse item, cb
get-plugin-one-by-one = ([item, ...rest], cb)->
return cb null, [] if not item?
err, plugin <- get-plugin item
return cb err if err?
err, other <- get-plugin-one-by-one rest
return cb err if err?
all = [plugin] ++ other
cb null, all
export get-install-list = (cb)->
err, data <- get-registry
return cb err if err?
err, res <- get-plugin-one-by-one data
return cb err if err?
cb null, res
save-registry = (registry)->
resulted-string = JSON.stringify registry
local-storage.set-item \plugin-registry, resulted-string
add-to-registry = (name, cb)->
err, registry <- get-registry
return cb err if err?
return if registry.index-of(name) > -1
registry.push name
save-registry registry
cb null
remove-from-registry = (name, cb)->
err, registry <- get-registry
return cb err if err?
index = registry.index-of(name) is -1
return cb null if index is -1
registry.splice index, 1
save-registry registry
cb null
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
err <- add-to-registry name
return cb err if err?
cb null
uninstall-plugin = (cweb3, token, cb)->
return cb "expected string argument" if typeof! token isnt \String
name = build-name token
err <- remove-from-registry name
return cb err if err?
local-storage.set-item name, ""
cweb3.refresh cb
ask-user = (cweb3, store, plugin, cb)->
err, registry <- get-registry
return cb err if err?
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-quick-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 <- install-plugin plugin
return cb err if err?
cweb3.refresh cb
export build-uninstall = (cweb3, store)-> (name, cb)->
uninstall-plugin cweb3, 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" .end
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