cli/src/squeebotd.ts

69 lines
1.8 KiB
TypeScript
Raw Permalink Normal View History

2020-11-21 15:41:31 +00:00
#!/usr/bin/env node
import * as path from 'path';
import readline from 'readline';
import yargs from 'yargs';
import { loadEnvironment } from '@squeebot/core/lib/core';
import { Squeebot } from './core';
2020-11-28 19:08:56 +00:00
import { SqueebotCLI } from './cli';
2020-11-21 15:41:31 +00:00
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
async function start(argv: any): Promise<void> {
if (!argv.environment) {
throw new Error('Environment file is missing.');
}
let root;
if (argv.root) {
root = path.resolve(argv.root);
}
2021-09-18 22:20:34 +00:00
const env = await loadEnvironment(argv.environment, root);
2020-11-21 15:41:31 +00:00
2020-12-07 18:49:44 +00:00
// Change working directory to the environment
process.chdir(env.path as string);
2021-09-18 22:20:34 +00:00
// Create and initialize the Squeebot instance
2020-11-21 15:41:31 +00:00
const sb = new Squeebot(env);
2020-11-28 19:08:56 +00:00
await sb.initialize(argv.e !== true);
// Create a CLI if interactive mode is enabled
if (argv.i === true) {
const sbrl = new SqueebotCLI(sb);
sbrl.attach(rl);
}
2020-11-21 15:41:31 +00:00
sb.attachReadline(rl);
}
yargs.scriptName('squeebotd')
.command('$0 [environment]', 'start Squeebot daemon', (y) => {
y.positional('environment', {
demandOption: 'The environment file is mandatory.',
describe: 'The environment file to use, in json format',
})
2021-12-18 08:36:28 +00:00
.option('r', {
alias: 'root',
describe: 'Change the root directory of execution to differ from the one in your environment file',
nargs: 1,
type: 'string',
})
.option('e', {
alias: 'no-enable',
describe: 'Do not automatically execute enabled plugins on startup',
type: 'boolean',
})
.option('i', {
alias: 'interactive',
describe: 'Enable built-in command line interface',
type: 'boolean',
});
2020-11-21 15:41:31 +00:00
});
start(yargs.argv).catch((e) => console.error(e.stack));