This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
/
index.ts
142 lines (131 loc) · 3.61 KB
/
index.ts
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const BasePlugin = require('@midwayjs/command-core/dist/plugin');
const { getCoreBaseDir } = require('@midwayjs/command-core/dist/npm');
import { existsSync, readdirSync, readFileSync } from 'fs';
import { join } from 'path';
import { exec } from 'child_process';
class PluginManager extends BasePlugin {
commands = {
plugin: {
usage: 'manage plugins',
options: {
update: {
usage: 'update a plugin',
shortcut: 'u',
},
remove: {
usage: 'remove a plugin',
shortcut: 'r',
},
install: {
usage: 'install a plugin',
shortcut: 'i',
},
list: {
usage: 'list all plugin',
shortcut: 'l',
},
root: {
usage: 'get plugin root path',
},
all: {
usage: 'do something all',
shortcut: 'a',
},
npm: {
usage: 'npm',
shortcut: 'n',
},
},
lifecycleEvents: ['plugin'],
},
};
hooks = {
'plugin:plugin': async () => {
if (this.options.update) {
await this.update();
} else if (this.options.install) {
await this.install();
} else if (this.options.list) {
await this.list();
} else if (this.options.root) {
this.core.cli.log(` - plugin root path is ${getCoreBaseDir()}`);
}
},
};
async update() {
if (this.options.a) {
console.log('update all');
} else if (typeof this.options.update === 'string') {
await this.updatePlugin(this.options.update);
}
}
async list() {
const plugins = this.getAllPlugins();
plugins.forEach(plugin => {
const version = this.getPluginVersion(plugin);
this.core.cli.log(` - ${plugin}${version ? `@${version}` : ''}`);
});
}
async install() {
if (typeof this.options.install === 'string') {
const plugin = this.options.install.replace(/@.*$/, '');
const dir: string = getCoreBaseDir();
await this.execPromise(
`cd ${join(dir, '../')};${this.options.npm || 'npm'} install ${
this.options.install
} --production`
);
this.core.cli.log(` - installed '${plugin}'`);
} else {
this.core.cli.log(`please user plugin -i=<plugin name>`);
}
}
getAllPlugins() {
const dir: string = getCoreBaseDir();
if (!existsSync(dir)) {
return [];
}
const files = readdirSync(dir);
const plugins = files.filter(file => {
return /midway-plugin/.test(file);
});
return plugins;
}
getPluginVersion(plugin) {
const dir: string = getCoreBaseDir();
const pkgJson = join(dir, plugin, 'package.json');
let version = '';
try {
version = JSON.parse(readFileSync(pkgJson).toString()).version;
} catch (E) {}
return version;
}
async updatePlugin(newPlugin) {
const plugin = newPlugin.replace(/@.*$/, '');
const oldVersion = this.getPluginVersion(plugin);
const dir: string = getCoreBaseDir();
await this.execPromise(`cd ${dir};rm -rf ${plugin}`);
await this.execPromise(
`cd ${join(dir, '../')};${this.options.npm ||
'npm'} install ${newPlugin} --production`
);
const newVersion = this.getPluginVersion(plugin);
this.core.cli.log(
` - update '${plugin}'${oldVersion ? ` from ${oldVersion}` : ''}${
newVersion ? ` to ${newVersion}` : ''
}`
);
}
execPromise(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, err => {
if (err) {
reject(err);
return;
}
resolve();
});
});
}
}
export default PluginManager;