refactor: impl
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { createStore } from "solid-js/store";
|
||||
import type { MdCommanderCommand, MdCommanderCommandMap } from "../types";
|
||||
import { setupHelpCommand, clearCommand, rollCommand, trackCommand, untrackCommand, listTrackCommand } from "../commands";
|
||||
import {resolvePath} from "../../utils/path";
|
||||
import {loadCSV} from "../../utils/csv-loader";
|
||||
|
||||
const defaultCommands: MdCommanderCommandMap = {
|
||||
help: setupHelpCommand({}),
|
||||
@@ -62,55 +64,49 @@ export function getCommand(name: string): MdCommanderCommand | undefined {
|
||||
* 从 CSV 文件加载命令模板并更新命令定义
|
||||
*/
|
||||
export async function loadCommandTemplatesFromCSV(
|
||||
csvPaths: string | string[],
|
||||
resolvePath: (base: string, path: string) => string,
|
||||
path: string,
|
||||
articlePath: string
|
||||
): Promise<void> {
|
||||
const paths = Array.isArray(csvPaths) ? csvPaths : [csvPaths];
|
||||
try {
|
||||
const csv = await loadCSV<CommandTemplateRow>(resolvePath(articlePath, path));
|
||||
|
||||
for (const path of paths) {
|
||||
try {
|
||||
const { loadCSV } = await import("../../utils/csv-loader");
|
||||
const csv = await loadCSV<CommandTemplateRow>(resolvePath(articlePath, path));
|
||||
// 按命令分组模板
|
||||
const templatesByCommand = new Map<string, CommandTemplateRow[]>();
|
||||
for (const row of csv) {
|
||||
if (!row.command || !row.label || !row.insertedText) continue;
|
||||
|
||||
// 按命令分组模板
|
||||
const templatesByCommand = new Map<string, CommandTemplateRow[]>();
|
||||
for (const row of csv) {
|
||||
if (!row.command || !row.label || !row.insertedText) continue;
|
||||
|
||||
if (!templatesByCommand.has(row.command)) {
|
||||
templatesByCommand.set(row.command, []);
|
||||
}
|
||||
templatesByCommand.get(row.command)!.push(row);
|
||||
if (!templatesByCommand.has(row.command)) {
|
||||
templatesByCommand.set(row.command, []);
|
||||
}
|
||||
|
||||
// 为每个命令添加模板
|
||||
setCommandsStore("commands", (prev) => {
|
||||
const updated = { ...prev };
|
||||
for (const [commandName, rows] of templatesByCommand.entries()) {
|
||||
const cmd = updated[commandName];
|
||||
if (!cmd || !cmd.parameters) continue;
|
||||
|
||||
const templates = rows.map((row) => ({
|
||||
label: row.label,
|
||||
description: row.description || "",
|
||||
insertText: row.insertedText,
|
||||
}));
|
||||
|
||||
// 为每个参数添加模板
|
||||
updated[commandName] = {
|
||||
...cmd,
|
||||
parameters: cmd.parameters.map((param) => ({
|
||||
...param,
|
||||
templates: param.templates ? [...param.templates, ...templates] : templates,
|
||||
})),
|
||||
};
|
||||
}
|
||||
return updated;
|
||||
});
|
||||
} catch (error) {
|
||||
console.warn(`Error loading command templates from ${path}:`, error);
|
||||
templatesByCommand.get(row.command)!.push(row);
|
||||
}
|
||||
|
||||
// 为每个命令添加模板
|
||||
setCommandsStore("commands", (prev) => {
|
||||
const updated = { ...prev };
|
||||
for (const [commandName, rows] of templatesByCommand.entries()) {
|
||||
const cmd = updated[commandName];
|
||||
if (!cmd || !cmd.parameters) continue;
|
||||
|
||||
const templates = rows.map((row) => ({
|
||||
label: row.label,
|
||||
description: row.description || "",
|
||||
insertText: row.insertedText,
|
||||
}));
|
||||
|
||||
// 为每个参数添加模板
|
||||
updated[commandName] = {
|
||||
...cmd,
|
||||
parameters: cmd.parameters.map((param) => ({
|
||||
...param,
|
||||
templates: param.templates ? [...param.templates, ...templates] : templates,
|
||||
})),
|
||||
};
|
||||
}
|
||||
return updated;
|
||||
});
|
||||
} catch (error) {
|
||||
console.warn(`Error loading command templates from ${path}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user