getCommandToSlashCommandCodemod creates a codemod for migrating on.command to on.slash_command
()
| 10 | |
| 11 | // getCommandToSlashCommandCodemod creates a codemod for migrating on.command to on.slash_command |
| 12 | func getCommandToSlashCommandCodemod() Codemod { |
| 13 | return Codemod{ |
| 14 | ID: "command-to-slash-command-migration", |
| 15 | Name: "Migrate on.command to on.slash_command", |
| 16 | Description: "Replaces deprecated 'on.command' field with 'on.slash_command'", |
| 17 | IntroducedIn: "0.2.0", |
| 18 | Apply: func(content string, frontmatter map[string]any) (string, bool, error) { |
| 19 | // Check if on.command exists |
| 20 | onValue, hasOn := frontmatter["on"] |
| 21 | if !hasOn { |
| 22 | return content, false, nil |
| 23 | } |
| 24 | |
| 25 | onMap, ok := onValue.(map[string]any) |
| 26 | if !ok { |
| 27 | return content, false, nil |
| 28 | } |
| 29 | |
| 30 | // Check if command field exists in on |
| 31 | _, hasCommand := onMap["command"] |
| 32 | if !hasCommand { |
| 33 | return content, false, nil |
| 34 | } |
| 35 | |
| 36 | newContent, applied, err := applyFrontmatterLineTransform(content, func(lines []string) ([]string, bool) { |
| 37 | var modified bool |
| 38 | var inOnBlock bool |
| 39 | var onIndent string |
| 40 | result := make([]string, len(lines)) |
| 41 | for i, line := range lines { |
| 42 | trimmedLine := strings.TrimSpace(line) |
| 43 | |
| 44 | // Track if we're in the on block |
| 45 | if strings.HasPrefix(trimmedLine, "on:") { |
| 46 | inOnBlock = true |
| 47 | onIndent = getIndentation(line) |
| 48 | result[i] = line |
| 49 | continue |
| 50 | } |
| 51 | |
| 52 | // Check if we've left the on block |
| 53 | if inOnBlock && len(trimmedLine) > 0 && !strings.HasPrefix(trimmedLine, "#") { |
| 54 | if hasExitedBlock(line, onIndent) { |
| 55 | inOnBlock = false |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Replace command with slash_command if in on block |
| 60 | if inOnBlock && strings.HasPrefix(trimmedLine, "command:") { |
| 61 | replacedLine, didReplace := findAndReplaceInLine(line, "command", "slash_command") |
| 62 | if didReplace { |
| 63 | result[i] = replacedLine |
| 64 | modified = true |
| 65 | slashCommandCodemodLog.Printf("Replaced on.command with on.slash_command on line %d", i+1) |
| 66 | } else { |
| 67 | result[i] = line |
| 68 | } |
| 69 | } else { |