From da519cfac1558bcb2d6054555f60e29c987b3e7f Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Thu, 26 May 2016 15:04:26 +0300 Subject: [PATCH] package.json indent preserved --- lib/services/project-data-service.ts | 31 +++++++++++++++++++++------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/services/project-data-service.ts b/lib/services/project-data-service.ts index 8de79dfa45..a81e3e6053 100644 --- a/lib/services/project-data-service.ts +++ b/lib/services/project-data-service.ts @@ -9,6 +9,7 @@ export class ProjectDataService implements IProjectDataService { private projectFilePath: string; private projectData: IDictionary; + private projectFileIndent: string; constructor(private $fs: IFileSystem, private $staticConfig: IStaticConfig, @@ -17,7 +18,7 @@ export class ProjectDataService implements IProjectDataService { } public initialize(projectDir: string): void { - if(!this.projectFilePath) { + if (!this.projectFilePath) { this.projectFilePath = path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME); } } @@ -32,19 +33,19 @@ export class ProjectDataService implements IProjectDataService { public setValue(key: string, value: any): IFuture { return (() => { this.loadProjectFile().wait(); - if(!this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE]) { + if (!this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE]) { this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE] = Object.create(null); } this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][key] = value; - this.$fs.writeJson(this.projectFilePath, this.projectData, "\t").wait(); + this.$fs.writeJson(this.projectFilePath, this.projectData, this.projectFileIndent).wait(); }).future()(); } public removeProperty(propertyName: string): IFuture { return (() => { this.loadProjectFile().wait(); - delete this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][propertyName]; - this.$fs.writeJson(this.projectFilePath, this.projectData, "\t").wait(); + delete this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][propertyName]; + this.$fs.writeJson(this.projectFilePath, this.projectData, this.projectFileIndent).wait(); }).future()(); } @@ -52,7 +53,7 @@ export class ProjectDataService implements IProjectDataService { return (() => { this.loadProjectFile().wait(); delete this.projectData[ProjectDataService.DEPENDENCIES_KEY_NAME][dependencyName]; - this.$fs.writeJson(this.projectFilePath, this.projectData, "\t").wait(); + this.$fs.writeJson(this.projectFilePath, this.projectData, this.projectFileIndent).wait(); }).future()(); } @@ -60,7 +61,7 @@ export class ProjectDataService implements IProjectDataService { return (() => { assert.ok(this.projectFilePath, "Initialize method of projectDataService is not called."); - if(!this.$fs.exists(this.projectFilePath).wait()) { + if (!this.$fs.exists(this.projectFilePath).wait()) { this.$fs.writeJson(this.projectFilePath, { "description": "NativeScript Application", "license": "SEE LICENSE IN ", @@ -69,8 +70,22 @@ export class ProjectDataService implements IProjectDataService { }).wait(); } - this.projectData = this.$fs.readJson(this.projectFilePath).wait() || Object.create(null); + // Detect indent and use it later to write JSON. + let projectFileContent = this.$fs.readText(this.projectFilePath).wait(); + + this.projectFileIndent = projectFileContent ? this.detectIndent(projectFileContent) : "\t"; + + this.projectData = projectFileContent ? JSON.parse(projectFileContent) : Object.create(null); + }).future()(); } + + private detectIndent(content: string): any { + const leadingSpace = content.match(/(^[ ]+)\S/m); + if (leadingSpace) { + return leadingSpace[1].length; + } + return "\t"; + } } $injector.register("projectDataService", ProjectDataService);