Skip to content

Commit 40d87f2

Browse files
committed
Merge pull request #889 from kondei/windows-can-save-lf-tsconfig
tsconfig.json is saved with the same new line characters as the previous one
2 parents bcc9113 + 4166e18 commit 40d87f2

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

dist/main/tsconfig/tsconfig.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ var path = require('path');
7171
var tsconfig = require('tsconfig');
7272
var os = require('os');
7373
var detectIndent = require('detect-indent');
74+
var detectNewline = require('detect-newline');
7475
var formatting = require('./formatting');
7576
var projectFileName = 'tsconfig.json';
7677
var defaultFilesGlob = [
@@ -221,7 +222,7 @@ function getProjectSync(pathOrSrcFile) {
221222
};
222223
}
223224
if (projectSpec.filesGlob) {
224-
var prettyJSONProjectSpec = prettyJSON(projectSpec, detectIndent(projectFileTextContent).indent);
225+
var prettyJSONProjectSpec = prettyJSON(projectSpec, detectIndent(projectFileTextContent).indent, detectNewline(projectFileTextContent));
225226
if (prettyJSONProjectSpec !== projectFileTextContent && projectSpec.atom.rewriteTsconfig) {
226227
fs.writeFileSync(projectFile, prettyJSONProjectSpec);
227228
}
@@ -427,8 +428,9 @@ function getDefinitionsForNodeModules(projectDir, files) {
427428
.filter(function (x) { return existing[x]; });
428429
return { implicit: implicit, ours: ours, packagejson: packagejson };
429430
}
430-
function prettyJSON(object, indent) {
431+
function prettyJSON(object, indent, newLine) {
431432
if (indent === void 0) { indent = 4; }
433+
if (newLine === void 0) { newLine = os.EOL; }
432434
var cache = [];
433435
var value = JSON.stringify(object, function (key, value) {
434436
if (typeof value === 'object' && value !== null) {
@@ -439,7 +441,7 @@ function prettyJSON(object, indent) {
439441
}
440442
return value;
441443
}, indent);
442-
value = value.split('\n').join(os.EOL) + os.EOL;
444+
value = value.replace(/(?:\r\n|\r|\n)/g, newLine) + newLine;
443445
cache = null;
444446
return value;
445447
}

lib/globals.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ declare module 'detect-indent' {
3939
export = detectIndent;
4040
}
4141

42+
declare module 'detect-newline' {
43+
function detectNewline (string: string): string;
44+
export = detectNewline;
45+
}
46+
4247
declare module 'xtend' {
4348
function extend <T, U> (dest: T, src: U): T & U;
4449
export = extend;

lib/main/tsconfig/tsconfig.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ import path = require('path');
212212
import tsconfig = require('tsconfig');
213213
import os = require('os');
214214
import detectIndent = require('detect-indent');
215+
import detectNewline = require('detect-newline');
215216
import extend = require('xtend');
216217
import formatting = require('./formatting');
217218

@@ -406,7 +407,7 @@ export function getProjectSync(pathOrSrcFile: string): TypeScriptProjectFileDeta
406407
}
407408

408409
if (projectSpec.filesGlob) { // for filesGlob we keep the files in sync
409-
var prettyJSONProjectSpec = prettyJSON(projectSpec, detectIndent(projectFileTextContent).indent);
410+
var prettyJSONProjectSpec = prettyJSON(projectSpec, detectIndent(projectFileTextContent).indent, detectNewline(projectFileTextContent));
410411

411412
if (prettyJSONProjectSpec !== projectFileTextContent && projectSpec.atom.rewriteTsconfig) {
412413
fs.writeFileSync(projectFile, prettyJSONProjectSpec);
@@ -703,7 +704,7 @@ function getDefinitionsForNodeModules(projectDir: string, files: string[]): { ou
703704
return { implicit, ours, packagejson };
704705
}
705706

706-
export function prettyJSON(object: any, indent: string | number = 4): string {
707+
export function prettyJSON(object: any, indent: string | number = 4, newLine: string = os.EOL): string {
707708
var cache = [];
708709
var value = JSON.stringify(
709710
object,
@@ -721,7 +722,7 @@ export function prettyJSON(object: any, indent: string | number = 4): string {
721722
},
722723
indent
723724
);
724-
value = value.split('\n').join(os.EOL) + os.EOL;
725+
value = value.replace(/(?:\r\n|\r|\n)/g, newLine) + newLine;
725726
cache = null;
726727
return value;
727728
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"basarat-text-buffer": "6.0.0",
5252
"d3": "^3.5.5",
5353
"detect-indent": "^4.0.0",
54+
"detect-newline": "^2.1.0",
5455
"emissary": "^1.3.3",
5556
"escape-html": "^1.0.1",
5657
"findup": "^0.1.5",

0 commit comments

Comments
 (0)