import Fs from 'fs' ; import * as Url from 'url' ; import * as Path from 'path' ; import { pathExistsSync } from 'fs-extra' ; import { shell } from 'electron' ; export class File { protected static restricted = [ '.cmd' , '.exe' , '.bat' , '.sh' ] ; param extension public static isExtensionSafe ( extension ) { if ( __WIN32__ ) { return this . restricted . indexOf ( extension . toLowerCase ( ) ) === - 1 ; } return true ; } param path public static exists ( path ) { return pathExistsSync ( path ) ; } param path public static isSafe ( path ) { return this . isExtensionSafe ( Path . extname ( path ) ) ; } param path public static async open ( path ) { if ( this . isSafe ( path ) ) { return shell . openExternal ( `file://${ path }` ) ; } } param path public static reveal ( path ) { Fs . stat ( path , ( err , stats ) => { if ( err ) { log . error ( `Unable to find file at '${ path }'` , err ) ; return ; } if ( ! __DARWIN__ && stats . isDirectory ( ) ) { this . openDirectorySafe ( path ) ; } else { shell . showItemInFolder ( path ) ; } } ) ; } param path public static openDirectorySafe ( path ) { if ( __DARWIN__ ) { const directoryURL = Url . format ( { pathname : path , protocol : 'file:' , slashes : true } ) ; shell . openExternal ( directoryURL ) . catch ( err => log . error ( `Failed to open directory (${ path })` , err ) ) ; } else { shell . openPath ( path ) ; } } }