forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathplugin.js
124 lines (113 loc) · 3.31 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
'use strict';
var h = require('../helper');
var chalk = require('../chalk');
var config = require('../config');
var log = require('../log');
var Plugin = require('../plugin');
var session = require('../session');
var sprintf = require('../sprintf');
const cmd = {
command: 'plugin [name]',
aliases: ['extension', 'ext'],
desc: 'Manage plugins',
builder: function(yargs) {
return yargs
.option('c', {
alias: 'config',
type: 'boolean',
describe: 'Show plugin config',
default: false
})
.option('d', {
alias: 'disable',
type: 'boolean',
describe: 'Disable plugin',
default: false
})
.option('D', {
alias: 'delete',
type: 'boolean',
describe: 'Delete plugin',
default: false
})
.option('e', {
alias: 'enable',
type: 'boolean',
describe: 'Enable plugin',
default: false
})
.option('i', {
alias: 'install',
type: 'boolean',
describe: 'Install plugin',
default: false
})
.positional('name', {
type: 'string',
describe: 'Filter plugin by name',
default: ''
})
.example(chalk.yellow('leetcode plugin'), 'Show all plugins')
.example(chalk.yellow('leetcode plugin company'), 'Show company plugin')
.example(chalk.yellow('leetcode plugin company -c'), 'Show config of company plugin')
.example('', '')
.example(chalk.yellow('leetcode plugin -i'), 'Install all missing plugins from GitHub')
.example(chalk.yellow('leetcode plugin -i company'), 'Install company plugin from GitHub')
.example(chalk.yellow('leetcode plugin -d company'), 'Disable company plugin')
.example(chalk.yellow('leetcode plugin -e company'), 'Enable company plugin')
.example(chalk.yellow('leetcode plugin -D company'), 'Delete company plugin');
}
};
function print(plugins) {
log.info(chalk.gray(sprintf(' %6s %-18s %-15s %s', 'Active', 'Name', 'Version', 'Desc')));
log.info(chalk.gray('-'.repeat(100)));
plugins = plugins || Plugin.plugins;
for (let p of plugins)
log.printf(' %s %-18s %-15s %s',
h.prettyText('', p.enabled && !p.missing),
p.name, p.ver, p.desc);
}
cmd.handler = function(argv) {
session.argv = argv;
let plugins = Plugin.plugins;
const name = argv.name;
if (argv.install) {
const cb = function(e, p) {
if (e) return log.fatal(e);
p.help();
p.save();
Plugin.init();
print();
};
if (name) {
Plugin.install(name, cb);
} else {
Plugin.installMissings(cb);
}
return;
}
if (name) plugins = plugins.filter(x => x.name === name);
if (plugins.length === 0) return log.fatal('Plugin not found!');
const p = plugins[0];
if (p.missing && (argv.enable || argv.disable))
return log.fatal('Plugin missing, install it first');
if (argv.enable) {
p.enabled = true;
p.save();
print();
} else if (argv.disable) {
p.enabled = false;
p.save();
print();
} else if (argv.delete) {
p.delete();
p.save();
Plugin.init();
print();
} else if (argv.config) {
log.info(JSON.stringify(config.plugins[name] || {}, null, 2));
} else {
print(plugins);
}
};
module.exports = cmd;