diff --git a/bin/sf-clean.js b/bin/sf-clean.js index 9d8e66d..657c525 100755 --- a/bin/sf-clean.js +++ b/bin/sf-clean.js @@ -24,11 +24,21 @@ const gitignorePath = loadRootPath('.gitignore'); if (gitignorePath) { const VALID_SEGMENTS = ['CLEAN', 'CLEAN ALL']; const gitignore = readFileSync(join(gitignorePath, '.gitignore'), 'utf8'); + + // respect the file EOL (`CRLF` or `LF`). + // + // we can't use node's `os.EOL` because that assumes: + // * unix only uses `CL` + // * win only uses `CRLF` + // + // when all 4 scenarios are completely valid + const originalEOL = gitignore.includes('\r\n') ? '\r\n' : '\n'; + const segments = gitignore // Segments are defined by "# --" in the gitignore .split('# --') // Turn each segment into list of valid gitignore lines - .map((segment) => segment.split('\n').filter((line) => line && !line.startsWith('#'))) + .map((segment) => segment.split(originalEOL).filter((line) => line && !line.startsWith('#'))) // Maps segment name to list of valid gitignore lines .reduce((map, segment) => { const segmentName = (segment.shift() || '').trim(); diff --git a/package.json b/package.json index cb378d1..9d69375 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@salesforce/dev-scripts", - "version": "9.1.2", + "version": "9.1.3-dev.2", "description": "Standardize package.json scripts and config files for Salesforce projects.", "repository": "forcedotcom/dev-scripts", "bin": { diff --git a/utils/standardize-files.js b/utils/standardize-files.js index 0399acb..b47f4f9 100644 --- a/utils/standardize-files.js +++ b/utils/standardize-files.js @@ -83,11 +83,20 @@ function writeGitignore(targetDir) { const relevantPatterns = IGNORES.filter((entry) => !entry.plugin || (entry.plugin && isAPlugin)); let original = readFileSync(gitignoreTargetPath, 'utf-8'); + // respect the file EOL (`CRLF` or `LF`). + // + // we can't use node's `os.EOL` because that assumes: + // * unix only uses `CL` + // * win only uses `CRLF` + // + // when all 4 scenarios are completely valid + 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('\n')) + .map((segment) => segment.split(originalEOL)) // Maps segment name to list of valid gitignore lines .reduce((map, segment) => { const segmentName = (segment.shift() || '').trim(); @@ -112,7 +121,7 @@ function writeGitignore(targetDir) { } if (needsWrite) { - writeFileSync(gitignoreTargetPath, `${original}\n${toAdd.join('\n')}`); + writeFileSync(gitignoreTargetPath, `${original}${originalEOL}${toAdd.join(originalEOL)}`); return gitignoreTargetPath; } } else { @@ -135,11 +144,11 @@ function writeGitignore(targetDir) { for (const [section, lines] of Object.entries(updatedSegments)) { if (lines.length === 0) continue; original = original.replace( - `# -- ${section}\n${segments[section].join('\n')}`, - `# -- ${section}\n${[...segments[section], ...lines, '\n'].join('\n')}` + `# -- ${section}${originalEOL}${segments[section].join(originalEOL)}`, + `# -- ${section}${originalEOL}${[...segments[section], ...lines, originalEOL].join(originalEOL)}` ); } - writeFileSync(gitignoreTargetPath, original.trimEnd() + '\n'); + writeFileSync(gitignoreTargetPath, original.trimEnd() + originalEOL); return gitignoreTargetPath; } }