From 473c0754d6f3528d11189a816362394292ee521d Mon Sep 17 00:00:00 2001 From: Cristian Dominguez Date: Tue, 4 Jun 2024 16:04:27 -0300 Subject: [PATCH] fix: respect current line ending in file --- utils/standardize-files.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/utils/standardize-files.js b/utils/standardize-files.js index 25302664..f86b314c 100644 --- a/utils/standardize-files.js +++ b/utils/standardize-files.js @@ -6,7 +6,6 @@ */ const { join } = require('path'); -const { EOL } = require('node:os'); const { readFileSync, unlinkSync, copyFileSync, writeFileSync } = require('fs'); const log = require('./log'); const exists = require('./exists'); @@ -84,11 +83,13 @@ function writeGitignore(targetDir) { const relevantPatterns = IGNORES.filter((entry) => !entry.plugin || (entry.plugin && isAPlugin)); let original = readFileSync(gitignoreTargetPath, 'utf-8'); + const originalEOL = original.includes('\r\n') ? '\r\n' : '\n'; + const segments = original // Segments are defined by "# --" in the gitignore .split('# --') // Turn each segment into list of valid gitignore lines - .map((segment) => segment.split(EOL)) + .map((segment) => segment.split(originalEOL)) // Maps segment name to list of valid gitignore lines .reduce((map, segment) => { const segmentName = (segment.shift() || '').trim(); @@ -113,7 +114,7 @@ function writeGitignore(targetDir) { } if (needsWrite) { - writeFileSync(gitignoreTargetPath, `${original}${EOL}${toAdd.join(EOL)}`); + writeFileSync(gitignoreTargetPath, `${original}${originalEOL}${toAdd.join(originalEOL)}`); return gitignoreTargetPath; } } else { @@ -136,11 +137,11 @@ function writeGitignore(targetDir) { for (const [section, lines] of Object.entries(updatedSegments)) { if (lines.length === 0) continue; original = original.replace( - `# -- ${section}${EOL}${segments[section].join(EOL)}`, - `# -- ${section}${EOL}${[...segments[section], ...lines, EOL].join(EOL)}` + `# -- ${section}${originalEOL}${segments[section].join(originalEOL)}`, + `# -- ${section}${originalEOL}${[...segments[section], ...lines, originalEOL].join(originalEOL)}` ); } - writeFileSync(gitignoreTargetPath, original.trimEnd() + EOL); + writeFileSync(gitignoreTargetPath, original.trimEnd() + originalEOL); return gitignoreTargetPath; } }