forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNodeUtils.js
More file actions
1 lines (1 loc) · 7.62 KB
/
Copy pathNodeUtils.js
File metadata and controls
1 lines (1 loc) · 7.62 KB
1
define(function(require,exports,module){const Strings=require("strings"),NodeConnector=require("NodeConnector"),UTILS_NODE_CONNECTOR="ph_utils";let utilsConnector;async function fetchURLText(url,encoding){if(!Phoenix.isNativeApp)throw new Error("node not available in browser");const{buffer:buffer}=await utilsConnector.execPeer("getURLContent",{url:url});return iconv.decode(Buffer.from(buffer),encoding)}async function _updateNodeLocaleStrings(){return!!Phoenix.isNativeApp&&(await utilsConnector.execPeer("setLocaleStrings",Strings),!0)}async function getPhoenixBinaryVersion(){if(!Phoenix.isNativeApp)throw new Error("getPhoenixBinaryVersion not available in browser");const cliArgs=await window.__TAURI__.invoke("_get_commandline_args"),phoenixBinPath=cliArgs[0];return utilsConnector.execPeer("getPhoenixBinaryVersion",phoenixBinPath)}async function getLinuxOSFlavorName(){return"linux"===Phoenix.platform&&Phoenix.isNativeApp?utilsConnector.execPeer("getLinuxOSFlavorName"):null}async function openUrlInBrowser(url,browserName){if(!Phoenix.isNativeApp)throw new Error("openUrlInBrowser not available in browser");return"win"===Phoenix.platform?Phoenix.app._openUrlInBrowserWin(url,browserName):utilsConnector.execPeer("openUrlInBrowser",{url:url,browserName:browserName})}async function _loadNodeExtensionModule(moduleNativeDir){if(!Phoenix.isNativeApp)throw new Error("_loadNodeExtensionModule not available in browser");return utilsConnector.execPeer("_loadNodeExtensionModule",{moduleNativeDir:moduleNativeDir})}function _npmInstallInFolder(moduleNativeDir){if(!Phoenix.isNativeApp)throw new Error("_npmInstallInFolder not available in browser");const promise=utilsConnector.execPeer("_npmInstallInFolder",{moduleNativeDir:moduleNativeDir});return promise.cancel=function(){return utilsConnector.execPeer("_cancelNpmInstall",{moduleNativeDir:moduleNativeDir})},promise}Phoenix.isNativeApp&&(utilsConnector=NodeConnector.createNodeConnector("ph_utils",exports));const DOWNLOAD_PROGRESS_EVENT="downloadProgress";let _downloadCounter=0;function downloadFile(url,destFile,options){if(!Phoenix.isNativeApp)throw new Error("downloadFile not available in browser");const progress=options&&options.progress,cancelId="dl"+ ++_downloadCounter,eventName=DOWNLOAD_PROGRESS_EVENT+".dl"+_downloadCounter;progress&&utilsConnector.on(eventName,function(_evt,data){data&&data.url===url&&progress(data.transferred,data.total)});const promise=utilsConnector.execPeer("downloadFile",{url:url,destFile:destFile,sha256:options&&options.sha256||void 0,cancelId:cancelId}).finally(function(){progress&&utilsConnector.off(eventName)});return promise.cancel=function(){return utilsConnector.execPeer("cancelDownload",{cancelId:cancelId})},promise}async function extractZipFile(zipPath,destDir){if(!Phoenix.isNativeApp)throw new Error("extractZipFile not available in browser");return utilsConnector.execPeer("extractZipFile",{zipPath:zipPath,destDir:destDir})}async function setExecutableBits(filePath){if(!Phoenix.isNativeApp)throw new Error("setExecutableBits not available in browser");return utilsConnector.execPeer("setExecutableBits",{filePath:filePath})}async function execFileWithInput(command,args,options){if(!Phoenix.isNativeApp)throw new Error("execFileWithInput not available in browser");return utilsConnector.execPeer("execFileWithInput",{command:command,args:args||[],stdinText:options&&options.stdinText,cwd:options&&options.cwd,timeoutMs:options&&options.timeoutMs})}async function getEnvironmentVariable(varName){if(!Phoenix.isNativeApp)throw new Error("getEnvironmentVariable not available in browser");return utilsConnector.execPeer("getEnvironmentVariable",varName)}async function ESLintFile(text,fullFilePath,projectFullPath){if(!Phoenix.isNativeApp)throw new Error("ESLintFile not available in browser");return utilsConnector.execPeer("ESLintFile",{text:text,fullFilePath:window.fs.getTauriPlatformPath(fullFilePath),projectFullPath:window.fs.getTauriPlatformPath(projectFullPath)})}async function openNativeTerminal(cwd,usePowerShell=!1){if(!Phoenix.isNativeApp)throw new Error("openNativeTerminal not available in browser");return utilsConnector.execPeer("openNativeTerminal",{cwd:window.fs.getTauriPlatformPath(cwd),usePowerShell:usePowerShell})}async function openInDefaultApp(fullPath){if(!Phoenix.isNativeApp)throw new Error("openInDefaultApp not available in browser");return utilsConnector.execPeer("openInDefaultApp",window.fs.getTauriPlatformPath(fullPath))}let cachedDeviceID=void 0,_systemSettingsDir;async function getDeviceID(){if(!Phoenix.isNativeApp)throw new Error("getDeviceID not available in browser");if(void 0!==cachedDeviceID)return cachedDeviceID;try{return cachedDeviceID=await utilsConnector.execPeer("getDeviceID")}catch(err){cachedDeviceID=null,logger.reportError(err,"getDeviceID failed in NodeUtils")}return cachedDeviceID}async function addDeviceLicenseSystemWide(){if(!Phoenix.isNativeApp)throw new Error("addDeviceLicense not available in browser");try{return await utilsConnector.execPeer("addDeviceLicense"),!0}catch(err){logger.reportError(err,"system wide device license activation failed")}return!1}async function removeDeviceLicenseSystemWide(){if(!Phoenix.isNativeApp)throw new Error("removeDeviceLicense not available in browser");try{return await utilsConnector.execPeer("removeDeviceLicense"),!0}catch(err){logger.reportError(err,"system wide device license remove failed")}return!1}async function isLicensedDeviceSystemWide(){if(!Phoenix.isNativeApp)return console.error("isLicensedDevice not available in browser"),!1;try{return utilsConnector.execPeer("isLicensedDevice")}catch(err){logger.reportError(err,"system wide device check failed")}return!1}async function getOSUserName(){if(!Phoenix.isNativeApp)throw new Error("getOSUserName not available in browser");return utilsConnector.execPeer("getOSUserName")}async function getSystemSettingsDir(){if(!Phoenix.isNativeApp)throw new Error("getSystemSettingsDir is sudo folder is win/linux/mac. not available in browser.");return _systemSettingsDir||(_systemSettingsDir=await utilsConnector.execPeer("getSystemSettingsDir")),_systemSettingsDir}NodeConnector.isNodeAvailable()&&_updateNodeLocaleStrings();try{if(Phoenix.isTestWindow)if(Phoenix.isNativeApp){async function _setIsTestWindowGitHubActions(){const actionsEnv=await utilsConnector.execPeer("getEnvironmentVariable","GITHUB_ACTIONS");Phoenix.isTestWindowGitHubActions=!!actionsEnv}_setIsTestWindowGitHubActions().catch(e=>{console.error("Error setting Phoenix.isTestWindowGitHubActions",e)})}else{const urlSearchParams=new window.URLSearchParams(window.location.search||"");Phoenix.isTestWindowGitHubActions="yes"===urlSearchParams.get("isTestWindowGitHubActions")}}catch(e){console.error("Error setting Phoenix.isTestWindowGitHubActions",e)}exports._loadNodeExtensionModule=_loadNodeExtensionModule,exports._npmInstallInFolder=_npmInstallInFolder,exports.downloadFile=downloadFile,exports.extractZipFile=extractZipFile,exports.setExecutableBits=setExecutableBits,exports.execFileWithInput=execFileWithInput,exports.fetchURLText=fetchURLText,exports.getPhoenixBinaryVersion=getPhoenixBinaryVersion,exports.getLinuxOSFlavorName=getLinuxOSFlavorName,exports.openUrlInBrowser=openUrlInBrowser,exports.ESLintFile=ESLintFile,exports.getEnvironmentVariable=getEnvironmentVariable,exports.openNativeTerminal=openNativeTerminal,exports.openInDefaultApp=openInDefaultApp,exports.addDeviceLicenseSystemWide=addDeviceLicenseSystemWide,exports.removeDeviceLicenseSystemWide=removeDeviceLicenseSystemWide,exports.isLicensedDeviceSystemWide=isLicensedDeviceSystemWide,exports.getDeviceID=getDeviceID,exports.getOSUserName=getOSUserName,exports.getSystemSettingsDir=getSystemSettingsDir,exports.isNodeReady=NodeConnector.isNodeReady,window.NodeUtils=exports});