-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathproject-templates-service.ts
147 lines (130 loc) · 3.69 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
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 path from "path";
import * as semver from "semver";
import * as constants from "../constants";
import { performanceLog } from "../common/decorators";
import {
IProjectTemplatesService,
ITemplateData,
ITemplatePackageJsonContent,
} from "../definitions/project";
import {
IPackageInstallationManager,
INodePackageManager,
IStaticConfig,
} from "../declarations";
import {
IFileSystem,
IAnalyticsService,
IDictionary,
} from "../common/declarations";
import { injector } from "../common/yok";
export class ProjectTemplatesService implements IProjectTemplatesService {
private templatePackageContents: IDictionary<any> = {};
public constructor(
private $analyticsService: IAnalyticsService,
private $fs: IFileSystem,
private $logger: ILogger,
private $packageInstallationManager: IPackageInstallationManager,
private $pacoteService: IPacoteService,
private $packageManager: INodePackageManager,
private $staticConfig: IStaticConfig
) {}
@performanceLog()
public async prepareTemplate(
templateValue: string,
projectDir: string
): Promise<ITemplateData> {
if (!templateValue) {
templateValue = constants.RESERVED_TEMPLATE_NAMES["default"];
}
const templateNameParts = await this.$packageManager.getPackageNameParts(
templateValue
);
templateValue =
constants.RESERVED_TEMPLATE_NAMES[templateNameParts.name] ||
templateNameParts.name;
const version = await this.getDesiredVersion(
templateValue,
templateNameParts.version
);
const fullTemplateName = await this.$packageManager.getPackageFullName({
name: templateValue,
version: version,
});
const templatePackageJsonContent = await this.getTemplatePackageJsonContent(
fullTemplateName
);
const templateNameToBeTracked = this.getTemplateNameToBeTracked(
templateValue,
templatePackageJsonContent
);
if (templateNameToBeTracked) {
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: constants.TrackActionNames.CreateProject,
isForDevice: null,
additionalData: templateNameToBeTracked,
});
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: constants.TrackActionNames.UsingTemplate,
additionalData: templateNameToBeTracked,
});
}
return {
templateName: templateValue,
templatePackageJsonContent,
version,
};
}
private async getTemplatePackageJsonContent(
templateName: string
): Promise<ITemplatePackageJsonContent> {
if (!this.templatePackageContents[templateName]) {
this.templatePackageContents[
templateName
] = await this.$pacoteService.manifest(templateName, {
fullMetadata: true,
});
}
return this.templatePackageContents[templateName];
}
private getTemplateNameToBeTracked(
templateName: string,
packageJsonContent: any
): string {
try {
if (this.$fs.exists(templateName)) {
const templateNameToBeTracked =
(packageJsonContent && packageJsonContent.name) ||
path.basename(templateName);
return `${constants.ANALYTICS_LOCAL_TEMPLATE_PREFIX}${templateNameToBeTracked}`;
}
return templateName;
} catch (err) {
this.$logger.trace(
`Unable to get template name to be tracked, error is: ${err}`
);
}
}
private async getDesiredVersion(
templateName: string,
defaultVersion?: string
) {
if (defaultVersion) {
return defaultVersion;
}
if (this.$fs.exists(templateName)) {
return "";
}
try {
const cliMajorVersion = semver.parse(
semver.coerce(this.$staticConfig.version)
).major;
return `^${cliMajorVersion}.0.0`;
} catch (err) {
return this.$packageInstallationManager.getLatestCompatibleVersionSafe(
templateName
);
}
}
}
injector.register("projectTemplatesService", ProjectTemplatesService);