-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-data-service.ts
71 lines (59 loc) · 2.38 KB
/
project-data-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
///<reference path="../.d.ts"/>
"use strict";
import * as path from "path";
import * as assert from "assert";
export class ProjectDataService implements IProjectDataService {
private static DEPENDENCIES_KEY_NAME = "dependencies";
private projectFilePath: string;
private projectData: IDictionary<any>;
constructor(private $fs: IFileSystem,
private $staticConfig: IStaticConfig,
private $errors: IErrors,
private $logger: ILogger) {
}
public initialize(projectDir: string): void {
if(!this.projectFilePath) {
this.projectFilePath = path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
}
}
public getValue(propertyName: string): IFuture<any> {
return (() => {
this.loadProjectFile().wait();
return this.projectData ? this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][propertyName] : null;
}).future<string>()();
}
public setValue(key: string, value: any): IFuture<void> {
return (() => {
this.loadProjectFile().wait();
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();
}).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();
}).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();
}).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()) {
this.$fs.writeFile(this.projectFilePath, null).wait();
}
this.projectData = this.$fs.readJson(this.projectFilePath).wait() || Object.create(null);
}).future<void>()();
}
}
$injector.register("projectDataService", ProjectDataService);