Skip to content

Commit 917855b

Browse files
committed
Refactor plugin install.
Signed-off-by: Eric Wang <[email protected]>
1 parent 22d1786 commit 917855b

File tree

4 files changed

+193
-89
lines changed

4 files changed

+193
-89
lines changed

lib/cli.js

+25-14
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,7 @@ function initLogLevel() {
5454

5555
var cli = {};
5656

57-
cli.run = function() {
58-
config.init();
59-
cache.init();
60-
61-
initColor();
62-
initIcon();
63-
initLogLevel();
64-
65-
Plugin.init() && Plugin.save();
66-
67-
process.stdout.on('error', function(e) {
68-
if (e.code === 'EPIPE') process.exit();
69-
});
70-
57+
function runCommand() {
7158
var yargs = require('yargs');
7259
h.width = yargs.terminalWidth();
7360
yargs.commandDir('commands')
@@ -78,6 +65,30 @@ cli.run = function() {
7865
.epilog('Seek more help at https://skygragon.github.io/leetcode-cli/commands')
7966
.wrap(Math.min(h.width, 120))
8067
.argv;
68+
}
69+
70+
cli.run = function() {
71+
process.stdout.on('error', function(e) {
72+
if (e.code === 'EPIPE') process.exit();
73+
});
74+
75+
config.init();
76+
cache.init();
77+
78+
initColor();
79+
initIcon();
80+
initLogLevel();
81+
82+
if (Plugin.init()) {
83+
Plugin.save();
84+
runCommand();
85+
} else {
86+
Plugin.installMissings(function(e) {
87+
if (e) return log.error(e);
88+
Plugin.init();
89+
runCommand();
90+
});
91+
}
8192
};
8293

8394
module.exports = cli;

lib/commands/plugin.js

+16-32
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ var chalk = require('../chalk');
66
var config = require('../config');
77
var log = require('../log');
88
var Plugin = require('../plugin');
9-
var Queue = require('../queue');
109
var session = require('../session');
1110

1211
const cmd = {
@@ -74,60 +73,45 @@ function print(plugins) {
7473
Plugin.save();
7574
}
7675

77-
function install(plugins) {
78-
function doTask(plugin, queue, cb) {
79-
Plugin.install(plugin.name, function(e, p) {
80-
if (!e) {
81-
p.enable(plugin.enabled);
82-
p.save();
83-
p.help();
84-
}
85-
return cb(e);
86-
});
87-
}
88-
89-
const q = new Queue(plugins, {}, doTask);
90-
q.run(1, function(e) {
91-
if (e) return log.fail(e);
92-
Plugin.init();
93-
print();
94-
});
95-
}
96-
9776
cmd.handler = function(argv) {
9877
session.argv = argv;
9978

10079
let plugins = Plugin.plugins;
10180
const name = argv.name;
10281

10382
if (argv.install) {
83+
const cb = function(e) {
84+
if (e) return log.error(e);
85+
Plugin.init();
86+
print();
87+
};
88+
10489
if (name) {
105-
install([new Plugin(-1, name, 'missing')]);
90+
Plugin.install(name, cb);
10691
} else {
107-
plugins = plugins.filter(x => x.missing);
108-
install(plugins);
92+
Plugin.installMissings(cb);
10993
}
11094
return;
11195
}
11296

11397
if (name) plugins = plugins.filter(x => x.name === name);
11498
if (plugins.length === 0) return log.error('Plugin not found!');
11599

116-
const plugin = plugins[0];
117-
if (plugin.missing && (argv.enable || argv.disable))
100+
const p = plugins[0];
101+
if (p.missing && (argv.enable || argv.disable))
118102
return log.error('Plugin missing, install it first');
119103

120104
if (argv.enable) {
121-
plugin.enable(true);
122-
plugin.save();
105+
p.enable(true);
106+
p.save();
123107
print();
124108
} else if (argv.disable) {
125-
plugin.enable(false);
126-
plugin.save();
109+
p.enable(false);
110+
p.save();
127111
print();
128112
} else if (argv.delete) {
129-
plugin.delete();
130-
plugin.save();
113+
p.delete();
114+
p.save();
131115
Plugin.init();
132116
print();
133117
} else if (argv.config) {

lib/plugin.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var h = require('./helper');
1010
var cache = require('./cache');
1111
var config = require('./config');
1212
var log = require('./log');
13+
var Queue = require('./queue');
1314

1415
function Plugin(id, name, ver, desc, deps) {
1516
this.id = id;
@@ -171,16 +172,36 @@ Plugin.copy = function(src, cb) {
171172

172173
Plugin.install = function(name, cb) {
173174
Plugin.copy(name, function(e, fullpath) {
174-
if (e) return log.error(e);
175+
if (e) return cb(e);
175176
log.debug('copied to ' + fullpath);
176177

177-
const plugin = require(fullpath);
178-
plugin.install(function() {
179-
return cb(null, plugin);
178+
const p = require(fullpath);
179+
p.install(function() {
180+
return cb(null, p);
180181
});
181182
});
182183
};
183184

185+
Plugin.installMissings = function(cb) {
186+
function doTask(plugin, queue, cb) {
187+
Plugin.install(plugin.name, function(e, p) {
188+
if (!e) {
189+
p.enable(plugin.enabled);
190+
p.save();
191+
p.help();
192+
}
193+
return cb(e, p);
194+
});
195+
}
196+
197+
const plugins = Plugin.plugins.filter(x => x.missing);
198+
if (plugins.length === 0) return cb();
199+
200+
log.warn('Installing missing plugins, might take a while ...');
201+
const q = new Queue(plugins, {}, doTask);
202+
q.run(1, cb);
203+
};
204+
184205
Plugin.save = function() {
185206
for (let p of this.plugins) p.save();
186207
};

0 commit comments

Comments
 (0)