Skip to content

Commit

Permalink
fix: commandline argument passing
Browse files Browse the repository at this point in the history
fixes #32
  • Loading branch information
zcstarr committed Jun 4, 2019
1 parent 9fb0a0b commit 189f5af
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
45 changes: 45 additions & 0 deletions src/cli/commands.ts
@@ -0,0 +1,45 @@
import fs from "fs-extra";
import _ from "lodash";
import { Config } from "../lib/config";
import { makeLogger } from "src/lib/logging";
import { Command } from "commander";
import { ServiceRunnerServer } from "../";
const logger = makeLogger("ServiceRunner", "Commands");

interface ParsedCommands {
port: string;
dir: string;
extendedConfig: any;
test: boolean;
}

const parseCommands = async (prog: Command) => {
let dir = "./services";
let port = "8002";
let extendedConfig: any;
if (prog.config) { extendedConfig = await fs.readJSON(prog.config); }
if (prog.dir) { dir = prog.dir; }
if (prog.port) { port = prog.port; }
return {port, dir, test: prog.test && !_.isEmpty(prog.config), extendedConfig};
};

const testConfiguration = async (extendedConfig: any) => {
const cfg = new Config(extendedConfig);
logger.info(`Configuration is valid!`);
};

const launchCommands = async ({port, dir, extendedConfig}: ParsedCommands) => {
const serviceRunnerServer = new ServiceRunnerServer(extendedConfig, dir, port);
logger.info(`Service Runner starting on ${port}`);
const started = serviceRunnerServer.start();
logger.info(`Service Runner started on ${port}`);
return started;
};

export const startServiceRunner = async (program: any): Promise<void> => {
const commands = await parseCommands(program);
if (commands.test) {
return testConfiguration(commands.extendedConfig);
}
return launchCommands(commands);
};
31 changes: 17 additions & 14 deletions src/cli/index.ts
@@ -1,36 +1,39 @@
#!/usr/bin/env node
import program from "commander";
import program, { Command } from "commander";
import fs from "fs-extra";
const version = require("../../../package.json").version; // tslint:disable-line
import { ServiceRunnerServer } from "../";
import { makeLogger } from "../lib/logging";
import { startServiceRunner } from "./commands";
import _ from "lodash";

const logger = makeLogger("ServiceRunner", "CLI");
program
.version(version, "-v, --version")
.option(
"-c, --config",
"-c, --config <configFile>",
"JSON file path pointing to a service runner config file",
"./build/src/service-runner-config.json",
)
.option(
"-d, --dir",
"-d, --dir <directory>",
"Directory for storing services",
"./services",
)
.option(
"-p, --port",
"-p, --port <port>",
"Set port for service runner",
"8002",
)
.option(
"-t, --test",
"Test configuration",
)
.action(async () => {
let dir = "./services";
let port = "8002";
let extendedConfig: any;
if (program.config) { extendedConfig = await fs.readJSON(program.config); }
if (program.dir) { dir = program.dir; }
if (program.port) { port = program.port; }
const serviceRunnerServer = new ServiceRunnerServer(extendedConfig, dir, port);
logger.info(`Service Runner started on ${port}`);
await serviceRunnerServer.start();
try {
await startServiceRunner(program);
} catch (e) {
logger.error("Could not start service runner.");
logger.error(e);
}
})
.parse(process.argv);
1 change: 1 addition & 0 deletions tslint.json
Expand Up @@ -8,6 +8,7 @@
"no-unused-variable": true,
"object-literal-sort-keys": false,
"max-classes-per-file" : false,
"max-line-length": false,
"interface-name": [false]
}
}

0 comments on commit 189f5af

Please sign in to comment.