-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-data.ts
147 lines (127 loc) · 4.97 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import * as constants from "./constants";
import * as path from "path";
import { EOL } from "os";
interface IProjectType {
type: string;
requiredDependencies?: string[];
isDefaultProjectType?: boolean;
}
export class ProjectData implements IProjectData {
private static OLD_PROJECT_FILE_NAME = ".tnsproject";
/**
* NOTE: Order of the elements is important as the TypeScript dependencies are commonly included in Angular project as well.
*/
private static PROJECT_TYPES: IProjectType[] = [
{
type: "Pure JavaScript",
isDefaultProjectType: true
},
{
type: "Angular",
requiredDependencies: ["@angular/core", "nativescript-angular"]
},
{
type: "Pure TypeScript",
requiredDependencies: ["typescript", "nativescript-dev-typescript"]
}
];
public projectDir: string;
public platformsDir: string;
public projectFilePath: string;
public projectId: string;
public projectName: string;
public appDirectoryPath: string;
public appResourcesDirectoryPath: string;
public dependencies: any;
public devDependencies: IStringDictionary;
public projectType: string;
constructor(private $fs: IFileSystem,
private $errors: IErrors,
private $logger: ILogger,
private $projectHelper: IProjectHelper,
private $staticConfig: IStaticConfig,
private $options: IOptions) {
this.initializeProjectData();
}
private initializeProjectData(): void {
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)) {
let fileContent: any = null;
try {
fileContent = this.$fs.readJson(this.projectFilePath);
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;
this.devDependencies = fileContent.devDependencies;
this.projectType = this.getProjectType();
} else { // This is the case when we have package.json file but nativescipt key is not presented in it
this.tryToUpgradeProject();
}
}
} else { // This is the case when no project file found
this.tryToUpgradeProject();
}
}
private getProjectType(): string {
let detectedProjectType = _.find(ProjectData.PROJECT_TYPES, (projectType) => projectType.isDefaultProjectType).type;
const deps: string[] = _.keys(this.dependencies).concat(_.keys(this.devDependencies));
_.each(ProjectData.PROJECT_TYPES, projectType => {
if (_.some(projectType.requiredDependencies, requiredDependency => deps.indexOf(requiredDependency) !== -1)) {
detectedProjectType = projectType.type;
return false;
}
});
return detectedProjectType;
}
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(): void {
let projectDir = this.projectDir || path.resolve(this.$options.path || ".");
let oldProjectFilePath = path.join(projectDir, ProjectData.OLD_PROJECT_FILE_NAME);
if (this.$fs.exists(oldProjectFilePath)) {
this.upgrade(projectDir, oldProjectFilePath);
} else {
this.throwNoProjectFoundError();
}
}
private upgrade(projectDir: string, oldProjectFilePath: string): void {
try {
let oldProjectData = this.$fs.readJson(oldProjectFilePath);
let newProjectFilePath = this.projectFilePath || path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
let newProjectData = this.$fs.exists(newProjectFilePath) ? this.$fs.readJson(newProjectFilePath) : {};
newProjectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE] = oldProjectData;
this.$fs.writeJson(newProjectFilePath, newProjectData);
this.projectId = newProjectData[this.$staticConfig.CLIENT_NAME_KEY_IN_PROJECT_FILE].id;
this.$fs.deleteFile(oldProjectFilePath);
} 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.");
}
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);