-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugins.js
92 lines (76 loc) · 1.54 KB
/
plugins.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
'use strict';
const npm = require('global-npm');
const path = require('path');
module.exports = class Plugins {
constructor(storage, path) {
this.storage = storage;
this.path = path;
}
init() {
return this.storage.get('tinkerhub:plugins')
.then(data => {
this._plugins = data || {};
});
}
save() {
return this.storage.set('tinkerhub:plugins', this._plugins);
}
mapPlugin(key, data) {
let p;
if(data.module) {
p = path.join(this.path, 'node_modules', key);
} else {
p = data.file;
}
return {
id: key,
path: p
};
}
list() {
const result = [];
for(const key of Object.keys(this._plugins)) {
const data = this._plugins[key];
result.push(this.mapPlugin(key, data));
}
return result;
}
install(name) {
if(name.indexOf('tinkerhub-') !== 0) {
name = 'tinkerhub-' + name;
}
return new Promise((resolve, reject) => {
npm.load({ loglevel: 'silent' }, err => {
if(err) {
reject(err);
return;
}
npm.prefix = this.path;
npm.commands.install(this.path, [ name ], err => {
if(err) {
reject(err);
return;
}
const data = this._plugins[name] = {
module: true
};
this.save()
.then(() => resolve(this.mapPlugin(name, data)))
.catch(reject);
});
});
});
}
link(path) {
try {
require.resolve(path);
} catch(ex) {
return Promise.reject(new Error('Path must exist'));
}
const data = this._plugins[path] = {
file: path
};
return this.save()
.then(() => this.mapPlugin(path, data));
}
}