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) · 5.88 KB
/
NodeUtils.js
File metadata and controls
1 lines (1 loc) · 5.88 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})}async function _npmInstallInFolder(moduleNativeDir){if(!Phoenix.isNativeApp)throw new Error("_npmInstallInFolder not available in browser");return utilsConnector.execPeer("_npmInstallInFolder",{moduleNativeDir:moduleNativeDir})}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))}Phoenix.isNativeApp&&(utilsConnector=NodeConnector.createNodeConnector("ph_utils",exports));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.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});