skillhub/cli/src/platform/paths.ts
betterlmy f519b08a73 fix(cli): preflight canonical install targets
Signed-off-by: betterlmy <betterlmy@icloud.com>
2026-07-17 17:20:22 +08:00

40 lines
1.1 KiB
TypeScript

export function userStateDir(home = process.env.HOME || process.env.USERPROFILE || ''): string {
if (!home) {
throw new Error('Cannot resolve user home directory')
}
return `${home.replace(/\\/g, '/')}/.skillhub`
}
export function joinPath(...parts: string[]): string {
return parts.join('/').replace(/\/+/g, '/')
}
export async function ensureDir(dir: string): Promise<void> {
const { mkdir } = await import('node:fs/promises')
await mkdir(dir, { recursive: true })
}
export async function pathExists(path: string): Promise<boolean> {
const { access } = await import('node:fs/promises')
try {
await access(path)
return true
} catch {
return false
}
}
export async function canonicalizeExistingPath(path: string): Promise<string> {
const { realpath } = await import('node:fs/promises')
try {
return await realpath(path)
} catch {
return path
}
}
export async function applyCredentialPermissions(path: string): Promise<void> {
if (process.platform === 'win32') return
const { chmod } = await import('node:fs/promises')
await chmod(path, 0o600)
}