useSyncAssetsFile.ts
1.81 KB
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
import { useGlobSetting } from "@/hooks/external/setting"
import { SelectOption } from "naive-ui"
import { ProjectRuntimeEnvEnum } from "../../../build/external/envEnum"
export const useSyncAssetsFile = () => {
const { publicPath } = useGlobSetting()
const staticDir = 'static'
const isDev = [ProjectRuntimeEnvEnum.DEV, ProjectRuntimeEnvEnum.DEV_ALONE].includes(import.meta.env.MODE as ProjectRuntimeEnvEnum)
const jpgModules = import.meta.glob('../../assets/**/*.jpg')
const pngModules = import.meta.glob('../../assets/**/*.png')
const imagesModules = { ...jpgModules, ...pngModules }
const getFileInfo = (filePath: string) => {
const filePathArr = filePath.split('/')
const fileName = filePathArr.at(-1)
const fileExt = fileName?.split('.').at(-1)
return { fileName, fileExt }
}
const buildFilePath = (fileExt: string, fileName: string) => {
return `${publicPath}${staticDir}/${fileExt}/${fileName}`
}
const setPath = (filePath: string) => {
if (isDev && filePath) {
const { fileExt, fileName } = getFileInfo(filePath)
return buildFilePath(fileExt!, fileName!)
}
return filePath
}
const getPath = (filePath: string) => {
if (isDev && filePath) {
const { fileName } = getFileInfo(filePath)
const imagesKeys = Object.keys(imagesModules)
const path = imagesKeys.find(key => key.includes(fileName!))
return `/src/${path?.replaceAll('../', '')}`
}
return filePath ?? ''
}
const transformPath = (filesPath: string[]) => {
const _filesPath: SelectOption[] = []
for (const file of filesPath) {
const { fileExt, fileName } = getFileInfo(file)
_filesPath.push({
label: fileName,
value: buildFilePath(fileExt!, fileName!)
})
}
return _filesPath
}
return { setPath, getPath, transformPath }
}