From 13741d3b155b819542270bd239cbedcbd0e7ce3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Thu, 28 Nov 2024 15:56:14 +0800 Subject: [PATCH] =?UTF-8?q?optimize:=20=E4=BC=98=E5=8C=96=20getSystemPlatf?= =?UTF-8?q?orm=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/shell/shell.js | 15 +++++++++------ packages/gui/src/bridge/api/backend.js | 6 +++--- packages/gui/src/bridge/auto-start/backend.js | 4 +--- packages/gui/src/utils/util.apppath.js | 12 +++++++++--- packages/gui/src/view/components/setup-ca.vue | 3 +-- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/packages/core/src/shell/shell.js b/packages/core/src/shell/shell.js index c5e4f46b0b..0e1c615d67 100644 --- a/packages/core/src/shell/shell.js +++ b/packages/core/src/shell/shell.js @@ -93,20 +93,19 @@ function childExec (composeCmds, options = {}) { } function getSystemShell () { - switch (getSystemPlatform()) { + switch (getSystemPlatform(true)) { case 'mac': return DarwinSystemShell case 'linux': return LinuxSystemShell case 'windows': return WindowsSystemShell - case 'unknown os': default: throw new Error(`UNKNOWN OS TYPE ${os.platform()}`) } } -function getSystemPlatform () { +function getSystemPlatform (throwIfUnknown = false) { switch (os.platform()) { case 'darwin': return 'mac' @@ -116,14 +115,18 @@ function getSystemPlatform () { return 'windows' case 'win64': return 'windows' - case 'unknown os': default: - throw new Error(`UNKNOWN OS TYPE ${os.platform()}`) + log.error(`UNKNOWN OS TYPE: ${os.platform()}`) + if (throwIfUnknown) { + throw new Error(`UNKNOWN OS TYPE '${os.platform()}'`) + } else { + return 'unknown-os' + } } } async function execute (executor, args) { - return executor[getSystemPlatform()](getSystemShell().exec, args) + return executor[getSystemPlatform(true)](getSystemShell().exec, args) } async function execFile (file, args, options) { diff --git a/packages/gui/src/bridge/api/backend.js b/packages/gui/src/bridge/api/backend.js index 7fbd3414c1..e8582afe3f 100644 --- a/packages/gui/src/bridge/api/backend.js +++ b/packages/gui/src/bridge/api/backend.js @@ -30,7 +30,7 @@ const getDateTimeStr = function () { const localApi = { /** * 返回所有api列表,供vue来ipc调用 - * @returns {[]} + * @returns {[]} api列表 */ getApiList () { const core = lodash.cloneDeep(DevSidecar.api) @@ -50,8 +50,8 @@ const localApi = { getConfigDir () { return getDefaultConfigBasePath() }, - getSystemPlatform () { - return DevSidecar.api.shell.getSystemPlatform() + getSystemPlatform (throwIfUnknown = false) { + return DevSidecar.api.shell.getSystemPlatform(throwIfUnknown) }, }, /** diff --git a/packages/gui/src/bridge/auto-start/backend.js b/packages/gui/src/bridge/auto-start/backend.js index fed4d26513..8ae9d4b669 100644 --- a/packages/gui/src/bridge/auto-start/backend.js +++ b/packages/gui/src/bridge/auto-start/backend.js @@ -26,9 +26,7 @@ EOF } export default { install (context) { - const { ipcMain, dialog, log, app } = context - - const ex = app.getPath('exe') + const { ipcMain, app } = context // 定义事件,渲染进程中直接使用 diff --git a/packages/gui/src/utils/util.apppath.js b/packages/gui/src/utils/util.apppath.js index ea4941c807..8c397f9c8e 100644 --- a/packages/gui/src/utils/util.apppath.js +++ b/packages/gui/src/utils/util.apppath.js @@ -1,7 +1,8 @@ import os from 'node:os' import path from 'node:path' +import log from './util.log' -function getSystemPlatform () { +function getSystemPlatform (throwIfUnknown = false) { switch (os.platform()) { case 'darwin': return 'mac' @@ -11,11 +12,16 @@ function getSystemPlatform () { return 'windows' case 'win64': return 'windows' - case 'unknown os': default: - throw new Error(`UNKNOWN OS TYPE ${os.platform()}`) + log.error(`UNKNOWN OS TYPE: ${os.platform()}`) + if (throwIfUnknown) { + throw new Error(`UNKNOWN OS TYPE ${os.platform()}`) + } else { + return 'unknown-os' + } } } + export default { getAppRootPath (app) { const exePath = app.getPath('exe') diff --git a/packages/gui/src/view/components/setup-ca.vue b/packages/gui/src/view/components/setup-ca.vue index 8ad8fa9502..05e10f0263 100644 --- a/packages/gui/src/view/components/setup-ca.vue +++ b/packages/gui/src/view/components/setup-ca.vue @@ -30,8 +30,7 @@ export default { }, }, async created () { - const platform = await this.$api.info.getSystemPlatform() - this.systemPlatform = platform + this.systemPlatform = await this.$api.info.getSystemPlatform() }, methods: { async openExternal (url) {