pathCache.js 3.72 KB
import { PATH_CACHE_KEY } from './constants.js'
import { getDefaultPackageRoots, getRuntimePackageName } 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
    const pkg = getRuntimePackageName()
    return parsed
      .filter(item => item && item.path && !isRestrictedInternalPath(item.path, pkg))
      .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(currentRoot)
  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) {
  const pkg = getRuntimePackageName()
  return (cache || []).filter(
    item => !item.isCurrent && item.path && !isRestrictedInternalPath(item.path, pkg)
  )
}

/**
 * 将默认包根补进已有缓存(切换自定义基座 / 正式包后仍能扫到遗留路径)
 */
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(currentRoot)) {
    const normalized = normalizePath(path)
    if (!normalized || pathSet.has(normalized)) continue
    pathSet.add(normalized)
    list.push({
      path: normalized,
      isCurrent: normalized === normalizedCurrent
    })
  }

  return syncCurrentFlag(list, currentRoot)
}