|
| 1 | +import { Controller } from ".." |
| 2 | +import { RuleFileRequest, RuleFile } from "@shared/proto/file" |
| 3 | +import { FileMethodHandler } from "./index" |
| 4 | +import { |
| 5 | + createRuleFile as createRuleFileImpl, |
| 6 | + refreshClineRulesToggles, |
| 7 | +} from "@core/context/instructions/user-instructions/cline-rules" |
| 8 | +import * as vscode from "vscode" |
| 9 | +import * as path from "path" |
| 10 | +import { handleFileServiceRequest } from "./index" |
| 11 | +import { cwd } from "@core/task" |
| 12 | + |
| 13 | +/** |
| 14 | + * Creates a rule file in either global or workspace rules directory |
| 15 | + * @param controller The controller instance |
| 16 | + * @param request The request containing filename and isGlobal flag |
| 17 | + * @returns Result with file path and display name |
| 18 | + * @throws Error if operation fails |
| 19 | + */ |
| 20 | +export const createRuleFile: FileMethodHandler = async (controller: Controller, request: RuleFileRequest): Promise<RuleFile> => { |
| 21 | + if (typeof request.isGlobal !== "boolean" || typeof request.filename !== "string" || !request.filename) { |
| 22 | + console.error("createRuleFile: Missing or invalid parameters", { |
| 23 | + isGlobal: typeof request.isGlobal === "boolean" ? request.isGlobal : `Invalid: ${typeof request.isGlobal}`, |
| 24 | + filename: typeof request.filename === "string" ? request.filename : `Invalid: ${typeof request.filename}`, |
| 25 | + }) |
| 26 | + throw new Error("Missing or invalid parameters") |
| 27 | + } |
| 28 | + |
| 29 | + const { filePath, fileExists } = await createRuleFileImpl(request.isGlobal, request.filename, cwd) |
| 30 | + |
| 31 | + if (!filePath) { |
| 32 | + throw new Error("Failed to create rule file.") |
| 33 | + } |
| 34 | + |
| 35 | + if (fileExists) { |
| 36 | + vscode.window.showWarningMessage(`Rule file "${request.filename}" already exists.`) |
| 37 | + // Still open it for editing |
| 38 | + await handleFileServiceRequest(controller, "openFile", { value: filePath }) |
| 39 | + } else { |
| 40 | + await refreshClineRulesToggles(controller.context, cwd) |
| 41 | + await controller.postStateToWebview() |
| 42 | + |
| 43 | + await handleFileServiceRequest(controller, "openFile", { value: filePath }) |
| 44 | + |
| 45 | + vscode.window.showInformationMessage( |
| 46 | + `Created new ${request.isGlobal ? "global" : "workspace"} rule file: ${request.filename}`, |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + return RuleFile.create({ |
| 51 | + filePath: filePath, |
| 52 | + displayName: path.basename(filePath), |
| 53 | + alreadyExists: fileExists, |
| 54 | + }) |
| 55 | +} |
0 commit comments