@smj0x/homebridge-shortcuts-buttons
Version:
Run any Apple Shortcut with just the tap of a button, and execute a custom unix command (or another shortcut!) after completion to handle its success/failure, using x-callback-url.
104 lines • 4.1 kB
JavaScript
/**
* @module server
*/
import { PLUGIN_NAME } from '../settings.js';
import { HSBShortcut, HSBShortcutStatus } from '../shortcut.js';
import { HSBUtils } from '../utils.js';
class HSBXCallbackUrlServerCommandEnvironment {
SHORTCUT_NAME;
SHORTCUT_STATUS;
SHORTCUT_RESULT;
SHORTCUT_ERROR;
constructor({ shortcut, status, result, errorMessage }) {
if (!shortcut || !status) {
throw new Error(`${this.constructor.name} Invalid callback url search params`);
}
this.SHORTCUT_NAME = shortcut;
this.SHORTCUT_STATUS = status;
if (result !== null) {
this.SHORTCUT_RESULT = result;
}
if (errorMessage !== null) {
this.SHORTCUT_ERROR = errorMessage;
}
Object.freeze(this);
}
}
export class HSBXCallbackUrlServerCommand {
config;
utils;
environment;
constructor(searchParams, config, utils) {
this.config = config;
this.utils = utils;
this.environment = new HSBXCallbackUrlServerCommandEnvironment(searchParams);
}
run() {
switch (this.config.callbackCommandType) {
case 'Default (display notification)':
return this.executeCommand(this.callbackDefaultCommand);
case 'Custom unix command':
this.ensureValidCustomCommand();
return this.executeCommand(this.config.callbackCustomCommand);
case 'Shortcut name':
this.ensureValidCustomCommand();
return this.runShortcut();
default:
return this.throwRunError('Unexpected callback command type configuration value');
}
}
ensureValidCustomCommand() {
if (!HSBUtils.isNonEmptyString(this.config.callbackCustomCommand)) {
this.throwRunError('Missing custom command configuration value');
}
}
throwRunError(message) {
throw new Error(`${this.constructor.name}::${this.run.name} ` +
`${message} (callbackCommandType=${this.config.callbackCommandType})`);
}
executeCommand(command) {
return this.utils.execAsync(command, {
env: this.environment,
timeout: this.config.callbackCommandTimeout,
});
}
runShortcut() {
const input = Buffer.from(JSON.stringify(this.environment)).toString('base64');
const shortcut = new HSBShortcut(this.config.callbackCustomCommand, null, this.utils, input);
return shortcut.run();
}
get callbackDefaultCommand() {
let subtitle = this.environment.SHORTCUT_NAME;
let sound;
switch (this.environment.SHORTCUT_STATUS) {
case HSBShortcutStatus.SUCCESS:
subtitle += ' executed successfully';
if (HSBUtils.isNonEmptyString(this.environment.SHORTCUT_RESULT)) {
subtitle += `\nResult: ${this.environment.SHORTCUT_RESULT}`;
}
sound = 'Glass';
break;
case HSBShortcutStatus.ERROR:
subtitle += ' execution failed';
if (HSBUtils.isNonEmptyString(this.environment.SHORTCUT_ERROR)) {
subtitle += `\nError: ${this.environment.SHORTCUT_ERROR}`;
}
sound = 'Sosumi';
break;
case HSBShortcutStatus.CANCEL:
subtitle += ' execution was cancelled';
sound = 'Sosumi';
break;
default:
subtitle += ' received an unknown result status';
sound = 'Sosumi';
}
return (`open "${this.defaultCommandExecutableAbsolutePath}" ` +
`--env NOTIFICATION_TITLE="${this.config.name}" ` +
`--env NOTIFICATION_SUBTITLE="${subtitle}" ` +
`--env NOTIFICATION_SOUND="${sound}"`);
}
defaultCommandExecutableAbsolutePath = `/usr/local/lib/node_modules/${PLUGIN_NAME}` +
`/dist/bin/HomebridgeShortcutsButtons - Notify Shortcut Result.app`;
}
//# sourceMappingURL=command.js.map