import { app , remote } from 'electron' ; import path from 'path' ; import fs from 'fs' ; import { isPresent } from 'polar-shared/src/Preconditions' ; import { URLs } from 'polar-shared/src/util/URLs' ; import { URLStr } from "polar-shared/src/util/Strings" ; const USE_FILE_URL = false ; /**
 * Given a relative path, return a full path to a local app resource.
 *
 * Each module has a unique __dirname so with this mechanism we can reliably
 * find an path to a file as if we were in the root of the webapp.
 *
 */ export class ResourcePaths { /**
     * Create a full absolute path from a relative path.
     */ public static absoluteFromRelativePath ( relativePath ) { const baseDirs = ResourcePaths . getBaseDirs ( ) ; for ( const baseDir of baseDirs ) { const absolutePath = path . resolve ( baseDir , relativePath ) ; try { fs . readFileSync ( absolutePath ) ; return absolutePath ; } catch ( e ) { } } throw new Error ( `No file found for ${ relativePath } in baseDirs: ` + JSON . stringify ( baseDirs ) ) ; } param relativeURL public static resourceURLFromRelativeURL ( relativeURL , useFileURL = USE_FILE_URL ) { let relativePath = relativeURL ; let queryData = "" ; if ( relativeURL . indexOf ( "?" ) !== - 1 ) { relativePath = relativeURL . substring ( 0 , relativeURL . indexOf ( "?" ) ) ; queryData = relativeURL . substring ( relativeURL . indexOf ( "?" ) ) ; } if ( useFileURL ) { const absolutePath = ResourcePaths . absoluteFromRelativePath ( relativePath ) ; return 'file://' + absolutePath + queryData ; } else { const computeBase = ( ) => { if ( typeof window !== 'undefined' && window . location ) { return URLs . toBase ( window . location . href ) ; } return "http://localhost:8500" ; } ; const base = computeBase ( ) ; return base + relativeURL ; } } /**
     * Get the basedir of the current webapp.
     */ protected static getBaseDirs ( ) { const baseDirs = [ ] ; if ( ! isPresent ( app ) ) { baseDirs . push ( remote . app . getAppPath ( ) ) ; } else { baseDirs . push ( app . getAppPath ( ) ) ; } baseDirs . push ( process . cwd ( ) ) ; return baseDirs ; } } export class AppPathException extends Error { }