optimize: 优化 getSystemPlatform 方法

pull/408/head
王良 2024-11-28 15:56:14 +08:00
parent 81c1433820
commit 13741d3b15
5 changed files with 23 additions and 17 deletions

View File

@ -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) {

View File

@ -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)
},
},
/**

View File

@ -26,9 +26,7 @@ EOF
}
export default {
install (context) {
const { ipcMain, dialog, log, app } = context
const ex = app.getPath('exe')
const { ipcMain, app } = context
// 定义事件,渲染进程中直接使用

View File

@ -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')

View File

@ -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) {