This repository was archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins.js
More file actions
82 lines (63 loc) · 2.61 KB
/
plugins.js
File metadata and controls
82 lines (63 loc) · 2.61 KB
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
const read = require('./read');
const format = require('./format');
const serverConfig = require('./config-server');
const cliConfig = require('./config-cli');
const shell = require('./shell');
const style = require('./styles');
async function install(name) {
let { installPath } = await cliConfig.get();
let packageInfo = await shell('npm', ['info', name, '--json'], { cwd: installPath, logging: false });
packageInfo = JSON.parse(packageInfo);
// check to see if the plugin has the keyword 'corsica'. Give a warning prompt if not.
if (packageInfo.keywords && packageInfo.keywords.indexOf('corsica') === -1) {
console.log(style.info(`Package ${style.notice(name)} is not tagged with '${style.notice('corsica')}'!`));
console.log('Please double-check the name of the plugin.\n');
let userIsSure = await read({
prompt: style.notice('Are you sure you want to install it? (yes/no)'),
default: 'no'
});
if (userIsSure !== 'yes' && userIsSure !== 'y') {
throw style.bad('Installation canceled.');
}
}
await shell('npm', ['install', name], { cwd: installPath });
let cfg = await serverConfig.get();
if (cfg.plugins.indexOf(name) === -1) {
cfg.plugins.push(name);
}
await serverConfig.set('plugins', cfg.plugins);
}
async function remove(name) {
let { installPath } = await cliConfig.get();
let lsInfo = await shell('npm', ['ls', '--json'], { cwd: installPath, logging: false });
lsInfo = JSON.parse(lsInfo);
let npmPlugins = Object.keys(lsInfo.dependencies);
let cfg = await serverConfig.get();
if (cfg.plugins.indexOf(name) !== -1) {
if (npmPlugins.indexOf(name) === -1) {
throw style.bad(`Cannot remove built-in plugin ${name}!`);
} else {
cfg.plugins.splice(cfg.plugins.indexOf(name), 1);
await shell('npm', ['uninstall', name], { cwd: installPath });
await serverConfig.set('plugins', cfg.plugins);
}
} else {
throw style.bad(`Plugin ${name} not installed!`);
}
}
async function list() {
let { installPath } = await cliConfig.get();
let lsInfo = await shell('npm', ['ls', '--json'], { cwd: installPath, logging: false });
lsInfo = JSON.parse(lsInfo);
let npmPlugins = Object.keys(lsInfo.dependencies);
let cfg = await serverConfig.get();
console.log(style.info('Currently installed plugins:\n'));
format.table(cfg.plugins.map(p => {
if (npmPlugins.indexOf(p) === -1) {
return [p, '', '(built-in plugin)'];
} else {
return [p, 'v' + lsInfo.dependencies[p].version, 'installed from npm'];
}
}), [style.notice,, s => s[0] === '(' ? s : style.info(s)]);
}
module.exports = {install, remove, list};