homebridge-appletv-enhanced
Version:
Plugin that exposes the Apple TV to HomeKit with much richer features than the vanilla Apple TV implementation of HomeKit.
160 lines • 7.03 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = require("axios");
const axios_2 = __importDefault(require("axios"));
const PrefixLogger_1 = __importDefault(require("./PrefixLogger"));
const compare_versions_1 = require("compare-versions");
const package_json_1 = __importDefault(require("../package.json"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const fs_extra_1 = require("fs-extra");
const utils_1 = require("./utils");
const UIX_CUSTOM_PLUGIN_PATH = process.env.UIX_CUSTOM_PLUGIN_PATH !== undefined
? fs_1.default.realpathSync(process.env.UIX_CUSTOM_PLUGIN_PATH)
: undefined;
const UIX_USE_PNPM = process.env.UIX_USE_PNPM === '1';
class UpdateChecker {
autoUpdate;
includeBetas;
interval;
log;
updateCheckHour;
constructor(logger, autoUpdate = false, includeBetas = false, updateCheckHour = 3) {
this.log = new PrefixLogger_1.default(logger, 'Update check');
this.updateCheckHour = updateCheckHour;
this.includeBetas = includeBetas;
this.autoUpdate = autoUpdate;
this.log.info(`The update checker is configured to check for updates between ${updateCheckHour}:00 and ${updateCheckHour}:59, \
${includeBetas ? 'including' : 'excluding'} betas. Auto updating is turned ${autoUpdate ? 'on' : 'off'}.`);
}
async check(infoOrDebugLogLevel = 'debug') {
const latestVersion = await this.getLatestVersion();
if (latestVersion === undefined) {
return;
}
const currentVersion = this.getCurrentVersion();
if ((0, compare_versions_1.compareVersions)(latestVersion, currentVersion) === 1) {
this.log.warn(`There is a new version of AppleTV Enhanced available (${this.includeBetas ? 'including' : 'excluding'} \
betas): ${latestVersion}. You are currently using ${currentVersion}`);
if (this.autoUpdate) {
await this.update(latestVersion);
}
}
else {
const msg = `You are using the latest version of AppleTV Enhanced (${this.includeBetas ? 'including' : 'excluding'} \
betas): ${currentVersion}`;
if (infoOrDebugLogLevel === 'debug') {
this.log.debug(msg);
}
else {
this.log.success(msg);
}
}
}
startInterval(skipInitialCheck = false) {
this.log.debug('Starting update check interval.');
if (skipInitialCheck) {
this.log.debug('Skipping initial update check.');
}
else {
this.intervalMethod();
}
this.interval = setInterval(this.intervalMethod.bind(this), 60 * 60 * 1000);
}
stopInterval() {
if (this.interval !== undefined) {
this.log.debug('Stopping update check interval.');
clearInterval(this.interval);
this.interval = undefined;
}
else {
this.log.warn('Could not stop update check interval since there is no active update check interval.');
}
}
getCurrentVersion() {
return package_json_1.default.version;
}
async getLatestVersion() {
let response = undefined;
try {
response = await axios_2.default.get('https://registry.npmjs.org/homebridge-appletv-enhanced');
}
catch (e) {
if (e instanceof axios_1.AxiosError) {
this.log.warn(`Could not query version information from NPM in order to check for updates: ${e.message}`);
}
else {
throw e;
}
return undefined;
}
const latestStableVersion = response.data['dist-tags'].latest;
const latestBetaVersion = response.data['dist-tags'].beta;
let outputVersion = undefined;
if (!this.includeBetas) {
outputVersion = latestStableVersion;
}
else {
if ((0, compare_versions_1.compareVersions)(latestStableVersion, latestBetaVersion) === 1) {
outputVersion = latestStableVersion;
}
else {
outputVersion = latestBetaVersion;
}
}
this.log.debug(`The latest AppleTV Enhanced version (${this.includeBetas ? 'including' : 'excluding'} betas) is ${outputVersion}`);
return outputVersion;
}
intervalMethod() {
if (new Date().getHours() === this.updateCheckHour) {
this.log.debug('Triggering the check method since the configured update check time is now.');
void this.check();
}
else {
this.log.debug(`Not triggering the check method as the update check is configured to run between ${this.updateCheckHour}:00 \
and ${this.updateCheckHour}:59.`);
}
}
// eslint-disable-next-line max-len
// https://github.com/homebridge/homebridge-config-ui-x/blob/01a008227b8b8f3650b23eaac3e9370347cec4f8/src/modules/plugins/plugins.service.ts#L384-L493
async update(version) {
this.log.info(`Attempting to update AppleTV Enhanced to version ${version}`);
if (UIX_CUSTOM_PLUGIN_PATH === undefined) {
this.log.error('Could not determine the path where to install the plugin since the environment variable UIX_CUSTOM_PLUGIN_PATH \
is not set.');
return;
}
const npm = UIX_USE_PNPM ? 'pnpm' : 'npm';
let installPath = path_1.default.resolve(__dirname, '..', '..');
this.log.debug(`custom plugin path - ${UIX_CUSTOM_PLUGIN_PATH}`);
this.log.debug(`install path - ${installPath}`);
const installOptions = !UIX_USE_PNPM
? [
'--no-audit',
'--no-fund',
]
: [];
// check to see if custom plugin path is using a package.json file
if (installPath === UIX_CUSTOM_PLUGIN_PATH &&
(0, fs_extra_1.pathExistsSync)(path_1.default.resolve(installPath, '../package.json')) === true) {
installOptions.push('--save');
}
// install path is one level up
installPath = path_1.default.resolve(installPath, '..');
const args = ['install', ...installOptions, `homebridge-appletv-enhanced@${version}`];
this.log.info(`CMD: ${npm} "${args.join('" "')}" (cwd: ${installPath})`);
const [, , exitCode] = await (0, utils_1.runCommand)(this.log, npm, args, { cwd: installPath });
if (exitCode === 0) {
this.log.success(`AppleTV Enhanced has successfully been updated to ${version}. Restarting now ...`);
process.exit(0);
}
else {
this.log.error(`An error has occurred while updating AppleTV Enhanced. Exit code: ${exitCode}.`);
}
}
}
exports.default = UpdateChecker;
//# sourceMappingURL=UpdateChecker.js.map