| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- const { spawn } = require('child_process')
- const { execSync } = require('child_process')
- const path = require('path')
- const fs = require('fs')
- const config = require(path.join(__dirname, '..', '..', 'configs', 'config.js'))
- const projectRoot = path.resolve(__dirname, '..', '..')
- const adbPath = path.resolve(projectRoot, config.adbPath.path)
- const scrcpyDir = path.dirname(adbPath)
- const scrcpyPath = path.join(scrcpyDir, 'scrcpy.exe')
- const scrcpyVbs = path.join(scrcpyDir, 'scrcpy-noconsole.vbs')
- const stopBat = path.join(scrcpyDir, 'stop.bat')
- const pidFile = path.join(projectRoot, 'static', 'scrcpy-pid.json')
- const action = process.argv[2]
- const pidArg = process.argv[3]
- // 获取 scrcpy 进程 PID
- function getScrcpyPid() {
- const output = execSync(`tasklist /FI "IMAGENAME eq scrcpy.exe" /FO CSV`, { encoding: 'utf-8' })
- const lines = output.split('\n').filter(line => line.includes('scrcpy.exe'))
- if (lines.length === 0) {
- return null
- }
- const pidMatch = lines[0].match(/"(\d+)"/)
- if (!pidMatch) {
- return null
- }
- return parseInt(pidMatch[1])
- }
- // 检查并终止指定 PID 的进程
- function killPidIfRunning(pid) {
- spawn('taskkill.exe', ['/F', '/PID', pid.toString()], {
- stdio: 'ignore',
- detached: true
- }).unref()
- }
- /** 判断是否为 IP(或 IP:port),用于无线设备选择器 */
- function isDeviceIp(val) {
- return val && !/^\d+$/.test(val) && /[\d.]/.test(val)
- }
- /** 根据传入的 IP 得到 adb/scrcpy 设备选择器,无线默认 5555 */
- function toDeviceSelector(ipOrSelector) {
- return ipOrSelector.includes(':') ? ipOrSelector : `${ipOrSelector}:5555`
- }
- // 启动 scrcpy:若传入 IP 则先 adb connect 再以该设备启动 lib/scrcpy-adb/scrcpy-noconsole.vbs
- function startScrcpy() {
- const targetPid = pidArg && /^\d+$/.test(pidArg) ? parseInt(pidArg) : 7788
- killPidIfRunning(targetPid)
- let deviceSelector = ''
- if (isDeviceIp(pidArg)) {
- deviceSelector = toDeviceSelector(pidArg)
- execSync(`"${adbPath}" connect ${deviceSelector}`, { encoding: 'utf-8', cwd: scrcpyDir })
- }
- const vbsArgs = deviceSelector ? ` "${deviceSelector}"` : (pidArg && !/^\d+$/.test(pidArg) ? ` "${pidArg}"` : '')
- execSync(`wscript.exe "${scrcpyVbs}"${vbsArgs}`, {
- cwd: scrcpyDir,
- stdio: 'ignore'
- })
-
- const sleep = (ms) => {
- const start = Date.now()
- while (Date.now() - start < ms) {}
- }
- sleep(2000)
-
- const runningPid = getScrcpyPid()
- if (!runningPid) {
- console.log(JSON.stringify({
- success: false,
- error: 'Failed to start scrcpy'
- }))
- process.exit(1)
- return
- }
-
- fs.writeFileSync(pidFile, JSON.stringify({ pid: runningPid }), 'utf-8')
- console.log(JSON.stringify({
- success: true,
- pid: runningPid
- }))
- process.exit(0)
- }
- // 停止 scrcpy
- function stopScrcpy() {
- const pid = JSON.parse(fs.readFileSync(pidFile, 'utf-8')).pid
- execSync(`"${stopBat}" ${pid}`, { stdio: 'ignore', cwd: scrcpyDir })
- fs.unlinkSync(pidFile)
- console.log(JSON.stringify({
- success: true
- }))
- process.exit(0)
- }
- switch (action) {
- case 'start':
- startScrcpy()
- break
- case 'stop':
- stopScrcpy()
- break
- default:
- console.log(JSON.stringify({
- success: false,
- error: 'Usage: node screenshot.js [start|stop] [pid|deviceIp]'
- }))
- process.exit(1)
- }
|