forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathplugin.js
216 lines (180 loc) · 5.1 KB
/
plugin.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
'use strict';
var cp = require('child_process');
var fs = require('fs');
var path = require('path');
var _ = require('underscore');
var request = require('request');
var h = require('./helper');
var file = require('./file');
var cache = require('./cache');
var config = require('./config');
var log = require('./log');
var Queue = require('./queue');
function Plugin(id, name, ver, desc, deps) {
this.id = id;
this.name = name;
this.ver = ver || 'default';
this.desc = desc || '';
this.enabled = true;
this.deleted = false;
this.missing = (this.ver === 'missing');
this.builtin = (this.ver === 'default');
// only need deps for current platform
this.deps = _.chain(deps || [])
.filter(x => ! x.includes(':') || x.includes(':' + process.platform))
.map(x => x.split(':')[0])
.value();
}
Plugin.prototype.init = function() {
this.config = config.plugins[this.name] || {};
this.next = null;
};
Plugin.prototype.setNext = function(next) {
Object.setPrototypeOf(this, next);
this.next = next;
};
Plugin.prototype.delete = function() {
if (!this.missing) {
try {
const fullpath = file.pluginFile(this.file);
file.rm(fullpath);
} catch(e) {
return log.error(e.message);
}
}
this.deleted = true;
};
Plugin.prototype.save = function() {
const stats = cache.get(h.KEYS.plugins) || {};
if (this.deleted) delete stats[this.name];
else if (this.missing) return;
else stats[this.name] = this.enabled;
cache.set(h.KEYS.plugins, stats);
};
Plugin.prototype.install = function(cb) {
if (this.deps.length === 0) return cb();
const cmd = 'npm install --save ' + this.deps.join(' ');
log.debug(cmd);
const spin = h.spin(cmd);
cp.exec(cmd, {cwd: file.codeDir()}, function(e) {
spin.stop();
return cb(e);
});
};
Plugin.prototype.help = function() {};
Plugin.plugins = [];
Plugin.init = function(head) {
log.trace('initializing all plugins');
head = head || require('./core');
const stats = cache.get(h.KEYS.plugins) || {};
// 1. find installed plugins
let installed = [];
for (let f of file.listCodeDir('lib/plugins')) {
const p = f.data;
if (!p) continue;
log.trace('found plugin: ' + p.name + '=' + p.ver);
p.file = f.file;
p.enabled = stats[p.name];
if (!(p.name in stats)) {
if (p.builtin) {
log.trace('new builtin plugin, enable by default');
p.enabled = true;
} else {
log.trace('new 3rd party plugin, disable by default');
p.enabled = false;
}
}
installed.push(p);
}
// the one with bigger `id` comes first
installed = _.sortBy(installed, x => -x.id);
// 2. init all in reversed order
for (let i = installed.length - 1; i >= 0; --i) {
const p = installed[i];
if (p.enabled) {
p.init();
log.trace('inited plugin: ' + p.name);
} else {
log.trace('skipped plugin: ' + p.name);
}
}
// 3. chain together
const plugins = installed.filter(x => x.enabled);
let last = head;
for (let p of plugins) {
last.setNext(p);
last = p;
}
// 4. check missing plugins
const missings = [];
for (let k of _.keys(stats)) {
if (installed.find(x => x.name === k)) continue;
const p = new Plugin(-1, k, 'missing');
p.enabled = stats[k];
missings.push(p);
log.trace('missing plugin:' + p.name);
}
Plugin.plugins = installed.concat(missings);
return missings.length === 0;
};
Plugin.copy = function(src, cb) {
// FIXME: remove local file support?
if (path.extname(src) !== '.js') {
src = config.sys.urls.plugin.replace('$name', src);
}
const dst = file.pluginFile(src);
const srcstream = src.startsWith('https://') ? request(src) : fs.createReadStream(src);
const dststream = fs.createWriteStream(dst);
let error;
srcstream.on('response', function(resp) {
if (resp.statusCode !== 200)
srcstream.emit('error', 'HTTP Error: ' + resp.statusCode);
});
srcstream.on('error', function(e) {
dststream.emit('error', e);
});
dststream.on('error', function(e) {
error = e;
dststream.end();
});
dststream.on('close', function() {
spin.stop();
if (error) file.rm(dst);
return cb(error, dst);
});
log.debug('copying from ' + src);
const spin = h.spin('Downloading ' + src);
srcstream.pipe(dststream);
};
Plugin.install = function(name, cb) {
Plugin.copy(name, function(e, fullpath) {
if (e) return cb(e);
log.debug('copied to ' + fullpath);
const p = require(fullpath);
p.file = path.basename(fullpath);
p.install(function() {
return cb(null, p);
});
});
};
Plugin.installMissings = function(cb) {
function doTask(plugin, queue, cb) {
Plugin.install(plugin.name, function(e, p) {
if (!e) {
p.enabled = plugin.enabled;
p.save();
p.help();
}
return cb(e, p);
});
}
const missings = Plugin.plugins.filter(x => x.missing);
if (missings.length === 0) return cb();
log.warn('Installing missing plugins, might take a while ...');
const q = new Queue(missings, {}, doTask);
q.run(1, cb);
};
Plugin.save = function() {
for (let p of this.plugins) p.save();
};
module.exports = Plugin;