Skip to content

Commit

Permalink
Merge pull request #329 from forcedotcom/prerelease/line-endings
Browse files Browse the repository at this point in the history
fix: respect current EOL in gitignore
  • Loading branch information
mdonnalley authored Jun 5, 2024
2 parents e2efc93 + fbc9faa commit ace3484
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
12 changes: 11 additions & 1 deletion bin/sf-clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
19 changes: 14 additions & 5 deletions utils/standardize-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 {
Expand All @@ -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;
}
}
Expand Down

0 comments on commit ace3484

Please sign in to comment.