Skip to content

Commit

Permalink
feat: add pretty address printer to userland
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianCataldo committed May 26, 2024
1 parent 6722d49 commit 89cac9f
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 71 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions packages/engine/src/dev/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,5 @@ export async function startUserProvidedServer(options: {
// },
// };
}

export { printNodeHttpServerAddressInfos as printAddressInfos } from '../server/utils.js';
2 changes: 2 additions & 0 deletions packages/engine/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ export const createHandler: CreateHandler = async ({

return { handlers: [gracileHandler as RequestHandler], vite: null };
};

export { printNodeHttpServerAddressInfos as printAddressInfos } from './utils.js';
49 changes: 26 additions & 23 deletions packages/engine/src/server/utils.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
// NOTE: Util. to pretty print for user provided server.

// import { logger } from '@gracile/internal-utils/logger';
// import { DEV } from 'esm-env';
// import type { Server } from 'http';
// import c from 'picocolors';
import { logger } from '@gracile/internal-utils/logger';
import { DEV } from 'esm-env';
import type { Server } from 'http';
import c from 'picocolors';

// export function printNodeHttpServerAddressInfos(
// instance: Server,
// expose: boolean = true,
// ) {
// const infos = instance.address();
// logger.info(c.green(`${DEV ? 'development' : 'production'} server started`), {
// timestamp: true,
// });
import { IP_EXPOSED } from './env.js';

// if (typeof infos === 'object' && infos && infos.port && infos.address) {
// logger.info(
// `
// ${c.dim('┃')} Local ${c.cyan(`http://localhost:${infos.port}/`)}
// ${c.dim('┃')} Network ${expose ? c.cyan(`http://${infos.address}:${infos.port}/`) : c.dim(`use ${c.bold('--host')} to expose`)}
// `,
// );
export function printNodeHttpServerAddressInfos(instance: Server) {
const infos = instance.address();
logger.info(c.green(`${DEV ? 'development' : 'production'} server started`), {
timestamp: true,
});

// return infos;
// }
if (typeof infos === 'object' && infos && infos.port && infos.address) {
logger.info(
`
${c.dim('┃')} Local ${c.cyan(`http://localhost:${infos.port}/`)}` +
`${
infos.address === IP_EXPOSED
? `${c.dim('┃')} Network ${c.cyan(`http://${infos.address}:${infos.port}/`)}`
: ''
}
`,
);

// throw Error('Invalid address/port.');
// }
return infos;
}

throw Error('Invalid address/port.');
}
6 changes: 3 additions & 3 deletions packages/internal/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
"exports": {
"./*": "./dist/*.js",
"./logger": {
"development": "./dist/logger.js",
"test": "./dist/logger.js",
"development": "./dist/logger.dev.js",
"test": "./dist/logger.dev.js",
"preview": "./dist/logger.preview.js",
"default": "./dist/logger.prod.js"
"default": "./dist/logger.build.js"
}
},
"files": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-unused-vars */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck TODO: Implement stubs

import type { Logger } from 'vite';

const NOT_PROD = process.env.NODE_ENV !== 'production';

export const logger: Logger = {
info(msg: string, options?: LogOptions): void {
// throw new Error('Function not implemented.');
if (NOT_PROD) console.info(msg);
},
warn(msg: string, options?: LogOptions): void {
// throw new Error('Function not implemented.');
if (NOT_PROD) console.warn(msg);
},
warnOnce(msg: string, options?: LogOptions): void {
// throw new Error('Function not implemented.');
if (NOT_PROD) console.warn(msg);
},
error(msg: string, options?: LogErrorOptions): void {
// throw new Error('Function not implemented.');
if (NOT_PROD) console.error(msg);
},
clearScreen(type: LogType): void {
// throw new Error('Function not implemented.');
if (NOT_PROD) console.clear();
},
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
hasErrorLogged(error: Error | RollupError): boolean {
// throw new Error('Function not implemented.');
if (NOT_PROD) console.error(error);
},
hasWarned: false,
};
File renamed without changes.
30 changes: 1 addition & 29 deletions packages/internal/utils/src/logger.preview.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-unused-vars */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck TODO: Implement stubs

import type { Logger } from 'vite';

export const logger: Logger = {
info(msg: string, options?: LogOptions): void {
console.info(msg, options);
},
warn(msg: string, options?: LogOptions): void {
console.warn(msg, options);
},
warnOnce(msg: string, options?: LogOptions): void {
console.warn(msg, options);
},
error(msg: string, options?: LogErrorOptions): void {
console.error(msg, options);
},
clearScreen(type: LogType): void {
console.clear();
},
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
hasErrorLogged(error: Error | RollupError): boolean {
console.error(error);
},
hasWarned: false,
};
// TODO: Separate from prod. and make prod. empty/customizable.

0 comments on commit 89cac9f

Please sign in to comment.