import path from 'path' ; import fs from 'fs' ; import os from 'os' ; import url from 'url' ; import { remote } from 'electron' ; declare var document ; export function injectApp ( scriptSrc , fallbackLoader ) { if ( typeof require === 'function' ) { console . log ( "Loading via fallbackLoader" ) ; fallbackLoader ( ) ; } else { console . log ( "Loading via script" ) ; injectScript ( scriptSrc ) ; } } export function injectScript ( src , type ? ) { const script = document . createElement ( 'script' ) ; script . src = src ; script . async = false ; script . defer = false ; if ( type ) { script . type = type ; } document . body . appendChild ( script ) ; } param loadPath { string } private export function load ( loadPath ) { /**
     * The os.type() method returns a string identifying the operating system
     * name as returned by uname(3). For example 'Linux' on Linux, 'Darwin' on
     * macOS and 'Windows_NT' on Windows.
     */ const osType = os . type ( ) ; return _loadFromHref ( document . location ! . href , loadPath , osType ) ; } export function loadFromAppPath ( relativePath ) { const appPath = remote . getGlobal ( "appPath" ) ; const entryPoint = path . join ( appPath , relativePath ) ; require ( entryPoint ) ; } param href param loadPath param osType private export function _loadFromHref ( href , loadPath , osType ) { const resolvedPath = _resolveFromHref ( href , loadPath , osType ) ; require ( resolvedPath ) ; } param href param os_type return { string } private export function _toPath ( href , loadPath , os_type ) { if ( href . startsWith ( 'file:' ) ) { return _toPathFromFileURL ( href , os_type ) ; } if ( href . startsWith ( 'http:' ) || href . startsWith ( 'https:' ) ) { throw new Error ( "http and https not supported" ) ; } throw new Error ( "Unable to load from href: " + href ) ; } export function _toPathFromFileURL ( href , osType ) { let result = href ; result = result . replace ( 'file://' , '' ) ; if ( osType === 'Windows_NT' ) { result = result . substring ( 1 ) ; result = result . replace ( /^(.+)\|/ , '$1:' ) ; result = result . replace ( /\//g , '\\' ) ; } result = result . replace ( /[^/.]+\.html([#?].*)?$/ , '' ) ; result = decodeURI ( result ) ; return result ; } param from param to return { string } private export function _resolveURL ( from , to ) { return url . resolve ( from , to ) ; } param href { string } param loadPath { string } param osType private export function _resolveFromHref ( href , loadPath , osType ) { const resolvedURL = _resolveURL ( href , loadPath ) ; const resolvedPath = _toPath ( resolvedURL , loadPath , osType ) ; if ( ! fs . existsSync ( resolvedPath ) ) { throw new Error ( `Could not find ${ loadPath } (not found): ${ resolvedPath }` ) ; } return resolvedPath ; } export interface LoadOptions { /**
     * When true we first determine the path from the file URL given, then we
     * change the URL via document
     */ readonly useLocalWebURL : boolean ; }