26 lines
678 B
TypeScript
26 lines
678 B
TypeScript
import { join, dirname } from "path"
|
|
import { readdirSync } from "fs"
|
|
import { projectDir } from "@/project"
|
|
import { getState, setState } from "@/session"
|
|
|
|
export default async function (path: string) {
|
|
if (path.endsWith("/")) path = path.slice(0, path.length - 1)
|
|
const cwd = getState("cwd")
|
|
const root = cwd || projectDir()
|
|
|
|
if (path == "..") {
|
|
if (root === projectDir()) return
|
|
|
|
setState("cwd", dirname(cwd))
|
|
return
|
|
}
|
|
|
|
for (const file of readdirSync(root, { withFileTypes: true })) {
|
|
if (file.name === path && file.isDirectory()) {
|
|
setState("cwd", join(root, file.name))
|
|
return
|
|
}
|
|
}
|
|
|
|
return { error: `${path} doesn't exist` }
|
|
} |