mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-02 10:27:15 +00:00
30 lines
635 B
TypeScript
30 lines
635 B
TypeScript
import { promises as fsp } from 'node:fs'
|
|
import { dirname } from 'pathe'
|
|
|
|
// Check if a file exists
|
|
export async function exists (path: string) {
|
|
try {
|
|
await fsp.access(path)
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
export async function clearDir (path: string) {
|
|
await fsp.rm(path, { recursive: true, force: true })
|
|
await fsp.mkdir(path, { recursive: true })
|
|
}
|
|
|
|
export function findup<T> (rootDir: string, fn: (dir: string) => T | undefined): T | null {
|
|
let dir = rootDir
|
|
while (dir !== dirname(dir)) {
|
|
const res = fn(dir)
|
|
if (res) {
|
|
return res
|
|
}
|
|
dir = dirname(dir)
|
|
}
|
|
return null
|
|
}
|