-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-templates-service.ts
73 lines (60 loc) · 3.1 KB
/
project-templates-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
import * as path from "path";
import * as temp from "temp";
import * as constants from "../constants";
temp.track();
export class ProjectTemplatesService implements IProjectTemplatesService {
public constructor(private $analyticsService: IAnalyticsService,
private $fs: IFileSystem,
private $logger: ILogger,
private $npmInstallationManager: INpmInstallationManager) { }
public async prepareTemplate(originalTemplateName: string, projectDir: string): Promise<string> {
// support <reserved_name>@<version> syntax
const data = originalTemplateName.split("@"),
name = data[0],
version = data[1];
const templateName = constants.RESERVED_TEMPLATE_NAMES[name.toLowerCase()] || name;
const realTemplatePath = await this.prepareNativeScriptTemplate(templateName, version, projectDir);
await this.$analyticsService.track("Template used for project creation", templateName);
const templateNameToBeTracked = this.getTemplateNameToBeTracked(templateName, realTemplatePath);
if (templateNameToBeTracked) {
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: constants.TrackActionNames.CreateProject,
isForDevice: null,
additionalData: templateNameToBeTracked
});
}
// this removes dependencies from templates so they are not copied to app folder
this.$fs.deleteDirectory(path.join(realTemplatePath, constants.NODE_MODULES_FOLDER_NAME));
return realTemplatePath;
}
/**
* Install verified NativeScript template in the npm cache.
* The "special" here is that npmInstallationManager will check current CLI version and will instal best matching version of the template.
* For example in case CLI is version 10.12.8, npmInstallationManager will try to find latest 10.12.x version of the template.
* @param {string} templateName The name of the verified NativeScript template.
* @param {string} version The version of the template specified by user.
* @return {string} Path to the directory where the template is installed.
*/
private async prepareNativeScriptTemplate(templateName: string, version?: string, projectDir?: string): Promise<string> {
this.$logger.trace(`Using NativeScript verified template: ${templateName} with version ${version}.`);
return this.$npmInstallationManager.install(templateName, projectDir, { version: version, dependencyType: "save" });
}
private getTemplateNameToBeTracked(templateName: string, realTemplatePath: string): string {
try {
if (this.$fs.exists(templateName)) {
// local template is used
const pathToPackageJson = path.join(realTemplatePath, constants.PACKAGE_JSON_FILE_NAME);
let templateNameToTrack = path.basename(templateName);
if (this.$fs.exists(pathToPackageJson)) {
const templatePackageJsonContent = this.$fs.readJson(pathToPackageJson);
templateNameToTrack = templatePackageJsonContent.name;
}
return `${constants.ANALYTICS_LOCAL_TEMPLATE_PREFIX}${templateNameToTrack}`;
}
return templateName;
} catch (err) {
this.$logger.trace(`Unable to get template name to be tracked, error is: ${err}`);
}
}
}
$injector.register("projectTemplatesService", ProjectTemplatesService);