(dataPath: string)
| 666 | } |
| 667 | |
| 668 | export function loadJSON(dataPath: string): any { |
| 669 | const [filePath, fieldPath] = dataPath.split("@") |
| 670 | const resolvedFilePath = path.resolve(process.cwd(), filePath) |
| 671 | let json |
| 672 | try { |
| 673 | json = JSON.parse(fs.readFileSync(resolvedFilePath, "utf-8")) |
| 674 | } catch (err: any) { |
| 675 | throw new Error( |
| 676 | `Failed to load JSON args from file "${resolvedFilePath}": ${err}`, |
| 677 | ) |
| 678 | } |
| 679 | const fields = (fieldPath ?? "") |
| 680 | .trim() |
| 681 | .split(".") |
| 682 | .filter(x => x !== "") |
| 683 | for (const field of fields) { |
| 684 | json = json[field] |
| 685 | if (json === undefined) { |
| 686 | throw new Error( |
| 687 | `Failed to load JSON args from file "${filePath}": missing required field ${fieldPath}`, |
| 688 | ) |
| 689 | } |
| 690 | } |
| 691 | return json |
| 692 | } |
no test coverage detected