2023-10-17 11:54:29 +02:00
|
|
|
import { spawn } from 'node:child_process'
|
2023-10-19 05:44:17 +02:00
|
|
|
import appStore from '@electron/helpers/store.js'
|
2023-10-17 09:27:08 +02:00
|
|
|
import { adbPath, scrcpyPath } from '@electron/configs/index.js'
|
2023-09-18 09:07:28 +02:00
|
|
|
|
2023-10-17 11:54:29 +02:00
|
|
|
const shell = async (command, { stdout, stderr } = {}) => {
|
2023-10-20 08:23:48 +02:00
|
|
|
const spawnPath = appStore.get('scrcpy.global.scrcpyPath') || scrcpyPath
|
2023-10-17 11:54:29 +02:00
|
|
|
const args = command.split(' ')
|
2023-10-20 08:23:48 +02:00
|
|
|
|
|
|
|
const scrcpyProcess = spawn(spawnPath, args, {
|
|
|
|
env: { ...process.env, ADB: adbPath },
|
|
|
|
shell: true,
|
|
|
|
})
|
2023-09-18 09:07:28 +02:00
|
|
|
|
2023-10-17 11:54:29 +02:00
|
|
|
scrcpyProcess.stdout.on('data', (data) => {
|
|
|
|
const stringData = data.toString()
|
|
|
|
|
|
|
|
console.log('scrcpyProcess.stdout.data:', stringData)
|
|
|
|
|
|
|
|
if (stdout) {
|
2023-10-20 12:17:58 +02:00
|
|
|
stdout(stringData, scrcpyProcess)
|
2023-10-17 11:54:29 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
scrcpyProcess.stderr.on('data', (data) => {
|
|
|
|
const stringData = data.toString()
|
|
|
|
|
|
|
|
console.error('scrcpyProcess.stderr.data:', stringData)
|
|
|
|
|
|
|
|
if (stderr) {
|
2023-10-20 12:17:58 +02:00
|
|
|
stderr(stringData, scrcpyProcess)
|
2023-10-17 11:54:29 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
scrcpyProcess.on('close', (code) => {
|
|
|
|
if (code === 0) {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
reject(new Error(`Command failed with code ${code}`))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
scrcpyProcess.on('error', (err) => {
|
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2023-09-18 09:07:28 +02:00
|
|
|
|
|
|
|
export default () => ({
|
|
|
|
shell,
|
|
|
|
})
|