Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
fix(start): add start command
Browse files Browse the repository at this point in the history
  • Loading branch information
nahtnam committed May 22, 2019
1 parent 44b529e commit 2be1b2c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/cli/commands/dev.tsx → src/cli/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import importRoute from '../../utils/import-route';

export const command = 'dev [dir]';
export const aliases: string[] = ['d'];
export const desc = 'start a development srvr';
export const desc = 'start a development server';

export const builder: CommandBuilder = {
log: {
Expand Down Expand Up @@ -73,6 +73,7 @@ const handle = async (argv: Args): Promise<void> => {
watcher.on('change', (p: string): void => {
logger.hmr(`swapping out ${chalk.yellow(relative(cwd, p))}`);
delete require.cache[p];
app.router.unrouteAll();
const routes = findRoutes(routesPath);
routes.forEach((route: Route): void => {
delete require.cache[route.path];
Expand Down
72 changes: 72 additions & 0 deletions src/cli/commands/start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { CommandBuilder } from 'yargs'; // eslint-disable-line
import { join } from 'path';
import emojic from 'emojic';
import logger from '../../utils/logger';

import { server } from '../../index';

export const command = 'start [dir]';
export const aliases: string[] = ['s'];
export const desc = 'start a production server';

export const builder: CommandBuilder = {
log: {
alias: 'l',
description: 'enable or disable logs',
boolean: true,
default: true,
},
port: {
alias: 'p',
description: 'specify which port the server should run on',
},
dir: {
default: './',
description: 'base directory for the light server',
hidden: true,
},
};

interface Args {
log: boolean;
dir: string;
port?: string;
}

const handle = async (argv: Args): Promise<void> => {
logger.start(`${emojic.fire} igniting the server ${emojic.fire}`);

const cwd = join(process.cwd(), argv.dir);
const routesPath = join(cwd, './routes');

const app = server({
routes: routesPath,
log: argv.log,
});

interface ProcessEnv {
[key: string]: string | number | undefined;
}

const {
HOST = '0.0.0.0',
}: ProcessEnv = process.env;

let {
PORT = 3000,
}: ProcessEnv = process.env;
if (argv.port) {
PORT = argv.port;
}

app.server.listen(PORT, (HOST as any), (): void => {
logger.listening(`on port ${PORT}`);
});
};

export const handler = (argv: Args): void => {
handle(argv).catch((err: Error): void => {
logger.fatal(err);
process.exit(1);
});
};
17 changes: 9 additions & 8 deletions src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,6 @@ export default (route: Route): Handler => {
}
};

// TODO: Fix this
/* istanbul ignore next */
const isAWS: boolean = !!(process.env.LIGHT_ENVIRONMENT && process.env.LIGHT_ENVIRONMENT.toLowerCase() === 'aws');
/* istanbul ignore if */
if (isAWS) {
return AWSServerlessMicro(handleErrors(exec));
}

return run(Req, Res, handleErrors(youchErrors(exec)));
};

Expand All @@ -136,6 +128,15 @@ export default (route: Route): Handler => {
fn.module = __dirname;
fn.handler = fn;

// TODO: Fix this
/* istanbul ignore next */
const { LIGHT_ENVIRONMENT } = process.env;
const isAWS: boolean = LIGHT_ENVIRONMENT === 'aws';
/* istanbul ignore if */
if (isAWS) {
return AWSServerlessMicro(fn);
}

const isRunKit: boolean = !!(process.env.LIGHT_ENVIRONMENT && process.env.LIGHT_ENVIRONMENT.toLowerCase() === 'runkit');
if (isRunKit) {
return {
Expand Down

0 comments on commit 2be1b2c

Please sign in to comment.