2021-07-26 14:46:19 +00:00
|
|
|
import { promises as fsp } from 'fs'
|
2021-10-18 14:22:02 +00:00
|
|
|
import { promisify } from 'util'
|
|
|
|
import rimraf from 'rimraf'
|
2021-10-29 08:41:04 +00:00
|
|
|
import { dirname } from 'pathe'
|
2021-07-26 14:46:19 +00:00
|
|
|
|
|
|
|
// Check if a file exists
|
|
|
|
export async function exists (path: string) {
|
|
|
|
try {
|
|
|
|
await fsp.access(path)
|
|
|
|
return true
|
|
|
|
} catch {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2021-10-18 14:22:02 +00:00
|
|
|
|
|
|
|
export async function clearDir (path: string) {
|
|
|
|
await promisify(rimraf)(path)
|
|
|
|
await fsp.mkdir(path, { recursive: true })
|
|
|
|
}
|
2021-10-29 08:41:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|