UNPKG

7.11 kBJavaScriptView Raw
1"use strict";
2var __values = (this && this.__values) || function(o) {
3 var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4 if (m) return m.call(o);
5 if (o && typeof o.length === "number") return {
6 next: function () {
7 if (o && i >= o.length) o = void 0;
8 return { value: o && o[i++], done: !o };
9 }
10 };
11 throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12};
13var __read = (this && this.__read) || function (o, n) {
14 var m = typeof Symbol === "function" && o[Symbol.iterator];
15 if (!m) return o;
16 var i = m.call(o), r, ar = [], e;
17 try {
18 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19 }
20 catch (error) { e = { error: error }; }
21 finally {
22 try {
23 if (r && !r.done && (m = i["return"])) m.call(i);
24 }
25 finally { if (e) throw e.error; }
26 }
27 return ar;
28};
29var __spread = (this && this.__spread) || function () {
30 for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
31 return ar;
32};
33var __importDefault = (this && this.__importDefault) || function (mod) {
34 return (mod && mod.__esModule) ? mod : { "default": mod };
35};
36Object.defineProperty(exports, "__esModule", { value: true });
37var path_1 = __importDefault(require("path"));
38var fs_1 = __importDefault(require("fs"));
39var chalk_1 = __importDefault(require("chalk"));
40var applyPlugins_1 = require("../plugin/applyPlugins");
41var constant_1 = require("../../shared/constant");
42function loadModuleList(ctx) {
43 var packagePath = ctx.rootPkg;
44 var pluginDir = path_1.default.join(ctx.root, 'node_modules');
45 var extend = function (target, source) {
46 for (var obj in source) {
47 target[obj] = source[obj];
48 }
49 return target;
50 };
51 if (fs_1.default.existsSync(packagePath)) {
52 var content = fs_1.default.readFileSync(packagePath, 'utf8');
53 var json = JSON.parse(content);
54 var deps = extend(json.dependencies || {}, json.devDependencies || {});
55 var keys = Object.keys(deps);
56 var list = keys
57 .filter(function (name) {
58 if (!/^feflow-plugin-|^@[^/]+\/feflow-plugin-|generator-|^@[^/]+\/generator-/.test(name))
59 return false;
60 var pluginPath = path_1.default.join(pluginDir, name);
61 return fs_1.default.existsSync(pluginPath);
62 })
63 .map(function (key) {
64 return {
65 name: key,
66 version: getModuleVersion(pluginDir, key)
67 };
68 });
69 return list;
70 }
71 else {
72 return [];
73 }
74}
75function getModuleVersion(dir, name) {
76 var packagePath = path_1.default.resolve(dir, name, 'package.json');
77 if (fs_1.default.existsSync(packagePath)) {
78 var content = fs_1.default.readFileSync(packagePath, 'utf8');
79 var json = JSON.parse(content);
80 return (json && json.version) || 'unknown';
81 }
82 else {
83 return 'unknown';
84 }
85}
86function loadUniversalPlugin(ctx) {
87 var e_1, _a;
88 var universalPkg = ctx.universalPkg;
89 var availablePlugins = [];
90 try {
91 for (var _b = __values(universalPkg.getInstalled()), _c = _b.next(); !_c.done; _c = _b.next()) {
92 var _d = __read(_c.value, 2), pkg = _d[0], version = _d[1];
93 availablePlugins.push({
94 name: pkg,
95 version: version
96 });
97 }
98 }
99 catch (e_1_1) { e_1 = { error: e_1_1 }; }
100 finally {
101 try {
102 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
103 }
104 finally { if (e_1) throw e_1.error; }
105 }
106 return availablePlugins;
107}
108function showPlugin(ctx, from, item) {
109 var repoPath = path_1.default.join(ctx === null || ctx === void 0 ? void 0 : ctx.universalModules, item.name + "@" + item.version);
110 var useCommand = item.name.replace(constant_1.FEFLOW_PLUGIN_PREFIX, '');
111 var plugin = applyPlugins_1.resolvePlugin(ctx, repoPath);
112 if (plugin.name) {
113 useCommand = plugin.name;
114 }
115 console.log(chalk_1.default.magenta(from + "(command: " + useCommand + ", version: " + item.version + ")"));
116}
117module.exports = function (ctx) {
118 ctx.commander.register('list', 'Show all plugins installed.', function () {
119 var list = loadModuleList(ctx);
120 var universalPlugins = loadUniversalPlugin(ctx);
121 list.push.apply(list, __spread(universalPlugins));
122 console.log('You can search more templates or plugins through https://feflowjs.com/encology/');
123 if (!list.length) {
124 console.log(chalk_1.default.magenta('No templates and plugins have been installed'));
125 return;
126 }
127 var plugins = [];
128 var templates = [];
129 list.forEach(function (item) {
130 if (/generator-|^@[^/]+\/generator-/.test(item.name)) {
131 templates.push(item);
132 }
133 else {
134 plugins.push(item);
135 }
136 });
137 console.log('templates');
138 if (templates.length == 0) {
139 console.log(chalk_1.default.magenta('No templates have been installed'));
140 }
141 else {
142 templates.forEach(function (item) {
143 return console.log(chalk_1.default.magenta(item.name + "(" + item.version + ")"));
144 });
145 }
146 var storePlugins = [];
147 var gitPlugins = [];
148 var localPlugins = [];
149 plugins.forEach(function (item) {
150 if (item.name.startsWith(constant_1.FEFLOW_PLUGIN_GIT_PREFIX)) {
151 gitPlugins.push(item);
152 }
153 else if (item.name.startsWith(constant_1.FEFLOW_PLUGIN_LOCAL_PREFIX)) {
154 localPlugins.push(item);
155 }
156 else if (item.name.startsWith(constant_1.FEFLOW_PLUGIN_PREFIX) ||
157 /^@[^/]+\/feflow-plugin-/.test(item.name)) {
158 storePlugins.push(item);
159 }
160 });
161 console.log('plugins');
162 if (storePlugins.length == 0 && gitPlugins.length == 0) {
163 console.log(chalk_1.default.magenta('No plugins have been installed'));
164 }
165 else {
166 storePlugins.forEach(function (item) {
167 return console.log(chalk_1.default.magenta(item.name + "(" + item.version + ")"));
168 });
169 }
170 if (gitPlugins.length > 0) {
171 console.log('git plugins');
172 gitPlugins.forEach(function (item) {
173 var url = 'http://' +
174 decodeURIComponent(item.name.replace(constant_1.FEFLOW_PLUGIN_GIT_PREFIX, ''));
175 showPlugin(ctx, url, item);
176 });
177 }
178 if (localPlugins.length > 0) {
179 console.log('local plugins');
180 localPlugins.forEach(function (item) {
181 var localPath = decodeURIComponent(item.name.replace(constant_1.FEFLOW_PLUGIN_LOCAL_PREFIX, ''));
182 showPlugin(ctx, localPath, item);
183 });
184 }
185 });
186};
187//# sourceMappingURL=list.js.map
\No newline at end of file