Open History to see the last commit
1import { basenamePath, extnamePath } from './edge-path.js'
2import { hasDrive, stripDrive } from './utils.js'
3
4const BINARY_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.ico']
5
6export function isBinaryFile(path: string): boolean {
7 return BINARY_EXTENSIONS.includes(extnamePath(path))
8}
9
10export function isBase64(content: string): boolean {
11 return content.startsWith('base64::')
12}
13
14export function toCleanPath(absolutePath: string, baseDir: string): string {
15 const normalizedPath = absolutePath.replace(/\\/g, '/')
16 const normalizedBase = baseDir.replace(/\\/g, '/')
17 let cleanPath = normalizedPath
18 if (normalizedPath.startsWith(normalizedBase)) {
19 cleanPath = normalizedPath.slice(normalizedBase.length)
20 } else if (hasDrive(normalizedPath) !== hasDrive(normalizedBase)) {
21 const pathNoDrive = stripDrive(normalizedPath)
22 const baseNoDrive = stripDrive(normalizedBase)
23 if (pathNoDrive.startsWith(baseNoDrive)) {
24 cleanPath = pathNoDrive.slice(baseNoDrive.length)
25 }
26 }
27 if (cleanPath.startsWith('/')) {
28 cleanPath = cleanPath.slice(1)
29 }
30 return cleanPath
31}
32
33export function relativePath(
34 from: string,
35 to: string,
36 stripExtension: boolean = false,
37) {
38 const normalized = from.replace(/\\/g, '/')
39 const cleanedFrom = normalized.startsWith('./')
40 ? normalized.slice(2)
41 : normalized
42 const cleanedTo = to.startsWith('./') ? to.slice(2) : to
43
44 const fromSegments = cleanedFrom.split('/')
45 const toSegments = cleanedTo.split('/')
46
47 fromSegments.pop()
48 toSegments.pop()
49
50 let commonIndex = 0
51 while (
52 commonIndex < fromSegments.length &&
53 commonIndex < toSegments.length &&
54 fromSegments[commonIndex] === toSegments[commonIndex]
55 ) {
56 commonIndex++
57 }
58
59 const upLevels = fromSegments.length - commonIndex
60 const downLevels = toSegments.slice(commonIndex)
61 const target = stripExtension ? to.replace(extnamePath(to), '') : to
62
63 if (upLevels === 0 && downLevels.length === 0) {
64 return `./${basenamePath(target)}`
65 } else if (upLevels === 0 && downLevels.length > 0) {
66 return `./${downLevels.join('/')}/${basenamePath(target)}`
67 } else {
68 const relative = [...Array(upLevels).fill('..'), ...downLevels].join('/')
69 return `${relative}/${basenamePath(target)}`
70 }
71}
72
73export function isDemoFilePath(path?: string): boolean {
74 if (!path) return false
75 const normalized = path.replace(/\\/g, '/')
76
77 if (
78 normalized.includes('/routes/demo/') ||
79 normalized.includes('/routes/example/')
80 ) {
81 return true
82 }
83
84 const filename = normalized.split('/').pop() || ''
85 return (
86 filename.startsWith('demo.') ||
87 filename.startsWith('demo-') ||
88 filename.startsWith('example.') ||
89 filename.startsWith('example-')
90 )
91}
92
93export function cleanUpFiles(
94 files: Record<string, string>,
95 targetDir?: string,
96) {
97 return Object.keys(files).reduce<Record<string, string>>((acc, file) => {
98 if (basenamePath(file) !== '.cta.json') {
99 acc[targetDir ? toCleanPath(file, targetDir) : file] = files[file]
100 }
101 return acc
102 }, {})
103}
104
105export function cleanUpFileArray(files: Array<string>, targetDir?: string) {
106 return files.reduce<Array<string>>((acc, file) => {
107 if (basenamePath(file) !== '.cta.json') {
108 acc.push(targetDir ? toCleanPath(file, targetDir) : file)
109 }
110 return acc
111 }, [])
112}