Last active
November 16, 2022 12:19
-
-
Save WillsB3/b563ab288f7bf5ec50ba8a3acdab52fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This file can be used to reformat the output of https://www.npmjs.com/package/diff-package-lock | |
* so that the diff can be displayed with diff syntax highlighting in a GitHub | |
* PR. | |
* | |
* How do I use this? | |
* 1. Download this script to your local machine | |
* 2. Run `npx diff-package-lock main` | |
* 3. Copy and paste the output into a new file: package-diff.txt (required as | |
* diff-package-lock doesn't let us disable colorization of its output). | |
* 3. Run this script passing the file created in step 3 as the first | |
* argument and direct the output to `pbcopy`: | |
* `node ./package-lock-diff.js package-diff.txt | pbcopy` | |
* 4. In your GitHub PR, add a diff code block and paste the result: | |
* ```diff | |
* <paste> | |
* ``` | |
* 5. Preview your PR description - the diff should show added packages in green and removed | |
* packages in red. Packages which have had a version change show in the default text colour. | |
*/ | |
const fs = require('fs') | |
const ADDED_PATTERN = / \+ / | |
const REMOVED_PATTERN = / - / | |
const CHANGED_PATTERN = / -> / | |
const filename = process.argv[2] | |
const dest = process.argv[3] | |
const lines = fs.readFileSync(filename, 'utf-8').split('\n') | |
const newLines = [] | |
lines.forEach((line) => { | |
if (ADDED_PATTERN.test(line)) { | |
newLines.push(`+ ${line}`) | |
} else if (REMOVED_PATTERN.test(line)) { | |
newLines.push(`- ${line}`) | |
} else if (CHANGED_PATTERN.test(line)) { | |
newLines.push(` ${line}`) | |
} | |
}) | |
newLines.forEach((line) => { | |
console.log(line) | |
}) | |
if (dest) { | |
fs.writeFileSync(dest, newLines.join('\n')) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment