forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit_vfs.js
More file actions
1 lines (1 loc) · 6.22 KB
/
Copy pathinit_vfs.js
File metadata and controls
1 lines (1 loc) · 6.22 KB
1
let extensionDIR,appSupportDIR,tauriAssetServeDir,tauriAssetServeBaseURL,documentsDIR,tempDIR,userProjectsDir;function _getNativeAssetUrl(fullFilePath){if(window.__TAURI__){if(fullFilePath.startsWith(tauriAssetServeDir)){const platformPath=fs.getTauriPlatformPath(fullFilePath).replace(/\\/g,"/");return decodeURIComponent(window.__TAURI__.tauri.convertFileSrc(platformPath))}return null}if(window.__ELECTRON__){if(fullFilePath.startsWith(tauriAssetServeDir)){const platformPath=fs.getTauriPlatformPath(fullFilePath);return window.electronAPI.convertToAssetURL(platformPath)}return null}return null}function _setupVFS(fsLib,pathLib){return Phoenix.VFS={getRootDir:()=>"/fs/",getMountDir:()=>"/mnt/",getTauriDir:()=>"/tauri/",getAppSupportDir:()=>appSupportDIR,getExtensionDir:()=>extensionDIR,getUserExtensionDir:()=>`${extensionDIR}user`,getDevExtensionDir:()=>`${extensionDIR}dev`,getDevTempExtensionDir:()=>`${extensionDIR}devTemp`,getTempDir:()=>tempDIR,getTauriAssetServeDir:()=>tauriAssetServeDir,getUserDocumentsDirectory:()=>documentsDIR,getUserProjectsDirectory:()=>userProjectsDir,_getVirtualDocumentsDirectory:()=>"/fs/local/",getDefaultProjectDir:()=>`${userProjectsDir}default project/`,ensureTrailingSlash:function(path){return path.endsWith("/")?path:`${path}/`},getTauriVirtualPath:fs.getTauriVirtualPath,isLocalDiscPath:function(fullPath){return!(!fullPath||!fullPath.startsWith(Phoenix.VFS.getTauriDir())&&!fullPath.startsWith(Phoenix.VFS.getMountDir()))},ensureExistsDir:function(path,cb){Phoenix.VFS.exists(path,exists=>{exists?cb():Phoenix.fs.mkdirs(path,493,!0,function(err){err&&"EEXIST"!==err.code&&cb(err),cb()})})},ensureExistsDirAsync:async function(path){return new Promise((resolve,reject)=>{Phoenix.VFS.ensureExistsDir(path,err=>{err?reject(err):resolve()})})},getPathForVirtualServingURL:function(fullURL){if(Phoenix.isNativeApp){if(fullURL.startsWith(tauriAssetServeBaseURL)){const assetRelativePath=decodeURIComponent(fullURL.replace(tauriAssetServeBaseURL,"")).replace(/\\/g,"/");return`${tauriAssetServeDir}${assetRelativePath}`}return null}return window.fsServerUrl&&fullURL.startsWith(window.fsServerUrl)?fullURL.replace(window.fsServerUrl,"/"):null},getVirtualServingURLForPath:function(fullPath){return Phoenix.isNativeApp?_getNativeAssetUrl(fullPath):window.fsServerUrl.slice(0,-1)+fullPath},exists:function(path,cb){fs.stat(path,function(err,stats){cb(!(!stats||err))})},existsAsync:async function(path){return new Promise(resolve=>{Phoenix.VFS.exists(path,exists=>{resolve(exists)})})},unlinkAsync:async function(filePath){return new Promise((resolve,reject)=>{fs.unlink(filePath,err=>{err?reject(err):resolve()})})},unlinkResolves:async function(filePath){return new Promise(resolve=>{fs.unlink(filePath,error=>{resolve(error?{error:error}:{})})})},readFileResolves:function(filePath,encoding){return new Promise(resolve=>{fs.readFile(filePath,encoding,function(error,data){resolve(error?{error:error}:{data:data})})})},readFileAsync:function(filePath,encoding){return new Promise((resolve,reject)=>{fs.readFile(filePath,encoding,function(error,data){error?reject(error):resolve(data)})})},writeFileAsync:function(filePath,content,encoding){return new Promise((resolve,reject)=>{fs.writeFile(filePath,content,encoding,err=>{err?reject(err):resolve()})})},fs:fsLib,path:pathLib},Phoenix.fs=fsLib,Phoenix.path=pathLib,Phoenix.VFS}async function setupAppSupportAndExtensionsDir(){Phoenix.isNativeApp?((appSupportDIR=fs.getTauriVirtualPath(window._tauriBootVars.appLocalDir)).endsWith("/")||(appSupportDIR=`${appSupportDIR}/`),tauriAssetServeBaseURL=_getNativeAssetUrl(tauriAssetServeDir=`${appSupportDIR}assets/`),extensionDIR=`${tauriAssetServeDir}extensions/`,Phoenix.VFS.ensureExistsDirAsync(Phoenix.VFS.getRootDir()).catch(console.error)):(extensionDIR=`${appSupportDIR="/fs/app/"}extensions/`,await Phoenix.VFS.ensureExistsDirAsync(Phoenix.VFS.getRootDir())),await Promise.all([Phoenix.VFS.ensureExistsDirAsync(Phoenix.VFS.getAppSupportDir()),Phoenix.VFS.ensureExistsDirAsync(Phoenix.VFS.getExtensionDir()),Phoenix.VFS.ensureExistsDirAsync(Phoenix.VFS.getUserExtensionDir()),Phoenix.VFS.ensureExistsDirAsync(Phoenix.VFS.getDevExtensionDir()),Phoenix.VFS.ensureExistsDirAsync(Phoenix.VFS.getDevTempExtensionDir())])}async function setupDocumentsDir(){if(Phoenix.isNativeApp){(documentsDIR=fs.getTauriVirtualPath(window._tauriBootVars.documentDir)).endsWith("/")||(documentsDIR=`${documentsDIR}/`);const appName=window._tauriBootVars.appname;userProjectsDir=`${documentsDIR}${appName}/`}else documentsDIR=Phoenix.VFS._getVirtualDocumentsDirectory(),userProjectsDir=documentsDIR;await Phoenix.VFS.ensureExistsDirAsync(documentsDIR),console.log("Documents dir setup done")}async function setupTempDir(){if(Phoenix.isNativeApp){(tempDIR=fs.getTauriVirtualPath(window._tauriBootVars.tempDir)).endsWith("/")||(tempDIR=`${tempDIR}/`);const appName=window._tauriBootVars.appname;tempDIR=`${tempDIR}${appName}/`}else tempDIR="/temp/";await Phoenix.VFS.ensureExistsDirAsync(tempDIR),console.log("Temp dir setup done")}const _createAppDirs=async function(){console.log("Waiting for tauri boot variables..."),window._tauriBootVarsPromise&&await window._tauriBootVarsPromise,console.log("Creating appdirs..."),await Promise.all([setupAppSupportAndExtensionsDir(),setupDocumentsDir(),setupTempDir()]),console.log("Appdirs created...")},CORE_LIB_GUARD_INTERVAL=5e3,_FS_ERROR_MESSAGE=Phoenix.isNativeApp&&"mac"===Phoenix.platform?"Oops. Could not start due to missing file system library.\n\nPhoenix Code requires `macOS 12 Monterey` or higher":"Oops. Could not start due to missing file system library.\n\nPlease use a modern browser (released within the last 4 years).";export default function initVFS(){if(!window.fs||!window.path||!window.Phoenix)throw window.alert(_FS_ERROR_MESSAGE),new Error(_FS_ERROR_MESSAGE);const savedfs=window.fs,savedPath=window.path;setInterval(()=>{window.fs!==savedfs&&(console.error("window.fs overwrite detected!! Some extension may have corrupted this. attempting to revert to original lib."),window.fs=savedfs),window.path!==savedPath&&(console.error("window.path overwrite detected!! Some extension may have corrupted this. attempting to revert to original lib."),window.path=savedPath)},CORE_LIB_GUARD_INTERVAL),_setupVFS(window.fs,window.path),window._phoenixfsAppDirsCreatePromise=_createAppDirs()}