pooliot-client
Version:
96 lines (84 loc) • 2.99 kB
JavaScript
/* eslint-disable no-console */
/* global fetch */
import 'alp-node/fetch';
import Logger, { listenUnhandledErrors } from 'nightingale/src';
import updateNotifier from 'update-notifier';
import program from 'commander';
import argv from 'minimist-argv';
import pkg from '../../package.json';
import install from './install/run';
updateNotifier({ pkg }).notify();
const logger = new Logger('cli');
listenUnhandledErrors(logger);
program.option('-p, --webPort <port>', 'pooliot web port').version(pkg.version);
program
.command('install')
.alias('i')
.option('-h, --host <host>', 'deprecated - replaced by address')
.option('--port <port>', 'deprecated - replaced by address')
.option('--userId <userId>', 'deprecated - replaced by token')
.option('--piUser <piUser>', 'deprecated - replaced by user')
.option('-i, --interactive', 'ask what you want to install')
.option('-a, --address <host>:<port>', 'remote pooliot server')
.option('-u, --user <user>', 'user used to run programs')
.option('-t, --token <token>', 'authentification token')
.action(options => {
if (!options.address && options.host) {
options.address = `${options.host}:${options.port}`;
}
install(options, pkg).catch(err => {
console.error(err);
process.exit(1);
});
});
const call = (requestUri, options, retry) =>
fetch(`http://localhost:${argv.webPort || '80'}/api/${requestUri}`, options)
.then(res => res.text())
.then(res => console.log(res))
.catch(err => {
console.log(err.message || err);
if (!retry) setTimeout(() => call(requestUri, options, true), 3000);
});
program
.command('self-update')
.alias('selfupdate')
.alias('selfUpdate')
.action(() => call('self-update', { method: 'POST' }));
program
.command('screen [state]')
.description(
`get or set the state of the screen:
on: turn the screen on
off: turn the screen off`,
)
.action(state => {
if (state === 'on' || state === 'off') {
return call('screen', { method: 'POST', body: JSON.stringify({ state }) });
}
return call('screen');
});
program
.command('display [action]')
.description(
`action on the current display program (chromium / livestreamer / ...):
status: display status
restart: restart the current display
refresh: refresh the page (only works for browsers)
openbox-started: notify that openbox is started (internal)`,
)
.action((action = 'status') => {
switch (action) {
case 'status':
return call('display');
case 'start':
return call('display/start', { method: 'POST' });
case 'openbox-started':
return call('display/openbox-started', { method: 'POST' });
case 'restart':
return call('display/restart', { method: 'POST' });
case 'refresh':
return call('display/refresh', { method: 'POST' });
}
throw new Error(`Unsupported display instruction: ${argv._[1]}`);
});
program.parse(process.argv);