forked from thisdot/framework.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-entry.ts
342 lines (323 loc) · 8.53 KB
/
add-entry.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import fs from 'fs';
import { Readable } from 'stream';
import { finished } from 'stream/promises';
import { exec } from 'node:child_process';
import prompts from 'prompts';
import { kebabCase } from 'lodash';
import { titleCase } from 'title-case';
import { getTagsByType } from '@framework/site/src/utils/tags';
import { CollectionTypes, Frameworks } from '@framework/site/src/types';
type Metadata = {
framework: Frameworks;
collection: CollectionTypes;
};
(async () => {
const metadata: Metadata = await prompts([
{
type: 'autocomplete',
name: 'framework',
message: 'Which framework(s) should we add this entry to?',
choices: [
{ title: 'Angular', value: 'angular' },
{ title: 'Deno', value: 'deno' },
{ title: 'GraphQL', value: 'graphql' },
{ title: 'NodeJS', value: 'nodejs' },
{ title: 'Qwik', value: 'qwik' },
{ title: 'React', value: 'react' },
{ title: 'SolidJS', value: 'solidjs' },
{ title: 'Svelte', value: 'svelte' },
{ title: 'Vue', value: 'vue' },
],
},
{
type: 'autocomplete',
name: 'collection',
message: 'Which collection should we add this entry to?',
choices: [
{ title: 'Blogs', value: 'blogs' },
{ title: 'Books', value: 'books' },
{ title: 'Communities', value: 'communities' },
{ title: 'Courses', value: 'courses' },
{ title: 'Libraries', value: 'libraries' },
{ title: 'Podcasts', value: 'podcasts' },
{ title: 'Tools', value: 'tools' },
],
},
]);
const entryData = await prompts(getPromptsForMetadata(metadata));
try {
const pathName = `packages/site/src/content/${metadata.framework}-${metadata.collection}`;
const fileName = getFileName(metadata, entryData);
fs.writeFileSync(
`${pathName}/${fileName}.json`,
JSON.stringify({
...entryData,
image: await storeImageAndGetPath(entryData.image, pathName, fileName),
}),
);
exec(`yarn format ${pathName}/${fileName}.json`);
} catch (err) {
console.error(err);
}
})();
const AUTHOR_PROMPT = {
type: 'text',
name: 'author',
message: 'Who is the author of this resource?',
};
const LEVEL_PROMPT = {
type: 'select',
name: 'level',
message: 'What level of difficulty is this resource?',
choices: [
{ title: 'Beginner', value: 'beginner' },
{ title: 'Intermediate', value: 'intermediate' },
{ title: 'Advanced', value: 'advanced' },
],
};
const DARK_IMAGE_BACKGROUND_PROMPT = {
type: 'confirm',
name: 'darkImageBackground',
message: 'Is the image an SVG that supports a dark mode switcher?',
initial: false,
};
function getPromptsForMetadata(metadata: Metadata) {
const availableTags = getTagsByType(metadata.collection, metadata.framework)
.filter((tag) => !!tag)
.map((tag) => ({
title: titleCase(tag.toLowerCase()),
value: tag,
}));
const defaultPrompts = [
getLabelPrompt(metadata.collection),
{
type: 'text',
name: 'href',
message: `What is the URL for this resource?`,
},
{
type: 'text',
name: 'image',
message: 'What is the URL for the image for this resource?',
},
{
type: 'text',
name: 'description',
message: 'What is the description for this resource?',
},
{
type: () => (availableTags.length > 0 ? 'multiselect' : null),
name: 'tags',
message: 'How should this resource be tagged?',
choices: availableTags,
hint: '- Space to select. Return to submit',
},
];
switch (metadata.collection) {
case 'blogs':
case 'tools':
return [...defaultPrompts, AUTHOR_PROMPT];
case 'books':
return [
...defaultPrompts,
{
type: 'list',
name: 'authors',
message:
'Who is the book author? Please use a comma to separate multiple authors.',
initial: '',
separator: ',',
},
{
type: 'number',
name: 'yearOfPublication',
message: 'What year was this book published?',
},
{
type: 'number',
name: 'numberOfPages',
message: 'How many pages are in the book?',
},
LEVEL_PROMPT,
];
case 'communities':
return [
...defaultPrompts,
DARK_IMAGE_BACKGROUND_PROMPT,
{
type: 'select',
name: 'type',
message: 'What type of community is this?',
choices: [
{ value: 'Discord' },
{ value: 'Live and Online Events' },
{ value: 'Live Events' },
{ value: 'Online Events' },
{ value: 'Online' },
{ value: 'Reddit' },
],
},
];
case 'courses':
return [
...defaultPrompts,
AUTHOR_PROMPT,
LEVEL_PROMPT,
{
type: 'select',
name: 'paymentType',
message: 'Is this course free or paid?',
choices: [
{ title: 'Free', value: 'free' },
{ title: 'Paid', value: 'paid' },
],
},
{
type: 'select',
name: 'format',
message: 'What format is the course in?',
choices: [
{ title: 'Text', value: 'text' },
{ title: 'Interactive', value: 'interactive' },
{ title: 'Video', value: 'video' },
],
},
];
case 'libraries':
return [
...defaultPrompts,
AUTHOR_PROMPT,
DARK_IMAGE_BACKGROUND_PROMPT,
{
type: 'text',
name: 'repo',
message:
'What is the URL for the repository containing this library? (e.g. https://www.github.com/withastro/astro)',
},
{
type: 'text',
name: 'package',
message:
'What is the public listing URL for this repo on its package manager? (e.g. https://www.npmjs.com/package/astro)',
},
{
type: 'select',
name: 'language',
message: 'What language is this library for?',
choices: [
{ value: 'C#' },
{ value: 'CSS' },
{ value: 'Elixir' },
{ value: 'Go' },
{ value: 'Haskell' },
{ value: 'Java' },
{ value: 'JavaScript' },
{ value: 'NodeJS' },
{ value: 'Python' },
{ value: 'Scala' },
{ value: 'Swift' },
{ value: 'TypeScript' },
{ value: 'unknown' },
],
initial: 6,
},
];
case 'podcasts':
return [
...defaultPrompts,
{
type: 'list',
name: 'hosts',
message:
'Who is the host of the podcast? Please use a comma to separate multiple hosts.',
initial: '',
separator: ',',
},
{
type: 'text',
name: 'rss',
message:
'What is the RSS feed URL for this podcast? (e.g. https://www.podbean.com/site/podcatcher/index/blog/7YqKYcoGcvP)',
},
];
default:
return [...defaultPrompts];
}
}
function getLabelPrompt(collectionType: CollectionTypes) {
if (['books', 'blogs', 'courses', 'podcast'].includes(collectionType)) {
return {
type: 'text',
name: 'title',
message: 'What is the title of the entry?',
};
}
if (['communities', 'libraries', 'podcasts'].includes(collectionType)) {
return {
type: 'text',
name: 'name',
message: 'What is the name of the entry?',
};
}
throw new Error(
`No label prompt found for collection type: ${collectionType}`,
);
}
function getFileName(metadata: Metadata, entryData: any) {
switch (metadata.collection) {
case 'books':
return kebabCase(
`${entryData.title.toLowerCase()} ${entryData.authors.join(' ').toLowerCase()}`,
);
case 'courses':
return kebabCase(
`${entryData.title.toLowerCase()} ${entryData.author.toLowerCase()}`,
);
case 'communities':
case 'libraries':
case 'tools':
return kebabCase(entryData.name.toLowerCase());
case 'blogs':
case 'podcasts':
default:
return kebabCase(entryData.title.toLowerCase());
}
}
async function storeImageAndGetPath(
srcImagePath: string,
destPath: string,
destFileName: string,
) {
// if image is base64 encoded
if (srcImagePath.startsWith('data:image')) {
const base64Data = srcImagePath.replace(/^data:image\/\w+;base64,/, '');
const buffer = Buffer.from(base64Data, 'base64');
const filePath = `${destPath}/assets/${destFileName}.png`;
fs.writeFileSync(filePath, buffer);
return `./assets/${destFileName}.png`;
}
try {
const response = await fetch(srcImagePath);
let fileType = 'png';
const contentType = response.headers.get('Content-Type');
if (contentType) {
if (contentType?.split('/')[0] !== 'image') {
console.warn(`${srcImagePath} file is unknown; got ${contentType}`);
}
fileType = contentType.split('/')[1];
if (fileType === 'svg+xml') {
fileType = 'svg';
}
}
const fileStream = fs.createWriteStream(
`${destPath}/assets/${destFileName}.${fileType}`,
{ flags: 'w' },
);
// @ts-expect-error this definitely works but there's no type for it
await finished(Readable.fromWeb(response.body).pipe(fileStream));
return `./assets/${destFileName}.${fileType}`;
} catch (err) {
console.error(err);
console.error(`Failed to fetch ${srcImagePath}`);
}
}