| 140 | } |
| 141 | |
| 142 | function parseEdits(parsed: Record<string, string>): TextEdit[] { |
| 143 | let edits: TextEdit[]; |
| 144 | |
| 145 | if (parsed.edits) { |
| 146 | let parsedJson: unknown; |
| 147 | try { |
| 148 | parsedJson = JSON.parse(parsed.edits); |
| 149 | } catch (e: any) { |
| 150 | fail(`Invalid --edits JSON: ${e.message}`); |
| 151 | } |
| 152 | if (!Array.isArray(parsedJson) || parsedJson.length === 0) { |
| 153 | fail("--edits must be a non-empty JSON array of {find, replace} objects"); |
| 154 | } |
| 155 | edits = parsedJson as TextEdit[]; |
| 156 | } else if (parsed.find !== undefined) { |
| 157 | if (parsed.find === "") fail("--find cannot be empty"); |
| 158 | edits = [{ find: parsed.find, replace: parsed.replace ?? "" }]; |
| 159 | } else { |
| 160 | fail("Either --find/--replace or --edits is required"); |
| 161 | } |
| 162 | |
| 163 | for (const edit of edits!) { |
| 164 | if (typeof edit.find !== "string" || edit.find === "") { |
| 165 | fail("Each edit must have a non-empty 'find' string"); |
| 166 | } |
| 167 | if (edit.replace === undefined || edit.replace === null) { |
| 168 | edit.replace = ""; |
| 169 | } |
| 170 | if (typeof edit.replace !== "string") { |
| 171 | fail("Each edit's 'replace' field must be a string"); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return edits!; |
| 176 | } |
| 177 | |
| 178 | function preview(s: string): string { |
| 179 | const max = 60; |