@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
77 lines (76 loc) • 3.2 kB
JavaScript
;
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.openFileCommand = exports.OpenFileCommand = void 0;
const IToolCommand_1 = require("../IToolCommand");
const AutocompleteProviders_1 = require("../AutocompleteProviders");
class OpenFileCommand extends IToolCommand_1.ToolCommandBase {
metadata = {
name: "find",
description: "Find a project item by name or path and print its location",
aliases: ["locate", "f"],
category: "Project",
requiresProject: true,
arguments: [
{
name: "filename",
description: "Name or path fragment of the item to locate",
type: "string",
required: true,
autocompleteProvider: AutocompleteProviders_1.projectItemNameProvider,
},
],
isWriteCommand: false,
examples: ["/find my_entity", "/locate behavior_pack/entities/sheep.json"],
};
async execute(context, args, _flags) {
if (!context.project) {
context.output.error("No active project — open a project before running /find.");
return this.error("NO_PROJECT", "No active project");
}
const validationError = this.validateRequiredArgs(args);
if (validationError)
return validationError;
const query = args[0];
const lower = query.toLowerCase();
const items = context.project.items || [];
const matches = items.filter((item) => {
const name = (item.name || "").toLowerCase();
const path = (item.projectPath || "").toLowerCase();
return name === lower || name.includes(lower) || path.includes(lower);
});
if (matches.length === 0) {
const message = `No project item matches '${query}'.`;
context.output.warn(message);
return this.error("ITEM_NOT_FOUND", message);
}
const exact = matches.find((item) => (item.name || "").toLowerCase() === lower);
const primary = exact || matches[0];
context.output.success(`Found: ${primary.name}`);
if (primary.projectPath) {
context.output.info(`Path: ${primary.projectPath}`);
}
if (matches.length > 1) {
context.output.info(`(${matches.length - 1} other match${matches.length - 1 === 1 ? "" : "es"})`);
const limit = Math.min(matches.length, 6);
for (let i = 0; i < limit; i++) {
const m = matches[i];
if (m === primary)
continue;
context.output.info(` • ${m.name} — ${m.projectPath || "(no path)"}`);
}
}
return this.success(`Located ${primary.name}`, {
query,
match: {
name: primary.name,
projectPath: primary.projectPath,
itemType: primary.itemType,
},
totalMatches: matches.length,
});
}
}
exports.OpenFileCommand = OpenFileCommand;
exports.openFileCommand = new OpenFileCommand();