@initx-plugin/manager 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 imba97 <https://github.com/imba97>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,53 @@
1
+ ## @initx-plugin/manager
2
+
3
+ `initx` plugin manager
4
+
5
+ ## Usage
6
+
7
+ ```bash
8
+ npm i @initx-plugin/manager -g
9
+ ```
10
+
11
+ ### List
12
+
13
+ ```bash
14
+ npx initx plugin list
15
+ ```
16
+
17
+ List all installed plugins
18
+
19
+ ### Add
20
+
21
+ ```bash
22
+ # npx initx plugin add <plugin-name>
23
+ npx initx plugin add git
24
+ ```
25
+
26
+ This command will search and install `@initx-plugin/git` or `initx-plugin-git`
27
+
28
+ If there are multiple, let the user choose
29
+
30
+ ### Update
31
+
32
+ ```bash
33
+ npx initx plugin update
34
+ ```
35
+
36
+ Detect the versions of all plugins and update the plugins that need to be updated
37
+
38
+ Automatically filter local development plugins
39
+
40
+ ### Remove
41
+
42
+ ```bash
43
+ # npx initx plugin remove <plugin-name>
44
+ npx initx plugin remove git
45
+ ```
46
+
47
+ This command will remove `@initx-plugin/git` or `initx-plugin-git`
48
+
49
+ If there are multiple, let the user choose
50
+
51
+ ## Documentation
52
+
53
+ [initx](https://github.com/initx-collective/initx)
@@ -0,0 +1,11 @@
1
+ import { InitxPlugin, InitxContext } from '@initx-plugin/core';
2
+
3
+ declare class PluginManagerPlugin extends InitxPlugin {
4
+ matchers: {
5
+ matching: string;
6
+ description: string;
7
+ }[];
8
+ handle(_ctx: InitxContext, type: string, ...others: string[]): Promise<void>;
9
+ }
10
+
11
+ export { PluginManagerPlugin as default };
@@ -0,0 +1,11 @@
1
+ import { InitxPlugin, InitxContext } from '@initx-plugin/core';
2
+
3
+ declare class PluginManagerPlugin extends InitxPlugin {
4
+ matchers: {
5
+ matching: string;
6
+ description: string;
7
+ }[];
8
+ handle(_ctx: InitxContext, type: string, ...others: string[]): Promise<void>;
9
+ }
10
+
11
+ export { PluginManagerPlugin as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,286 @@
1
+ import { fetchPlugins, InitxPlugin } from '@initx-plugin/core';
2
+ import { c, log, inquirer } from '@initx-plugin/utils';
3
+ import { green, blue, reset, dim, gray } from 'picocolors';
4
+ import ora from 'ora';
5
+ import path from 'node:path';
6
+ import fs from 'fs-extra';
7
+ import columnify from 'columnify';
8
+
9
+ const installedPluginInfo = {
10
+ once: false,
11
+ names: []
12
+ };
13
+ const officialName = (targetName) => `@initx-plugin/${targetName}`;
14
+ const communityName = (targetName) => `initx-plugin-${targetName}`;
15
+ function isInitxPlugin(name) {
16
+ return /^@initx-plugin\/|^initx-plugin-/.test(name);
17
+ }
18
+ function isCompleteMatchName(targetName, searchedName) {
19
+ return officialName(targetName) === searchedName || communityName(targetName) === searchedName;
20
+ }
21
+ function nameColor(name) {
22
+ if (/^@initx-plugin\//.test(name)) {
23
+ return green(name);
24
+ }
25
+ return blue(name);
26
+ }
27
+ async function loadingFunction(message, fn) {
28
+ const spinner = ora(message).start();
29
+ return fn().finally(() => {
30
+ spinner.stop();
31
+ });
32
+ }
33
+ async function getInstalledPluginNames() {
34
+ if (installedPluginInfo.once) {
35
+ return installedPluginInfo.names;
36
+ }
37
+ const fetchedPlugins = await fetchPlugins();
38
+ installedPluginInfo.names = fetchedPlugins.map(({ name }) => name);
39
+ installedPluginInfo.once = true;
40
+ return installedPluginInfo.names;
41
+ }
42
+ async function isInstalledPlugin(name) {
43
+ const installedPluginNames = await getInstalledPluginNames();
44
+ return installedPluginNames.includes(name);
45
+ }
46
+ async function searchPlugin(pluginNames) {
47
+ const plugins = [];
48
+ const finedNames = [];
49
+ for (const name of pluginNames) {
50
+ const result = await c("npm", ["search", "--json", name]);
51
+ if (!result.success) {
52
+ continue;
53
+ }
54
+ try {
55
+ const json = JSON.parse(result.content);
56
+ if (finedNames.includes(json.name)) {
57
+ continue;
58
+ }
59
+ finedNames.push(json.name);
60
+ json.forEach((plugin) => {
61
+ plugins.push({
62
+ name: plugin.name,
63
+ version: plugin.version,
64
+ description: plugin.description
65
+ });
66
+ });
67
+ } catch (e) {
68
+ return [];
69
+ }
70
+ }
71
+ return plugins;
72
+ }
73
+
74
+ async function addPlugin(targetPlugin) {
75
+ const availablePlugins = await loadingFunction("Searching plugin", () => searchAvailablePlugins(targetPlugin));
76
+ if (availablePlugins.length === 0) {
77
+ log.error(`Plugin ${nameColor(officialName(targetPlugin))} or ${nameColor(communityName(targetPlugin))} not found`);
78
+ return;
79
+ }
80
+ let index = 0;
81
+ let needConfirm = true;
82
+ if (availablePlugins.length === 2) {
83
+ const displayContnet = [];
84
+ for (const plugin of availablePlugins) {
85
+ displayContnet.push(await displayInfo(plugin));
86
+ }
87
+ index = await inquirer.select(
88
+ "Which plugin do you want to install?",
89
+ displayContnet
90
+ );
91
+ needConfirm = false;
92
+ }
93
+ const pluginInfo = {
94
+ name: availablePlugins[index].name,
95
+ version: availablePlugins[index].version,
96
+ description: availablePlugins[index].description
97
+ };
98
+ if (needConfirm) {
99
+ const confirm = await inquirer.confirm(
100
+ `Do you want to install ${await displayInfo(pluginInfo)} ?`
101
+ );
102
+ if (!confirm) {
103
+ log.warn("Installation canceled");
104
+ return;
105
+ }
106
+ }
107
+ const installResult = await loadingFunction("Installing plugin", () => installPlugin(pluginInfo.name));
108
+ if (!installResult.success) {
109
+ log.error(`Failed to install plugin ${nameColor(pluginInfo.name)}`);
110
+ console.log(installResult.content);
111
+ return;
112
+ }
113
+ log.success(`Plugin ${nameColor(pluginInfo.name)} installed`);
114
+ }
115
+ async function searchAvailablePlugins(targetPlugin) {
116
+ const searchPluginNames = [
117
+ officialName(targetPlugin),
118
+ communityName(targetPlugin)
119
+ ];
120
+ const searchResults = await searchPlugin(searchPluginNames);
121
+ const availablePlugins = searchResults?.filter(
122
+ (plugin) => isInitxPlugin(plugin.name) && isCompleteMatchName(targetPlugin, plugin.name)
123
+ );
124
+ return availablePlugins;
125
+ }
126
+ async function installPlugin(name) {
127
+ return c("npm", ["install", "-g", name]);
128
+ }
129
+ async function displayInfo({ name, version, description }) {
130
+ const isInstalled = await isInstalledPlugin(name);
131
+ const display = {
132
+ name: nameColor(name),
133
+ version: reset(dim(gray(`@${version}`))),
134
+ description: reset(description),
135
+ installed: isInstalled ? dim(green(" [already]")) : " "
136
+ };
137
+ return `${display.name}${display.version}${display.installed} ${display.description}`;
138
+ }
139
+
140
+ async function showPluginList() {
141
+ const plugins = await loadingFunction("Fetching plugins", fetchPlugins);
142
+ const displayTable = [];
143
+ plugins.forEach(({ root }) => {
144
+ const info = fs.readJsonSync(path.join(root, "package.json"));
145
+ displayTable.push({
146
+ name: nameColor(info.name),
147
+ version: gray(info.version),
148
+ description: gray(info.description)
149
+ });
150
+ });
151
+ console.log(columnify(displayTable));
152
+ }
153
+
154
+ async function updatePlugin() {
155
+ const excludedPlugins = await fetchExcludedPlugins();
156
+ const fetchedPlugins = await fetchPlugins();
157
+ const pluginNames = [];
158
+ const pluginRootMap = fetchedPlugins.reduce((acc, { name, root }) => {
159
+ if (excludedPlugins.includes(name)) {
160
+ return acc;
161
+ }
162
+ pluginNames.push(name);
163
+ acc[name] = root;
164
+ return acc;
165
+ }, {});
166
+ const searchPluginsInfo = await loadingFunction("Checking plugins", () => searchPlugin(pluginNames));
167
+ const needUpdatePlugins = [];
168
+ Object.keys(pluginRootMap).forEach((name) => {
169
+ const packageInfo = fs.readJsonSync(path.join(pluginRootMap[name], "package.json"));
170
+ const pluginInfo = searchPluginsInfo.find((plugin) => plugin.name === name);
171
+ if (!pluginInfo || packageInfo.version === pluginInfo.version) {
172
+ return;
173
+ }
174
+ needUpdatePlugins.push({
175
+ name,
176
+ version: packageInfo.version,
177
+ target: pluginInfo.version
178
+ });
179
+ });
180
+ if (needUpdatePlugins.length === 0) {
181
+ log.success("All plugins are up to date");
182
+ return;
183
+ }
184
+ log.info("Need update plugins:");
185
+ needUpdatePlugins.forEach(({ name, version, target }) => {
186
+ console.log(`${nameColor(name)} ${dim(gray(`${version} ->`))} ${target}`);
187
+ });
188
+ const confirm = await inquirer.confirm("Do you want to update these plugins?");
189
+ if (!confirm) {
190
+ log.warn("Update canceled");
191
+ return;
192
+ }
193
+ const displayNames = needUpdatePlugins.map(({ name, target }) => `${nameColor(name)}${dim(gray(`@${target}`))}`).join(" ");
194
+ await loadingFunction(
195
+ `Updating ${displayNames}`,
196
+ () => c("npm", ["install", "-g", ...needUpdatePlugins.map(({ name, target }) => `${name}@${target}`)])
197
+ );
198
+ log.success(`Plugins updated: ${displayNames}`);
199
+ }
200
+ async function fetchExcludedPlugins() {
201
+ const npmListResult = await c("npm", ["list", "-g", "--depth=0"]);
202
+ const excludedPlugins = [];
203
+ npmListResult.content.split(/\r?\n/).forEach((line) => {
204
+ const metched = /(?:@initx-plugin\/|initx-plugin-)[^@]+/.exec(line);
205
+ if (metched && line.includes("->")) {
206
+ excludedPlugins.push(metched[0]);
207
+ }
208
+ });
209
+ return excludedPlugins;
210
+ }
211
+
212
+ async function removePlugin(targetName) {
213
+ const plugins = await fetchPlugins();
214
+ const removePluginNames = [
215
+ officialName(targetName),
216
+ communityName(targetName)
217
+ ];
218
+ const removePlugins = plugins.filter(({ name }) => removePluginNames.includes(name));
219
+ if (removePlugins.length === 0) {
220
+ const displayNames = removePluginNames.map(nameColor).join(" or ");
221
+ log.warn(`Plugin ${displayNames} is not installed`);
222
+ return;
223
+ }
224
+ let index = 0;
225
+ let needConfirm = true;
226
+ if (removePlugins.length === 2) {
227
+ index = await inquirer.select("Which plugin do you want to remove?", removePlugins.map(({ name }) => nameColor(name)));
228
+ needConfirm = false;
229
+ }
230
+ const removePlugin2 = removePlugins[index];
231
+ if (needConfirm) {
232
+ const confirmResult = await inquirer.confirm(`Are you sure you want to remove ${nameColor(removePlugin2.name)}?`);
233
+ if (!confirmResult) {
234
+ return;
235
+ }
236
+ }
237
+ await loadingFunction(
238
+ `Removing ${nameColor(removePlugin2.name)}...`,
239
+ () => c("npm", ["uninstall", "-g", removePlugin2.name])
240
+ );
241
+ log.success(`Removed ${nameColor(removePlugin2.name)}`);
242
+ }
243
+
244
+ var __defProp = Object.defineProperty;
245
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
246
+ var __publicField = (obj, key, value) => {
247
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
248
+ return value;
249
+ };
250
+ class PluginManagerPlugin extends InitxPlugin {
251
+ constructor() {
252
+ super(...arguments);
253
+ __publicField(this, "matchers", [
254
+ {
255
+ matching: "plugin",
256
+ description: "Plugin Manager"
257
+ }
258
+ ]);
259
+ }
260
+ async handle(_ctx, type, ...others) {
261
+ const [name] = others;
262
+ switch (type) {
263
+ case "list": {
264
+ showPluginList();
265
+ break;
266
+ }
267
+ case "add": {
268
+ await addPlugin(name);
269
+ break;
270
+ }
271
+ case "update": {
272
+ await updatePlugin();
273
+ break;
274
+ }
275
+ case "remove": {
276
+ await removePlugin(name);
277
+ break;
278
+ }
279
+ default: {
280
+ log.warn(`Unknown command: ${type}`);
281
+ }
282
+ }
283
+ }
284
+ }
285
+
286
+ export { PluginManagerPlugin as default };
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@initx-plugin/manager",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "description": "initx plugin manager",
6
+ "author": "imba97",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/initx-collective/initx-plugin-manager#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git@github.com:initx-collective/initx-plugin-manager.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/initx-collective/initx-plugin-manager/issues"
15
+ },
16
+ "keywords": [
17
+ "initx",
18
+ "initx-plugin"
19
+ ],
20
+ "main": "dist/index.mjs",
21
+ "module": "dist/index.mjs",
22
+ "types": "dist/index.d.ts",
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "dependencies": {
27
+ "@initx-plugin/core": "^0.0.23",
28
+ "@initx-plugin/utils": "^0.0.23",
29
+ "columnify": "^1.6.0",
30
+ "fs-extra": "^11.2.0",
31
+ "ora": "^8.1.1",
32
+ "picocolors": "^1.1.1"
33
+ },
34
+ "devDependencies": {
35
+ "@imba97/eslint-config": "^0.0.4",
36
+ "@types/columnify": "^1.5.4",
37
+ "@types/fs-extra": "^11.0.4",
38
+ "@types/node": "^22.9.0",
39
+ "bumpp": "^9.8.1",
40
+ "eslint": "^9.14.0",
41
+ "lint-staged": "^15.2.10",
42
+ "simple-git-hooks": "^2.11.1",
43
+ "typescript": "^5.6.3",
44
+ "unbuild": "^2.0.0"
45
+ },
46
+ "simple-git-hooks": {
47
+ "pre-commit": "pnpm lint-staged"
48
+ },
49
+ "lint-staged": {
50
+ "*": "eslint --cache --flag unstable_ts_config --fix"
51
+ },
52
+ "scripts": {
53
+ "stub": "unbuild --stub",
54
+ "build": "unbuild",
55
+ "release": "bumpp",
56
+ "lint": "eslint --cache --flag unstable_ts_config"
57
+ }
58
+ }