Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit 9c24826

Browse files
Keep the current indentation of json files
Since our other clients use 2 spaces for indentation in json files we should use 2 spaces too. Since some users are using only the CLI we should not change their current indentation.
1 parent a3bfb06 commit 9c24826

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

file-system.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import * as shelljs from "shelljs";
99

1010
@injector.register("fs")
1111
export class FileSystem implements IFileSystem {
12+
private static DEFAULT_INDENTATION_CHARACTER = "\t";
13+
private static JSON_OBJECT_REGEXP = new RegExp(`{\\r*\\n*(\\W*)"`, "m");
14+
1215
constructor(private $injector: IInjector) { }
1316

1417
//TODO: try 'archiver' module for zipping
@@ -269,7 +272,11 @@ export class FileSystem implements IFileSystem {
269272
return future;
270273
}
271274

272-
public writeJson(filename: string, data: any, space: string = "\t", encoding?: string): IFuture<void> {
275+
public writeJson(filename: string, data: any, space?: string, encoding?: string): IFuture<void> {
276+
if (!space) {
277+
space = this.getIndentationCharacter(filename).wait();
278+
}
279+
273280
return this.writeFile(filename, JSON.stringify(data, null, space), encoding);
274281
}
275282

@@ -523,4 +530,23 @@ export class FileSystem implements IFileSystem {
523530
}
524531
}).future<void>()();
525532
}
533+
534+
private getIndentationCharacter(filePath: string): IFuture<string> {
535+
return ((): string => {
536+
if (!this.exists(filePath).wait()) {
537+
return FileSystem.DEFAULT_INDENTATION_CHARACTER;
538+
}
539+
540+
let fileContent = this.readText(filePath).wait().trim();
541+
let matches = fileContent.match(FileSystem.JSON_OBJECT_REGEXP);
542+
543+
if (!matches || !matches[1]) {
544+
return FileSystem.DEFAULT_INDENTATION_CHARACTER;
545+
}
546+
547+
let indentation = matches[1];
548+
549+
return indentation[0] === " " ? indentation : FileSystem.DEFAULT_INDENTATION_CHARACTER;
550+
}).future<string>()();
551+
}
526552
}

services/user-settings-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class UserSettingsServiceBase implements IUserSettingsService {
4141
this.userSettingsData[propertyName] = data[propertyName];
4242
});
4343

44-
this.$fs.writeJson(this.userSettingsFilePath, this.userSettingsData, "\t").wait();
44+
this.$fs.writeJson(this.userSettingsFilePath, this.userSettingsData).wait();
4545
}).future<void>()();
4646
}
4747

test/unit-tests/mobile/android-device-file-system.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ describe("Android device file system tests", () => {
146146
isDirectory: () => false,
147147
isFile: () => true
148148
});
149+
fs.readText = () => Future.fromResult("");
149150

150151
let androidDeviceFileSystem = createAndroidDeviceFileSystem(injector);
151152
androidDeviceFileSystem.transferFile = (localPath: string, devicePath: string) => {
@@ -173,6 +174,7 @@ describe("Android device file system tests", () => {
173174
isDirectory: () => false,
174175
isFile: () => true
175176
});
177+
fs.readText = () => Future.fromResult("");
176178

177179
let androidDeviceFileSystem = createAndroidDeviceFileSystem(injector);
178180
let transferedFilesOnDevice: string[] = [];
@@ -206,6 +208,7 @@ describe("Android device file system tests", () => {
206208
isDirectory: () => false,
207209
isFile: () => true
208210
});
211+
fs.readText = () => Future.fromResult("");
209212

210213
let androidDeviceFileSystem = createAndroidDeviceFileSystem(injector);
211214
androidDeviceFileSystem.transferFile = (localPath: string, devicePath: string) => {
@@ -233,6 +236,7 @@ describe("Android device file system tests", () => {
233236
isDirectory: () => false,
234237
isFile: () => true
235238
});
239+
fs.readText = () => Future.fromResult("");
236240

237241
let androidDeviceFileSystem = createAndroidDeviceFileSystem(injector);
238242
androidDeviceFileSystem.transferFile = (localPath: string, devicePath: string) => {

0 commit comments

Comments
 (0)