Skip to content

Commit da519cf

Browse files
author
Vladimir Enchev
committed
package.json indent preserved
1 parent 7334ee9 commit da519cf

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

lib/services/project-data-service.ts

+23-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class ProjectDataService implements IProjectDataService {
99

1010
private projectFilePath: string;
1111
private projectData: IDictionary<any>;
12+
private projectFileIndent: string;
1213

1314
constructor(private $fs: IFileSystem,
1415
private $staticConfig: IStaticConfig,
@@ -17,7 +18,7 @@ export class ProjectDataService implements IProjectDataService {
1718
}
1819

1920
public initialize(projectDir: string): void {
20-
if(!this.projectFilePath) {
21+
if (!this.projectFilePath) {
2122
this.projectFilePath = path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
2223
}
2324
}
@@ -32,35 +33,35 @@ export class ProjectDataService implements IProjectDataService {
3233
public setValue(key: string, value: any): IFuture<void> {
3334
return (() => {
3435
this.loadProjectFile().wait();
35-
if(!this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE]) {
36+
if (!this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE]) {
3637
this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE] = Object.create(null);
3738
}
3839
this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][key] = value;
39-
this.$fs.writeJson(this.projectFilePath, this.projectData, "\t").wait();
40+
this.$fs.writeJson(this.projectFilePath, this.projectData, this.projectFileIndent).wait();
4041
}).future<void>()();
4142
}
4243

4344
public removeProperty(propertyName: string): IFuture<void> {
4445
return (() => {
4546
this.loadProjectFile().wait();
46-
delete this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][propertyName];
47-
this.$fs.writeJson(this.projectFilePath, this.projectData, "\t").wait();
47+
delete this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][propertyName];
48+
this.$fs.writeJson(this.projectFilePath, this.projectData, this.projectFileIndent).wait();
4849
}).future<void>()();
4950
}
5051

5152
public removeDependency(dependencyName: string): IFuture<void> {
5253
return (() => {
5354
this.loadProjectFile().wait();
5455
delete this.projectData[ProjectDataService.DEPENDENCIES_KEY_NAME][dependencyName];
55-
this.$fs.writeJson(this.projectFilePath, this.projectData, "\t").wait();
56+
this.$fs.writeJson(this.projectFilePath, this.projectData, this.projectFileIndent).wait();
5657
}).future<void>()();
5758
}
5859

5960
private loadProjectFile(): IFuture<void> {
6061
return (() => {
6162
assert.ok(this.projectFilePath, "Initialize method of projectDataService is not called.");
6263

63-
if(!this.$fs.exists(this.projectFilePath).wait()) {
64+
if (!this.$fs.exists(this.projectFilePath).wait()) {
6465
this.$fs.writeJson(this.projectFilePath, {
6566
"description": "NativeScript Application",
6667
"license": "SEE LICENSE IN <your-license-filename>",
@@ -69,8 +70,22 @@ export class ProjectDataService implements IProjectDataService {
6970
}).wait();
7071
}
7172

72-
this.projectData = this.$fs.readJson(this.projectFilePath).wait() || Object.create(null);
73+
// Detect indent and use it later to write JSON.
74+
let projectFileContent = this.$fs.readText(this.projectFilePath).wait();
75+
76+
this.projectFileIndent = projectFileContent ? this.detectIndent(projectFileContent) : "\t";
77+
78+
this.projectData = projectFileContent ? JSON.parse(projectFileContent) : Object.create(null);
79+
7380
}).future<void>()();
7481
}
82+
83+
private detectIndent(content: string): any {
84+
const leadingSpace = content.match(/(^[ ]+)\S/m);
85+
if (leadingSpace) {
86+
return leadingSpace[1].length;
87+
}
88+
return "\t";
89+
}
7590
}
7691
$injector.register("projectDataService", ProjectDataService);

0 commit comments

Comments
 (0)