Просмотр исходного кода

feat(core): Prettier console greeting on server start

Michael Bromley 5 лет назад
Родитель
Сommit
fc30dfd22c
2 измененных файлов с 27 добавлено и 13 удалено
  1. 19 7
      packages/core/src/bootstrap.ts
  2. 8 6
      packages/core/src/plugin/plugin-utils.ts

+ 19 - 7
packages/core/src/bootstrap.ts

@@ -15,7 +15,7 @@ import { registerCustomEntityFields } from './entity/register-custom-entity-fiel
 import { setEntityIdStrategy } from './entity/set-entity-id-strategy';
 import { validateCustomFieldsConfig } from './entity/validate-custom-fields-config';
 import { getConfigurationFunction, getEntitiesFromPlugins } from './plugin/plugin-metadata';
-import { logProxyMiddlewares } from './plugin/plugin-utils';
+import { getProxyMiddlewareCliGreetings } from './plugin/plugin-utils';
 
 export type VendureBootstrapFunction = (config: VendureConfig) => Promise<INestApplication>;
 
@@ -267,12 +267,24 @@ function logWelcomeMessage(config: RuntimeVendureConfig) {
         version = ' unknown';
     }
     const { port, shopApiPath, adminApiPath } = config.apiOptions;
-    Logger.info(`=================================================`);
-    Logger.info(`Vendure server (v${version}) now running on port ${port}`);
-    Logger.info(`Shop API: http://localhost:${port}/${shopApiPath}`);
-    Logger.info(`Admin API: http://localhost:${port}/${adminApiPath}`);
-    logProxyMiddlewares(config);
-    Logger.info(`=================================================`);
+    const apiCliGreetings: Array<[string, string]> = [];
+    apiCliGreetings.push(['Shop API', `http://localhost:${port}/${shopApiPath}`]);
+    apiCliGreetings.push(['Admin API', `http://localhost:${port}/${adminApiPath}`]);
+    apiCliGreetings.push(...getProxyMiddlewareCliGreetings(config));
+    const columnarGreetings = arrangeCliGreetingsInColumns(apiCliGreetings);
+    const title = `Vendure server (v${version}) now running on port ${port}`;
+    const maxLineLength = Math.max(title.length, ...columnarGreetings.map(l => l.length));
+    const titlePadLength = title.length < maxLineLength ? Math.floor((maxLineLength - title.length) / 2) : 0;
+    Logger.info(`=`.repeat(maxLineLength));
+    Logger.info(title.padStart(title.length + titlePadLength));
+    Logger.info('-'.repeat(maxLineLength).padStart(titlePadLength));
+    columnarGreetings.forEach(line => Logger.info(line));
+    Logger.info(`=`.repeat(maxLineLength));
+}
+
+function arrangeCliGreetingsInColumns(lines: Array<[string, string]>): string[] {
+    const columnWidth = Math.max(...lines.map(l => l[0].length)) + 2;
+    return lines.map(l => `${(l[0] + ':').padEnd(columnWidth)}${l[1]}`);
 }
 
 /**

+ 8 - 6
packages/core/src/plugin/plugin-utils.ts

@@ -108,19 +108,21 @@ export interface ProxyOptions {
 }
 
 /**
- * If any proxy middleware has been set up using the createProxyHandler function, log this information.
+ * Generate CLI greeting lines for any proxy middleware that was set up with the createProxyHandler function.
  */
-export function logProxyMiddlewares(config: RuntimeVendureConfig) {
-    const {} = config.apiOptions;
+export function getProxyMiddlewareCliGreetings(config: RuntimeVendureConfig): Array<[string, string]> {
+    const output: Array<[string, string]> = [];
     for (const middleware of config.apiOptions.middleware || []) {
         if ((middleware.handler as any).proxyMiddleware) {
             const { port, hostname, label, route, basePath } = (middleware.handler as any)
                 .proxyMiddleware as ProxyOptions;
-            Logger.info(
-                `${label}: http://${config.apiOptions.hostname || 'localhost'}:${
+            output.push([
+                label,
+                `http://${config.apiOptions.hostname || 'localhost'}:${
                     config.apiOptions.port
                 }/${route}/ -> http://${hostname || 'localhost'}:${port}${basePath ? `/${basePath}` : ''}`,
-            );
+            ]);
         }
     }
+    return output;
 }