import fs from "fs/promises" ; import path from "path" ; function onNotFound < T , V > ( promise , value ) { return promise . catch ( ( e ) => { if ( e . code === "ENOENT" ) return value ; throw e ; } ) ; } export function readFile ( filePath ) ; export function readFile ( filePath , decode ) ; export function readFile ( filePath , decode ? ) { return onNotFound ( fs . readFile ( filePath , decode && "utf8" ) , undefined ) ; } export async function writeFile ( filePath , data ) { await fs . mkdir ( path . dirname ( filePath ) , { recursive : true } ) ; await fs . writeFile ( filePath , data , typeof data === "string" ? "utf8" : undefined ) ; } export function deleteFile ( filePath ) { return onNotFound ( fs . unlink ( filePath ) . then ( ( ) => true ) , false ) ; } export function readDir ( filePath ) { return onNotFound ( fs . readdir ( filePath ) , [ ] ) ; } export async function * walk ( rootPath ) { const fileNames = await readDir ( rootPath ) ; for ( const fileName of fileNames ) { const filePath = path . join ( rootPath , fileName ) ; if ( ( await fs . stat ( filePath ) ) . isDirectory ( ) ) { yield * walk ( filePath ) ; } else { yield filePath ; } } }