Skip to content

Commit 88538d1

Browse files
alexanderogilviemattcompiles
authored andcommitted
fix: Normalize line endings for Windows (seek-oss#20)
1 parent 9476685 commit 88538d1

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

index.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const getNoDeclarationFileError = ({ filename }) =>
1414
);
1515

1616
const getTypeMismatchError = ({ filename, expected, actual }) => {
17-
const diff = new LineDiff(actual, expected).toString();
17+
const diff = new LineDiff(enforceLFLineSeparators(actual), expected).toString();
1818

1919
return new Error(
2020
`Generated type declaration file is outdated. Run webpack and commit the updated type declaration for '${filename}'\n\n${diff}`
@@ -36,6 +36,19 @@ const filenameToTypingsFilename = filename => {
3636
return path.join(dirName, `${baseName}.d.ts`);
3737
};
3838

39+
const enforceLFLineSeparators = text => {
40+
if (text) {
41+
// replace all CRLFs (Windows) by LFs (Unix)
42+
return text.replace(/\r\n/g, "\n");
43+
} else {
44+
return text;
45+
}
46+
};
47+
48+
const compareText = (contentA, contentB) => {
49+
return enforceLFLineSeparators(contentA) === enforceLFLineSeparators(contentB);
50+
};
51+
3952
const validModes = ['emit', 'verify'];
4053

4154
const isFileNotFound = err => err && err.code === 'ENOENT';
@@ -91,7 +104,7 @@ module.exports = function(content, ...rest) {
91104
return failed(err);
92105
}
93106

94-
if (cssModuleDefinition !== fileContents) {
107+
if (!compareText(cssModuleDefinition, fileContents)) {
95108
return failed(
96109
getTypeMismatchError({
97110
filename: cssModuleInterfaceFilename,
@@ -105,7 +118,7 @@ module.exports = function(content, ...rest) {
105118
});
106119
} else {
107120
read((_, fileContents) => {
108-
if (cssModuleDefinition !== fileContents) {
121+
if (!compareText(cssModuleDefinition, fileContents)) {
109122
write(cssModuleDefinition, err => {
110123
if (err) {
111124
failed(err);

test/verify-invalid-declaration/verify-invalid-declaration.test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ test('Can error on invalid declaration', async () => {
99
mode: 'verify'
1010
});
1111
} catch (err) {
12-
expect(getErrorMessage(err.errors[0])).toMatchSnapshot();
12+
// make test robust for Windows by replacing backslashes in the file path with slashes
13+
let errorMessage = getErrorMessage(err.errors[0]).replace(/(?<=Error:.*)\\/g, "/");
14+
15+
expect(errorMessage).toMatchSnapshot();
1316
}
1417
});

test/verify-missing-declaration/verify-missing-declaration.test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ test('Can error on invalid declaration', async () => {
99
mode: 'verify'
1010
});
1111
} catch (err) {
12-
expect(getErrorMessage(err.errors[0])).toMatchSnapshot();
12+
// make test robust for Windows by replacing backslashes in the file path with slashes
13+
let errorMessage = getErrorMessage(err.errors[0]).replace(/(?<=Error:.*)\\/g, "/");
14+
15+
expect(errorMessage).toMatchSnapshot();
1316
}
1417
});

0 commit comments

Comments
 (0)