-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-service.ts
187 lines (149 loc) · 7.91 KB
/
project-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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import * as constants from "../constants";
import * as path from "path";
import * as shelljs from "shelljs";
import { exported } from "../common/decorators";
export class ProjectService implements IProjectService {
constructor(private $npm: INodePackageManager,
private $errors: IErrors,
private $fs: IFileSystem,
private $logger: ILogger,
private $projectData: IProjectData,
private $projectDataService: IProjectDataService,
private $projectHelper: IProjectHelper,
private $projectNameService: IProjectNameService,
private $projectTemplatesService: IProjectTemplatesService,
private $staticConfig: IStaticConfig) { }
@exported("projectService")
public async createProject(projectOptions: IProjectSettings): Promise<void> {
let projectName = projectOptions.projectName,
selectedTemplate = projectOptions.template;
if (!projectName) {
this.$errors.fail("You must specify <App name> when creating a new project.");
}
projectName = await this.$projectNameService.ensureValidName(projectName, { force: projectOptions.force });
const selectedPath = path.resolve(projectOptions.pathToProject || ".");
const projectDir = path.join(selectedPath, projectName);
this.$fs.createDirectory(projectDir);
if (this.$fs.exists(projectDir) && !this.$fs.isEmptyDir(projectDir)) {
this.$errors.fail("Path already exists and is not empty %s", projectDir);
}
let projectId = projectOptions.appId || this.$projectHelper.generateDefaultAppId(projectName, constants.DEFAULT_APP_IDENTIFIER_PREFIX);
this.createPackageJson(projectDir, projectId);
this.$logger.trace(`Creating a new NativeScript project with name ${projectName} and id ${projectId} at location ${projectDir}`);
if (!selectedTemplate) {
selectedTemplate = constants.RESERVED_TEMPLATE_NAMES["default"];
}
try {
let templatePath = await this.$projectTemplatesService.prepareTemplate(selectedTemplate, projectDir);
await this.extractTemplate(projectDir, templatePath);
await this.ensureAppResourcesExist(projectDir);
let packageName = constants.TNS_CORE_MODULES_NAME;
await this.$npm.install(packageName, projectDir, { save: true, "save-exact": true });
let templatePackageJsonData = this.getDataFromJson(templatePath);
this.mergeProjectAndTemplateProperties(projectDir, templatePackageJsonData); //merging dependencies from template (dev && prod)
this.removeMergedDependencies(projectDir, templatePackageJsonData);
await this.$npm.install(projectDir, projectDir, { "ignore-scripts": projectOptions.ignoreScripts });
let templatePackageJson = this.$fs.readJson(path.join(templatePath, "package.json"));
await this.$npm.uninstall(templatePackageJson.name, { save: true }, projectDir);
} catch (err) {
this.$fs.deleteDirectory(projectDir);
throw err;
}
this.$logger.printMarkdown("Project `%s` was successfully created.", projectName);
}
@exported("projectService")
public isValidNativeScriptProject(pathToProject?: string): boolean {
try {
this.$projectData.initializeProjectData(pathToProject);
} catch (e) {
return false;
}
return true;
}
private getDataFromJson(templatePath: string): any {
let templatePackageJsonPath = path.join(templatePath, constants.PACKAGE_JSON_FILE_NAME);
if (this.$fs.exists(templatePackageJsonPath)) {
let templatePackageJsonData = this.$fs.readJson(templatePackageJsonPath);
return templatePackageJsonData;
} else {
this.$logger.trace(`Template ${templatePath} does not have ${constants.PACKAGE_JSON_FILE_NAME} file.`);
}
return null;
}
private async extractTemplate(projectDir: string, realTemplatePath: string): Promise<void> {
this.$fs.ensureDirectoryExists(projectDir);
let appDestinationPath = path.join(projectDir, constants.APP_FOLDER_NAME);
this.$fs.createDirectory(appDestinationPath);
this.$logger.trace(`Copying application from '${realTemplatePath}' into '${appDestinationPath}'.`);
shelljs.cp('-R', path.join(realTemplatePath, "*"), appDestinationPath);
this.$fs.createDirectory(path.join(projectDir, "platforms"));
}
private async ensureAppResourcesExist(projectDir: string): Promise<void> {
let appPath = path.join(projectDir, constants.APP_FOLDER_NAME),
appResourcesDestinationPath = path.join(appPath, constants.APP_RESOURCES_FOLDER_NAME);
if (!this.$fs.exists(appResourcesDestinationPath)) {
this.$fs.createDirectory(appResourcesDestinationPath);
// the template installed doesn't have App_Resources -> get from a default template
let defaultTemplateName = constants.RESERVED_TEMPLATE_NAMES["default"];
await this.$npm.install(defaultTemplateName, projectDir, { save: true, });
let defaultTemplateAppResourcesPath = path.join(projectDir, constants.NODE_MODULES_FOLDER_NAME,
defaultTemplateName, constants.APP_RESOURCES_FOLDER_NAME);
if (this.$fs.exists(defaultTemplateAppResourcesPath)) {
shelljs.cp('-R', defaultTemplateAppResourcesPath, appPath);
}
await this.$npm.uninstall(defaultTemplateName, { save: true }, projectDir);
}
}
private removeMergedDependencies(projectDir: string, templatePackageJsonData: any): void {
let extractedTemplatePackageJsonPath = path.join(projectDir, constants.APP_FOLDER_NAME, constants.PACKAGE_JSON_FILE_NAME);
for (let key in templatePackageJsonData) {
if (constants.PackageJsonKeysToKeep.indexOf(key) === -1) {
delete templatePackageJsonData[key];
}
}
this.$logger.trace("Deleting unnecessary information from template json.");
this.$fs.writeJson(extractedTemplatePackageJsonPath, templatePackageJsonData);
}
private mergeProjectAndTemplateProperties(projectDir: string, templatePackageJsonData: any): void {
if (templatePackageJsonData) {
let projectPackageJsonPath = path.join(projectDir, constants.PACKAGE_JSON_FILE_NAME);
let projectPackageJsonData = this.$fs.readJson(projectPackageJsonPath);
this.$logger.trace("Initial project package.json data: ", projectPackageJsonData);
if (projectPackageJsonData.dependencies || templatePackageJsonData.dependencies) {
projectPackageJsonData.dependencies = this.mergeDependencies(projectPackageJsonData.dependencies, templatePackageJsonData.dependencies);
}
if (projectPackageJsonData.devDependencies || templatePackageJsonData.devDependencies) {
projectPackageJsonData.devDependencies = this.mergeDependencies(projectPackageJsonData.devDependencies, templatePackageJsonData.devDependencies);
}
this.$logger.trace("New project package.json data: ", projectPackageJsonData);
this.$fs.writeJson(projectPackageJsonPath, projectPackageJsonData);
} else {
this.$errors.failWithoutHelp(`Couldn't find package.json data in installed template`);
}
}
private mergeDependencies(projectDependencies: IStringDictionary, templateDependencies: IStringDictionary): IStringDictionary {
// Cast to any when logging as logger thinks it can print only string.
// Cannot use toString() because we want to print the whole objects, not [Object object]
this.$logger.trace("Merging dependencies, projectDependencies are: ", <any>projectDependencies, " templateDependencies are: ", <any>templateDependencies);
projectDependencies = projectDependencies || {};
_.extend(projectDependencies, templateDependencies || {});
let sortedDeps: IStringDictionary = {};
let dependenciesNames = _.keys(projectDependencies).sort();
_.each(dependenciesNames, (key: string) => {
sortedDeps[key] = projectDependencies[key];
});
this.$logger.trace("Sorted merged dependencies are: ", <any>sortedDeps);
return sortedDeps;
}
private createPackageJson(projectDir: string, projectId: string): void {
const projectFilePath = path.join(projectDir, this.$staticConfig.PROJECT_FILE_NAME);
this.$fs.writeJson(projectFilePath, {
"description": "NativeScript Application",
"license": "SEE LICENSE IN <your-license-filename>",
"readme": "NativeScript Application",
"repository": "<fill-your-repository-here>"
});
this.$projectDataService.setNSValue(projectDir, "id", projectId);
}
}
$injector.register("projectService", ProjectService);