@minesa-org/mini-interaction 0.0.2 → 0.0.3
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.
|
@@ -169,7 +169,7 @@ export declare class MiniInteraction {
|
|
|
169
169
|
*/
|
|
170
170
|
private resolveComponentsDirectory;
|
|
171
171
|
/**
|
|
172
|
-
* Resolves a directory relative to the
|
|
172
|
+
* Resolves a directory relative to the project "src" or "dist" folders with optional overrides.
|
|
173
173
|
*/
|
|
174
174
|
private resolveDirectory;
|
|
175
175
|
/**
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
1
2
|
import { readdir, stat } from "node:fs/promises";
|
|
2
3
|
import path from "node:path";
|
|
3
|
-
import {
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
4
5
|
import { InteractionResponseType, InteractionType, } from "discord-api-types/v10";
|
|
5
6
|
import { verifyKey } from "discord-interactions";
|
|
6
7
|
import { DISCORD_BASE_URL } from "../utils/constants.js";
|
|
@@ -467,27 +468,70 @@ export class MiniInteraction {
|
|
|
467
468
|
* Resolves the absolute commands directory path from configuration.
|
|
468
469
|
*/
|
|
469
470
|
resolveCommandsDirectory(commandsDirectory) {
|
|
470
|
-
return this.resolveDirectory("
|
|
471
|
+
return this.resolveDirectory("commands", commandsDirectory);
|
|
471
472
|
}
|
|
472
473
|
/**
|
|
473
474
|
* Resolves the absolute components directory path from configuration.
|
|
474
475
|
*/
|
|
475
476
|
resolveComponentsDirectory(componentsDirectory) {
|
|
476
|
-
return this.resolveDirectory("
|
|
477
|
+
return this.resolveDirectory("components", componentsDirectory);
|
|
477
478
|
}
|
|
478
479
|
/**
|
|
479
|
-
* Resolves a directory relative to the
|
|
480
|
+
* Resolves a directory relative to the project "src" or "dist" folders with optional overrides.
|
|
480
481
|
*/
|
|
481
|
-
resolveDirectory(
|
|
482
|
-
const
|
|
483
|
-
const
|
|
484
|
-
const
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
482
|
+
resolveDirectory(defaultFolder, overrideDirectory) {
|
|
483
|
+
const projectRoot = process.cwd();
|
|
484
|
+
const allowedRoots = ["src", "dist"].map((folder) => path.resolve(projectRoot, folder));
|
|
485
|
+
const candidates = [];
|
|
486
|
+
const isWithin = (parent, child) => {
|
|
487
|
+
const relative = path.relative(parent, child);
|
|
488
|
+
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
|
|
489
|
+
};
|
|
490
|
+
const pushCandidate = (candidate) => {
|
|
491
|
+
if (!candidates.includes(candidate)) {
|
|
492
|
+
candidates.push(candidate);
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
const ensureWithinAllowedRoots = (absolutePath) => {
|
|
496
|
+
if (!allowedRoots.some((root) => isWithin(root, absolutePath))) {
|
|
497
|
+
throw new Error(`[MiniInteraction] Directory overrides must be located within "${path.join(projectRoot, "src")}" or "${path.join(projectRoot, "dist")}". Received: ${absolutePath}`);
|
|
498
|
+
}
|
|
499
|
+
pushCandidate(absolutePath);
|
|
500
|
+
};
|
|
501
|
+
const addOverrideCandidates = (overridePath) => {
|
|
502
|
+
const trimmed = overridePath.trim();
|
|
503
|
+
if (!trimmed) {
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
if (path.isAbsolute(trimmed)) {
|
|
507
|
+
ensureWithinAllowedRoots(trimmed);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
const normalised = trimmed.replace(/^[./\\]+/, "");
|
|
511
|
+
if (!normalised) {
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
if (normalised.startsWith("src") || normalised.startsWith("dist")) {
|
|
515
|
+
const absolutePath = path.resolve(projectRoot, normalised);
|
|
516
|
+
ensureWithinAllowedRoots(absolutePath);
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
for (const root of allowedRoots) {
|
|
520
|
+
ensureWithinAllowedRoots(path.resolve(root, normalised));
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
if (overrideDirectory) {
|
|
524
|
+
addOverrideCandidates(overrideDirectory);
|
|
525
|
+
}
|
|
526
|
+
for (const root of allowedRoots) {
|
|
527
|
+
pushCandidate(path.resolve(root, defaultFolder));
|
|
528
|
+
}
|
|
529
|
+
for (const candidate of candidates) {
|
|
530
|
+
if (existsSync(candidate)) {
|
|
531
|
+
return candidate;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return candidates[0];
|
|
491
535
|
}
|
|
492
536
|
/**
|
|
493
537
|
* Handles execution of a message component interaction.
|
package/package.json
CHANGED