-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-responsive-images.mjs
121 lines (110 loc) · 3.43 KB
/
generate-responsive-images.mjs
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
import sharp from 'sharp';
import { createReadStream } from 'fs';
import { readFile } from 'fs/promises';
import { normalize, resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = fileURLToPath(dirname(import.meta.url));
const testsJson = JSON.parse(
await readFile(
resolve(
__dirname,
'./src/app/api/tests/tests.json'
)
)
);
const gamesJson = JSON.parse(
await readFile(
resolve(
__dirname,
'./src/app/api/games/games.json'
)
)
);
// Get-ChildItem *@*.webp -Recurse | foreach { Remove-Item -Path $_.FullName }
async function resizePicture(directory, gameId, pathIcon) {
const sharpStream = sharp({ failOn: 'none' });
const readableStream = createReadStream(pathIcon);
// set up pipe
readableStream.pipe(sharpStream)
const promises = [];
const config = [
// generate small picture
{
width: 150,
height: 150,
suffix: "small"
},
// generate medium picture
{
width: 200,
height: 200,
suffix: "medium"
},
// generate big picture
{
width: 250,
height: 250,
suffix: "big"
},
]
for(let setting of config) {
promises.push(
sharpStream
.clone()
.resize({ width: setting.width, height: setting.height, fit: "inside" })
.toFile(`${directory}/${gameId}/cover@${setting.suffix}.webp`)
);
}
return await Promise.all(promises);
}
// For big bang pictures refactoring
async function resizePicturesInFolder() {
const all_games = {
games: {
coversRootPath: "covers",
defaultCoverFile: "cover.webp",
games: gamesJson
},
tests: {
coversRootPath: "testscovers",
defaultCoverFile: "cover.webp",
games: testsJson
}
}
for (let [folderKey, store] of Object.entries(all_games)) {
let directory = normalize(`${__dirname}/public/${ store.coversRootPath }`);
for(let game of store.games) {
const gameId = `${game.playlistId || game.videoId}`
let gameIcon = `${directory}/${gameId}/${ game.coverFile || store.defaultCoverFile }`
try {
await resizePicture(directory, gameId, gameIcon);
console.log(`${game.title} - finished`);
} catch (error) {
console.error(`${folderKey} - Cannot generate responsive images for ${gameId} - ${game.title}`);
}
}
}
}
// For new game / test entry
async function resizePicturesInSingleFolder(folder, game, icon) {
let directory = normalize(`${__dirname}/public/${ folder }`);
let gameIcon = `${directory}/${game}/${ icon }`
try {
await resizePicture(directory, game, gameIcon);
console.log(`${game} - finished`);
} catch (error) {
console.error(`Cannot generate responsive images for ${game}`);
}
}
// Main
const args = process.argv.slice(2);
switch (args[0]) {
case 'singleGame':
console.log("Resize single game");
const [_, gameId, folder = "covers", icon = "cover.jpg", ...rest] = args;
resizePicturesInSingleFolder(folder, gameId, icon);
break;
default:
console.log("Resize all pictures ....");
resizePicturesInFolder();
}