Skip to content

Commit

Permalink
Removed DBus module and directly used exec in system
Browse files Browse the repository at this point in the history
  • Loading branch information
6XGate committed Nov 17, 2024
1 parent 4dc4b9a commit 1984752
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 313 deletions.
298 changes: 0 additions & 298 deletions src/main/services/dbus.ts

This file was deleted.

32 changes: 20 additions & 12 deletions src/main/services/system.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import Logger from 'electron-log'
import { execa } from 'execa'
import { memo } from 'radash'
import useDbus from './dbus'

const useSystem = memo(function useSystem() {
const { dbusBind } = useDbus()
async function powerOff(interactive = false) {
const args = interactive ? ['boolean:true'] : ['boolean:false']
const params = [
'--system',
'--print-reply',
'--dest=org.freedesktop.login1',
'/org/freedesktop/login1',
'org.freedesktop.login1.Manager.PowerOff',
...args
]

const powerOffByDbus = dbusBind(
'--system',
'org.freedesktop.login1',
'/org/freedesktop/login1',
'org.freedesktop.login1.Manager',
'PowerOff',
['boolean']
)
Logger.debug('execa:dbus-send:params', ...params)
const { stdout, stderr, exitCode } = await execa('dbus-send', params)
if (exitCode !== 0) {
throw new Error(stderr)
}

async function powerOff(interactive = false) {
await powerOffByDbus(interactive)
if (stdout) {
Logger.debug('execa:dbus-send:output', stdout)
}
}

return {
Expand Down
7 changes: 4 additions & 3 deletions src/tests/main/services/system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Mock } from 'vitest'

let execaSpy: Mock

let stdout = ''
let stderr = ''
let error = null as null | Error

Expand All @@ -16,18 +17,18 @@ beforeAll(() => {
})

beforeEach(() => {
stdout = ''
stderr = ''
error = null

execaSpy.mockImplementation(async () =>
error == null
? await Promise.resolve({ stdout: '', stderr, exitCode: stderr ? 1 : 0 })
: await Promise.reject(error)
error == null ? await Promise.resolve({ stdout, stderr, exitCode: stderr ? 1 : 0 }) : await Promise.reject(error)
)
})

describe('powerOff', () => {
test('success', async () => {
stdout = 'shutting down'
const system = (await import('../../../main/services/system')).default()

await expect(system.powerOff()).resolves.toBeUndefined()
Expand Down

0 comments on commit 1984752

Please sign in to comment.