| 1 | import * as path from "path"; |
| 2 | |
| 3 | export function parseSearchAndReplaceArgs(args: any): { |
| 4 | filepath: string; |
| 5 | diffs: string[]; |
| 6 | } { |
| 7 | const { filepath, diffs: diffsArg } = args; |
| 8 | |
| 9 | if (!filepath || !diffsArg) { |
| 10 | throw new Error("filepath and diffs args are required"); |
| 11 | } |
| 12 | |
| 13 | if (typeof filepath !== "string") { |
| 14 | throw new Error("filepath must be a string"); |
| 15 | } |
| 16 | |
| 17 | const diffs: string[] = []; |
| 18 | if (!Array.isArray(diffsArg)) { |
| 19 | throw new Error("diffs must be an array of string search/replace blocks"); |
| 20 | } |
| 21 | diffsArg.forEach((diff, i) => { |
| 22 | if (typeof diff !== "string") { |
| 23 | throw new Error(`Diff at ${i + 1} is not a string`); |
| 24 | } |
| 25 | diffs.push(diff); |
| 26 | }); |
| 27 | |
| 28 | const fixedPath = filepath.replace(/\/|\\/g, path.sep); |
| 29 | |
| 30 | return { |
| 31 | filepath: fixedPath, |
| 32 | diffs, |
| 33 | }; |
| 34 | } |