From fcbe06feb36bd3d24d892cfe7a7caeb03e930087 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 17 Feb 2017 09:24:29 +0000 Subject: [PATCH] Add CLI --- package.json | 7 ++++--- src/cli.txt | 15 +++++++++++++++ src/index.ts | 28 ++++++++++++++++++++++++++++ src/server.ts | 4 ++-- src/types/fakes.d.ts | 1 + 5 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 src/cli.txt create mode 100644 src/index.ts diff --git a/package.json b/package.json index 122e88f..672322e 100644 --- a/package.json +++ b/package.json @@ -2,12 +2,12 @@ "name": "tstatic", "version": "1.0.0", "description": "Container to host simple static applications using a node server, so files can be deployed using rsync", - "main": "node ./dist/server.js", + "main": "node ./dist/index.js", "bin": { - "tstatic": "node ./dist/server.js" + "tstatic": "node ./dist/index.js" }, "scripts": { - "start": "node ./dist/server.js site/", + "start": "node ./dist/index.js", "postinstall": "typings install", "build": "tsc", "test": "npm run build && nsp check" @@ -28,6 +28,7 @@ "basic-auth": "=1.1.0", "compression": "=1.6.2", "connect-static-file": "=1.1.2", + "docopt": "=0.6.2", "express": "=4.14.1", "express-basic-auth": "=0.3.2", "express-ip-access-control": "=1.0.5", diff --git a/src/cli.txt b/src/cli.txt new file mode 100644 index 0000000..aa03abd --- /dev/null +++ b/src/cli.txt @@ -0,0 +1,15 @@ +tstatic. + +Usage: + tstatic [options] + tstatic -h | --help + tstatic --version + +Options: + -h --help Show this screen. + --version Show version. + -b --basic-auth= Enable basic-auth. + -i --ips= Allowed IP addresses. + -l --list-dir List Directory. + -o --opbeat Enable Opbeat. + diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..847aa1f --- /dev/null +++ b/src/index.ts @@ -0,0 +1,28 @@ +import { docopt } from 'docopt'; +import { readFileSync } from 'fs'; +import { join } from 'path'; +import { Options } from './types'; +import createServer from './server'; + +const ARG_DATA = readFileSync(join(__dirname, '..', 'src', 'cli.txt')).toString(); + +function getArgs() : Options { + const rawArgs = docopt(ARG_DATA, { + version: require('../package.json').version, + help: true + }); + return { + allowed_ips: rawArgs['--ips'] ? rawArgs['--ips'].split(',') : [], + basicAuth: rawArgs['--basic-auth'] || '', + dirList: rawArgs['--list-dir'], + serveDir: rawArgs[''], + opbeat: rawArgs['--opbeat'] + } +} + + +const app = createServer(getArgs()); + +const server = app.listen(5000, function () { + console.log("Server started on port " + server.address().port); +}); diff --git a/src/server.ts b/src/server.ts index 6903366..1eff99a 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,4 +1,4 @@ -import express from 'express'; +import express, { Application } from 'express'; import AccessControl from 'express-ip-access-control'; import compression from 'compression'; @@ -12,7 +12,7 @@ import handle404 from './middleware/404'; import { Options } from './types'; -export default function createServer(opts : Options) { +export default function createServer(opts : Options) : Application { const app = express(); const opbeatHandle = opbeat.start({ active: opts.opbeat diff --git a/src/types/fakes.d.ts b/src/types/fakes.d.ts index 2665eeb..2513eb8 100644 --- a/src/types/fakes.d.ts +++ b/src/types/fakes.d.ts @@ -6,3 +6,4 @@ declare module 'express-basic-auth'; declare module 'winston'; // doesnt like console transport declare module 'express-winston'; declare module 'opbeat'; +declare module 'docopt';