(filename: string)
| 63 | * @param filename The path of the file |
| 64 | */ |
| 65 | const JSONPatchForFile = async (filename: string) => { |
| 66 | // We already have access to the diff, so see if the file is in there |
| 67 | // if it's not return an empty diff |
| 68 | if (!gitJSONRep.modified_files.includes(filename)) { |
| 69 | return null |
| 70 | } |
| 71 | |
| 72 | // Grab the two files contents. |
| 73 | const baseFile = await config.getFileContents(filename, config.repo, config.baseSHA) |
| 74 | const headFile = await config.getFileContents(filename, config.repo, config.headSHA) |
| 75 | |
| 76 | // Parse JSON. `fileContents` returns empty string for files that are |
| 77 | // missing in one of the refs, ie. when the file is created or deleted. |
| 78 | const baseJSON = baseFile === "" ? {} : JSON5.parse(baseFile) |
| 79 | const headJSON = headFile === "" ? {} : JSON5.parse(headFile) |
| 80 | |
| 81 | // Tiny bit of hand-waving here around the types. JSONPatchOperation is |
| 82 | // a simpler version of all operations inside the rfc6902 d.ts. Users |
| 83 | // of danger wont care that much, so I'm smudging the classes slightly |
| 84 | // to be ones we can add to the hosted docs. |
| 85 | return { |
| 86 | before: baseFile === "" ? null : baseJSON, |
| 87 | after: headFile === "" ? null : headJSON, |
| 88 | diff: jsonDiff.compare(baseJSON, headJSON) as JSONPatchOperation[], |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Takes a path, generates a JSON patch for it, then parses that into something |
no test coverage detected