This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
plugin.ts
160 lines (145 loc) · 3.94 KB
/
plugin.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Created by axetroy on 17-3-13.
*/
import { EventEmitter } from 'events';
import * as path from 'path';
import * as spawn from 'cross-spawn';
import * as fs from 'fs-extra';
import * as globalPackageLoader from 'global-modules-path';
import * as _ from 'lodash';
import 'console.table';
import chalk from 'chalk';
import { camelcase } from './utils';
const GLOBAL_NODE_MODULES_PATH = globalPackageLoader.getPath('./');
export interface Plugin$ {
name?: string;
add?: Function;
foreach?: Function;
__gpm__path__?: string;
}
export interface Plugins$ {
[actionName: string]: Plugin$[];
}
export interface PluginLoader$ {
[pluginName: string]: Plugin$;
}
export interface PluginObj$ {
name: string;
description: string;
homepage: string;
}
export interface LoaderOutput$ {
[pluginName: string]: Plugin$;
}
// 支持的插件action
type ACTION$ = 'add' | 'foreach';
class Plugin extends EventEmitter {
private plugins: Plugins$ = {};
private $: PluginLoader$ = this.loader();
constructor() {
super();
}
/**
* get a plugin
* @param {ACTION$} action
* @returns {Plugin$[]}
*/
get(action: ACTION$): Plugin$[] {
return (this.plugins[action] || []).slice();
}
/**
* remove a plugin
* @param {string} pluginName
* @returns {Promise<number>}
*/
remove(pluginName: string): Promise<number> {
return new Promise((resolve, reject) => {
const uninstall = spawn(
'npm',
['uninstall', `gpm-plugin-${pluginName}`],
{ stdio: 'inherit' }
);
uninstall.on('close', (code: number, signal: string) => {
if (code !== 0) {
reject(code);
throw new Error(`Error Code: ${code}, Exist Signal: ${signal}`);
} else {
resolve(code);
}
});
});
}
/**
* print the list of plugins
* @returns {PluginObj$[]}
*/
list(): PluginObj$[] {
let table: PluginObj$[] = [];
_.each(this.$, (v: any, name) => {
const pkg = fs.readJsonSync(path.join(v.__gpm__path__, 'package.json'));
table.push({
name: chalk.yellow(pkg.name || ''),
description: chalk.gray(pkg.description || ''),
homepage: pkg.homepage || ''
});
});
console.table(table);
return table;
}
/**
* get one plugin
* @param {string} action
* @param {string} pluginName
* @returns {Plugin$}
*/
load(action: ACTION$, pluginName: string): Plugin$ {
const container: Plugin$[] = (this.plugins[action] =
this.plugins[action] || []);
let plugin: Plugin$ = {};
if (pluginName) {
const pluginFullName: string = `gpm-plugin-${pluginName}`;
const pluginPath: string = globalPackageLoader.getPath(pluginFullName);
if (!pluginFullName) {
throw new Error(
`Can not found ${pluginFullName}, Please make sure you have install it in global`
);
}
try {
plugin = <Plugin$>require(pluginPath);
} catch (err) {
throw err;
}
plugin.name = plugin.name || pluginName;
if (_.isEmpty(plugin))
throw new Error(
`Can not found ${chalk.green(
pluginFullName
)}, Please make sure you have install it in global`
);
container.push(plugin);
}
return plugin;
}
/**
* load all plugins
* @returns {LoaderOutput$}
*/
loader(): LoaderOutput$ {
const output: LoaderOutput$ = {};
const modules: string[] = fs.readdirSync(GLOBAL_NODE_MODULES_PATH) || [];
modules
.filter(module => /gpm-plugin-[\w\-\_]+/.test(module))
.forEach((moduleName: string) => {
const shortName: string = moduleName.replace(/^gpm-plugin-/, '');
const modulePath: string = path.join(
GLOBAL_NODE_MODULES_PATH,
moduleName
);
const module: Plugin$ = require(modulePath);
module.__gpm__path__ = modulePath;
output[camelcase(shortName)] = module;
});
return output;
}
}
export default new Plugin();