/
githubmirror
/
Motrix
Обзор
Документация
Войти
/
githubmirror
/
Motrix
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/core/Engine.js
136 строк
3 KB
Dr_rOot
refactor: convert to arrow function
11 май 2023, 16:40
11 май 2023, 16:40
cc27cb3
Код
Авторство
О чём код?
import { spawn } from 'node:child_process' import { existsSync, writeFile, unlink } from 'node:fs' import is from 'electron-is' import logger from './Logger' import { getI18n } from '../ui/Locale' import { getEnginePidPath, getAria2BinPath, getAria2ConfPath, getSessionPath, transformConfig } from '../utils/index' const { platform, arch } = process export default class Engine { // ChildProcess | null static instance = null constructor (options = {}) { this.options = options this.i18n = getI18n() this.systemConfig = options.systemConfig this.userConfig = options.userConfig } start () { const pidPath = getEnginePidPath() logger.info('[Motrix] Engie pid path:', pidPath) if (this.instance) { return } const binPath = this.getEngineBinPath() const args = this.getStartArgs() this.instance = spawn(binPath, args, { windowsHide: false, stdio: is.dev() ? 'pipe' : 'ignore' }) const pid = this.instance.pid.toString() this.writePidFile(pidPath, pid) this.instance.once('close', () => { try { unlink(pidPath, (err) => { if (err) { logger.warn(`[Motrix] Unlink engine process pid file failed: ${err}`) } }) } catch (err) { logger.warn(`[Motrix] Unlink engine process pid file failed: ${err}`) } }) if (is.dev()) { this.instance.stdout.on('data', (data) => { logger.log('[Motrix] engine stdout===>', data.toString()) }) this.instance.stderr.on('data', (data) => { logger.log('[Motrix] engine stderr===>', data.toString()) }) } } stop () { logger.info('[Motrix] engine.stop.instance') if (this.instance) { this.instance.kill() this.instance = null } } writePidFile (pidPath, pid) { writeFile(pidPath, pid, (err) => { if (err) { logger.error(`[Motrix] Write engine process pid failed: ${err}`) } }) } getEngineBinPath () { const result = getAria2BinPath(platform, arch) const binIsExist = existsSync(result) if (!binIsExist) { logger.error('[Motrix] engine bin is not exist:', result) throw new Error(this.i18n.t('app.engine-missing-message')) } return result } getStartArgs () { const confPath = getAria2ConfPath(platform, arch) const sessionPath = getSessionPath() const sessionIsExist = existsSync(sessionPath) let result = [`--conf-path=${confPath}`, `--save-session=${sessionPath}`] if (sessionIsExist) { result = [...result, `--input-file=${sessionPath}`] } const extraConfig = { ...this.systemConfig } const keepSeeding = this.userConfig['keep-seeding'] const seedRatio = this.systemConfig['seed-ratio'] if (keepSeeding || seedRatio === 0) { extraConfig['seed-ratio'] = 0 delete extraConfig['seed-time'] } console.log('extraConfig===>', extraConfig) const extra = transformConfig(extraConfig) result = [...result, ...extra] return result } isRunning (pid) { try { return process.kill(pid, 0) } catch (e) { return e.code === 'EPERM' } } restart () { this.stop() this.start() } }