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

Commit

Permalink
fix(log): update log format
Browse files Browse the repository at this point in the history
  • Loading branch information
nahtnam committed Apr 19, 2019
1 parent a0eddcf commit 53f475f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/cli/commands/dev.tsx
Expand Up @@ -2,10 +2,11 @@ const start = Date.now();
import light from '../../index';
import { CommandBuilder } from 'yargs'; // eslint-disable-line
import { join, relative } from 'path';
import { black, bgGreen, bgBlueBright, redBright, yellow } from 'colorette';
import emojic from 'emojic';
import { yellow, blueBright } from 'colorette';

import importRoute from '../../utils/import-route';
import log from '../log';

export const command: string = 'dev';
export const aliases: string[] = ['d'];
Expand All @@ -25,7 +26,10 @@ interface Args {
}

const handle = async (argv: Args) => {
console.log(redBright(`${emojic.fire} igniting the server ${emojic.fire}`))
log('start', `${emojic.fire} igniting the server ${emojic.fire}`, {
titleColor: 'brightred',
messageColor: 'brightred',
});

const cwd = process.cwd();
const routesPath = join(cwd, './routes');
Expand All @@ -45,14 +49,22 @@ const handle = async (argv: Args) => {
}: ProcessEnv = process.env;

app.server.listen(PORT, (HOST as any), () => {
console.log(bgGreen(black(` ${Date.now() - start}ms `)), 'listening on 3000');
log(`${Date.now() - start}ms`, 'listening on 3000', {
titleColor: 'green',
});

console.log(bgBlueBright(black(' DEV ')), 'starting the hot reloader');
log('hmr', 'starting the hot reloader', {
titleColor: 'brightblue',
});
const chokidar = require('chokidar');
const watcher = chokidar.watch(routesPath);
watcher.on('ready', () => console.log(bgBlueBright(black(' DEV ')), 'watching for changes'));
watcher.on('ready', () => log('hmr', 'watching for changes', {
titleColor: 'brightblue',
}))
watcher.on('change', (p: string) => {
console.log(bgBlueBright(black(' DEV ')), `swapping out ${yellow(relative(cwd, p))}`);
log('hmr', `swapping out ${yellow(relative(cwd, p))}`, {
titleColor: 'brightblue',
});
delete require.cache[p];
importRoute(app.router, {
path: p,
Expand Down
38 changes: 38 additions & 0 deletions src/cli/log.ts
@@ -0,0 +1,38 @@
import {
blueBright, green, redBright, yellow,
} from 'colorette';

interface Options {
messageColor?: string;
titleColor?: string;
}

const color = (clr: string | undefined, message: string): string => {
switch (clr) {
case 'brightblue':
return blueBright(message);
case 'brightred':
return redBright(message);
case 'green':
return green(message);
case 'yellow':
return yellow(message);
default:
return message;
}
};

export default (title: string, message: string, opts: Options = {}): void => {
let str = '';
if (title) {
str += `[ ${color(opts.titleColor, title)} ] `;
}
let i = title.length;
while (i < 6) {
str += ' ';
i += 1;
}

str += color(opts.messageColor, message);
console.log(str); // eslint-disable-line
};

0 comments on commit 53f475f

Please sign in to comment.