@tutinoko2048/mcutil 1.0.3 → 1.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/cli.mjs +36 -124
- package/package.json +2 -3
- package/src/commands/pkg.ts +40 -131
package/dist/cli.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import inquirer from "inquirer";
|
|
|
4
4
|
import fs from "node:fs/promises";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import semver from "semver";
|
|
7
|
-
import {
|
|
7
|
+
import { installPackage } from "@antfu/install-pkg";
|
|
8
8
|
|
|
9
9
|
//#region src/commands/link.ts
|
|
10
10
|
function getAppData() {
|
|
@@ -104,11 +104,26 @@ function registerLinkCommand(program) {
|
|
|
104
104
|
//#endregion
|
|
105
105
|
//#region src/commands/pkg.ts
|
|
106
106
|
const PACKAGES = [
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
107
|
+
{
|
|
108
|
+
name: "@minecraft/server",
|
|
109
|
+
dev: true
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: "@minecraft/server-ui",
|
|
113
|
+
dev: true
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: "@minecraft/server-net",
|
|
117
|
+
dev: true
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
name: "@minecraft/server-admin",
|
|
121
|
+
dev: true
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
name: "@minecraft/vanilla-data",
|
|
125
|
+
dev: false
|
|
126
|
+
}
|
|
112
127
|
];
|
|
113
128
|
const CATEGORY_ORDER = [
|
|
114
129
|
"release",
|
|
@@ -162,114 +177,6 @@ async function readPackageJson(cwd) {
|
|
|
162
177
|
function getCurrentVersion(pkgJson, name) {
|
|
163
178
|
return pkgJson.dependencies?.[name] ?? pkgJson.devDependencies?.[name] ?? null;
|
|
164
179
|
}
|
|
165
|
-
async function detectPackageManagers(cwd) {
|
|
166
|
-
const checks = [
|
|
167
|
-
{
|
|
168
|
-
name: "pnpm",
|
|
169
|
-
files: ["pnpm-lock.yaml"]
|
|
170
|
-
},
|
|
171
|
-
{
|
|
172
|
-
name: "npm",
|
|
173
|
-
files: ["package-lock.json"]
|
|
174
|
-
},
|
|
175
|
-
{
|
|
176
|
-
name: "yarn",
|
|
177
|
-
files: ["yarn.lock"]
|
|
178
|
-
},
|
|
179
|
-
{
|
|
180
|
-
name: "bun",
|
|
181
|
-
files: ["bun.lock", "bun.lockb"]
|
|
182
|
-
}
|
|
183
|
-
];
|
|
184
|
-
const matches = [];
|
|
185
|
-
await Promise.all(checks.map(async (check) => {
|
|
186
|
-
try {
|
|
187
|
-
await Promise.all(check.files.map((file) => fs.access(path.join(cwd, file))));
|
|
188
|
-
matches.push(check.name);
|
|
189
|
-
} catch {
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
}));
|
|
193
|
-
return matches;
|
|
194
|
-
}
|
|
195
|
-
async function resolvePackageManager(cwd) {
|
|
196
|
-
const matches = await detectPackageManagers(cwd);
|
|
197
|
-
if (matches.length === 1) return matches[0];
|
|
198
|
-
const { manager } = await inquirer.prompt([{
|
|
199
|
-
name: "manager",
|
|
200
|
-
type: "select",
|
|
201
|
-
message: "Select package manager:",
|
|
202
|
-
choices: [
|
|
203
|
-
{
|
|
204
|
-
name: "pnpm",
|
|
205
|
-
value: "pnpm"
|
|
206
|
-
},
|
|
207
|
-
{
|
|
208
|
-
name: "npm",
|
|
209
|
-
value: "npm"
|
|
210
|
-
},
|
|
211
|
-
{
|
|
212
|
-
name: "yarn",
|
|
213
|
-
value: "yarn"
|
|
214
|
-
},
|
|
215
|
-
{
|
|
216
|
-
name: "bun",
|
|
217
|
-
value: "bun"
|
|
218
|
-
}
|
|
219
|
-
]
|
|
220
|
-
}]);
|
|
221
|
-
return manager;
|
|
222
|
-
}
|
|
223
|
-
function buildInstallCommand(manager, packages) {
|
|
224
|
-
const baseCommand = process.platform === "win32" ? `${manager}.cmd` : manager;
|
|
225
|
-
switch (manager) {
|
|
226
|
-
case "pnpm": return {
|
|
227
|
-
command: baseCommand,
|
|
228
|
-
args: ["add", ...packages]
|
|
229
|
-
};
|
|
230
|
-
case "yarn": return {
|
|
231
|
-
command: baseCommand,
|
|
232
|
-
args: ["add", ...packages]
|
|
233
|
-
};
|
|
234
|
-
case "bun": return {
|
|
235
|
-
command: baseCommand,
|
|
236
|
-
args: ["add", ...packages]
|
|
237
|
-
};
|
|
238
|
-
default: return {
|
|
239
|
-
command: baseCommand,
|
|
240
|
-
args: ["install", ...packages]
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
async function runInstall(manager, packages) {
|
|
245
|
-
if (packages.length === 0) return;
|
|
246
|
-
const { command, args } = buildInstallCommand(manager, packages);
|
|
247
|
-
const spawnCommand = process.platform === "win32" ? "cmd.exe" : command;
|
|
248
|
-
const spawnArgs = process.platform === "win32" ? [
|
|
249
|
-
"/d",
|
|
250
|
-
"/s",
|
|
251
|
-
"/c",
|
|
252
|
-
toWindowsCommand([command, ...args])
|
|
253
|
-
] : args;
|
|
254
|
-
await new Promise((resolve, reject) => {
|
|
255
|
-
const child = spawn(spawnCommand, spawnArgs, { stdio: "inherit" });
|
|
256
|
-
child.on("close", (code) => {
|
|
257
|
-
if (code === 0) {
|
|
258
|
-
resolve();
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
reject(/* @__PURE__ */ new Error(`${command} exited with code ${code ?? "unknown"}`));
|
|
262
|
-
});
|
|
263
|
-
child.on("error", reject);
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
function toWindowsCommand(parts) {
|
|
267
|
-
return parts.map(quoteWindowsArg).join(" ");
|
|
268
|
-
}
|
|
269
|
-
function quoteWindowsArg(value) {
|
|
270
|
-
if (!/[\s"^&|<>]/.test(value)) return value;
|
|
271
|
-
return `"${value.replace(/"/g, "\\\"")}"`;
|
|
272
|
-
}
|
|
273
180
|
function groupVersionsByCategory(versions) {
|
|
274
181
|
const grouped = /* @__PURE__ */ new Map();
|
|
275
182
|
for (const version of versions) {
|
|
@@ -286,11 +193,11 @@ async function promptForPackage(pkgJson) {
|
|
|
286
193
|
name: "target",
|
|
287
194
|
type: "select",
|
|
288
195
|
message: "Select package:",
|
|
289
|
-
choices: PACKAGES.map((
|
|
290
|
-
const current = getCurrentVersion(pkgJson, name);
|
|
196
|
+
choices: PACKAGES.map((pkg) => {
|
|
197
|
+
const current = getCurrentVersion(pkgJson, pkg.name);
|
|
291
198
|
return {
|
|
292
|
-
name: current ? `${name} (current: ${current})` : `${name} (current: -)`,
|
|
293
|
-
value:
|
|
199
|
+
name: current ? `${pkg.name} (current: ${current})` : `${pkg.name} (current: -)`,
|
|
200
|
+
value: pkg
|
|
294
201
|
};
|
|
295
202
|
})
|
|
296
203
|
}];
|
|
@@ -334,16 +241,21 @@ async function runPkgFlow() {
|
|
|
334
241
|
const cwd = process.cwd();
|
|
335
242
|
try {
|
|
336
243
|
const pkgJson = await readPackageJson(cwd);
|
|
337
|
-
const manager = await resolvePackageManager(cwd);
|
|
338
244
|
const target = await promptForPackage(pkgJson);
|
|
339
245
|
if (!target) {
|
|
340
246
|
console.log("No package selected.");
|
|
341
247
|
return;
|
|
342
248
|
}
|
|
343
|
-
const currentCategory = inferCategoryFromCurrentVersion(getCurrentVersion(pkgJson, target));
|
|
344
|
-
const grouped = groupVersionsByCategory(await fetchPackageVersions(target));
|
|
345
|
-
const category = await promptForCategory(target, grouped, currentCategory);
|
|
346
|
-
|
|
249
|
+
const currentCategory = inferCategoryFromCurrentVersion(getCurrentVersion(pkgJson, target.name));
|
|
250
|
+
const grouped = groupVersionsByCategory(await fetchPackageVersions(target.name));
|
|
251
|
+
const category = await promptForCategory(target.name, grouped, currentCategory);
|
|
252
|
+
const list = grouped.get(category) ?? [];
|
|
253
|
+
await promptForVersion(target.name, list);
|
|
254
|
+
await installPackage(target.name, {
|
|
255
|
+
silent: false,
|
|
256
|
+
cwd,
|
|
257
|
+
dev: target.dev
|
|
258
|
+
});
|
|
347
259
|
} catch (error) {
|
|
348
260
|
const err = error;
|
|
349
261
|
console.error(err.message ?? String(err));
|
|
@@ -356,7 +268,7 @@ function registerPkgCommand(program) {
|
|
|
356
268
|
|
|
357
269
|
//#endregion
|
|
358
270
|
//#region package.json
|
|
359
|
-
var version = "1.0.
|
|
271
|
+
var version = "1.0.6";
|
|
360
272
|
|
|
361
273
|
//#endregion
|
|
362
274
|
//#region src/cli.ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tutinoko2048/mcutil",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/cli.mjs",
|
|
@@ -21,8 +21,7 @@
|
|
|
21
21
|
"typescript": "^5.9.3"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@
|
|
25
|
-
"@minecraft/server-ui": "2.1.0-beta.1.26.0-stable",
|
|
24
|
+
"@antfu/install-pkg": "^1.1.0",
|
|
26
25
|
"commander": "^14.0.3",
|
|
27
26
|
"inquirer": "^13.2.2",
|
|
28
27
|
"semver": "^7.7.4"
|
package/src/commands/pkg.ts
CHANGED
|
@@ -1,24 +1,42 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
2
|
import inquirer, { DistinctQuestion } from "inquirer";
|
|
3
3
|
import semver from "semver";
|
|
4
|
-
import { spawn } from "node:child_process";
|
|
5
4
|
import fs from "node:fs/promises";
|
|
6
5
|
import path from "node:path";
|
|
7
|
-
|
|
8
|
-
type PackageManager = "pnpm" | "npm" | "yarn" | "bun";
|
|
6
|
+
import { installPackage } from "@antfu/install-pkg";
|
|
9
7
|
|
|
10
8
|
type PackageJson = {
|
|
11
9
|
dependencies?: Record<string, string>;
|
|
12
10
|
devDependencies?: Record<string, string>;
|
|
13
11
|
};
|
|
14
12
|
|
|
13
|
+
interface PackageInfo {
|
|
14
|
+
name: string;
|
|
15
|
+
dev: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
15
18
|
const PACKAGES = [
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
{
|
|
20
|
+
name: "@minecraft/server",
|
|
21
|
+
dev: true,
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: "@minecraft/server-ui",
|
|
25
|
+
dev: true,
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: "@minecraft/server-net",
|
|
29
|
+
dev: true,
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: "@minecraft/server-admin",
|
|
33
|
+
dev: true,
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: "@minecraft/vanilla-data",
|
|
37
|
+
dev: false,
|
|
38
|
+
},
|
|
39
|
+
] as const satisfies PackageInfo[];
|
|
22
40
|
|
|
23
41
|
const CATEGORY_ORDER = [
|
|
24
42
|
"release",
|
|
@@ -93,115 +111,6 @@ function getCurrentVersion(pkgJson: PackageJson, name: string): string | null {
|
|
|
93
111
|
return pkgJson.dependencies?.[name] ?? pkgJson.devDependencies?.[name] ?? null;
|
|
94
112
|
}
|
|
95
113
|
|
|
96
|
-
async function detectPackageManagers(cwd: string): Promise<PackageManager[]> {
|
|
97
|
-
const checks: Array<{ name: PackageManager; files: string[] }> = [
|
|
98
|
-
{ name: "pnpm", files: ["pnpm-lock.yaml"] },
|
|
99
|
-
{ name: "npm", files: ["package-lock.json"] },
|
|
100
|
-
{ name: "yarn", files: ["yarn.lock"] },
|
|
101
|
-
{ name: "bun", files: ["bun.lock", "bun.lockb"] },
|
|
102
|
-
];
|
|
103
|
-
|
|
104
|
-
const matches: PackageManager[] = [];
|
|
105
|
-
await Promise.all(
|
|
106
|
-
checks.map(async (check) => {
|
|
107
|
-
try {
|
|
108
|
-
await Promise.all(check.files.map(file => fs.access(path.join(cwd, file))));
|
|
109
|
-
matches.push(check.name);
|
|
110
|
-
} catch {
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
})
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
return matches;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async function resolvePackageManager(cwd: string): Promise<PackageManager> {
|
|
120
|
-
const matches = await detectPackageManagers(cwd);
|
|
121
|
-
if (matches.length === 1) {
|
|
122
|
-
return matches[0];
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const questions: DistinctQuestion<{ manager: PackageManager }>[] = [
|
|
126
|
-
{
|
|
127
|
-
name: "manager",
|
|
128
|
-
type: "select",
|
|
129
|
-
message: "Select package manager:",
|
|
130
|
-
choices: [
|
|
131
|
-
{ name: "pnpm", value: "pnpm" },
|
|
132
|
-
{ name: "npm", value: "npm" },
|
|
133
|
-
{ name: "yarn", value: "yarn" },
|
|
134
|
-
{ name: "bun", value: "bun" },
|
|
135
|
-
],
|
|
136
|
-
},
|
|
137
|
-
];
|
|
138
|
-
|
|
139
|
-
const { manager } = await inquirer.prompt<{ manager: PackageManager }>(
|
|
140
|
-
questions
|
|
141
|
-
);
|
|
142
|
-
return manager;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function buildInstallCommand(
|
|
146
|
-
manager: PackageManager,
|
|
147
|
-
packages: string[]
|
|
148
|
-
): { command: string; args: string[] } {
|
|
149
|
-
const baseCommand =
|
|
150
|
-
process.platform === "win32" ? `${manager}.cmd` : manager;
|
|
151
|
-
switch (manager) {
|
|
152
|
-
case "pnpm":
|
|
153
|
-
return { command: baseCommand, args: ["add", ...packages] };
|
|
154
|
-
case "yarn":
|
|
155
|
-
return { command: baseCommand, args: ["add", ...packages] };
|
|
156
|
-
case "bun":
|
|
157
|
-
return { command: baseCommand, args: ["add", ...packages] };
|
|
158
|
-
case "npm":
|
|
159
|
-
default:
|
|
160
|
-
return { command: baseCommand, args: ["install", ...packages] };
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
async function runInstall(
|
|
165
|
-
manager: PackageManager,
|
|
166
|
-
packages: string[]
|
|
167
|
-
): Promise<void> {
|
|
168
|
-
if (packages.length === 0) {
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const { command, args } = buildInstallCommand(manager, packages);
|
|
173
|
-
|
|
174
|
-
const spawnCommand =
|
|
175
|
-
process.platform === "win32" ? "cmd.exe" : command;
|
|
176
|
-
const spawnArgs =
|
|
177
|
-
process.platform === "win32"
|
|
178
|
-
? ["/d", "/s", "/c", toWindowsCommand([command, ...args])]
|
|
179
|
-
: args;
|
|
180
|
-
|
|
181
|
-
await new Promise<void>((resolve, reject) => {
|
|
182
|
-
const child = spawn(spawnCommand, spawnArgs, { stdio: "inherit" });
|
|
183
|
-
child.on("close", (code) => {
|
|
184
|
-
if (code === 0) {
|
|
185
|
-
resolve();
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
reject(new Error(`${command} exited with code ${code ?? "unknown"}`));
|
|
189
|
-
});
|
|
190
|
-
child.on("error", reject);
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
function toWindowsCommand(parts: string[]): string {
|
|
195
|
-
return parts.map(quoteWindowsArg).join(" ");
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function quoteWindowsArg(value: string): string {
|
|
199
|
-
if (!/[\s"^&|<>]/.test(value)) {
|
|
200
|
-
return value;
|
|
201
|
-
}
|
|
202
|
-
return `"${value.replace(/"/g, '\\"')}"`;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
114
|
function groupVersionsByCategory(versions: string[]): Map<VersionCategory, string[]> {
|
|
206
115
|
const grouped = new Map<VersionCategory, string[]>();
|
|
207
116
|
for (const version of versions) {
|
|
@@ -216,14 +125,14 @@ function groupVersionsByCategory(versions: string[]): Map<VersionCategory, strin
|
|
|
216
125
|
return grouped;
|
|
217
126
|
}
|
|
218
127
|
|
|
219
|
-
async function promptForPackage(pkgJson: PackageJson): Promise<
|
|
220
|
-
const choices = PACKAGES.map((
|
|
221
|
-
const current = getCurrentVersion(pkgJson, name);
|
|
222
|
-
const label = current ? `${name} (current: ${current})` : `${name} (current: -)`;
|
|
223
|
-
return { name: label, value:
|
|
128
|
+
async function promptForPackage(pkgJson: PackageJson): Promise<PackageInfo | null> {
|
|
129
|
+
const choices = PACKAGES.map((pkg) => {
|
|
130
|
+
const current = getCurrentVersion(pkgJson, pkg.name);
|
|
131
|
+
const label = current ? `${pkg.name} (current: ${current})` : `${pkg.name} (current: -)`;
|
|
132
|
+
return { name: label, value: pkg };
|
|
224
133
|
});
|
|
225
134
|
|
|
226
|
-
const questions: DistinctQuestion<{ target:
|
|
135
|
+
const questions: DistinctQuestion<{ target: PackageInfo }>[] = [
|
|
227
136
|
{
|
|
228
137
|
name: "target",
|
|
229
138
|
type: "select",
|
|
@@ -232,7 +141,7 @@ async function promptForPackage(pkgJson: PackageJson): Promise<string | null> {
|
|
|
232
141
|
},
|
|
233
142
|
];
|
|
234
143
|
|
|
235
|
-
const { target } = await inquirer.prompt<{ target:
|
|
144
|
+
const { target } = await inquirer.prompt<{ target: PackageInfo }>(questions);
|
|
236
145
|
return target ?? null;
|
|
237
146
|
}
|
|
238
147
|
|
|
@@ -292,7 +201,6 @@ export async function runPkgFlow(): Promise<void> {
|
|
|
292
201
|
const cwd = process.cwd();
|
|
293
202
|
try {
|
|
294
203
|
const pkgJson = await readPackageJson(cwd);
|
|
295
|
-
const manager = await resolvePackageManager(cwd);
|
|
296
204
|
|
|
297
205
|
const target = await promptForPackage(pkgJson);
|
|
298
206
|
if (!target) {
|
|
@@ -300,15 +208,16 @@ export async function runPkgFlow(): Promise<void> {
|
|
|
300
208
|
return;
|
|
301
209
|
}
|
|
302
210
|
|
|
303
|
-
const currentVersion = getCurrentVersion(pkgJson, target);
|
|
211
|
+
const currentVersion = getCurrentVersion(pkgJson, target.name);
|
|
304
212
|
const currentCategory = inferCategoryFromCurrentVersion(currentVersion);
|
|
305
|
-
const versions = await fetchPackageVersions(target);
|
|
213
|
+
const versions = await fetchPackageVersions(target.name);
|
|
306
214
|
const grouped = groupVersionsByCategory(versions);
|
|
307
|
-
const category = await promptForCategory(target, grouped, currentCategory);
|
|
215
|
+
const category = await promptForCategory(target.name, grouped, currentCategory);
|
|
308
216
|
const list = grouped.get(category) ?? [];
|
|
309
|
-
const version = await promptForVersion(target, list);
|
|
217
|
+
const version = await promptForVersion(target.name, list);
|
|
218
|
+
|
|
219
|
+
await installPackage(target.name, { silent: false, cwd, dev: target.dev });
|
|
310
220
|
|
|
311
|
-
await runInstall(manager, [`${target}@${version}`]);
|
|
312
221
|
} catch (error) {
|
|
313
222
|
const err = error as NodeJS.ErrnoException;
|
|
314
223
|
console.error(err.message ?? String(err));
|