import { promises as fs , constants as fs_constants } from "fs" ; const { F_OK , W_OK , R_OK } = fs_constants ; import { join } from "path" ; import { homedir } from "os" ; import { isArray } from "lodash" ; import { getLogger } from "./logger" ; const { info , warn } = getLogger ( "dedicated-disks" ) ; import { getProjectConfig } from "./project-setup" ; async function ensureSymlink ( name ) { const local = join ( "/" , "local" ) ; const disk = join ( local , name ) ; const link = join ( homedir ( ) , "disks" ) ; try { await fs . access ( disk , F_OK | R_OK | W_OK ) ; } catch { warn ( `disk directory ${ disk } not writeable -- abort` ) ; return false ; } try { await fs . access ( link , F_OK ) ; info ( `'${ link }' already exists` ) ; } catch { try { await fs . symlink ( local , link ) ; info ( `successfully symlinked ${ link } → ${ disk }` ) ; } catch ( err ) { warn ( `problem symlinking ${ link } → ${ disk } -- ${ err }` ) ; } } return true ; } export async function init ( ) { info ( "initializing" ) ; const conf = getProjectConfig ( ) ; if ( conf ?. quota ?. dedicated_disks == null ) return ; if ( ! isArray ( conf . quota . dedicated_disks ) ) return ; for ( const disk of conf . quota . dedicated_disks ) { if ( typeof disk . name === "string" ) { if ( await ensureSymlink ( disk . name ) ) { return ; } } } }