migrateRunInstallScriptsLines transforms frontmatter lines by removing the top-level run-install-scripts key and injecting it under runtimes.node.
(lines []string, risStr string, alreadyNested bool)
| 65 | // migrateRunInstallScriptsLines transforms frontmatter lines by removing the top-level |
| 66 | // run-install-scripts key and injecting it under runtimes.node. |
| 67 | func migrateRunInstallScriptsLines(lines []string, risStr string, alreadyNested bool) ([]string, bool) { |
| 68 | // Step 1: Find the top-level run-install-scripts line |
| 69 | topLevelIdx := -1 |
| 70 | for i, line := range lines { |
| 71 | if isTopLevelKey(line) && strings.HasPrefix(strings.TrimSpace(line), "run-install-scripts:") { |
| 72 | topLevelIdx = i |
| 73 | break |
| 74 | } |
| 75 | } |
| 76 | if topLevelIdx == -1 { |
| 77 | return lines, false |
| 78 | } |
| 79 | |
| 80 | // Remove the top-level line |
| 81 | result := make([]string, 0, len(lines)) |
| 82 | for i, line := range lines { |
| 83 | if i != topLevelIdx { |
| 84 | result = append(result, line) |
| 85 | } |
| 86 | } |
| 87 | runInstallScriptsCodemodLog.Printf("Removed top-level 'run-install-scripts' on line %d", topLevelIdx+1) |
| 88 | |
| 89 | if alreadyNested { |
| 90 | // runtimes.node.run-install-scripts already exists; just remove the top-level duplicate. |
| 91 | runInstallScriptsCodemodLog.Print("'runtimes.node.run-install-scripts' already present – removed top-level duplicate only") |
| 92 | return result, true |
| 93 | } |
| 94 | |
| 95 | // Step 2: Detect the primary indentation unit used in the frontmatter |
| 96 | indent := detectFrontmatterIndent(result) |
| 97 | |
| 98 | // Step 3: Find the runtimes: block (top-level key) |
| 99 | runtimesIdx := -1 |
| 100 | for i, line := range result { |
| 101 | if isTopLevelKey(line) && strings.HasPrefix(strings.TrimSpace(line), "runtimes:") { |
| 102 | runtimesIdx = i |
| 103 | break |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if runtimesIdx == -1 { |
| 108 | // No runtimes block – append a new one at the end |
| 109 | newLines := []string{ |
| 110 | "runtimes:", |
| 111 | indent + "node:", |
| 112 | fmt.Sprintf("%s%srun-install-scripts: %s", indent, indent, risStr), |
| 113 | } |
| 114 | runInstallScriptsCodemodLog.Print("No 'runtimes:' block found – appending new block") |
| 115 | return append(result, newLines...), true |
| 116 | } |
| 117 | |
| 118 | // Step 4: Find node: as a direct child of runtimes: |
| 119 | runtimesIndent := getIndentation(result[runtimesIdx]) |
| 120 | nodeIdx := -1 |
| 121 | for i := runtimesIdx + 1; i < len(result); i++ { |
| 122 | line := result[i] |
| 123 | trimmed := strings.TrimSpace(line) |
| 124 | if trimmed == "" { |
no test coverage detected