Skip to content

Commit

Permalink
Normalise line endings when patching user files
Browse files Browse the repository at this point in the history
We want to make sure that on Windows we can compare files regardless of
what line endings the users files have, and that any changes we make
have the same line endings.
  • Loading branch information
lfdebrux committed Aug 2, 2022
1 parent 17cd9b2 commit 364bfae
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/_update/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,21 @@ async function getProjectVersion () {
}

async function patchUserFile (originalVersion, filePath) {
const original = await fetchOriginal(originalVersion, filePath)
const theirs = await fs.readFile(path.resolve(projectDir, filePath), 'utf8')
const ours = await fs.readFile(path.resolve(updateDir, filePath), 'utf8')
let original = await fetchOriginal(originalVersion, filePath)
let ours = await fs.readFile(path.resolve(updateDir, filePath), 'utf8')

// normalise line endings
var eol = '\n'
if (theirs.includes('\r\n')) {
eol = '\r\n'
if (!original.includes(eol)) {
original = original.replaceAll('\n', eol)
}
if (!ours.includes(eol)) {
ours = ours.replaceAll('\n', eol)
}
}

// It is possible that the file has already been upgraded, in which case there is nothing to do
if (theirs === ours) {
Expand Down

0 comments on commit 364bfae

Please sign in to comment.