|
1 | 1 | 'use strict';
|
2 |
| -var fs = require('fs'); |
3 |
| - |
4 | 2 | var sprintf = require('sprintf-js').sprintf;
|
5 | 3 |
|
6 | 4 | var h = require('../helper');
|
7 | 5 | var chalk = require('../chalk');
|
8 | 6 | var config = require('../config');
|
9 | 7 | var log = require('../log');
|
10 | 8 | var Plugin = require('../plugin');
|
| 9 | +var Queue = require('../queue'); |
11 | 10 | var session = require('../session');
|
12 | 11 |
|
13 | 12 | const cmd = {
|
@@ -55,59 +54,86 @@ const cmd = {
|
55 | 54 | .example(chalk.yellow('leetcode plugin company'), 'Show company plugin')
|
56 | 55 | .example(chalk.yellow('leetcode plugin company -c'), 'Show config of company plugin')
|
57 | 56 | .example('', '')
|
| 57 | + .example(chalk.yellow('leetcode plugin -i'), 'Install all missing plugins from GtiHub') |
58 | 58 | .example(chalk.yellow('leetcode plugin -i company'), 'Install company plugin from GtiHub')
|
59 | 59 | .example(chalk.yellow('leetcode plugin -d company'), 'Disable company plugin')
|
60 | 60 | .example(chalk.yellow('leetcode plugin -e company'), 'Enable comapny plugin')
|
61 | 61 | .example(chalk.yellow('leetcode plugin -D company'), 'Delete company plugin');
|
62 | 62 | }
|
63 | 63 | };
|
64 | 64 |
|
65 |
| -function printPlugins(plugins) { |
| 65 | +function print(plugins) { |
66 | 66 | log.info(chalk.gray(sprintf(' %6s %-18s %-15s %s', 'Active', 'Name', 'Version', 'Desc')));
|
67 | 67 | log.info(chalk.gray('-'.repeat(100)));
|
68 | 68 |
|
69 | 69 | plugins = plugins || Plugin.plugins;
|
70 | 70 | for (let p of plugins)
|
71 | 71 | log.printf(' %s %-18s %-15s %s',
|
72 |
| - h.prettyText('', p.enabled), p.name, p.ver, p.desc); |
| 72 | + h.prettyText('', p.enabled && !p.missing), |
| 73 | + p.name, p.ver, p.desc); |
73 | 74 | Plugin.save();
|
74 | 75 | }
|
75 | 76 |
|
| 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 | + |
76 | 97 | cmd.handler = function(argv) {
|
77 | 98 | session.argv = argv;
|
78 | 99 |
|
| 100 | + let plugins = Plugin.plugins; |
79 | 101 | const name = argv.name;
|
| 102 | + |
80 | 103 | if (argv.install) {
|
81 |
| - Plugin.install(name, function(e, plugin) { |
82 |
| - if (e) return log.error(e); |
83 |
| - plugin.help(); |
84 |
| - }); |
| 104 | + if (name) { |
| 105 | + install([new Plugin(-1, name, 'missing')]); |
| 106 | + } else { |
| 107 | + plugins = plugins.filter(x => x.missing); |
| 108 | + install(plugins); |
| 109 | + } |
85 | 110 | return;
|
86 | 111 | }
|
87 | 112 |
|
88 |
| - let plugins = Plugin.plugins; |
89 |
| - if (name) { |
90 |
| - plugins = plugins.filter(x => x.name === name); |
91 |
| - } |
| 113 | + if (name) plugins = plugins.filter(x => x.name === name); |
92 | 114 | if (plugins.length === 0) return log.error('Plugin not found!');
|
93 | 115 |
|
94 | 116 | const plugin = plugins[0];
|
95 |
| - const fullpath = h.getPluginFile(plugin.file); |
| 117 | + if (plugin.missing && (argv.enable || argv.disable)) |
| 118 | + return log.error('Plugin missing, install it first'); |
96 | 119 |
|
97 | 120 | if (argv.enable) {
|
98 | 121 | plugin.enable(true);
|
99 |
| - printPlugins(); |
| 122 | + plugin.save(); |
| 123 | + print(); |
100 | 124 | } else if (argv.disable) {
|
101 | 125 | plugin.enable(false);
|
102 |
| - printPlugins(); |
| 126 | + plugin.save(); |
| 127 | + print(); |
103 | 128 | } else if (argv.delete) {
|
104 |
| - fs.unlink(fullpath, function(e) { |
105 |
| - if (e) log.error(e.message); |
106 |
| - }); |
| 129 | + plugin.delete(); |
| 130 | + plugin.save(); |
| 131 | + Plugin.init(); |
| 132 | + print(); |
107 | 133 | } else if (argv.config) {
|
108 | 134 | log.info(JSON.stringify(config.plugins[name] || {}, null, 2));
|
109 | 135 | } else {
|
110 |
| - printPlugins(plugins); |
| 136 | + print(plugins); |
111 | 137 | }
|
112 | 138 | };
|
113 | 139 |
|
|
0 commit comments