-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-data.ts
111 lines (96 loc) · 4.03 KB
/
project-data.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
///<reference path=".d.ts"/>
"use strict";
import * as constants from "./constants";
import * as path from "path";
import {EOL} from "os";
export class ProjectData implements IProjectData {
private static OLD_PROJECT_FILE_NAME = ".tnsproject";
public projectDir: string;
public platformsDir: string;
public projectFilePath: string;
public projectId: string;
public projectName: string;
public appDirectoryPath: string;
public appResourcesDirectoryPath: string;
public dependencies: any;
constructor(private $fs: IFileSystem,
private $errors: IErrors,
private $logger: ILogger,
private $projectHelper: IProjectHelper,
private $staticConfig: IStaticConfig,
private $options: IOptions) {
this.initializeProjectData().wait();
}
private initializeProjectData(): IFuture<void> {
return(() => {
let projectDir = this.$projectHelper.projectDir;
// If no project found, projectDir should be null
if(projectDir) {
this.initializeProjectDataCore(projectDir);
let data: any = null;
if (this.$fs.exists(this.projectFilePath).wait()) {
let fileContent: any = null;
try {
fileContent = this.$fs.readJson(this.projectFilePath).wait();
data = fileContent[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE];
} catch (err) {
this.$errors.fail({formatStr: "The project file %s is corrupted." + EOL +
"Consider restoring an earlier version from your source control or backup." + EOL +
"Additional technical info: %s",
suppressCommandHelp: true},
this.projectFilePath, err.toString());
}
if(data) {
this.projectId = data.id;
this.dependencies = fileContent.dependencies;
} else { // This is the case when we have package.json file but nativescipt key is not presented in it
this.tryToUpgradeProject().wait();
}
}
} else { // This is the case when no project file found
this.tryToUpgradeProject().wait();
}
}).future<void>()();
}
private throwNoProjectFoundError(): void {
this.$errors.fail("No project found at or above '%s' and neither was a --path specified.", this.$options.path || path.resolve("."));
}
private tryToUpgradeProject(): IFuture<void> {
return (() => {
let projectDir = this.projectDir || path.resolve(this.$options.path || ".");
let oldProjectFilePath = path.join(projectDir, ProjectData.OLD_PROJECT_FILE_NAME);
if(this.$fs.exists(oldProjectFilePath).wait()) {
this.upgrade(projectDir, oldProjectFilePath).wait();
} else {
this.throwNoProjectFoundError();
}
}).future<void>()();
}
private upgrade(projectDir: string, oldProjectFilePath: string): IFuture<void> {
return (() => {
try {
let oldProjectData = this.$fs.readJson(oldProjectFilePath).wait();
let newProjectFilePath = this.projectFilePath || path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
let newProjectData = this.$fs.exists(newProjectFilePath).wait() ? this.$fs.readJson(newProjectFilePath).wait() : {};
newProjectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE] = oldProjectData;
this.$fs.writeJson(newProjectFilePath, newProjectData).wait();
this.projectId = newProjectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE].id;
this.$fs.deleteFile(oldProjectFilePath).wait();
} catch(err) {
this.$logger.out("An error occurred while upgrading your project.");
throw err;
}
this.initializeProjectDataCore(projectDir);
this.$logger.out("Successfully upgraded your project file.");
}).future<void>()();
}
private initializeProjectDataCore(projectDir: string): void {
this.projectDir = projectDir;
this.projectName = this.$projectHelper.sanitizeName(path.basename(projectDir));
this.platformsDir = path.join(projectDir, "platforms");
this.projectFilePath = path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
this.appDirectoryPath = path.join(projectDir, constants.APP_FOLDER_NAME);
this.appResourcesDirectoryPath = path.join(projectDir, constants.APP_FOLDER_NAME, constants.APP_RESOURCES_FOLDER_NAME);
}
}
$injector.register("projectData", ProjectData);