This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
createEnvFile.js
121 lines (103 loc) · 3.19 KB
/
createEnvFile.js
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
const fs = require('fs');
const fetch = require('node-fetch');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const argv = yargs(hideBin(process.argv)).argv;
const credentials = `${argv.email}:${argv.apiKey}`;
const base64credentials = Buffer.from(credentials).toString('base64');
const get = async (endpoint) =>
await fetch(endpoint, {
headers: {
Accept: 'application/vnd.gathercontent.v2+json',
Authorization: `Basic ${base64credentials}`,
},
});
/**
* We currently have no way of identifying fields (e.g. slugs)
* so for now we're searching for fields with a specific label.
*/
const fieldLabelLookup = [
'Hours',
'Weeks',
'Credits',
'Photo of course',
'Course code',
'Core requisites',
'Course summary',
'Taught by',
'Prerequisites',
'Note',
'Total seats',
];
if (!argv.apiKey || !argv.email || !argv.projectId) {
console.log('ERROR: Missing Data');
console.log('You must supply an email, api key and project id.');
return;
}
(async () => {
try {
const templatesResponse = await get(
`https://api.gathercontent.com/projects/${argv.projectId}/templates`
);
const { data } = await templatesResponse.json();
if (!data) {
throw new Error('ERROR: Project templates not found.');
}
const template = data.filter((t) => t.name === 'Course record')[0];
if (!template) {
throw new Error('ERROR: Template not found.');
}
const templateResponse = await get(
`https://api.gathercontent.com/templates/${template.id}`
);
const { related } = await templateResponse.json();
if (!related) {
throw new Error('ERROR: Structure not found.');
}
const fieldsData = related.structure.groups.reduce((acc, group) => {
const fields = group.fields.filter(
(f) => fieldLabelLookup.indexOf(f.label) > -1
);
return !fields.length
? acc
: [
...acc,
...fields.map((fss) => ({
label: `<${fss.label}>`,
uuid: fss.uuid,
})),
];
}, []);
let envString = `GATHERCONTENT_API_USERNAME=${argv.email}
GATHERCONTENT_API_KEY=${argv.apiKey}
GATHERCONTENT_PROJECT_ID=${argv.projectId}
CONTENT_HERO_IMAGE_FIELD_UUID=<Photo of course>
CONTENT_HOURS_FIELD_UUID=<Hours>
CONTENT_WEEKS_FIELD_UUID=<Weeks>
CONTENT_CREDITS_FIELD_UUID=<Credits>
CONTENT_COURSE_CODE_FIELD_UUID=<Course code>
CONTENT_PREREQUISITES_FIELD_UUID=<Prerequisites>
CONTENT_CORE_REQUISITES_FIELD_UUID=<Core requisites>
CONTENT_COURSE_SUMMARY_FIELD_UUID=<Course summary>
CONTENT_NOTE_FIELD_UUID=<Note>
CONTENT_TAUGHT_BY_FIELD_UUID=<Taught by>
CONTENT_TOTAL_SEATS_FIELD_UUID=<Total seats>
`;
fieldsData.map((f) => {
envString = envString.replace(f.label, f.uuid);
});
const missingFields = fieldLabelLookup.filter(
(label) => !fieldsData.some((fd) => fd.label === `<${label}>`)
);
fs.writeFile('.env', envString, () => {
missingFields.map((mf) => {
console.log(
`WARNING: ${mf} is missing. Update the field label in your template to match ${mf}`
);
});
console.log('.env file updated.');
});
} catch (e) {
console.log(e);
}
})();