maoda-commander-tt 0.0.26 → 0.0.27
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/main.js +261 -18
- package/dist/main.js.map +1 -1
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -2227,9 +2227,9 @@ var ShortcutBaseCommand = class extends BaseCommand {
|
|
|
2227
2227
|
stdio: "inherit",
|
|
2228
2228
|
shell: true
|
|
2229
2229
|
});
|
|
2230
|
-
return new Promise((
|
|
2230
|
+
return new Promise((resolve10) => {
|
|
2231
2231
|
child.on("error", (error) => {
|
|
2232
|
-
|
|
2232
|
+
resolve10(
|
|
2233
2233
|
this._makeError(
|
|
2234
2234
|
1,
|
|
2235
2235
|
`\u6267\u884C ${cmdLine} \u5931\u8D25: ${error.message}`
|
|
@@ -2238,11 +2238,11 @@ var ShortcutBaseCommand = class extends BaseCommand {
|
|
|
2238
2238
|
});
|
|
2239
2239
|
child.on("close", (code, signal) => {
|
|
2240
2240
|
if (code === 0) {
|
|
2241
|
-
|
|
2241
|
+
resolve10(this._makeOk());
|
|
2242
2242
|
return;
|
|
2243
2243
|
}
|
|
2244
2244
|
const signalText = signal ? `, signal: ${signal}` : "";
|
|
2245
|
-
|
|
2245
|
+
resolve10(
|
|
2246
2246
|
this._makeError(
|
|
2247
2247
|
code ?? 1,
|
|
2248
2248
|
`\u6267\u884C ${cmdLine} \u5931\u8D25${signalText}`
|
|
@@ -2337,9 +2337,9 @@ var AiBaseCommand = class extends BaseCommand {
|
|
|
2337
2337
|
cwd: process.cwd(),
|
|
2338
2338
|
stdio: "inherit"
|
|
2339
2339
|
});
|
|
2340
|
-
return new Promise((
|
|
2340
|
+
return new Promise((resolve10) => {
|
|
2341
2341
|
child.on("error", (error) => {
|
|
2342
|
-
|
|
2342
|
+
resolve10(
|
|
2343
2343
|
this._makeError(
|
|
2344
2344
|
1,
|
|
2345
2345
|
`\u6267\u884C ${command} ${args.join(" ")} \u5931\u8D25: ${error.message}`
|
|
@@ -2348,11 +2348,11 @@ var AiBaseCommand = class extends BaseCommand {
|
|
|
2348
2348
|
});
|
|
2349
2349
|
child.on("close", (code, signal) => {
|
|
2350
2350
|
if (code === 0) {
|
|
2351
|
-
|
|
2351
|
+
resolve10(this._makeOk());
|
|
2352
2352
|
return;
|
|
2353
2353
|
}
|
|
2354
2354
|
const signalText = signal ? `, signal: ${signal}` : "";
|
|
2355
|
-
|
|
2355
|
+
resolve10(
|
|
2356
2356
|
this._makeError(
|
|
2357
2357
|
code ?? 1,
|
|
2358
2358
|
`\u6267\u884C ${command} ${args.join(" ")} \u5931\u8D25${signalText}`
|
|
@@ -2747,9 +2747,9 @@ var AiImageCommandV2 = class extends AiBaseCommand {
|
|
|
2747
2747
|
cwd: process.cwd(),
|
|
2748
2748
|
stdio: "ignore"
|
|
2749
2749
|
});
|
|
2750
|
-
return new Promise((
|
|
2750
|
+
return new Promise((resolve10) => {
|
|
2751
2751
|
child.on("error", (error) => {
|
|
2752
|
-
|
|
2752
|
+
resolve10(
|
|
2753
2753
|
this._makeError(
|
|
2754
2754
|
1,
|
|
2755
2755
|
`\u6267\u884C codex ${args.join(" ")} \u5931\u8D25: ${error.message}`
|
|
@@ -2758,11 +2758,11 @@ var AiImageCommandV2 = class extends AiBaseCommand {
|
|
|
2758
2758
|
});
|
|
2759
2759
|
child.on("close", (code, signal) => {
|
|
2760
2760
|
if (code === 0) {
|
|
2761
|
-
|
|
2761
|
+
resolve10(this._makeOk());
|
|
2762
2762
|
return;
|
|
2763
2763
|
}
|
|
2764
2764
|
const signalText = signal ? `, signal: ${signal}` : "";
|
|
2765
|
-
|
|
2765
|
+
resolve10(
|
|
2766
2766
|
this._makeError(
|
|
2767
2767
|
code ?? 1,
|
|
2768
2768
|
`\u6267\u884C codex ${args.join(" ")} \u5931\u8D25${signalText}`
|
|
@@ -3542,7 +3542,7 @@ function extractErrorText(payload) {
|
|
|
3542
3542
|
return void 0;
|
|
3543
3543
|
}
|
|
3544
3544
|
function delay(ms) {
|
|
3545
|
-
return new Promise((
|
|
3545
|
+
return new Promise((resolve10) => setTimeout(resolve10, ms));
|
|
3546
3546
|
}
|
|
3547
3547
|
async function appendPollingUrl(pollingUrl, generationId) {
|
|
3548
3548
|
const locationResult = await resolveConfigLocation();
|
|
@@ -3743,7 +3743,7 @@ var AiQueryTaskCommand = class extends AiBaseCommand {
|
|
|
3743
3743
|
PPE_ENV,
|
|
3744
3744
|
"--json"
|
|
3745
3745
|
];
|
|
3746
|
-
return new Promise((
|
|
3746
|
+
return new Promise((resolve10) => {
|
|
3747
3747
|
const child = spawn4(BIN, args, { cwd: process.cwd() });
|
|
3748
3748
|
let stdout = "";
|
|
3749
3749
|
let stderr = "";
|
|
@@ -3754,7 +3754,7 @@ var AiQueryTaskCommand = class extends AiBaseCommand {
|
|
|
3754
3754
|
stderr += chunk.toString();
|
|
3755
3755
|
});
|
|
3756
3756
|
child.on("error", (error) => {
|
|
3757
|
-
|
|
3757
|
+
resolve10(
|
|
3758
3758
|
makeError(
|
|
3759
3759
|
1,
|
|
3760
3760
|
`\u6267\u884C ${BIN} ${args.join(" ")} \u5931\u8D25: ${error.message}`
|
|
@@ -3763,13 +3763,13 @@ var AiQueryTaskCommand = class extends AiBaseCommand {
|
|
|
3763
3763
|
});
|
|
3764
3764
|
child.on("close", (code, signal) => {
|
|
3765
3765
|
if (code === 0) {
|
|
3766
|
-
|
|
3766
|
+
resolve10(makeOkWith({ stdout, stderr }));
|
|
3767
3767
|
return;
|
|
3768
3768
|
}
|
|
3769
3769
|
const signalText = signal ? `, signal: ${signal}` : "";
|
|
3770
3770
|
const stderrText = stderr.trim() ? `
|
|
3771
3771
|
${stderr.trim()}` : "";
|
|
3772
|
-
|
|
3772
|
+
resolve10(
|
|
3773
3773
|
makeError(
|
|
3774
3774
|
code ?? 1,
|
|
3775
3775
|
`\u6267\u884C ${BIN} ${args.join(" ")} \u5931\u8D25${signalText}${stderrText}`
|
|
@@ -3809,7 +3809,7 @@ function tryParseJson(text) {
|
|
|
3809
3809
|
}
|
|
3810
3810
|
}
|
|
3811
3811
|
function delay2(ms) {
|
|
3812
|
-
return new Promise((
|
|
3812
|
+
return new Promise((resolve10) => setTimeout(resolve10, ms));
|
|
3813
3813
|
}
|
|
3814
3814
|
|
|
3815
3815
|
// src/commands/ai/ai-command.ts
|
|
@@ -4266,6 +4266,248 @@ function asRecord2(value) {
|
|
|
4266
4266
|
return value;
|
|
4267
4267
|
}
|
|
4268
4268
|
|
|
4269
|
+
// src/commands/tool/tool-random-files-command.ts
|
|
4270
|
+
import { readdir as readdir3, stat as stat5 } from "fs/promises";
|
|
4271
|
+
import { homedir as homedir9 } from "os";
|
|
4272
|
+
import { extname as extname4, join as join13, resolve as resolve9 } from "path";
|
|
4273
|
+
var DEFAULT_COUNT = 1;
|
|
4274
|
+
var DEFAULT_SUFFIXES = ["png", "jpeg", "jpg"];
|
|
4275
|
+
var DEFAULT_DEPTH = 3;
|
|
4276
|
+
var ToolRandomFilesCommand = class extends BaseCommand {
|
|
4277
|
+
_meta() {
|
|
4278
|
+
return {
|
|
4279
|
+
name: "random-files",
|
|
4280
|
+
description: "\u4ECE\u6587\u4EF6\u5939\u968F\u673A\u9009\u62E9\u6587\u4EF6",
|
|
4281
|
+
aliases: ["randf"]
|
|
4282
|
+
};
|
|
4283
|
+
}
|
|
4284
|
+
_configureArguments(cmd) {
|
|
4285
|
+
cmd.argument("<folder...>", "\u76EE\u6807\u6587\u4EF6\u5939");
|
|
4286
|
+
}
|
|
4287
|
+
_configureOptions(cmd) {
|
|
4288
|
+
cmd.option("-c, --count <count>", "\u968F\u673A\u6587\u4EF6\u6570\u91CF", String(DEFAULT_COUNT));
|
|
4289
|
+
cmd.option(
|
|
4290
|
+
"-s, --suffix <suffixes>",
|
|
4291
|
+
"\u6587\u4EF6\u540E\u7F00\uFF0C\u591A\u4E2A\u540E\u7F00\u7528\u9017\u53F7\u5206\u9694",
|
|
4292
|
+
DEFAULT_SUFFIXES.join(",")
|
|
4293
|
+
);
|
|
4294
|
+
cmd.option(
|
|
4295
|
+
"-d, --depth <depth>",
|
|
4296
|
+
"\u9012\u5F52\u6587\u4EF6\u5C42\u7EA7\uFF0C\u76EE\u6807\u6587\u4EF6\u5939\u4E3A\u7B2C 1 \u5C42",
|
|
4297
|
+
String(DEFAULT_DEPTH)
|
|
4298
|
+
);
|
|
4299
|
+
cmd.addHelpText(
|
|
4300
|
+
"after",
|
|
4301
|
+
[
|
|
4302
|
+
"",
|
|
4303
|
+
"\u8F93\u5165\u6848\u4F8B:",
|
|
4304
|
+
" tt tool random-files ~/Pictures -c 3",
|
|
4305
|
+
" tt tool random-files ./assets --suffix webp,gif --depth 2"
|
|
4306
|
+
].join("\n")
|
|
4307
|
+
);
|
|
4308
|
+
}
|
|
4309
|
+
async _execute(ctx) {
|
|
4310
|
+
const folderInputResult = parseFolderInput(ctx.args[0]);
|
|
4311
|
+
if (!folderInputResult.ok) {
|
|
4312
|
+
return this._makeError(1, folderInputResult.msg);
|
|
4313
|
+
}
|
|
4314
|
+
const countResult = parsePositiveInteger(
|
|
4315
|
+
ctx.options.count,
|
|
4316
|
+
DEFAULT_COUNT,
|
|
4317
|
+
"\u968F\u673A\u6587\u4EF6\u6570\u91CF"
|
|
4318
|
+
);
|
|
4319
|
+
if (!countResult.ok) {
|
|
4320
|
+
return this._makeError(1, countResult.msg);
|
|
4321
|
+
}
|
|
4322
|
+
const suffixResult = parseSuffixes(ctx.options.suffix);
|
|
4323
|
+
if (!suffixResult.ok) {
|
|
4324
|
+
return this._makeError(1, suffixResult.msg);
|
|
4325
|
+
}
|
|
4326
|
+
const depthResult = parsePositiveInteger(
|
|
4327
|
+
ctx.options.depth,
|
|
4328
|
+
DEFAULT_DEPTH,
|
|
4329
|
+
"\u9012\u5F52\u6587\u4EF6\u5C42\u7EA7"
|
|
4330
|
+
);
|
|
4331
|
+
if (!depthResult.ok) {
|
|
4332
|
+
return this._makeError(1, depthResult.msg);
|
|
4333
|
+
}
|
|
4334
|
+
const folderResult = await resolveFolder(folderInputResult.value);
|
|
4335
|
+
if (!folderResult.ok) {
|
|
4336
|
+
return this._makeError(1, folderResult.msg);
|
|
4337
|
+
}
|
|
4338
|
+
const candidates = await collectMatchingFiles({
|
|
4339
|
+
folder: folderResult.value,
|
|
4340
|
+
suffixes: suffixResult.value,
|
|
4341
|
+
maxDepth: depthResult.value,
|
|
4342
|
+
currentDepth: 1
|
|
4343
|
+
});
|
|
4344
|
+
if (candidates.length === 0) {
|
|
4345
|
+
return this._makeError(
|
|
4346
|
+
1,
|
|
4347
|
+
`\u672A\u627E\u5230\u5339\u914D\u540E\u7F00\u7684\u6587\u4EF6: ${Array.from(suffixResult.value).join(",")}`
|
|
4348
|
+
);
|
|
4349
|
+
}
|
|
4350
|
+
if (candidates.length < countResult.value) {
|
|
4351
|
+
return this._makeError(
|
|
4352
|
+
1,
|
|
4353
|
+
`\u5339\u914D\u6587\u4EF6\u6570\u91CF\u4E0D\u8DB3: \u9700\u8981 ${countResult.value} \u4E2A\uFF0C\u627E\u5230 ${candidates.length} \u4E2A`
|
|
4354
|
+
);
|
|
4355
|
+
}
|
|
4356
|
+
this._outputJson({
|
|
4357
|
+
files: pickRandomFiles(candidates, countResult.value)
|
|
4358
|
+
});
|
|
4359
|
+
return this._makeOk();
|
|
4360
|
+
}
|
|
4361
|
+
_outputJson(data) {
|
|
4362
|
+
this._output(JSON.stringify(data, null, 2));
|
|
4363
|
+
}
|
|
4364
|
+
};
|
|
4365
|
+
function parseFolderInput(value) {
|
|
4366
|
+
const folder = stringifyFolderArg(value);
|
|
4367
|
+
if (!folder) {
|
|
4368
|
+
return { ok: false, msg: "\u76EE\u6807\u6587\u4EF6\u5939\u4E0D\u80FD\u4E3A\u7A7A" };
|
|
4369
|
+
}
|
|
4370
|
+
return {
|
|
4371
|
+
ok: true,
|
|
4372
|
+
value: {
|
|
4373
|
+
raw: folder,
|
|
4374
|
+
candidates: createFolderCandidates(folder)
|
|
4375
|
+
}
|
|
4376
|
+
};
|
|
4377
|
+
}
|
|
4378
|
+
function stringifyFolderArg(value) {
|
|
4379
|
+
if (Array.isArray(value)) {
|
|
4380
|
+
return value.map((item) => String(item)).join(" ").trim();
|
|
4381
|
+
}
|
|
4382
|
+
return String(value ?? "").trim();
|
|
4383
|
+
}
|
|
4384
|
+
function createFolderCandidates(value) {
|
|
4385
|
+
const candidates = [
|
|
4386
|
+
value,
|
|
4387
|
+
trimWrappingQuotes(value),
|
|
4388
|
+
unescapeShellPath(trimWrappingQuotes(value))
|
|
4389
|
+
];
|
|
4390
|
+
return uniqueStrings(candidates.map((candidate) => expandHomeDir2(candidate)));
|
|
4391
|
+
}
|
|
4392
|
+
function trimWrappingQuotes(value) {
|
|
4393
|
+
if (value.length < 2) {
|
|
4394
|
+
return value;
|
|
4395
|
+
}
|
|
4396
|
+
const firstChar = value[0];
|
|
4397
|
+
const lastChar = value[value.length - 1];
|
|
4398
|
+
if (firstChar === '"' && lastChar === '"' || firstChar === "'" && lastChar === "'") {
|
|
4399
|
+
return value.slice(1, -1);
|
|
4400
|
+
}
|
|
4401
|
+
return value;
|
|
4402
|
+
}
|
|
4403
|
+
function unescapeShellPath(value) {
|
|
4404
|
+
return value.replace(
|
|
4405
|
+
/\\([ \t\n\r"'`$&;()<>|*?[\]{}!#~\\])/g,
|
|
4406
|
+
"$1"
|
|
4407
|
+
);
|
|
4408
|
+
}
|
|
4409
|
+
function uniqueStrings(values) {
|
|
4410
|
+
return [...new Set(values)];
|
|
4411
|
+
}
|
|
4412
|
+
async function resolveFolder(folderInput) {
|
|
4413
|
+
let firstExistingPath;
|
|
4414
|
+
for (const candidate of folderInput.candidates) {
|
|
4415
|
+
const folderStat = await safeStat2(candidate);
|
|
4416
|
+
if (!folderStat) {
|
|
4417
|
+
continue;
|
|
4418
|
+
}
|
|
4419
|
+
if (folderStat.isDirectory()) {
|
|
4420
|
+
return { ok: true, value: candidate };
|
|
4421
|
+
}
|
|
4422
|
+
firstExistingPath ??= candidate;
|
|
4423
|
+
}
|
|
4424
|
+
if (firstExistingPath) {
|
|
4425
|
+
return { ok: false, msg: `\u76EE\u6807\u8DEF\u5F84\u4E0D\u662F\u6587\u4EF6\u5939: ${firstExistingPath}` };
|
|
4426
|
+
}
|
|
4427
|
+
return { ok: false, msg: `\u76EE\u6807\u6587\u4EF6\u5939\u4E0D\u5B58\u5728: ${folderInput.raw}` };
|
|
4428
|
+
}
|
|
4429
|
+
function parsePositiveInteger(value, defaultValue, label) {
|
|
4430
|
+
const rawValue = String(value ?? defaultValue).trim();
|
|
4431
|
+
const parsedValue = Number(rawValue);
|
|
4432
|
+
if (!Number.isInteger(parsedValue) || parsedValue <= 0) {
|
|
4433
|
+
return { ok: false, msg: `${label}\u5FC5\u987B\u662F\u6B63\u6574\u6570` };
|
|
4434
|
+
}
|
|
4435
|
+
return { ok: true, value: parsedValue };
|
|
4436
|
+
}
|
|
4437
|
+
function parseSuffixes(value) {
|
|
4438
|
+
const suffixes = String(value ?? DEFAULT_SUFFIXES.join(",")).split(/[,,\s]+/).map((suffix) => normalizeSuffix(suffix)).filter((suffix) => suffix.length > 0);
|
|
4439
|
+
if (suffixes.length === 0) {
|
|
4440
|
+
return { ok: false, msg: "\u6587\u4EF6\u540E\u7F00\u4E0D\u80FD\u4E3A\u7A7A" };
|
|
4441
|
+
}
|
|
4442
|
+
return { ok: true, value: new Set(suffixes) };
|
|
4443
|
+
}
|
|
4444
|
+
function normalizeSuffix(value) {
|
|
4445
|
+
return value.trim().replace(/^\.+/, "").toLowerCase();
|
|
4446
|
+
}
|
|
4447
|
+
async function collectMatchingFiles(options) {
|
|
4448
|
+
const entries = await safeReadDir(options.folder);
|
|
4449
|
+
const files = [];
|
|
4450
|
+
for (const entry of entries) {
|
|
4451
|
+
const entryPath = join13(options.folder, entry.name);
|
|
4452
|
+
if (entry.isFile()) {
|
|
4453
|
+
if (hasAllowedSuffix(entry.name, options.suffixes)) {
|
|
4454
|
+
files.push(entryPath);
|
|
4455
|
+
}
|
|
4456
|
+
continue;
|
|
4457
|
+
}
|
|
4458
|
+
if (entry.isDirectory() && options.currentDepth < options.maxDepth) {
|
|
4459
|
+
files.push(
|
|
4460
|
+
...await collectMatchingFiles({
|
|
4461
|
+
folder: entryPath,
|
|
4462
|
+
suffixes: options.suffixes,
|
|
4463
|
+
maxDepth: options.maxDepth,
|
|
4464
|
+
currentDepth: options.currentDepth + 1
|
|
4465
|
+
})
|
|
4466
|
+
);
|
|
4467
|
+
}
|
|
4468
|
+
}
|
|
4469
|
+
return files;
|
|
4470
|
+
}
|
|
4471
|
+
async function safeReadDir(folder) {
|
|
4472
|
+
try {
|
|
4473
|
+
const entries = await readdir3(folder, { withFileTypes: true });
|
|
4474
|
+
return entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
4475
|
+
} catch {
|
|
4476
|
+
return [];
|
|
4477
|
+
}
|
|
4478
|
+
}
|
|
4479
|
+
function hasAllowedSuffix(fileName, suffixes) {
|
|
4480
|
+
const suffix = normalizeSuffix(extname4(fileName));
|
|
4481
|
+
return suffixes.has(suffix);
|
|
4482
|
+
}
|
|
4483
|
+
function pickRandomFiles(files, count) {
|
|
4484
|
+
const shuffled = [...files];
|
|
4485
|
+
for (let index = shuffled.length - 1; index > 0; index -= 1) {
|
|
4486
|
+
const randomIndex = Math.floor(Math.random() * (index + 1));
|
|
4487
|
+
[shuffled[index], shuffled[randomIndex]] = [
|
|
4488
|
+
shuffled[randomIndex],
|
|
4489
|
+
shuffled[index]
|
|
4490
|
+
];
|
|
4491
|
+
}
|
|
4492
|
+
return shuffled.slice(0, count);
|
|
4493
|
+
}
|
|
4494
|
+
async function safeStat2(path10) {
|
|
4495
|
+
try {
|
|
4496
|
+
return await stat5(path10);
|
|
4497
|
+
} catch {
|
|
4498
|
+
return void 0;
|
|
4499
|
+
}
|
|
4500
|
+
}
|
|
4501
|
+
function expandHomeDir2(path10) {
|
|
4502
|
+
if (path10 === "~") {
|
|
4503
|
+
return homedir9();
|
|
4504
|
+
}
|
|
4505
|
+
if (path10.startsWith("~/")) {
|
|
4506
|
+
return join13(homedir9(), path10.slice(2));
|
|
4507
|
+
}
|
|
4508
|
+
return resolve9(path10);
|
|
4509
|
+
}
|
|
4510
|
+
|
|
4269
4511
|
// src/commands/tool/tool-command.ts
|
|
4270
4512
|
var ToolCommand = class extends BaseSubcommandHost {
|
|
4271
4513
|
_meta() {
|
|
@@ -4277,6 +4519,7 @@ var ToolCommand = class extends BaseSubcommandHost {
|
|
|
4277
4519
|
_registerSubcommands() {
|
|
4278
4520
|
this._addSubcommand(new ToolCanvasCommand());
|
|
4279
4521
|
this._addSubcommand(new ToolDrawCommand());
|
|
4522
|
+
this._addSubcommand(new ToolRandomFilesCommand());
|
|
4280
4523
|
}
|
|
4281
4524
|
};
|
|
4282
4525
|
|