Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #32
- Loading branch information
Showing
3 changed files
with
63 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters