-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy-images.mjs
42 lines (34 loc) · 1.19 KB
/
copy-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
import fs from 'fs'
import path from 'path'
import fsExtra from 'fs-extra'
const fsPromises = fs.promises
const targetDir = './public/images/blog'
const postsDir = './posts'
async function copyImagesToPublic(images, slug) {
for (const image of images) {
await fsPromises.copyFile(
`${postsDir}/${slug}/${image}`,
`${targetDir}/${slug}/${image}`
)
}
}
async function createPostImageFoldersForCopy() {
// Get every post folder: post-one, post-two etc.
const postSlugs = await fsPromises.readdir(postsDir)
for (const slug of postSlugs) {
const allowedImageFileExtensions = ['.png', '.jpg', '.jpeg', '.gif']
// Read all files inside current post folder
const postDirFiles = await fsPromises.readdir(`${postsDir}/${slug}`)
// Filter out files with allowed file extension (images)
const images = postDirFiles.filter((file) =>
allowedImageFileExtensions.includes(path.extname(file))
)
if (images.length) {
// Create a folder for images of this post inside public
await fsPromises.mkdir(`${targetDir}/${slug}`)
await copyImagesToPublic(images, slug)
}
}
}
await fsExtra.emptyDir(targetDir)
await createPostImageFoldersForCopy()