(cwd: string, input: CreateRuleInput)
| 88 | } |
| 89 | |
| 90 | export async function createRule(cwd: string, input: CreateRuleInput): Promise<string> { |
| 91 | const directoryPath = getTargetRuleDirectory(cwd, input) |
| 92 | const fileName = normalizeRuleFileName(input.fileName) |
| 93 | const filePath = path.join(directoryPath, fileName) |
| 94 | |
| 95 | assertPathInsideDirectory(filePath, directoryPath) |
| 96 | |
| 97 | try { |
| 98 | await fs.mkdir(directoryPath, { recursive: true }) |
| 99 | await fs.writeFile(filePath, createRuleTemplate(fileName, input), { encoding: "utf-8", flag: "wx" }) |
| 100 | return filePath |
| 101 | } catch (error) { |
| 102 | if ((error as NodeJS.ErrnoException).code === "EEXIST") { |
| 103 | throw new Error(`Rule file already exists: ${fileName}`) |
| 104 | } |
| 105 | |
| 106 | throw error |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | export async function deleteRule(cwd: string, input: DeleteRuleInput): Promise<void> { |
| 111 | const filePath = await resolveRuleFile(cwd, input) |
no test coverage detected