-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmigrate-to-collections.ts
173 lines (159 loc) · 4.55 KB
/
migrate-to-collections.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
import fs from 'fs';
import { kebabCase } from 'lodash';
import { Readable } from 'stream';
import { finished } from 'stream/promises';
// 1. automate it to handle every single directory for all the different packages we just deleted
// 1a. handle tag definitions
// 2. fetch all the images for all the entries and store them
(async () => {
const DATA_PACKS = [
'angular',
'deno',
'graphql',
'nodejs',
'qwik',
'react',
'solidjs',
'svelte',
'vue',
];
const COLLECTION_TYPE = {
blogs: {
collection: 'blogs',
tags: 'blogTags',
fileNameGn: (item) => kebabCase(`${item.title.toLowerCase()}`),
},
books: {
collection: 'books',
tags: 'bookTags',
fileNameGn: (item) =>
kebabCase(
`${item.title.toLowerCase()} ${item.authors.join(' ').toLowerCase()}`,
),
},
communities: {
collection: 'communities',
tags: 'communityTags',
fileNameGn: (item) => kebabCase(`${item.name.toLowerCase()}`),
},
courses: {
collection: 'courses',
tags: 'courseTags',
fileNameGn: (item) =>
kebabCase(`${item.title.toLowerCase()} ${item.author.toLowerCase()}`),
},
libraries: {
collection: 'libraries',
tags: null,
fileNameGn: (item) => kebabCase(`${item.name.toLowerCase()}`),
},
podcasts: {
collection: 'podcasts',
tags: 'podcastTags',
fileNameGn: (item) => kebabCase(`${item.title.toLowerCase()}`),
},
tools: {
collection: 'tools',
tags: 'toolTags',
fileNameGn: (item) => kebabCase(`${item.name.toLowerCase()}`),
},
};
const allTags = {};
Object.keys(COLLECTION_TYPE).forEach((type) => {
DATA_PACKS.forEach((pack) => {
fs.mkdirSync(`packages/site/src/content/${pack}-${type}`, {
recursive: true,
});
fs.mkdirSync(`packages/site/src/content/${pack}-${type}/assets`, {
recursive: true,
});
});
});
await Promise.all(
DATA_PACKS.map(async (pack) => {
await Promise.all(
Object.keys(COLLECTION_TYPE).map(async (type) => {
const data = await import(
`./packages/site/src/data/${pack}/${type}.ts`
);
const collection = data[COLLECTION_TYPE[type].collection];
const tags = data[COLLECTION_TYPE[type].tags] ?? [];
if (allTags[pack] === undefined) {
allTags[pack] = {};
}
allTags[pack][type] = tags;
collection.forEach(async (item) => {
try {
const pathName = `packages/site/src/content/${pack}-${type}`;
const fileName = COLLECTION_TYPE[type].fileNameGn(item);
fs.writeFileSync(
`${pathName}/${fileName}.json`,
JSON.stringify({
...item,
image: await storeImageAndGetPath(
item.image,
pathName,
fileName,
),
}),
);
} catch (err) {
console.error(err);
}
});
}),
);
}),
);
})();
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`;
}
// const header = res.headers.get('Content-Disposition');
// const parts = header!.split(';');
// filename = parts[1].split('=')[1];
// let fileType = srcImagePath.split('.').reverse()[0]
// if (!['png', 'jpg', 'jpeg', 'webp', 'svg'].includes(fileType)) {
// console.warn(`${srcImagePath} file is unknown`)
// fileType = 'png'
// }
try {
const response = await fetch(srcImagePath);
let fileType = 'png';
if (response.headers.get('Content-Type')) {
if (response.headers.get('Content-Type').split('/')[0] !== 'image') {
console.warn(
`${srcImagePath} file is unknown; got ${response.headers.get('Content-Type')}`,
);
}
fileType = response.headers.get('Content-Type').split('/')[1];
}
const fileStream = fs.createWriteStream(
`${destPath}/assets/${destFileName}.${fileType}`,
{ flags: 'w' },
);
await finished(Readable.fromWeb(response.body).pipe(fileStream));
// .then(async (res) => {
// const fileStream = fs.createWriteStream(`${destPath}/assets/${destFileName}.${fileType}`, { flags: 'w' });
// await finished(Readable.fromWeb(res.body).pipe(fileStream))
// fs.writeFileSync(
// `${destPath}/assets/${destFileName}.${fileType}`,
// await res.buffer(),
// )
// })
return `./assets/${destFileName}.${fileType}`;
} catch (err) {
console.error(err);
console.error(`Failed to fetch ${srcImagePath}`);
}
}