homebridge-config-ui-x
Version:
A web based management, configuration and control platform for Homebridge
128 lines • 5.64 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const os = require("os");
const color = require("bash-color");
const semver = require("semver");
const pty = require("node-pty-prebuilt-multiarch");
const child_process = require("child_process");
const common_1 = require("@nestjs/common");
const config_service_1 = require("../../core/config/config.service");
let LogService = class LogService {
constructor(configService) {
this.configService = configService;
if (typeof this.configService.ui.log !== 'object') {
this.logNotConfigured();
}
else if (this.configService.ui.log.method === 'file' && this.configService.ui.log.path) {
this.logFromFile();
}
else if (this.configService.ui.log.method === 'systemd') {
this.logFromSystemd();
}
else if (this.configService.ui.log.method === 'custom' && this.configService.ui.log.command) {
this.logFromCommand();
}
else {
this.logNotConfigured();
}
}
connect(client, size) {
if (!semver.satisfies(process.version, `>=${this.configService.minimumNodeVersion}`)) {
client.emit('stdout', color.yellow(`Node.js v${this.configService.minimumNodeVersion} higher is required for ${this.configService.name}.\n\r`));
client.emit('stdout', color.yellow(`You may experience issues while running on Node.js ${process.version}.\n\r\n\r`));
}
if (this.command) {
client.emit('stdout', color.cyan(`Loading logs using "${this.configService.ui.log.method}" method...\r\n`));
client.emit('stdout', color.cyan(`CMD: ${this.command.join(' ')}\r\n\r\n`));
this.tailLog(client, size);
}
else {
client.emit('stdout', color.red(`Cannot show logs. "log" option is not configured correctly in your Homebridge config.json file.\r\n\r\n`));
client.emit('stdout', color.cyan(`See https://github.com/oznu/homebridge-config-ui-x#log-viewer-configuration for instructions.\r\n`));
}
}
tailLog(client, size) {
const command = [...this.command];
const term = pty.spawn(command.shift(), command, {
name: 'xterm-color',
cols: size.cols,
rows: size.rows,
cwd: this.configService.storagePath,
env: process.env,
});
term.onData((data) => {
client.emit('stdout', data);
});
term.onExit((code) => {
try {
client.emit('stdout', '\n\r');
client.emit('stdout', color.red(`The log tail command "${command.join(' ')}" exited with code ${code}.\n\r`));
client.emit('stdout', color.red(`Please check the command in your config.json is correct.\n\r\n\r`));
client.emit('stdout', color.cyan(`See https://github.com/oznu/homebridge-config-ui-x#log-viewer-configuration for instructions.\r\n`));
}
catch (e) {
}
});
client.on('resize', (resize) => {
try {
term.resize(resize.cols, resize.rows);
}
catch (e) { }
});
const onEnd = () => {
client.removeAllListeners('resize');
client.removeAllListeners('end');
client.removeAllListeners('disconnect');
try {
term.kill();
}
catch (e) { }
if (this.configService.ui.sudo && term && term.pid) {
child_process.exec(`sudo -n kill -9 ${term.pid}`);
}
};
client.on('end', onEnd.bind(this));
client.on('disconnect', onEnd.bind(this));
}
logFromFile() {
let command;
if (os.platform() === 'win32') {
command = ['powershell.exe', '-command', `Get-Content -Path '${this.configService.ui.log.path}' -Wait -Tail 200`];
}
else {
command = ['tail', '-n', '500', '-f', this.configService.ui.log.path];
if (this.configService.ui.sudo) {
command.unshift('sudo', '-n');
}
}
this.command = command;
}
logFromSystemd() {
const command = ['journalctl', '-o', 'cat', '-n', '500', '-f', '-u', this.configService.ui.log.service || 'homebridge'];
if (this.configService.ui.sudo) {
command.unshift('sudo', '-n');
}
this.command = command;
}
logFromCommand() {
this.command = this.configService.ui.log.command.split(' ');
}
logNotConfigured() {
this.command = null;
}
};
LogService = __decorate([
common_1.Injectable(),
__metadata("design:paramtypes", [config_service_1.ConfigService])
], LogService);
exports.LogService = LogService;
//# sourceMappingURL=log.service.js.map