8000
We read every piece of feedback, and take your input very seriously.
1 parent 22d1786 commit 917855bCopy full SHA for 917855b
lib/cli.js
@@ -54,20 +54,7 @@ function initLogLevel() {
54
55
var cli = {};
56
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
+function runCommand() {
71
var yargs = require('yargs');
72
h.width = yargs.terminalWidth();
73
yargs.commandDir('commands')
@@ -78,6 +65,30 @@ cli.run = function() {
78
.epilog('Seek more help at https://skygragon.github.io/leetcode-cli/commands')
79
.wrap(Math.min(h.width, 120))
80
.argv;
+}
+
+cli.run = function() {
+ process.stdout.on('error', function(e) {
+ if (e.code === 'EPIPE') process.exit();
+ });
74
75
+ config.init();
76
+ cache.init();
77
+ initColor();
+ initIcon();
+ 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
90
91
+ }
92
};
93
94
module.exports = cli;
lib/commands/plugin.js
@@ -6,7 +6,6 @@ var chalk = require('../chalk');
6
var config = require('../config');
7
var log = require('../log');
8
var Plugin = require('../plugin');
9
-var Queue = require('../queue');
10
var session = require('../session');
11
12
const cmd = {
@@ -74,60 +73,45 @@ function print(plugins) {
Plugin.save();
}
-function install(plugins) {
- function doTask(plugin, queue, cb) {
- Plugin.install(plugin.name, function(e, p) {
- if (!e) {
- p.enable(plugin.enabled);
- p.save();
- p.help();
- }
- return cb(e);
- const q = new Queue(plugins, {}, doTask);
- q.run(1, function(e) {
- if (e) return log.fail(e);
- Plugin.init();
- print();
95
-}
96
97
cmd.handler = function(argv) {
98
session.argv = argv;
99
100
let plugins = Plugin.plugins;
101
const name = argv.name;
102
103
if (argv.install) {
+ const cb = function(e) {
+ print();
+ };
104
if (name) {
105
- install([new Plugin(-1, name, 'missing')]);
+ Plugin.install(name, cb);
106
} else {
107
- plugins = plugins.filter(x => x.missing);
108
- install(plugins);
+ Plugin.installMissings(cb);
109
110
return;
111
112
113
if (name) plugins = plugins.filter(x => x.name === name);
114
if (plugins.length === 0) return log.error('Plugin not found!');
115
116
- const plugin = plugins[0];
117
- if (plugin.missing && (argv.enable || argv.disable))
+ const p = plugins[0];
+ if (p.missing && (argv.enable || argv.disable))
118
return log.error('Plugin missing, install it first');
119
120
if (argv.enable) {
121
- plugin.enable(true);
122
- plugin.save();
+ p.enable(true);
+ p.save();
123
print();
124
} else if (argv.disable) {
125
- plugin.enable(false);
126
+ p.enable(false);
127
128
} else if (argv.delete) {
129
- plugin.delete();
130
+ p.delete();
131
Plugin.init();
132
133
} else if (argv.config) {
lib/plugin.js
@@ -10,6 +10,7 @@ var h = require('./helper');
var cache = require('./cache');
var config = require('./config');
var log = require('./log');
13
+var Queue = require('./queue');
14
15
function Plugin(id, name, ver, desc, deps) {
16
this.id = id;
@@ -171,16 +172,36 @@ Plugin.copy = function(src, cb) {
171
172
173
Plugin.install = functio FA4A n(name, cb) {
174
Plugin.copy(name, function(e, fullpath) {
- if (e) return log.error(e);
175
+ if (e) return cb(e);
176
log.debug('copied to ' + fullpath);
177
- const plugin = require(fullpath);
178
- plugin.install(function() {
179
- return cb(null, plugin);
+ const p = require(fullpath);
+ p.install(function() {
180
+ return cb(null, p);
181
});
182
183
184
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
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
205
Plugin.save = function() {
206
for (let p of this.plugins) p.save();
207