Skip to content

Commit 2d0e55f

Browse files
committed
fix: downloading
1 parent f0f44b3 commit 2d0e55f

3 files changed

Lines changed: 9 additions & 8 deletions

File tree

public/resources/data/avatars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

public/resources/data/status.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

scripts/downloadImages.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ const effects = new Set([
2121
]);
2222

2323
async function updateFile(path, data) {
24-
// Create blank if not exists
25-
await fs.writeFile(path, Array.isArray(data) ? [] : {}, { flag: 'wx' });
26-
2724
// Load existing
2825
const file = await fs.readFile(path);
2926
const existing = JSON.parse(file.toString());
@@ -45,23 +42,25 @@ async function download(url, file) {
4542
try {
4643
const image = await fetch(url);
4744
await fs.writeFile(file, image.body);
48-
} catch {
49-
console.error('Failed to save', basename(file));
45+
} catch (e) {
46+
console.error('Failed to save', basename(file), e.message || e);
5047
}
5148
}
5249

5350
async function downloadAvatars(images) {
5451
const path = join(base, 'images', 'avatars');
55-
for (const name in images) {
52+
await fs.mkdir(path, { recursive: true });
53+
for (const name of images) {
5654
const url = `https://undercards.net/images/cards/${name}.png`;
5755
await download(url, resolve(path, `${name}.png`));
5856
}
5957
}
6058

6159
async function downloadEffects() {
6260
const path = join(base, 'images', 'effects');
61+
await fs.mkdir(path, { recursive: true });
6362
const values = effects.values();
64-
for (const effect in values) {
63+
for (const effect of values) {
6564
const url = `https://undercards.net/images/powers/${effect}.png`;
6665
await download(url, resolve(path, `${effect}.png`));
6766
}
@@ -76,7 +75,7 @@ fetch('https://undercards.net/AllCards')
7675
})))
7776
.then((avatars) => Promise.all([
7877
updateFile(resolve(base, 'data', 'avatars.json'), avatars),
79-
updateFile(resolve(base, 'data', 'effects.json'), effects.values()),
78+
updateFile(resolve(base, 'data', 'status.json'), effects.values()),
8079
downloadAvatars(Object.values(avatars)),
8180
downloadEffects(),
8281
]));

0 commit comments

Comments
 (0)