Home Reference Source Repository

src/cli.js

#!/usr/bin/env node
import {ArgumentParser} from 'argparse'
import keypress from 'keypress'
import pkg from '../package.json'
import {RestTestServer} from './components/server'

const argParser = new ArgumentParser({
	version: pkg.version,
	addHelp: true,
	description: 'IMPORTANT: External content is loaded relative from current working directory.'
});

argParser.addArgument(
	['serverConfig'],
	{
		metavar: 'server_config',
		help: 'The JSON formatted data file that contains the initial data.'
	}
);

argParser.addArgument(
	['-c', '--config'],
	{
		metavar: 'HAPI_CONFIG',
		help: 'Server configuration file. Must either be in JSON format or export a configuration object. ' +
		'See http://hapijs.com/api#new-serveroptions for more details.'
	}
);

const args = argParser.parseArgs();

let cwd = process.cwd();
if (!cwd.endsWith('/')) cwd += '/';
const serverConfig = require(cwd + args.serverConfig);
const hapiConfig = args.config ? require(cwd + args.config) : {};

const server = new RestTestServer(serverConfig, hapiConfig);

keypress(process.stdin);
process.stdin.on('keypress', (ch, key) => {
	if (key && key.ctrl && key.name === 'c') process.exit(0);
	switch (ch) {
		case 'q':
			server.stop(() => {
				process.exit(0);
			});
			break;
		case 'r':
			server.restart(() => {
				console.log('REST API Test Server restarted.');
			});
			break;
		default:
			return;
	}
});

if (process.stdin.setRawMode && process.stdin.setRawMode.apply) {
	process.stdin.setRawMode(true);
}
process.stdin.resume();

console.log('Press Q at any time to shutdown the server.');
console.log('Press R at any time to reload the server.');

server.start(() => console.log('REST API Test Server started.'));