import { selectors } from "@nteract/core" ; import { ContentRef } from "@nteract/types" ; import { remote } from "electron" ; import * as fs from "fs" ; import * as path from "path" ; import { DesktopStore } from "../../../notebook/store" ; export const currentDocumentDirectory = ( store , contentRef ) => documentDirectoryFor ( selectors . filepath ( store . getState ( ) , { contentRef } ) ) ; export const documentDirectoryFor = ( filepath ) => filepath !== null ? path . dirname ( path . resolve ( filepath ) ) : systemDocumentDirectory ( ) ; export function systemDocumentDirectory ( ) { const cwd = path . normalize ( process . cwd ( ) ) ; const cwdIsProperDocumentDirectory = cwd !== "/" && cwd !== path . normalize ( path . dirname ( process . execPath ) ) && isWriteable ( cwd ) ; return cwdIsProperDocumentDirectory ? cwd : remote . app . getPath ( "documents" ) ; } function isWriteable ( pathToCheck ) { try { fs . accessSync ( pathToCheck , fs . constants . W_OK ) ; return true ; } catch ( err ) { return false ; } }