Skip to content

package.json indent preserved #1779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions lib/services/project-data-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class ProjectDataService implements IProjectDataService {

private projectFilePath: string;
private projectData: IDictionary<any>;
private projectFileIndent: string;

constructor(private $fs: IFileSystem,
private $staticConfig: IStaticConfig,
Expand All @@ -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);
}
}
Expand All @@ -32,35 +33,35 @@ export class ProjectDataService implements IProjectDataService {
public setValue(key: string, value: any): IFuture<void> {
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<void>()();
}

public removeProperty(propertyName: string): IFuture<void> {
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<void>()();
}

public removeDependency(dependencyName: string): IFuture<void> {
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<void>()();
}

private loadProjectFile(): IFuture<void> {
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 <your-license-filename>",
Expand All @@ -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<void>()();
}

private detectIndent(content: string): any {
const leadingSpace = content.match(/(^[ ]+)\S/m);
if (leadingSpace) {
return leadingSpace[1].length;
}
return "\t";
}
}
$injector.register("projectDataService", ProjectDataService);