-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-data-service.ts
91 lines (76 loc) · 3.1 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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>;
private projectFileIndent: string;
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, 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, 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, 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()) {
this.$fs.writeJson(this.projectFilePath, {
"description": "NativeScript Application",
"license": "SEE LICENSE IN <your-license-filename>",
"readme": "NativeScript Application",
"repository": "<fill-your-repository-here>",
"devDependencies": {
"nativescript-dev-android-snapshot": "^0.*.*"
}
}).wait();
}
// 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);