Skip to content

Commit

Permalink
optimize: 优化 getSystemPlatform 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Nov 28, 2024
1 parent 81c1433 commit 13741d3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
15 changes: 9 additions & 6 deletions packages/core/src/shell/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions packages/gui/src/bridge/api/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const getDateTimeStr = function () {
const localApi = {
/**
* 返回所有api列表,供vue来ipc调用
* @returns {[]}
* @returns {[]} api列表
*/
getApiList () {
const core = lodash.cloneDeep(DevSidecar.api)
Expand All @@ -50,8 +50,8 @@ const localApi = {
getConfigDir () {
return getDefaultConfigBasePath()
},
getSystemPlatform () {
return DevSidecar.api.shell.getSystemPlatform()
getSystemPlatform (throwIfUnknown = false) {
return DevSidecar.api.shell.getSystemPlatform(throwIfUnknown)
},
},
/**
Expand Down
4 changes: 1 addition & 3 deletions packages/gui/src/bridge/auto-start/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ EOF
}
export default {
install (context) {
const { ipcMain, dialog, log, app } = context

const ex = app.getPath('exe')
const { ipcMain, app } = context

// 定义事件,渲染进程中直接使用

Expand Down
12 changes: 9 additions & 3 deletions packages/gui/src/utils/util.apppath.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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')
Expand Down
3 changes: 1 addition & 2 deletions packages/gui/src/view/components/setup-ca.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 13741d3

Please sign in to comment.