1 | import path from 'path';
|
2 | import fs from 'fs';
|
3 | import chalk from 'chalk';
|
4 | import { UniversalPkg } from '../universal-pkg/dep/pkg';
|
5 | import { resolvePlugin } from '../plugin/applyPlugins';
|
6 | import {
|
7 | FEFLOW_PLUGIN_GIT_PREFIX,
|
8 | FEFLOW_PLUGIN_LOCAL_PREFIX,
|
9 | FEFLOW_PLUGIN_PREFIX
|
10 | } from '../../shared/constant';
|
11 |
|
12 | function loadModuleList(ctx: any) {
|
13 | const packagePath = ctx.rootPkg;
|
14 | const pluginDir = path.join(ctx.root, 'node_modules');
|
15 | const extend = function(target: any, source: any) {
|
16 | for (const obj in source) {
|
17 | target[obj] = source[obj];
|
18 | }
|
19 | return target;
|
20 | };
|
21 | if (fs.existsSync(packagePath)) {
|
22 | const content = fs.readFileSync(packagePath, 'utf8');
|
23 | const json = JSON.parse(content);
|
24 | const deps = extend(json.dependencies || {}, json.devDependencies || {});
|
25 | const keys = Object.keys(deps);
|
26 | const list = keys
|
27 | .filter(function(name) {
|
28 | if (
|
29 | !/^feflow-plugin-|^@[^/]+\/feflow-plugin-|generator-|^@[^/]+\/generator-/.test(
|
30 | name
|
31 | )
|
32 | )
|
33 | return false;
|
34 | const pluginPath = path.join(pluginDir, name);
|
35 | return fs.existsSync(pluginPath);
|
36 | })
|
37 | .map(key => {
|
38 | return {
|
39 | name: key,
|
40 | version: getModuleVersion(pluginDir, key)
|
41 | };
|
42 | });
|
43 | return list;
|
44 | } else {
|
45 | return [];
|
46 | }
|
47 | }
|
48 |
|
49 | function getModuleVersion(dir: string, name: string): string {
|
50 | const packagePath = path.resolve(dir, name, 'package.json');
|
51 | if (fs.existsSync(packagePath)) {
|
52 | const content = fs.readFileSync(packagePath, 'utf8');
|
53 | const json = JSON.parse(content);
|
54 | return (json && json.version) || 'unknown';
|
55 | } else {
|
56 | return 'unknown';
|
57 | }
|
58 | }
|
59 |
|
60 | function loadUniversalPlugin(ctx: any): any[] {
|
61 | const { universalPkg }: { universalPkg: UniversalPkg } = ctx;
|
62 | const availablePlugins: any[] = [];
|
63 |
|
64 | for (const [pkg, version] of universalPkg.getInstalled()) {
|
65 | availablePlugins.push({
|
66 | name: pkg,
|
67 | version: version
|
68 | });
|
69 | }
|
70 |
|
71 | return availablePlugins;
|
72 | }
|
73 |
|
74 | function showPlugin(ctx: any, from: string, item: any) {
|
75 | const repoPath = path.join(
|
76 | ctx?.universalModules,
|
77 | `${item.name}@${item.version}`
|
78 | );
|
79 | let useCommand = item.name.replace(FEFLOW_PLUGIN_PREFIX, '');
|
80 | const plugin = resolvePlugin(ctx, repoPath);
|
81 | if (plugin.name) {
|
82 | useCommand = plugin.name;
|
83 | }
|
84 | console.log(
|
85 | chalk.magenta(`${from}(command: ${useCommand}, version: ${item.version})`)
|
86 | );
|
87 | }
|
88 |
|
89 | module.exports = (ctx: any) => {
|
90 | ctx.commander.register('list', 'Show all plugins installed.', () => {
|
91 | const list = loadModuleList(ctx);
|
92 | const universalPlugins = loadUniversalPlugin(ctx);
|
93 | list.push(...universalPlugins);
|
94 |
|
95 | console.log(
|
96 | 'You can search more templates or plugins through https://feflowjs.com/encology/'
|
97 | );
|
98 | if (!list.length) {
|
99 | console.log(
|
100 | chalk.magenta('No templates and plugins have been installed')
|
101 | );
|
102 | return;
|
103 | }
|
104 |
|
105 | const plugins: any[] = [];
|
106 | const templates: any[] = [];
|
107 | list.forEach(item => {
|
108 | if (/generator-|^@[^/]+\/generator-/.test(item.name)) {
|
109 | templates.push(item);
|
110 | } else {
|
111 | plugins.push(item);
|
112 | }
|
113 | });
|
114 | console.log('templates');
|
115 | if (templates.length == 0) {
|
116 | console.log(chalk.magenta('No templates have been installed'));
|
117 | } else {
|
118 | templates.forEach(item =>
|
119 | console.log(chalk.magenta(`${item.name}(${item.version})`))
|
120 | );
|
121 | }
|
122 | const storePlugins: any[] = [];
|
123 | const gitPlugins: any[] = [];
|
124 | const localPlugins: any[] = [];
|
125 | plugins.forEach(item => {
|
126 | if (item.name.startsWith(FEFLOW_PLUGIN_GIT_PREFIX)) {
|
127 | gitPlugins.push(item);
|
128 | } else if (item.name.startsWith(FEFLOW_PLUGIN_LOCAL_PREFIX)) {
|
129 | localPlugins.push(item);
|
130 | } else if (
|
131 | item.name.startsWith(FEFLOW_PLUGIN_PREFIX) ||
|
132 | /^@[^/]+\/feflow-plugin-/.test(item.name)
|
133 | ) {
|
134 | storePlugins.push(item);
|
135 | }
|
136 | });
|
137 | console.log('plugins');
|
138 | if (storePlugins.length == 0 && gitPlugins.length == 0) {
|
139 | console.log(chalk.magenta('No plugins have been installed'));
|
140 | } else {
|
141 | storePlugins.forEach(item =>
|
142 | console.log(chalk.magenta(`${item.name}(${item.version})`))
|
143 | );
|
144 | }
|
145 | if (gitPlugins.length > 0) {
|
146 | console.log('git plugins');
|
147 | gitPlugins.forEach(item => {
|
148 | const url =
|
149 | 'http://' +
|
150 | decodeURIComponent(item.name.replace(FEFLOW_PLUGIN_GIT_PREFIX, ''));
|
151 | showPlugin(ctx, url, item);
|
152 | });
|
153 | }
|
154 | if (localPlugins.length > 0) {
|
155 | console.log('local plugins');
|
156 | localPlugins.forEach(item => {
|
157 | const localPath = decodeURIComponent(
|
158 | item.name.replace(FEFLOW_PLUGIN_LOCAL_PREFIX, '')
|
159 | );
|
160 | showPlugin(ctx, localPath, item);
|
161 | });
|
162 | }
|
163 | });
|
164 | };
|