-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
239 lines (208 loc) · 7.67 KB
/
index.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
var assert = require('assert')
var path = require('path')
var fs = require('fs')
var pull = require('pull-stream')
var cat = require('pull-cat')
var many = require('pull-many')
var pushable = require('pull-pushable')
var toPull = require('stream-to-pull-stream')
var spawn = require('cross-spawn')
var mkdirp = require('mkdirp')
var osenv = require('osenv')
var rimraf = require('rimraf')
var mv = require('mv')
var explain = require('explain-error')
var valid = require('muxrpc-validation')({})
function isObject(o) {
return o && 'object' === typeof o
}
function isString(s) {
return 'string' === typeof s
}
module.exports = {
name: 'plugins',
version: '1.0.0',
manifest: {
install: 'source',
uninstall: 'source',
enable: 'async',
disable: 'async',
help: 'sync'
},
permissions: {
master: {allow: ['install', 'uninstall', 'enable', 'disable']}
},
init: function (server, config) {
var installPath = config.path
config.plugins = config.plugins || {}
mkdirp.sync(path.join(installPath, 'node_modules'))
// helper to enable/disable plugins
function configPluginEnabled (b) {
return function (opts, cb) {
var pluginName
if(isString(opts)) {
pluginName = opts
opts = {}
}
checkInstalled(pluginName, function (err) {
if (err) return cb(err)
if(isObject(config.plugins[pluginName])) {
if(b) delete config.plugins[pluginName].enabled
else config.plugins[pluginName].enabled = false
}
else
config.plugins[pluginName] = b
writePluginConfig(pluginName, b)
if (b) cb(null, '\''+pluginName+'\' has been enabled. Restart ssb-server to use the plugin.')
else cb(null, '\''+pluginName+'\' has been disabled. Restart ssb-server to stop using the plugin.')
})
}
}
// helper to check if a plugin is installed
function checkInstalled (pluginName, cb) {
if (!pluginName || typeof pluginName !== 'string')
return cb(new Error('plugin name is required'))
var modulePath = path.join(installPath, 'node_modules', pluginName)
fs.stat(modulePath, function (err) {
if (err)
cb(new Error('Plugin "'+pluginName+'" is not installed.'))
else
cb()
})
}
// write the plugin config to ~/.ssb/config
function writePluginConfig (config) {
var cfgPath = path.join(config.path, 'config')
// load ~/.ssb/config
let existingConfig
fs.readFile(cfgPath, 'utf-8', (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
// only catch "file not found"
existingConfig = {}
} else {
throw err
}
} else {
existingConfig = JSON.parse(data)
}
// update the plugins config
existingConfig.plugins = config.plugins
// write to disc
fs.writeFileSync(cfgPath, JSON.stringify(existingConfig, null, 2), 'utf-8')
})
}
return {
install: function (opts, _opts) {
var pluginName
if(isString(opts)) {
pluginName = opts
opts = _opts
opts.module = pluginName
}
else {
pluginName = opts.name
}
var p = pushable()
var dryRun = opts && opts['dry-run']
var from = opts && opts.from
if (!pluginName || typeof pluginName !== 'string')
return pull.error(new Error('plugin name is required'))
// pull out the version, if given
if (pluginName.indexOf('@') !== -1) {
var pluginNameSplitted = pluginName.split('@')
pluginName = pluginNameSplitted[0]
var version = pluginNameSplitted[1]
if (version && !from)
from = pluginName + '@' + version
}
if (!validatePluginName(pluginName))
return pull.error(new Error('invalid plugin name: "'+pluginName+'"'))
// create a tmp directory to install into
var tmpInstallPath = path.join(osenv.tmpdir(), pluginName)
rimraf.sync(tmpInstallPath); mkdirp.sync(tmpInstallPath)
// build args
// --global-style: dont dedup at the top level, gives proper isolation between each plugin
// --loglevel error: dont output warnings, because npm just whines about the lack of a package.json in ~/.ssb
var args = ['install', from||pluginName, '--global-style', '--loglevel', 'error']
if (dryRun)
args.push('--dry-run')
// exec npm
var child = spawn('npm', args, { cwd: tmpInstallPath })
.on('close', function (code) {
if (code == 0 && !dryRun) {
var tmpInstallNMPath = path.join(tmpInstallPath, 'node_modules')
var finalInstallNMPath = path.join(installPath, 'node_modules')
// delete plugin, if it's already there
rimraf.sync(path.join(finalInstallNMPath, pluginName))
// move the plugin from the tmpdir into our install path
// ...using our given plugin name
var dirs = fs.readdirSync(tmpInstallNMPath)
.filter(function (name) { return name.charAt(0) !== '.' }) // filter out dot dirs, like '.bin'
mv(
path.join(tmpInstallNMPath, dirs[0]),
path.join(finalInstallNMPath, pluginName),
function (err) {
if (err)
return p.end(explain(err, '"'+pluginName+'" failed to install. See log output above.'))
// enable the plugin
// - use basename(), because plugins can be installed from the FS, in which case pluginName is a path
var name = path.basename(pluginName)
config.plugins[name] = {
subprocess: opts.subprocess
}
writePluginConfig(config)
p.push(Buffer.from('"'+pluginName+'" has been installed. Restart ssb-server to enable the plugin.\n', 'utf-8'))
p.end()
}
)
} else
p.end(new Error('"'+pluginName+'" failed to install. See log output above.'))
})
return cat([
pull.values([Buffer.from('Installing "'+pluginName+'"...\n', 'utf-8')]),
many([toPull(child.stdout), toPull(child.stderr)]),
p
])
},
uninstall: function (opts, _opts) {
var pluginName
if(isString(opts)) {
pluginName = opts
opts = _opts
}
else {
pluginName = opts.name
}
var p = pushable()
if (!pluginName || typeof pluginName !== 'string')
return pull.error(new Error('plugin name is required'))
var modulePath = path.join(installPath, 'node_modules', pluginName)
rimraf(modulePath, function (err) {
if (!err) {
delete config.plugins[pluginName]
writePluginConfig(config)
p.push(Buffer.from('"'+pluginName+'" has been uninstalled. Restart ssb-server to disable the plugin.\n', 'utf-8'))
p.end()
} else
p.end(err)
})
return p
},
enable: valid.async(configPluginEnabled(true), 'string'),
disable: valid.async(configPluginEnabled(false), 'string'),
help: function () { return require('./help') }
}
}
}
function validatePluginName (name) {
if (/^[._]/.test(name))
return false
// from npm-validate-package-name:
if (encodeURIComponent(name) !== name)
return false
return true
}
module.exports.loadUserPlugins = function (createSbot, config) {
return createSbot.use(require('./load-user-plugins')(config))
}