Skip to content

Commit

Permalink
improve CLI interface (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize authored Jan 20, 2024
2 parents e64d620 + 2d41179 commit 3c91532
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 42 deletions.
10 changes: 4 additions & 6 deletions packages/azure-functions/src/handler-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ export class AzureFunctionsHandlerResolver extends BaseHandlerResolver {
});
const startTime = performance.now();
warnUnsupportedFeature(endpoint);
logger.info(
`Executing 'Functions.${endpoint.name}' (Reason='This function was programmatically called via the host APIs.', Id=${context.invocationId})`
);
logger.info(`Executing 'Functions.${endpoint.name}' (Id=${context.invocationId})`);
let result: HttpResponse | string | undefined | unknown;
try {
result = await endpoint.invokeHandler(new HttpRequest(req), context);
Expand All @@ -148,8 +146,8 @@ export class AzureFunctionsHandlerResolver extends BaseHandlerResolver {
logger.warn(`\n\n${yellow('No functions registered, did you forget to add functions?')}\n`);
}

logger.info(`Running with runtime: ${app.runtime}`);
console.log(`runtime: ${app.runtime}, isDevelopment: ${app.isDevelopment}`);
logger.debug(`Running with runtime: ${app.runtime}`);
logger.debug(`runtime: ${app.runtime}, isDevelopment: ${app.isDevelopment}`);

if (app.runtime === 'express' && process.env.NAMMATHAM_ENV !== 'development') {
throw new Error(
Expand All @@ -158,7 +156,7 @@ export class AzureFunctionsHandlerResolver extends BaseHandlerResolver {
);
}
if (app.isDevelopment === true) {
logger.info(`Running in development mode`);
logger.debug(`Running in development mode`);
logger.debug(`Skipping Register Azure Function handler in development mode`);
return;
}
Expand Down
10 changes: 4 additions & 6 deletions packages/azure-functions/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AfterServerStartedMetadata, NammathamApp } from '@nammatham/core';

import { gray, yellow } from 'colorette';
import { trimSlash } from '@nammatham/core';
import { blue, green, yellow } from 'colorette';

import type { AzureFunctionsEndpoint } from './types';

Expand Down Expand Up @@ -29,11 +29,10 @@ export async function printRegisteredFunctions(
.filter(func => func.type === 'azure-functions')
.filter(func => func.endpointOption?.type === 'http') as AzureFunctionsEndpoint<unknown, unknown>[];
if (azureFunctions.length === 0) return [];
await delay(100);
console.log(`\n${yellow('Functions:')}\n`);
for (const func of azureFunctions) {
const methods = `[${getMethods(func).join(',')}]`;
console.log(`\t${yellow(func.name)}: ${blue(methods)} ${green(getFullUrl(func, option.port))}\n`);
console.log(` - ${func.name} ${gray(methods)} ${gray(getFullUrl(func, option.port))}`);
}
console.log('');
return azureFunctions;
Expand All @@ -50,12 +49,11 @@ export async function printRegisteredNonHttpFunctions(
.filter(func => func.type === 'azure-functions')
.filter(func => func.endpointOption?.type !== 'http') as AzureFunctionsEndpoint<unknown, unknown>[];
if (azureFunctions.length === 0) return [];
await delay(100);
console.log(`${yellow(`----------------------------------------------`)}\n`);
console.log(`\n${yellow('Non-HTTP Functions (In Develpment Mode Only):')}\n`);
for (const func of azureFunctions) {
const methods = `[GET]`;
console.log(`\t${yellow(func.name)}: ${blue(methods)} ${green(getFullUrl(func, option.port))}\n`);
const methods = `[${getMethods(func).join(',')}]`;
console.log(` - ${func.name} ${gray(methods)} ${gray(getFullUrl(func, option.port))}`);
}
console.log('');
return azureFunctions;
Expand Down
7 changes: 5 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
"author": "Thada Wangthammang",
"license": "MIT",
"dependencies": {
"@types/express": "^4.17.21",
"colorette": "^2.0.20",
"pino": "^8.17.1",
"pino-pretty": "^10.3.0",
"undici": "5.20.0",
"@types/express": "^4.17.21"
"undici": "5.20.0"
},
"repository": {
"type": "git",
"url": "https://github.com/thaitype/nammatham.git"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"devDependencies": {
"pino-dev": "^4.0.3"
}
}
2 changes: 1 addition & 1 deletion packages/core/src/init-nammatham.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function createRuntime<Adapter extends BaseRuntimeAdapter<unknown>>(adapter?: Ad

export const initNammatham = {
create<Adapter extends BaseRuntimeAdapter<unknown> = DefaultAdapter>(adapter?: Adapter): NammathamRuntime<Adapter> {
logger.info(`Using adapter: ${yellow(adapter?.constructor.name ?? 'DefaultAdapter')}`);
logger.debug(`Using adapter: ${yellow(adapter?.constructor.name ?? 'DefaultAdapter')}`);
if (adapter === undefined) {
return createRuntime<Adapter>(new DefaultAdapter() as any);
}
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ const isDevelopment = process.env.NAMMATHAM_ENV === 'development';
export const _logger = pino({
level: process.env.NAMMATHAM_LOG_LEVEL || 'info',
transport: {
target: 'pino-pretty',
options: {
colorize: true,
},
target: 'pino-dev',
},
});

Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/nammatham-app.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { magenta } from 'colorette';
import { bgBlue, blue } from 'colorette';

import type { NammamthamEndpoint } from './types';
import type { BaseHandlerResolver } from './bases';

import { logger } from './main';

export async function logo() {
return `${bgBlue(`\n nammatham `)} ${blue('v' + (await import('../package.json')).version)}`;
}

export class NammathamApp {
protected readonly _functions: NammamthamEndpoint[] = [];
/**
Expand All @@ -20,6 +24,7 @@ export class NammathamApp {
* because Azure Functions will start the server for us.
*/
private _isDevelopment: boolean | undefined;
public readonly startTime = performance.now();

constructor(public readonly handlerResolver: BaseHandlerResolver) {}

Expand All @@ -30,8 +35,8 @@ export class NammathamApp {
async start() {
logger.debug('Registering functions...');
await this.handlerResolver.resolveRegisterHandler(this);
logger.info('All functions registered');
console.log(magenta(`\nStart Nammatham, Type-safe Serverless Library\n`));
logger.debug('All functions registered');
console.log(`${logo()} \n`);
}

addFunctions(...functions: NammamthamEndpoint[]) {
Expand All @@ -44,7 +49,7 @@ export class NammathamApp {
addFunction(func: NammamthamEndpoint) {
logger.debug(`Adding function "${func.name}" on route: ${func.endpointOption?.route}`);
this._functions.push(func);
logger.info(`Function "${func.name}" added`);
logger.debug(`Function "${func.name}" added`);
return this;
}

Expand Down
5 changes: 3 additions & 2 deletions packages/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
"license": "MIT",
"dependencies": {
"@nammatham/core": "2.0.0-alpha.8",
"express": "^4.18.2",
"@types/express": "^4.17.21"
"@types/express": "^4.17.21",
"colorette": "^2.0.20",
"express": "^4.18.2"
},
"repository": {
"type": "git",
Expand Down
16 changes: 13 additions & 3 deletions packages/express/src/express-server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { BaseHandlerResolver, NammathamApp } from '@nammatham/core';

import express from 'express';
import { logger } from '@nammatham/core';
import { logger, logo } from '@nammatham/core';
import { blue, gray, greenBright } from 'colorette';

import type { NammathamHttpHandlerOption } from './types';

Expand All @@ -27,7 +28,7 @@ export function expressPlugin(option?: ExpressServerOption) {
}
app.setRuntime('express');
app.setDevelopment(isDevelopment);
logger.info(`Using plugin: expressPlugin`);
logger.debug(`Using plugin: expressPlugin`);
startExpress(
{
handlerResolver,
Expand Down Expand Up @@ -63,7 +64,16 @@ export function startExpress(
);

expressApp.listen(port, async () => {
logger.info(`Dev Server started at http://localhost:${port}`);
console.clear();
const endTime = performance.now();
const durationMs = Math.floor(endTime - app.startTime);
logger.debug(`Server started at http://localhost:${port}`);
console.log(`${await logo()} ${gray(`ready in ${durationMs}ms`)}\n`);
console.log(`\n${blue('Express server started')}\n`);
console.log(` ┃ Local ${greenBright(`http://localhost:${port}`)}`);
console.log(` ┃ Host ${gray('Not Available')} \n`);

await handlerResolver.afterServerStarted(app, { port, allowAllFunctionsAccessByHttp });
// console.log(`\nServer Ready \n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n`);
});
}
6 changes: 4 additions & 2 deletions packages/trpc-azure-functions/src/trpc-azure-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ export interface TrpcAzureFunctionsPluginOption<TRouter extends AnyRouter> {
* Unstable, some features are not implemented yet.
*/

export function unstable__tRpcAzureFunctionsPlugin<TRouter extends AnyRouter>(option: TrpcAzureFunctionsPluginOption<TRouter>) {
export function unstable__tRpcAzureFunctionsPlugin<TRouter extends AnyRouter>(
option: TrpcAzureFunctionsPluginOption<TRouter>
) {
return (app: NammathamApp, handlerResolver: BaseHandlerResolver) => {
logger.info(`Using plugin: tRPC for AzureFunctions`);
logger.debug(`Using plugin: tRPC for AzureFunctions`);

const prefix = trimSlash(option?.prefix ?? '/trpc');

Expand Down
Loading

0 comments on commit 3c91532

Please sign in to comment.