Repository navigation
Expand file tree
/
Copy pathInAppTutorial.js
More file actions
61 lines (56 loc) · 1.72 KB
/
Copy pathInAppTutorial.js
File metadata and controls
61 lines (56 loc) · 1.72 KB
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
// @ts-check
const fs = require('fs');
const path = require('path');
/**
* @typedef {import("../types").InAppTutorialShortHeader} InAppTutorialShortHeader
*/
class InAppTutorial {
/** @type {string} */
sourcePath;
/** @type {string} */
id;
/** @type {Array<string>} */
availableLocales;
/** @type {string | undefined} */
initialTemplateUrl;
/** @type { Record<string, string> | undefined} */
initialProjectData;
/**
* @param {string} sourcePath
*/
constructor(sourcePath) {
this.sourcePath = sourcePath;
try {
const tutorialContent = JSON.parse(fs.readFileSync(sourcePath, 'utf-8'));
const sourceFileName = path.parse(this.sourcePath).name;
if (tutorialContent.id !== sourceFileName) {
throw new Error(
`the in app tutorial with ${tutorialContent.id} is saved under a file that does not share the same name (${sourceFileName})`
);
}
this.id = tutorialContent.id;
this.availableLocales = tutorialContent.availableLocales;
this.initialTemplateUrl = tutorialContent.initialTemplateUrl;
this.initialProjectData = tutorialContent.initialProjectData;
} catch (error) {
console.error(
`An error occurred when reading tutorial file with path ${sourcePath}. The file might be corrupt.`,
error
);
throw error;
}
}
/**
* @returns {InAppTutorialShortHeader}
*/
buildShortHeader() {
return {
id: this.id,
contentUrl: `https://resources.gdevelop-app.com/in-app-tutorials/${this.id}.json`,
availableLocales: this.availableLocales,
initialTemplateUrl: this.initialTemplateUrl,
initialProjectData: this.initialProjectData,
};
}
}
module.exports = { InAppTutorial };