cd up/and/down
This commit is contained in:
parent
8544327f42
commit
d3504d09f3
|
|
@ -1,26 +1,43 @@
|
||||||
import { join, dirname } from "path"
|
import { dirname, resolve, isAbsolute } from "path"
|
||||||
import { readdirSync } from "fs"
|
import { statSync } from "fs"
|
||||||
import { projectDir } from "@/project"
|
import { projectDir } from "@/project"
|
||||||
import { getState, setState } from "@/session"
|
import { getState, setState } from "@/session"
|
||||||
|
|
||||||
export default async function (path: string) {
|
export default async function (path?: string) {
|
||||||
if (path.endsWith("/")) path = path.slice(0, path.length - 1)
|
const root = projectDir()
|
||||||
const cwd = getState("cwd")
|
const cwd = getState("cwd") || root
|
||||||
const root = cwd || projectDir()
|
|
||||||
|
|
||||||
if (path == "..") {
|
if (!path || path.trim() === "") {
|
||||||
if (root === projectDir()) return
|
setState("cwd", root)
|
||||||
|
|
||||||
setState("cwd", dirname(cwd))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const file of readdirSync(root, { withFileTypes: true })) {
|
if (path.endsWith("/")) path = path.slice(0, -1)
|
||||||
if (file.name === path && file.isDirectory()) {
|
|
||||||
setState("cwd", join(root, file.name))
|
if (path === ".") return
|
||||||
|
if (path === "..") {
|
||||||
|
if (cwd !== root) {
|
||||||
|
const parent = dirname(cwd)
|
||||||
|
if (parent.startsWith(root)) {
|
||||||
|
setState("cwd", parent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const target = isAbsolute(path) ? resolve(path) : resolve(cwd, path)
|
||||||
|
|
||||||
|
if (!target.startsWith(root))
|
||||||
|
return ""
|
||||||
|
|
||||||
|
try {
|
||||||
|
const stat = statSync(target)
|
||||||
|
if (stat.isDirectory()) {
|
||||||
|
setState("cwd", target)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
}
|
}
|
||||||
|
|
||||||
return { error: `${path} doesn't exist` }
|
return { error: `${path} doesn't exist` }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user