-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildPacks.ts
107 lines (84 loc) · 3.18 KB
/
buildPacks.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
import * as fs from "fs";
import * as path from "path";
import * as chalk from "chalk";
import { StickersCollection, StickersCollectionItem } from "./src/types/stickersCollection";
import { v2 as cloudinary } from "cloudinary";
import { config } from "dotenv";
/*
* Script for build packs of stickers
* Make json list of packs and resize images
* */
type FilesList = {
folder: string;
file: string;
}[];
const imagesSize = 250;
const allowedExtensions = [".jpg", ".jpeg", ".svg", ".png", ".webp"];
(async () => {
if (config()?.error) {
return console.log(chalk.red("Error while parse .env"));
}
cloudinary.config({
cloud_name: process.env.CLOUDINARY_NAME,
api_key: process.env.CLOUDINARY_KEY,
api_secret: process.env.CLOUDINARY_SECRET,
});
const stickerPacksPath = path.join(process.cwd(), "stickerPacks/sources");
const itemsInFolder = await fs.promises.readdir(stickerPacksPath);
const filesList: FilesList = [];
for await (const folder of itemsInFolder) {
const folderPath = path.join(stickerPacksPath, folder);
const stat = await fs.promises.stat(folderPath);
if (!stat.isDirectory()) continue;
const filesInDir = await fs.promises.readdir(folderPath);
if (!filesInDir.length) continue;
for await (const file of filesInDir) {
const filePath = path.join(folderPath, file);
const stat = await fs.promises.stat(filePath);
if (!stat.isFile()) continue;
if (!allowedExtensions.includes(path.extname(filePath))) continue;
filesList.push({ folder, file });
}
}
let packs: StickersCollection = [...new Set(filesList.map(({ folder }) => folder))].map(
(folder): StickersCollectionItem => ({
name: folder,
images: [],
}),
);
const progress = require("cli-progress");
const progressBar = new progress.SingleBar({}, progress.Presets.rect);
console.log(chalk.green("Upload stickers to cloudinary"));
progressBar.start(filesList.length, 0, {
speed: "N/A",
});
for await (const { folder, file } of filesList) {
const response = await cloudinary.uploader
.upload(path.join(stickerPacksPath, folder, file), {
// Если не вырезать расширение файла cloudinary.url вернёт кривой url
public_id: `${folder}/${file.split(".").slice(0, -1).join("")}`,
})
.catch(console.error);
if (!response) {
return console.log(chalk.red("Error loading image, empty response"));
}
packs = packs.map(pack => {
pack.name === folder &&
pack.images.push(
cloudinary
.url(response.public_id, { width: 250, crop: "fill" })
.replace("http://", "https://"), // lol, и никакой настройки чтобы отдавался сразу http не нашёл
);
return pack;
});
progressBar.increment();
}
progressBar.stop();
const buildPath = path.join(process.cwd(), "public/build");
const accessForBuildDir = await fs.promises.access(buildPath).then(
() => true,
() => false,
);
accessForBuildDir || (await fs.promises.mkdir(buildPath));
await fs.promises.writeFile(path.join(buildPath, "packs.json"), JSON.stringify(packs), "utf-8");
})();