forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathplugin.js
112 lines (102 loc) · 3.06 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
var fs = require('fs');
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 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(chalk.yellow('leetcode plugin -i company'), 'Install company plugin from GtiHub')
.example(chalk.yellow('leetcode plugin -d company'), 'Disable company plugin')
.example(chalk.yellow('leetcode plugin -e company'), 'Enable comapny plugin')
.example(chalk.yellow('leetcode plugin -D company'), 'Delete company plugin');
}
};
cmd.handler = function(argv) {
session.argv = argv;
var name = argv.name;
if (argv.install) {
Plugin.install(name, function(e, plugin) {
if (e) return log.error(e);
plugin.help();
});
return;
}
var plugins = Plugin.plugins;
if (name) {
plugins = plugins.filter(function(p) {
return p.name === name;
});
}
if (plugins.length === 0) return log.error('Plugin not found!');
var plugin = plugins[0];
var fullpath = h.getPluginFile(plugin.file);
var newname;
if (argv.enable) {
if (plugin.enabled) return;
newname = h.getPluginFile(plugin.file.substr(1));
fs.rename(fullpath, newname, function(e) {
if (e) log.error(e.message);
});
} else if (argv.disable) {
if (!plugin.enabled) return;
newname = h.getPluginFile('.' + plugin.file);
fs.rename(fullpath, newname, function(e) {
if (e) log.error(e.message);
});
} else if (argv.delete) {
fs.unlink(fullpath, function(e) {
if (e) log.error(e.message);
});
} else if (argv.config) {
log.info(JSON.stringify(config.plugins[name] || {}, null, 2));
} else {
plugins.forEach(function(p) {
log.printf('%s %-18s %-15s %s', h.prettyText('', p.enabled), p.name, p.ver, p.desc);
});
}
};
module.exports = cmd;