Skip to content

Commit 10c07a3

Browse files
fix: Do not fail creation in case we are unable to get template name to be tracked
1 parent 1e96f20 commit 10c07a3

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

lib/services/project-templates-service.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ export class ProjectTemplatesService implements IProjectTemplatesService {
2222

2323
await this.$analyticsService.track("Template used for project creation", templateName);
2424

25-
await this.$analyticsService.trackEventActionInGoogleAnalytics({
26-
action: constants.TrackActionNames.CreateProject,
27-
isForDevice: null,
28-
additionalData: this.getTemplateNameToBeTracked(templateName, realTemplatePath)
29-
});
25+
const templateNameToBeTracked = this.getTemplateNameToBeTracked(templateName, realTemplatePath);
26+
if (templateNameToBeTracked) {
27+
await this.$analyticsService.trackEventActionInGoogleAnalytics({
28+
action: constants.TrackActionNames.CreateProject,
29+
isForDevice: null,
30+
additionalData: templateNameToBeTracked
31+
});
32+
}
3033

3134
// this removes dependencies from templates so they are not copied to app folder
3235
this.$fs.deleteDirectory(path.join(realTemplatePath, constants.NODE_MODULES_FOLDER_NAME));
@@ -48,19 +51,23 @@ export class ProjectTemplatesService implements IProjectTemplatesService {
4851
}
4952

5053
private getTemplateNameToBeTracked(templateName: string, realTemplatePath: string): string {
51-
if (this.$fs.exists(templateName)) {
52-
// local template is used
53-
const pathToPackageJson = path.join(realTemplatePath, constants.PACKAGE_JSON_FILE_NAME);
54-
let templateNameToTrack = path.basename(templateName);
55-
if (this.$fs.exists(pathToPackageJson)) {
56-
const templatePackageJsonContent = this.$fs.readJson(pathToPackageJson);
57-
templateNameToTrack = templatePackageJsonContent.name;
54+
try {
55+
if (this.$fs.exists(templateName)) {
56+
// local template is used
57+
const pathToPackageJson = path.join(realTemplatePath, constants.PACKAGE_JSON_FILE_NAME);
58+
let templateNameToTrack = path.basename(templateName);
59+
if (this.$fs.exists(pathToPackageJson)) {
60+
const templatePackageJsonContent = this.$fs.readJson(pathToPackageJson);
61+
templateNameToTrack = templatePackageJsonContent.name;
62+
}
63+
64+
return `${constants.ANALYTICS_LOCAL_TEMPLATE_PREFIX}${templateNameToTrack}`;
5865
}
5966

60-
return `${constants.ANALYTICS_LOCAL_TEMPLATE_PREFIX}${templateNameToTrack}`;
67+
return templateName;
68+
} catch (err) {
69+
this.$logger.trace(`Unable to get template name to be tracked, error is: ${err}`);
6170
}
62-
63-
return templateName;
6471
}
6572
}
6673
$injector.register("projectTemplatesService", ProjectTemplatesService);

test/project-templates-service.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ describe("project-templates-service", () => {
151151
additionalData: `${constants.ANALYTICS_LOCAL_TEMPLATE_PREFIX}${templateName}`
152152
});
153153
});
154+
155+
it("does not send anything when trying to get template name fails", async () => {
156+
const templateName = "localtemplate";
157+
const localTemplatePath = `/Users/username/${templateName}`;
158+
const fs = testInjector.resolve<IFileSystem>("fs");
159+
fs.exists = (localPath: string): boolean => true;
160+
fs.readJson = (filename: string, encoding?: string): any => {
161+
throw new Error("Unable to read json");
162+
};
163+
164+
await projectTemplatesService.prepareTemplate(localTemplatePath, "tempFolder");
165+
166+
assert.deepEqual(dataSentToGoogleAnalytics, null);
167+
});
154168
});
155169
});
156170
});

0 commit comments

Comments
 (0)