|
1 | 1 | import * as path from "path";
|
2 |
| -import * as assert from "assert"; |
| 2 | + |
| 3 | +interface IProjectFileData { |
| 4 | + projectData: any; |
| 5 | + projectFilePath: string; |
| 6 | +} |
3 | 7 |
|
4 | 8 | export class ProjectDataService implements IProjectDataService {
|
5 | 9 | private static DEPENDENCIES_KEY_NAME = "dependencies";
|
6 | 10 |
|
7 |
| - private projectFilePath: string; |
8 |
| - private projectData: IDictionary<any>; |
9 |
| - private projectFileIndent: string; |
10 |
| - |
11 | 11 | constructor(private $fs: IFileSystem,
|
12 |
| - private $staticConfig: IStaticConfig) { |
| 12 | + private $staticConfig: IStaticConfig, |
| 13 | + private $logger: ILogger) { |
13 | 14 | }
|
14 | 15 |
|
15 |
| - public initialize(projectDir: string): void { |
16 |
| - if (!this.projectFilePath) { |
17 |
| - this.projectFilePath = path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME); |
18 |
| - } |
| 16 | + public getNSValue(projectDir: string, propertyName: string): any { |
| 17 | + return this.getValue(projectDir, this.getNativeScriptPropertyName(propertyName)); |
19 | 18 | }
|
20 | 19 |
|
21 |
| - public getValue(propertyName: string): any { |
22 |
| - this.loadProjectFile(); |
23 |
| - return this.projectData ? this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][propertyName] : null; |
| 20 | + public setNSValue(projectDir: string, key: string, value: any): void { |
| 21 | + this.setValue(projectDir, this.getNativeScriptPropertyName(key), value); |
24 | 22 | }
|
25 | 23 |
|
26 |
| - public setValue(key: string, value: any): void { |
27 |
| - this.loadProjectFile(); |
28 |
| - if (!this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE]) { |
29 |
| - this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE] = Object.create(null); |
30 |
| - } |
31 |
| - this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][key] = value; |
32 |
| - this.$fs.writeJson(this.projectFilePath, this.projectData, this.projectFileIndent); |
| 24 | + public removeNSProperty(projectDir: string, propertyName: string): void { |
| 25 | + this.removeProperty(projectDir, this.getNativeScriptPropertyName(propertyName)); |
33 | 26 | }
|
34 | 27 |
|
35 |
| - public removeProperty(propertyName: string): void { |
36 |
| - this.loadProjectFile(); |
37 |
| - delete this.projectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE][propertyName]; |
38 |
| - this.$fs.writeJson(this.projectFilePath, this.projectData, this.projectFileIndent); |
| 28 | + public removeDependency(projectDir: string, dependencyName: string): void { |
| 29 | + const projectFileInfo = this.getProjectFileData(projectDir); |
| 30 | + delete projectFileInfo.projectData[ProjectDataService.DEPENDENCIES_KEY_NAME][dependencyName]; |
| 31 | + this.$fs.writeJson(projectFileInfo.projectFilePath, projectFileInfo.projectData); |
| 32 | + } |
| 33 | + |
| 34 | + private getValue(projectDir: string, propertyName: string): any { |
| 35 | + const projectData = this.getProjectFileData(projectDir).projectData; |
| 36 | + |
| 37 | + if (projectData) { |
| 38 | + try { |
| 39 | + return this.getPropertyValueFromJson(projectData, propertyName); |
| 40 | + } catch (err) { |
| 41 | + this.$logger.trace(`Error while trying to get property ${propertyName} from ${projectDir}. Error is:`, err); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return null; |
39 | 46 | }
|
40 | 47 |
|
41 |
| - public removeDependency(dependencyName: string): void { |
42 |
| - this.loadProjectFile(); |
43 |
| - delete this.projectData[ProjectDataService.DEPENDENCIES_KEY_NAME][dependencyName]; |
44 |
| - this.$fs.writeJson(this.projectFilePath, this.projectData, this.projectFileIndent); |
| 48 | + private getNativeScriptPropertyName(propertyName: string) { |
| 49 | + return `${this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE}.${propertyName}`; |
45 | 50 | }
|
46 | 51 |
|
47 |
| - private loadProjectFile(): void { |
48 |
| - assert.ok(this.projectFilePath, "Initialize method of projectDataService is not called."); |
| 52 | + private getPropertyValueFromJson(jsonData: any, dottedPropertyName: string): any { |
| 53 | + const props = dottedPropertyName.split("."); |
| 54 | + let result = jsonData[props.shift()]; |
49 | 55 |
|
50 |
| - if (!this.$fs.exists(this.projectFilePath)) { |
51 |
| - this.$fs.writeJson(this.projectFilePath, { |
52 |
| - "description": "NativeScript Application", |
53 |
| - "license": "SEE LICENSE IN <your-license-filename>", |
54 |
| - "readme": "NativeScript Application", |
55 |
| - "repository": "<fill-your-repository-here>" |
56 |
| - }); |
| 56 | + for (let prop of props) { |
| 57 | + result = result[prop]; |
57 | 58 | }
|
58 | 59 |
|
59 |
| - // Detect indent and use it later to write JSON. |
60 |
| - let projectFileContent = this.$fs.readText(this.projectFilePath); |
| 60 | + return result; |
| 61 | + } |
| 62 | + |
| 63 | + private setValue(projectDir: string, key: string, value: any): void { |
| 64 | + const projectFileInfo = this.getProjectFileData(projectDir); |
| 65 | + |
| 66 | + const props = key.split("."); |
| 67 | + let data: any = projectFileInfo.projectData; |
| 68 | + let currentData = data; |
61 | 69 |
|
62 |
| - this.projectFileIndent = projectFileContent ? this.detectIndent(projectFileContent) : "\t"; |
| 70 | + _.each(props, (prop, index: number) => { |
| 71 | + if (index === (props.length - 1)) { |
| 72 | + currentData[prop] = value; |
| 73 | + } else { |
| 74 | + currentData[prop] = currentData[prop] || Object.create(null); |
| 75 | + } |
63 | 76 |
|
64 |
| - this.projectData = projectFileContent ? JSON.parse(projectFileContent) : Object.create(null); |
| 77 | + currentData = currentData[prop]; |
| 78 | + }); |
| 79 | + |
| 80 | + this.$fs.writeJson(projectFileInfo.projectFilePath, data); |
65 | 81 | }
|
66 | 82 |
|
67 |
| - private detectIndent(content: string): any { |
68 |
| - const leadingSpace = content.match(/(^[ ]+)\S/m); |
69 |
| - if (leadingSpace) { |
70 |
| - return leadingSpace[1].length; |
71 |
| - } |
72 |
| - return "\t"; |
| 83 | + private removeProperty(projectDir: string, propertyName: string): void { |
| 84 | + const projectFileInfo = this.getProjectFileData(projectDir); |
| 85 | + let data: any = projectFileInfo.projectData; |
| 86 | + let currentData = data; |
| 87 | + const props = propertyName.split("."); |
| 88 | + const propertyToDelete = props.splice(props.length - 1, 1)[0]; |
| 89 | + |
| 90 | + _.each(props, (prop) => { |
| 91 | + currentData = currentData[prop]; |
| 92 | + }); |
| 93 | + |
| 94 | + delete currentData[propertyToDelete]; |
| 95 | + this.$fs.writeJson(projectFileInfo.projectFilePath, data); |
| 96 | + } |
| 97 | + |
| 98 | + private getProjectFileData(projectDir: string): IProjectFileData { |
| 99 | + const projectFilePath = path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME); |
| 100 | + const projectFileContent = this.$fs.readText(projectFilePath); |
| 101 | + const projectData = projectFileContent ? JSON.parse(projectFileContent) : Object.create(null); |
| 102 | + |
| 103 | + return { |
| 104 | + projectData, |
| 105 | + projectFilePath |
| 106 | + }; |
73 | 107 | }
|
74 | 108 | }
|
75 | 109 | $injector.register("projectDataService", ProjectDataService);
|
0 commit comments