@initx-plugin/manager 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,10 +1,7 @@
1
- import { InitxPlugin, InitxContext } from '@initx-plugin/core';
1
+ import { InitxPlugin, InitxMatcherRules, InitxContext } from '@initx-plugin/core';
2
2
 
3
3
  declare class PluginManagerPlugin extends InitxPlugin {
4
- matchers: {
5
- matching: string;
6
- description: string;
7
- }[];
4
+ rules: InitxMatcherRules;
8
5
  handle(context: InitxContext, type: string, ...others: string[]): Promise<void>;
9
6
  }
10
7
 
package/dist/index.d.ts CHANGED
@@ -1,10 +1,7 @@
1
- import { InitxPlugin, InitxContext } from '@initx-plugin/core';
1
+ import { InitxPlugin, InitxMatcherRules, InitxContext } from '@initx-plugin/core';
2
2
 
3
3
  declare class PluginManagerPlugin extends InitxPlugin {
4
- matchers: {
5
- matching: string;
6
- description: string;
7
- }[];
4
+ rules: InitxMatcherRules;
8
5
  handle(context: InitxContext, type: string, ...others: string[]): Promise<void>;
9
6
  }
10
7
 
package/dist/index.mjs CHANGED
@@ -1,285 +1 @@
1
- import { fetchPlugins, InitxPlugin } from '@initx-plugin/core';
2
- import { c, loadingFunction, log, inquirer } from '@initx-plugin/utils';
3
- import { green, blue, reset, dim, gray } from 'picocolors';
4
- import path from 'node:path';
5
- import columnify from 'columnify';
6
- import fs from 'fs-extra';
7
-
8
- const installedPluginInfo = {
9
- once: false,
10
- names: []
11
- };
12
- const officialName = (targetName) => `@initx-plugin/${targetName}`;
13
- const communityName = (targetName) => `initx-plugin-${targetName}`;
14
- function isInitxPlugin(name) {
15
- return /^@initx-plugin\/|^initx-plugin-/.test(name);
16
- }
17
- function isCompleteMatchName(targetName, searchedName) {
18
- return officialName(targetName) === searchedName || communityName(targetName) === searchedName;
19
- }
20
- function nameColor(name) {
21
- if (/^@initx-plugin\//.test(name)) {
22
- return green(name);
23
- }
24
- return blue(name);
25
- }
26
- async function getInstalledPluginNames() {
27
- if (installedPluginInfo.once) {
28
- return installedPluginInfo.names;
29
- }
30
- const fetchedPlugins = await fetchPlugins();
31
- installedPluginInfo.names = fetchedPlugins.map(({ name }) => name);
32
- installedPluginInfo.once = true;
33
- return installedPluginInfo.names;
34
- }
35
- async function isInstalledPlugin(name) {
36
- const installedPluginNames = await getInstalledPluginNames();
37
- return installedPluginNames.includes(name);
38
- }
39
- async function searchPlugin(pluginNames) {
40
- const plugins = [];
41
- const finedNames = [];
42
- for (const name of pluginNames) {
43
- const result = await c("npm", ["search", "--json", name]);
44
- if (!result.success) {
45
- continue;
46
- }
47
- try {
48
- const json = JSON.parse(result.content);
49
- json.forEach((plugin) => {
50
- if (finedNames.includes(plugin.name)) {
51
- return;
52
- }
53
- finedNames.push(plugin.name);
54
- plugins.push({
55
- name: plugin.name,
56
- version: plugin.version,
57
- description: plugin.description
58
- });
59
- });
60
- } catch (e) {
61
- return [];
62
- }
63
- }
64
- return plugins;
65
- }
66
-
67
- async function addPlugin(targetPlugin) {
68
- const availablePlugins = await loadingFunction("Searching plugin", () => searchAvailablePlugins(targetPlugin));
69
- if (availablePlugins.length === 0) {
70
- log.error(`Plugin ${nameColor(officialName(targetPlugin))} or ${nameColor(communityName(targetPlugin))} not found`);
71
- return;
72
- }
73
- let index = 0;
74
- let needConfirm = true;
75
- if (availablePlugins.length === 2) {
76
- const displayContnet = [];
77
- for (const plugin of availablePlugins) {
78
- displayContnet.push(await displayInfo(plugin, true));
79
- }
80
- index = await inquirer.select(
81
- "Which plugin do you want to install?",
82
- displayContnet
83
- );
84
- needConfirm = false;
85
- }
86
- const pluginInfo = {
87
- name: availablePlugins[index].name,
88
- version: availablePlugins[index].version,
89
- description: availablePlugins[index].description
90
- };
91
- if (needConfirm) {
92
- const confirm = await inquirer.confirm(
93
- `Do you want to install ${await displayInfo(pluginInfo)} ?`
94
- );
95
- if (!confirm) {
96
- log.warn("Installation canceled");
97
- return;
98
- }
99
- }
100
- const installResult = await loadingFunction("Installing plugin", () => installPlugin(pluginInfo.name));
101
- if (!installResult.success) {
102
- log.error(`Failed to install plugin ${nameColor(pluginInfo.name)}`);
103
- console.log(installResult.content);
104
- return;
105
- }
106
- log.success(`Plugin ${nameColor(pluginInfo.name)} installed`);
107
- }
108
- async function searchAvailablePlugins(targetPlugin) {
109
- const searchPluginNames = [
110
- officialName(targetPlugin),
111
- communityName(targetPlugin)
112
- ];
113
- const searchResults = await searchPlugin(searchPluginNames);
114
- const availablePlugins = searchResults?.filter(
115
- (plugin) => isInitxPlugin(plugin.name) && isCompleteMatchName(targetPlugin, plugin.name)
116
- );
117
- return availablePlugins;
118
- }
119
- async function installPlugin(name) {
120
- return c("npm", ["install", "-g", name]);
121
- }
122
- async function displayInfo({ name, version, description }, hasTab = false) {
123
- const isInstalled = await isInstalledPlugin(name);
124
- const spaceChar = hasTab ? " " : " ";
125
- const display = {
126
- name: nameColor(name),
127
- version: reset(dim(gray(`@${version}`))),
128
- description: `${spaceChar}${reset(description)}`,
129
- installed: isInstalled ? dim(green(" [already]")) : spaceChar
130
- };
131
- return `${display.name}${display.version}${display.installed}${display.description}`;
132
- }
133
-
134
- async function showPluginList() {
135
- const plugins = await loadingFunction("Fetching plugins", fetchPlugins);
136
- const displayTable = [];
137
- plugins.forEach(({ root }) => {
138
- const info = fs.readJsonSync(path.join(root, "package.json"));
139
- displayTable.push({
140
- name: nameColor(info.name),
141
- version: gray(info.version),
142
- description: gray(info.description)
143
- });
144
- });
145
- console.log(columnify(displayTable));
146
- }
147
-
148
- async function removePlugin(targetName) {
149
- const plugins = await fetchPlugins();
150
- const removePluginNames = [
151
- officialName(targetName),
152
- communityName(targetName)
153
- ];
154
- const removePlugins = plugins.filter(({ name }) => removePluginNames.includes(name));
155
- if (removePlugins.length === 0) {
156
- const displayNames = removePluginNames.map(nameColor).join(" or ");
157
- log.warn(`Plugin ${displayNames} is not installed`);
158
- return;
159
- }
160
- let index = 0;
161
- let needConfirm = true;
162
- if (removePlugins.length === 2) {
163
- index = await inquirer.select("Which plugin do you want to remove?", removePlugins.map(({ name }) => nameColor(name)));
164
- needConfirm = false;
165
- }
166
- const removePlugin2 = removePlugins[index];
167
- if (needConfirm) {
168
- const confirmResult = await inquirer.confirm(`Are you sure you want to remove ${nameColor(removePlugin2.name)}?`);
169
- if (!confirmResult) {
170
- return;
171
- }
172
- }
173
- await loadingFunction(
174
- `Removing ${nameColor(removePlugin2.name)}...`,
175
- () => c("npm", ["uninstall", "-g", removePlugin2.name])
176
- );
177
- log.success(`Removed ${nameColor(removePlugin2.name)}`);
178
- }
179
-
180
- async function updatePlugin(options) {
181
- const includeDev = options.dev || false;
182
- const [developmentPlugins, fetchedPlugins] = await loadingFunction(
183
- "Fetching plugins",
184
- () => Promise.all([
185
- fetchDevelopmentPlugins(),
186
- fetchPlugins()
187
- ])
188
- );
189
- const pluginNames = [];
190
- const pluginRootMap = fetchedPlugins.reduce((acc, { name, root }) => {
191
- if (!includeDev && developmentPlugins.includes(name)) {
192
- return acc;
193
- }
194
- pluginNames.push(name);
195
- acc[name] = root;
196
- return acc;
197
- }, {});
198
- const searchPluginsInfo = await loadingFunction("Checking plugins", () => searchPlugin(pluginNames));
199
- const needUpdatePlugins = [];
200
- Object.keys(pluginRootMap).forEach((name) => {
201
- const packageInfo = fs.readJsonSync(path.join(pluginRootMap[name], "package.json"));
202
- const pluginInfo = searchPluginsInfo.find((plugin) => plugin.name === name);
203
- if (!pluginInfo) {
204
- return;
205
- }
206
- const isDevelopmentPlugin = developmentPlugins.includes(name);
207
- const condition = isDevelopmentPlugin ? !includeDev : packageInfo.version === pluginInfo.version;
208
- if (condition) {
209
- return;
210
- }
211
- needUpdatePlugins.push({
212
- name,
213
- version: packageInfo.version,
214
- target: pluginInfo.version,
215
- isDev: isDevelopmentPlugin
216
- });
217
- });
218
- if (needUpdatePlugins.length === 0) {
219
- log.success("All plugins are up to date");
220
- return;
221
- }
222
- log.info("Need update plugins:");
223
- console.log(columnify(needUpdatePlugins.map(({ name, version, target, isDev }) => ({
224
- name: nameColor(name),
225
- version: dim(gray(`${isDev ? "[dev] " : ""}${version}`)),
226
- target
227
- }))));
228
- const confirm = await inquirer.confirm("Do you want to update these plugins?");
229
- if (!confirm) {
230
- log.warn("Update canceled");
231
- return;
232
- }
233
- const displayNames = needUpdatePlugins.map(({ name, target }) => `${nameColor(name)}${dim(gray(`@${target}`))}`).join(" ");
234
- await loadingFunction(
235
- `Updating ${displayNames}`,
236
- () => c("npm", ["install", "-g", ...needUpdatePlugins.map(({ name, target }) => `${name}@${target}`)])
237
- );
238
- log.success(`Plugins updated: ${displayNames}`);
239
- }
240
- async function fetchDevelopmentPlugins() {
241
- const npmListResult = await c("npm", ["list", "-g", "--depth=0"]);
242
- const pluginNames = [];
243
- npmListResult.content.split(/\r?\n/).forEach((line) => {
244
- const metched = /(?:@initx-plugin\/|initx-plugin-)[^@]+/.exec(line);
245
- if (metched && line.includes("->")) {
246
- pluginNames.push(metched[0]);
247
- }
248
- });
249
- return pluginNames;
250
- }
251
-
252
- class PluginManagerPlugin extends InitxPlugin {
253
- matchers = [
254
- {
255
- matching: "plugin",
256
- description: "Plugin Manager"
257
- }
258
- ];
259
- async handle(context, type, ...others) {
260
- const [name] = others;
261
- switch (type) {
262
- case "list": {
263
- await showPluginList();
264
- break;
265
- }
266
- case "add": {
267
- await addPlugin(name);
268
- break;
269
- }
270
- case "update": {
271
- await updatePlugin(context.cliOptions);
272
- break;
273
- }
274
- case "remove": {
275
- await removePlugin(name);
276
- break;
277
- }
278
- default: {
279
- log.warn(`Unknown command: ${type}`);
280
- }
281
- }
282
- }
283
- }
284
-
285
- export { PluginManagerPlugin as default };
1
+ import{fetchPlugins as v,InitxPlugin as U}from"@initx-plugin/core";import{c as d,loadingFunction as g,log as m,inquirer as f}from"@initx-plugin/utils";import{green as j,blue as C,dim as y,reset as b,gray as w}from"picocolors";import N from"node:path";import I from"columnify";import D from"fs-extra";const h={once:!1,names:[]},P=n=>`@initx-plugin/${n}`,x=n=>`initx-plugin-${n}`;function J(n){return/^@initx-plugin\/|^initx-plugin-/.test(n)}function O(n,e){return P(n)===e||x(n)===e}function l(n){return/^@initx-plugin\//.test(n)?j(n):C(n)}async function A(){if(h.once)return h.names;const n=await v();return h.names=n.map(({name:e})=>e),h.once=!0,h.names}async function M(n){return(await A()).includes(n)}async function E(n){const e=[],t=[];for(const i of n){const a=await d("npm",["search","--json",i]);if(a.success)try{JSON.parse(a.content).forEach(s=>{t.includes(s.name)||(t.push(s.name),e.push({name:s.name,version:s.version,description:s.description}))})}catch{return[]}}return e}async function R(n){const e=await g("Searching plugin",()=>W(n));if(e.length===0){m.error(`Plugin ${l(P(n))} or ${l(x(n))} not found`);return}let t=0,i=!0;if(e.length===2){const o=[];for(const r of e)o.push(await F(r,!0));t=await f.select("Which plugin do you want to install?",o),i=!1}const a={name:e[t].name,version:e[t].version,description:e[t].description};if(i&&!await f.confirm(`Do you want to install ${await F(a)} ?`)){m.warn("Installation canceled");return}const s=await g("Installing plugin",()=>q(a.name));if(!s.success){m.error(`Failed to install plugin ${l(a.name)}`),console.log(s.content);return}m.success(`Plugin ${l(a.name)} installed`)}async function W(n){const e=[P(n),x(n)];return(await E(e))?.filter(t=>J(t.name)&&O(n,t.name))}async function q(n){return d("npm",["install","-g",n])}async function F({name:n,version:e,description:t},i=!1){const a=await M(n),s=i?" ":" ",o={name:l(n),version:b(y(w(`@${e}`))),description:`${s}${b(t)}`,installed:a?y(j(" [already]")):s};return`${o.name}${o.version}${o.installed}${o.description}`}async function L(){const n=await g("Fetching plugins",v),e=[];n.forEach(({root:t})=>{const i=D.readJsonSync(N.join(t,"package.json"));e.push({name:l(i.name),version:w(i.version),description:w(i.description)})}),console.log(I(e))}async function z(n){const e=await v(),t=[P(n),x(n)],i=e.filter(({name:r})=>t.includes(r));if(i.length===0){const r=t.map(l).join(" or ");m.warn(`Plugin ${r} is not installed`);return}let a=0,s=!0;i.length===2&&(a=await f.select("Which plugin do you want to remove?",i.map(({name:r})=>l(r))),s=!1);const o=i[a];s&&!await f.confirm(`Are you sure you want to remove ${l(o.name)}?`)||(await g(`Removing ${l(o.name)}...`,()=>d("npm",["uninstall","-g",o.name])),m.success(`Removed ${l(o.name)}`))}async function B(n){const e=n.dev||!1,[t,i]=await g("Fetching plugins",()=>Promise.all([G(),v()])),a=[],s=i.reduce((c,{name:u,root:p})=>(!e&&t.includes(u)||(a.push(u),c[u]=p),c),{}),o=await g("Checking plugins",()=>E(a)),r=[];if(Object.keys(s).forEach(c=>{const u=D.readJsonSync(N.join(s[c],"package.json")),p=o.find(S=>S.name===c);if(!p)return;const $=t.includes(c);($?!e:u.version===p.version)||r.push({name:c,version:u.version,target:p.version,isDev:$})}),r.length===0){m.success("All plugins are up to date");return}if(m.info("Need update plugins:"),console.log(I(r.map(({name:c,version:u,target:p,isDev:$})=>({name:l(c),version:y(w(`${$?"[dev] ":""}${u}`)),target:p})))),!await f.confirm("Do you want to update these plugins?")){m.warn("Update canceled");return}const k=r.map(({name:c,target:u})=>`${l(c)}${y(w(`@${u}`))}`).join(" ");await g(`Updating ${k}`,()=>d("npm",["install","-g",...r.map(({name:c,target:u})=>`${c}@${u}`)])),m.success(`Plugins updated: ${k}`)}async function G(){const n=await d("npm",["list","-g","--depth=0"]),e=[];return n.content.split(/\r?\n/).forEach(t=>{const i=/(?:@initx-plugin\/|initx-plugin-)[^@]+/.exec(t);i&&t.includes("->")&&e.push(i[0])}),e}class H extends U{rules=[{matching:"plugin",description:"Plugin Manager",optional:["list","add","update","remove"]}];async handle(e,t,...i){const[a]=i;switch(t){case"list":{await L();break}case"add":{await R(a);break}case"update":{await B(e.cliOptions);break}case"remove":{await z(a);break}default:m.warn(`Unknown command: ${t}`)}}}export{H as default};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@initx-plugin/manager",
3
3
  "type": "module",
4
- "version": "0.0.4",
4
+ "version": "0.0.6",
5
5
  "description": "initx plugin manager",
6
6
  "author": "imba97",
7
7
  "license": "MIT",
@@ -24,34 +24,34 @@
24
24
  "dist"
25
25
  ],
26
26
  "dependencies": {
27
- "@initx-plugin/core": "^0.0.27",
28
- "@initx-plugin/utils": "^0.0.27",
27
+ "@initx-plugin/core": "^0.0.30",
28
+ "@initx-plugin/utils": "^0.0.30",
29
29
  "columnify": "^1.6.0",
30
- "fs-extra": "^11.2.0",
30
+ "fs-extra": "^11.3.0",
31
31
  "picocolors": "^1.1.1"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@imba97/eslint-config": "^0.0.5",
35
35
  "@types/columnify": "^1.5.4",
36
36
  "@types/fs-extra": "^11.0.4",
37
- "@types/node": "^22.10.2",
38
- "bumpp": "^9.9.2",
39
- "eslint": "^9.17.0",
40
- "lint-staged": "^15.3.0",
37
+ "@types/node": "^22.13.9",
38
+ "bumpp": "^10.0.3",
39
+ "eslint": "^9.21.0",
40
+ "lint-staged": "^15.4.3",
41
41
  "simple-git-hooks": "^2.11.1",
42
- "typescript": "^5.7.2",
43
- "unbuild": "^3.1.0"
42
+ "typescript": "^5.8.2",
43
+ "unbuild": "^3.5.0"
44
44
  },
45
45
  "simple-git-hooks": {
46
46
  "pre-commit": "pnpm lint-staged"
47
47
  },
48
48
  "lint-staged": {
49
- "*": "eslint --cache --flag unstable_ts_config --fix"
49
+ "*": "eslint --cache --fix"
50
50
  },
51
51
  "scripts": {
52
52
  "stub": "unbuild --stub",
53
53
  "build": "unbuild",
54
54
  "release": "bumpp",
55
- "lint": "eslint --cache --flag unstable_ts_config"
55
+ "lint": "eslint --cache"
56
56
  }
57
57
  }