homebridge-appletv-enhanced
Version:
Plugin that exposes the Apple TV to HomeKit with much richer features than the vanilla Apple TV implementation of HomeKit.
104 lines • 3.51 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.delay = void 0;
exports.capitalizeFirstLetter = capitalizeFirstLetter;
exports.getLocalIP = getLocalIP;
exports.normalizePath = normalizePath;
exports.trimToMaxLength = trimToMaxLength;
exports.removeSpecialCharacters = removeSpecialCharacters;
exports.snakeCaseToTitleCase = snakeCaseToTitleCase;
exports.camelCaseToTitleCase = camelCaseToTitleCase;
exports.runCommand = runCommand;
const child_process_1 = require("child_process");
const os_1 = require("os");
const path_1 = __importDefault(require("path"));
function capitalizeFirstLetter(value) {
return value.charAt(0).toUpperCase() + value.slice(1);
}
const delay = async (ms) => new Promise((res) => setTimeout(res, ms));
exports.delay = delay;
function getLocalIP() {
const interfaces = (0, os_1.networkInterfaces)();
for (const k in interfaces) {
const networkInterface = interfaces[k];
for (const info of networkInterface) {
if (!info.internal && info.family === 'IPv4' && !info.address.startsWith('172.')) {
return info.address;
}
}
}
return 'homebridge.local';
}
function normalizePath(p) {
if (p.startsWith('~')) {
if (process.env.HOME === undefined) {
return undefined;
}
// join method does also normalize
return path_1.default.join(process.env.HOME, p.slice(1));
}
else {
return path_1.default.normalize(p);
}
}
function trimToMaxLength(value, maxLength) {
if (value.length <= maxLength) {
return value;
}
else {
return value.substring(0, maxLength);
}
}
function removeSpecialCharacters(str) {
return str.replace(/[^\p{L}\p{N} ']/gu, '').replace(/\s{2,}/g, ' ').trim();
}
function snakeCaseToTitleCase(str) {
return str
.replace(/^[-_]*(.)/, (_, c) => c.toUpperCase()) // Initial char (after -/_)
.replace(/[-_]+(.)/g, (_, c) => ' ' + c.toUpperCase()); // First char after each -/_
}
function camelCaseToTitleCase(str) {
const spacedStr = str.replace(/([A-Z])/g, ' $1');
const titleCase = spacedStr.charAt(0).toUpperCase() + spacedStr.slice(1);
return titleCase.replace('I Tunes', 'iTunes');
}
async function runCommand(logger, command, args, options, hideStdout = false, hideStderr = false) {
let running = true;
let stdout = '';
let stderr = '';
const p = (0, child_process_1.spawn)(command, args, options);
p.stdout.setEncoding('utf8');
p.stdout.on('data', (data) => {
stdout += data;
data = data.trim();
if (!hideStdout && data !== '') {
logger.info(data);
}
});
p.stderr.setEncoding('utf8');
p.stderr.on('data', (data) => {
stderr += data;
data = data.trim();
if (!hideStderr) {
if (data.startsWith('WARNING')) {
data = data.replace('WARNING: ', '');
logger.warn(data.replaceAll('\n', ''));
}
else {
data = data.replace('ERROR: ', '');
logger.error(data);
}
}
});
p.on('close', () => {
running = false;
});
while (running) {
await (0, exports.delay)(100);
}
return [stdout, stderr, p.exitCode];
}
//# sourceMappingURL=utils.js.map