-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmigration.ts
More file actions
223 lines (177 loc) · 7.01 KB
/
migration.ts
File metadata and controls
223 lines (177 loc) · 7.01 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import * as fs from "fs";
import { join as pathJoin, basename as pathBasename } from "path";
import { assert } from "tsafe/assert";
type LocalizedString = string | { en?: string; fr?: string };
type EducationalResource = {
name: LocalizedString;
abstract: LocalizedString;
/** List must contain at least one author */
authors: [LocalizedString, ...LocalizedString[]];
/** Epoch timestamp, get it for a specific date here: https://www.epochconverter.com */
dateTime?: number;
contributors?: LocalizedString[];
/** Eg: video game, course, tutorial ... */
types: LocalizedString[];
tags: string;
category: string;
keywords?: string[];
imageUrl?: string;
/** Expressed in minutes */
timeRequired?: number;
/** At least one of the following must be specified */
articleUrl?: LocalizedString;
deploymentUrl?: LocalizedString | Record<string /* ide name */, LocalizedString>;
};
type EducationalResourceDirectory = {
name: LocalizedString;
abstract: LocalizedString;
imageUrl?: string;
parts: (EducationalResource | EducationalResourceDirectory)[];
};
(async () => {
const educationalResources: (EducationalResource | EducationalResourceDirectory)[] =
JSON.parse(fs.readFileSync("educationalResources.json").toString("utf8"));
const fileBasenames: string[] = [];
const paths_toDelete = new Set<string>();
for (const er of educationalResources) {
const fileBasename = (() => {
const { name } = er;
const name_en = typeof name === "string" ? name : (name.en ?? name.fr);
assert(name_en !== undefined, "no name");
let fileBasename = name_en;
fileBasename = fileBasename.toLowerCase();
fileBasename = fileBasename.split("_-_")[0];
fileBasename = fileBasename.split("(")[0];
fileBasename = fileBasename.replaceAll(" ", "_");
fileBasename = fileBasename.replaceAll("-", "_");
fileBasename = fileBasename.replaceAll("__", "_");
fileBasename = fileBasename.replaceAll("'", "_");
fileBasename = fileBasename.replaceAll("à", "a");
fileBasename = fileBasename.replaceAll("é", "e");
fileBasename = fileBasename.replaceAll("è", "e");
fileBasename = fileBasename + ".ts";
return fileBasename;
})();
const imageUrls: string[] = [];
(function collectImageUrls(
er: EducationalResource | EducationalResourceDirectory,
) {
root: {
const { imageUrl } = er;
if (imageUrl === undefined) {
break root;
}
if (imageUrls.find(url => url === imageUrl) !== undefined) {
break root;
}
imageUrls.push(imageUrl);
}
if (!("parts" in er)) {
return;
}
for (const er_i of er.parts) {
collectImageUrls(er_i);
}
})(er);
let er_serialized = JSON.stringify(er, null, 2);
{
const dirPath = pathJoin("trainings_and_tutorials", "_assets");
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
const imageImportLines: string[] = [];
for (const imageUrl of imageUrls) {
const { identifierName, fileBasename } = (() => {
const [urlBasename] = imageUrl.split("/").reverse();
const [basename_withoutExt, ext, ...rest] = urlBasename.split(".");
assert(rest.length === 0);
const identifierName = `${basename_withoutExt.replaceAll("-", "_")}_${ext}_url`;
const fileBasename = `${basename_withoutExt.replaceAll("-", "_")}.${ext}`;
return { identifierName, fileBasename };
})();
const fileRelativePath_target = pathJoin(
"trainings_and_tutorials",
"_assets",
pathBasename(fileBasename),
);
if (imageUrl.startsWith("https://")) {
const result = await fetch(imageUrl);
fs.writeFileSync(
fileRelativePath_target,
Buffer.from(await result.arrayBuffer()),
);
} else {
assert(imageUrl.startsWith("/"));
fs.writeFileSync(
fileRelativePath_target,
fs.readFileSync(`.${imageUrl}`),
);
paths_toDelete.add(`.${imageUrl}`);
}
er_serialized = er_serialized.replaceAll(`"${imageUrl}"`, identifierName);
imageImportLines.push(
`import ${identifierName} from "./_assets/${pathBasename(fileRelativePath_target)}";`,
);
}
const isDirectory = "parts" in er;
const typeIdentifier = isDirectory
? "EducationalResourceDirectory"
: "EducationalResource";
fs.writeFileSync(
pathJoin("trainings_and_tutorials", fileBasename),
`
import type { ${typeIdentifier} } from "./__index";
${imageImportLines.join("\n")}
export const ${fileBasename.replace(/\.ts$/, "")}: ${typeIdentifier} = ${er_serialized};
`,
);
fileBasenames.push(fileBasename);
}
paths_toDelete.forEach(filePath => {
fs.rmSync(filePath);
});
fs.writeFileSync(
pathJoin("trainings_and_tutorials", "__index.ts"),
Buffer.from(
`
import type { LocalizedString } from "i18n";
/* spell-checker: disable */
export type EducationalResource = {
name: LocalizedString;
abstract: LocalizedString;
/** List must contain at least one author */
authors: [LocalizedString, ...LocalizedString[]];
/** Epoch timestamp, get it for a specific date here: https://www.epochconverter.com */
dateTime?: number;
contributors?: LocalizedString[];
/** Eg: video game, course, tutorial ... */
types: LocalizedString[];
tags: import("./_tags").EducationalResourceTag[];
category: import("./_categories").EducationalResourceCategory;
keywords?: string[];
imageUrl?: string;
/** Expressed in minutes */
timeRequired?: number;
/** At least one of the following must be specified */
articleUrl?: LocalizedString;
deploymentUrl?: LocalizedString | Record<string /* ide name */, LocalizedString>;
};
export type EducationalResourceDirectory = {
name: LocalizedString;
abstract: LocalizedString;
imageUrl?: string;
parts: (EducationalResource | EducationalResourceDirectory)[];
};
${fileBasenames.map(fileBasename => `import { ${fileBasename.replace(/\.ts$/, "")} } from "./${fileBasename}";`).join("\n")}
export const educationalResources: (
| EducationalResource
| EducationalResourceDirectory
)[] = [
${fileBasenames.map(fileBasename => fileBasename.replace(/\.ts$/, "")).join(",\n")}
];
`,
"utf8",
),
);
})();