forked from Atyantik/react-pwa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudinary.js
48 lines (43 loc) · 1.22 KB
/
cloudinary.js
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
const cloudinary = require('cloudinary').v2;
const fs = require('fs');
const path = require('path');
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
const walkSync = (dir, fileList = [], rootDir = '') => {
const files = fs.readdirSync(dir);
files.forEach((file) => {
if (fs.statSync(`${dir}/${file}`).isDirectory()) {
// eslint-disable-next-line
fileList = walkSync(dir + '/' + file, fileList, rootDir);
} else {
fileList.push(`${dir}/${file}`.replace(rootDir, ''));
}
});
return fileList;
};
const rootDir = path.resolve(path.join(__dirname, 'dist', 'build'));
const cdnFiles = walkSync(rootDir, [], rootDir);
cdnFiles.forEach((file) => {
cloudinary.uploader.upload(path.resolve(path.join(rootDir, file)), {
public_id: file
.replace(/^\//, '')
.split('.')
.slice(0, -1)
.join('.'),
version: 'v1',
use_filename: true,
overwrite: true,
resource_type: 'raw',
}, (error, res) => {
if (error) {
// eslint-disable-next-line
console.log(error);
} else {
// eslint-disable-next-line
console.log(res);
}
});
});