#!/usr/bin/env node import path from 'path' import fs from 'fs' import WebSocket from 'ws' const identifier = 'de.lubiland.ts5.hotkeys' const XDG_CONFIG_HOME = process.env.XDG_CONFIG_HOME ?? path.join(process.env.HOME, '.config') const configDirectory = path.join(XDG_CONFIG_HOME, 'ts5-hotkeys') const keyFile = path.join(configDirectory, 'apikey') fs.readFile(keyFile, 'utf8', (err, data) => { if (!err && data) { // connect and authentiate with our existing API key const apiKey = data const ws = new WebSocket('ws://127.0.0.1:5899') ws.onopen = event => { console.log('Authenticating...') ws.send(JSON.stringify(({ 'type': 'auth', 'payload': { 'identifier': identifier, 'content': { 'apiKey': apiKey } } }))) } ws.onmessage = event => { if (JSON.parse(event.data).type === 'auth') { console.log('Triggering button event...') const buttonName = process.argv[2] let buttonEvent = { "type": "buttonPress", "payload": { "button": buttonName } } // send key-down and key-up event buttonEvent.payload.state = true console.log(buttonEvent) ws.send(JSON.stringify(buttonEvent)) buttonEvent.payload.state = false console.log(buttonEvent) ws.send(JSON.stringify(buttonEvent)) ws.close() } } } else { // connect and do an initial app registration const ws = new WebSocket('ws://127.0.0.1:5899') ws.onopen = event => { console.log('Registering app...') ws.send(JSON.stringify(({ 'type': 'auth', 'payload': { 'identifier': identifier, 'version': '0.0.1', 'name': 'External Hotkeys', 'description': 'External Hotkeys', 'content': { 'apiKey': '' } } }))) } ws.onmessage = event => { if (JSON.parse(event.data).type === 'auth') { const apiKey = JSON.parse(event.data).payload.apiKey console.log('Writing API key...') fs.mkdir(configDirectory, {recursive: true}, (err, path) => { if (!err) { fs.writeFile(keyFile, apiKey, err => { if (err) { throw err } }) } else if (err) { throw err } }) ws.close() } } } })