pathCache.js
3.59 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { PATH_CACHE_KEY } from './constants.js'
import { getDefaultPackageRoots } from './packageName.js'
import { isRestrictedInternalPath } from './pathAccess.js'
export function normalizePath(path) {
if (!path) return ''
let p = String(path).trim()
if (p.startsWith('file://')) {
p = p.replace(/^file:\/\//, '')
}
return p.replace(/\/+$/, '')
}
export function loadPathCache() {
try {
const raw = uni.getStorageSync(PATH_CACHE_KEY)
if (!raw) return null
const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw
if (!Array.isArray(parsed)) return null
return parsed
.filter(item => item && item.path && !isRestrictedInternalPath(item.path))
.map(item => ({
path: normalizePath(item.path),
isCurrent: !!item.isCurrent
}))
} catch (e) {
return null
}
}
export function savePathCache(cache) {
uni.setStorageSync(PATH_CACHE_KEY, JSON.stringify(cache))
}
export function createDefaultCache(currentRoot) {
const roots = getDefaultPackageRoots()
const normalizedCurrent = normalizePath(currentRoot)
const seen = new Set()
const cache = []
for (const path of roots) {
const normalized = normalizePath(path)
if (!normalized || seen.has(normalized)) continue
seen.add(normalized)
cache.push({
path: normalized,
isCurrent: normalized === normalizedCurrent
})
}
if (normalizedCurrent && !seen.has(normalizedCurrent)) {
cache.push({ path: normalizedCurrent, isCurrent: true })
cache.forEach(item => {
if (item.path !== normalizedCurrent) item.isCurrent = false
})
}
if (!cache.some(item => item.isCurrent) && cache.length > 0) {
const match = cache.find(item => item.path === normalizedCurrent)
if (match) {
match.isCurrent = true
} else if (normalizedCurrent) {
cache.push({ path: normalizedCurrent, isCurrent: true })
cache.forEach(item => {
if (item.path !== normalizedCurrent) item.isCurrent = false
})
} else {
cache[0].isCurrent = true
}
}
return cache
}
/**
* 同步 CurrentPath 与 PathCache 的 isCurrent 标记
*/
export function syncCurrentFlag(cache, currentRoot) {
const normalizedCurrent = normalizePath(currentRoot)
if (!normalizedCurrent) return cache
let list = Array.isArray(cache) ? [...cache] : []
const existing = list.find(item => normalizePath(item.path) === normalizedCurrent)
if (existing) {
list = list.map(item => ({
...item,
path: normalizePath(item.path),
isCurrent: normalizePath(item.path) === normalizedCurrent
}))
} else {
list = list.map(item => ({
...item,
path: normalizePath(item.path),
isCurrent: false
}))
list.push({ path: normalizedCurrent, isCurrent: true })
}
return list
}
export function getNonCurrentPaths(cache) {
return (cache || []).filter(
item => !item.isCurrent && item.path && !isRestrictedInternalPath(item.path)
)
}
/**
* 将默认包根补进已有缓存(切换自定义基座 / 正式包后仍能扫到遗留路径)
*/
export function ensureDefaultRootsInCache(cache, currentRoot) {
const normalizedCurrent = normalizePath(currentRoot)
let list = Array.isArray(cache) ? cache.map(item => ({ ...item })) : []
const pathSet = new Set(list.map(item => normalizePath(item.path)))
for (const path of getDefaultPackageRoots()) {
const normalized = normalizePath(path)
if (!normalized || pathSet.has(normalized)) continue
pathSet.add(normalized)
list.push({
path: normalized,
isCurrent: normalized === normalizedCurrent
})
}
return syncCurrentFlag(list, currentRoot)
}