pathResolver.js
1.77 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
import { PathUtils } from '@/utils/recordManager/index.js'
import { DB_FILE } from './constants.js'
import { normalizePath } from './pathCache.js'
import { fileExists } from './fileIo.js'
import { getDefaultPackageRoots } from './packageName.js'
export { getDefaultPackageRoots, getRuntimePackageName, getPackageNamesForMerge } from './packageName.js'
export function getAppId() {
if (typeof plus !== 'undefined' && plus.runtime && plus.runtime.appid) {
return plus.runtime.appid
}
return '__UNI__D3BD3DF'
}
/**
* 从 doc 目录 fullPath 推导包根目录
*/
export function derivePackageRootFromDocPath(docPath) {
const normalized = normalizePath(docPath)
if (!normalized) return ''
const appId = getAppId()
const docSuffix = `/apps/${appId}/doc`
if (normalized.endsWith(docSuffix)) {
return normalized.substring(0, normalized.length - docSuffix.length)
}
const appsMarker = '/apps/'
const appsIdx = normalized.indexOf(appsMarker)
if (appsIdx > 0) {
return normalized.substring(0, appsIdx)
}
if (normalized.endsWith('/doc')) {
return normalized.substring(0, normalized.length - 4)
}
return normalized
}
export async function getCurrentPackageRoot() {
const docPath = await PathUtils.getFilePath()
return derivePackageRootFromDocPath(docPath)
}
/**
* 包根下 zyzs.db 的候选绝对路径(无 file:// 前缀)
*/
export function resolveDbFilePaths(packageRoot) {
const root = normalizePath(packageRoot)
const appId = getAppId()
return [
`${root}/${DB_FILE}`,
`${root}/apps/${appId}/doc/${DB_FILE}`
]
}
export async function findDbInPackageRoot(packageRoot) {
const candidates = resolveDbFilePaths(packageRoot)
for (const path of candidates) {
if (await fileExists(path)) {
return path
}
}
return null
}