From 5ebc03aaf7d2e612ed13895cde8a617820a08788 Mon Sep 17 00:00:00 2001 From: Romain Lenzotti Date: Wed, 27 Nov 2024 08:40:57 +0100 Subject: [PATCH 1/2] feat(cli): update index template --- packages/cli/templates/init/src/index.ts.hbs | 40 ++++++++++++++------ 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/packages/cli/templates/init/src/index.ts.hbs b/packages/cli/templates/init/src/index.ts.hbs index a131cfcf0..650dccab2 100644 --- a/packages/cli/templates/init/src/index.ts.hbs +++ b/packages/cli/templates/init/src/index.ts.hbs @@ -2,17 +2,35 @@ import {$log} from "@tsed/logger"; import { {{platformSymbol}} } from "@tsed/platform-{{platform}}"; import {Server} from "./{{entryServer}}.js"; -async function bootstrap() { - try { - const platform = await {{platformSymbol}}.bootstrap(Server); - await platform.listen(); +const SIG_EVENTS = [ + "beforeExit", + "SIGHUP", + "SIGINT", + "SIGQUIT", + "SIGILL", + "SIGTRAP", + "SIGABRT", + "SIGBUS", + "SIGFPE", + "SIGUSR1", + "SIGSEGV", + "SIGUSR2", + "SIGTERM" +]; - process.on("SIGINT", () => { - platform.stop(); - }); - } catch (error) { - $log.error({event: "SERVER_BOOTSTRAP_ERROR", message: error.message, stack: error.stack}); - } +try { + const platform = await {{platformSymbol}}.bootstrap(Server); + await platform.listen(); + + SIG_EVENTS.forEach((evt) => process.on(evt, close)); + + ["uncaughtException", "unhandledRejection"].forEach((evt) => + process.on(evt, async (error) => { + $log.error({event: "SERVER_" + evt.toUpperCase(), message: error.message, stack: error.stack}); + await stop(); + }) + ); +} catch (error) { + $log.error({event: "SERVER_BOOTSTRAP_ERROR", message: error.message, stack: error.stack}); } -bootstrap(); From 784eaeb1cfd4628172973560dc4dbb9d73e9a906 Mon Sep 17 00:00:00 2001 From: Romain Lenzotti Date: Wed, 27 Nov 2024 08:55:18 +0100 Subject: [PATCH 2/2] feat(cli): update template to use new Functional API when it's possible --- .../init.integration.spec.ts.snap | 20 ++--- .../init.integration.spec.ts.snap | 10 +-- .../init.integration.spec.ts.snap | 10 +-- packages/cli/templates/generate/factory.hbs | 40 ++------- packages/cli/templates/generate/server.hbs | 10 +-- packages/cli/templates/generate/value.hbs | 33 +------ .../init.integration.spec.ts.snap | 90 ++++++------------- .../init/init.integration.spec.ts | 18 ++-- 8 files changed, 61 insertions(+), 170 deletions(-) diff --git a/packages/cli-plugin-oidc-provider/test/init/__snapshots__/init.integration.spec.ts.snap b/packages/cli-plugin-oidc-provider/test/init/__snapshots__/init.integration.spec.ts.snap index d823b9e46..80b4a7984 100644 --- a/packages/cli-plugin-oidc-provider/test/init/__snapshots__/init.integration.spec.ts.snap +++ b/packages/cli-plugin-oidc-provider/test/init/__snapshots__/init.integration.spec.ts.snap @@ -62,8 +62,8 @@ exports[`Init OIDC Provider project > should generate a project with oidc 1`] = exports[`Init OIDC Provider project > should generate a project with oidc 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -109,11 +109,7 @@ import {InteractionsController} from "./controllers/oidc/InteractionsController. ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; @@ -178,8 +174,8 @@ exports[`Init OIDC Provider project > should generate a project with oidc and sw exports[`Init OIDC Provider project > should generate a project with oidc and swagger 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -233,11 +229,7 @@ import {InteractionsController} from "./controllers/oidc/InteractionsController. ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; diff --git a/packages/cli-plugin-typegraphql/test/init/__snapshots__/init.integration.spec.ts.snap b/packages/cli-plugin-typegraphql/test/init/__snapshots__/init.integration.spec.ts.snap index e9fa4a4d4..98f75e251 100644 --- a/packages/cli-plugin-typegraphql/test/init/__snapshots__/init.integration.spec.ts.snap +++ b/packages/cli-plugin-typegraphql/test/init/__snapshots__/init.integration.spec.ts.snap @@ -2,8 +2,8 @@ exports[`Init TypeGraphQL project > should generate a project with typegraphql 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -46,11 +46,7 @@ import * as rest from "./controllers/rest/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; diff --git a/packages/cli-plugin-typeorm/test/integrations/init/__snapshots__/init.integration.spec.ts.snap b/packages/cli-plugin-typeorm/test/integrations/init/__snapshots__/init.integration.spec.ts.snap index 74695f2eb..f97b1f7b5 100644 --- a/packages/cli-plugin-typeorm/test/integrations/init/__snapshots__/init.integration.spec.ts.snap +++ b/packages/cli-plugin-typeorm/test/integrations/init/__snapshots__/init.integration.spec.ts.snap @@ -2,8 +2,8 @@ exports[`TypeORM: Init cmd > should generate a project with the right options 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -43,11 +43,7 @@ import * as rest from "./controllers/rest/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; diff --git a/packages/cli/templates/generate/factory.hbs b/packages/cli/templates/generate/factory.hbs index b4d258f6c..1a4746bbe 100644 --- a/packages/cli/templates/generate/factory.hbs +++ b/packages/cli/templates/generate/factory.hbs @@ -1,35 +1,11 @@ -import {Configuration, Inject, registerProvider} from "@tsed/di"; - -/** - * Inject {{symbolName}} to the service, controller, etc... - * - * ## Usage - * - * ```typescript - * import {Injectable} from "@tsed/di"; - * import {{{symbolName}}} from "./{{symbolName}}"; - * - * @Injectable() - * class MyService { - * @{{symbolName}}() - * private factory: {{symbolName}}; - * - * // or - * constructor(@{{symbolName}}() private factory: {{symbolName}}){} - * } - * - * @decorator - */ -export function {{symbolName}}() { - return Inject({{symbolName}}); -} +import {constant, injectable} from "@tsed/di"; export type {{symbolName}} = any; // implement type or interface for type checking +export const {{symbolName}} = injectable(Symbol.for("{{symbolName}}") + .factory(() => { + // retrieve custom configuration from settings (e.g.: `@Configuration({hello: "value"})`) + const hello = constant("hello"); -registerProvider({ - provide: {{symbolName}}, - deps: [Configuration], - useFactory(settings: Configuration) { - return {}; - } -}); + return {hello}; + }) + .token(); diff --git a/packages/cli/templates/generate/server.hbs b/packages/cli/templates/generate/server.hbs index 85d83094f..654310ebf 100644 --- a/packages/cli/templates/generate/server.hbs +++ b/packages/cli/templates/generate/server.hbs @@ -1,6 +1,6 @@ import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; {{#forEach imports}}{{#if tsIngore}} // @ts-ignore {{/if}}import {{symbols}}{{#if symbols}} from {{/if}}"{{from}}";{{comment}} @@ -53,9 +53,5 @@ import {PlatformApplication} from "@tsed/platform-http"; ] }) export class {{symbolName}} { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } diff --git a/packages/cli/templates/generate/value.hbs b/packages/cli/templates/generate/value.hbs index be8555d26..0ac52c3c2 100644 --- a/packages/cli/templates/generate/value.hbs +++ b/packages/cli/templates/generate/value.hbs @@ -1,32 +1,3 @@ -import {Configuration, Inject, registerProvider} from "@tsed/di"; +import {injectable} from "@tsed/di"; -/** - * Inject {{symbolName}} to the service, controller, etc... - * - * ## Usage - * - * ```typescript - * import {Injectable} from "@tsed/di"; - * import {{{symbolName}}} from "./{{symbolName}}"; - * - * @Injectable() - * class MyService { - * @{{symbolName}}() - * private value: {{symbolName}}; - * - * // or - * constructor(@{{symbolName}}() private value: {{symbolName}}){} - * } - * - * @decorator - */ -export function {{symbolName}}() { - return Inject({{symbolName}}); -} - -export type {{symbolName}} = any; // implement type or interface for type checking - -registerProvider({ - provide: {{symbolName}}, - useValue: {} -}); +export const {{symbolName}} = injectable(Symbol.for("{{symbolName}}")).value({}).token(); diff --git a/packages/cli/test/integrations/init/__snapshots__/init.integration.spec.ts.snap b/packages/cli/test/integrations/init/__snapshots__/init.integration.spec.ts.snap index de14ed6f9..bb642e808 100644 --- a/packages/cli/test/integrations/init/__snapshots__/init.integration.spec.ts.snap +++ b/packages/cli/test/integrations/init/__snapshots__/init.integration.spec.ts.snap @@ -2,8 +2,8 @@ exports[`Init cmd > Express.js > should generate a project with Arch FEATURE 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -54,19 +54,15 @@ import * as pages from "./pages/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Express.js > should generate a project with Babel 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -106,19 +102,15 @@ import * as rest from "./controllers/rest/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Express.js > should generate a project with Bun 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -158,19 +150,15 @@ import * as rest from "./controllers/rest/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Express.js > should generate a project with Convention ANGULAR 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -221,19 +209,15 @@ import * as pages from "./controllers/pages/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Express.js > should generate a project with NPM 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -273,19 +257,15 @@ import * as rest from "./controllers/rest/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Express.js > should generate a project with Webpack 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -325,19 +305,15 @@ import * as rest from "./controllers/rest/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Express.js > should generate a project with swagger 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -388,19 +364,15 @@ import * as pages from "./controllers/pages/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Express.js > should generate a project with the right options 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-express"; // /!\\ keep this import import "@tsed/ajv"; @@ -440,19 +412,15 @@ import * as rest from "./controllers/rest/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; exports[`Init cmd > Koa.js > should generate a project with the right options 2`] = ` "import {join} from "path"; -import {Configuration, Inject} from "@tsed/di"; -import {PlatformApplication} from "@tsed/platform-http"; +import {Configuration} from "@tsed/di"; +import {application} from "@tsed/platform-http"; import "@tsed/platform-log-request"; // remove this import if you don't want log request import "@tsed/platform-koa"; // /!\\ keep this import import "@tsed/ajv"; @@ -490,11 +458,7 @@ import * as rest from "./controllers/rest/index.js"; ] }) export class Server { - @Inject() - protected app: PlatformApplication; - - @Configuration() - protected settings: Configuration; + protected app = application(); } " `; diff --git a/packages/cli/test/integrations/init/init.integration.spec.ts b/packages/cli/test/integrations/init/init.integration.spec.ts index 374a73893..620a473eb 100644 --- a/packages/cli/test/integrations/init/init.integration.spec.ts +++ b/packages/cli/test/integrations/init/init.integration.spec.ts @@ -69,7 +69,7 @@ describe("Init cmd", () => { `); const content = FakeCliFs.entries.get("project-name/src/Server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot(); @@ -178,7 +178,7 @@ describe("Init cmd", () => { const content = FakeCliFs.entries.get("project-name/src/Server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toContain('import * as pages from "./controllers/pages/index.js"'); @@ -270,7 +270,7 @@ describe("Init cmd", () => { `); const content = FakeCliFs.entries.get("project-name/src/Server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot(); @@ -362,7 +362,7 @@ describe("Init cmd", () => { `); const content = FakeCliFs.entries.get("project-name/src/Server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot(); @@ -455,7 +455,7 @@ describe("Init cmd", () => { `); const content = FakeCliFs.entries.get("project-name/src/Server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot(); @@ -548,7 +548,7 @@ describe("Init cmd", () => { `); const content = FakeCliFs.entries.get("project-name/src/Server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot(); @@ -649,7 +649,7 @@ describe("Init cmd", () => { `); const content = FakeCliFs.entries.get("project-name/src/server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot(); @@ -715,7 +715,7 @@ describe("Init cmd", () => { `); const content = FakeCliFs.entries.get("project-name/src/server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-express"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot(); @@ -777,7 +777,7 @@ describe("Init cmd", () => { const content = FakeCliFs.entries.get("project-name/src/Server.ts")!; - expect(content).toContain('import {Configuration, Inject} from "@tsed/di"'); + expect(content).toContain('import {application} from "@tsed/platform-http"'); expect(content).toContain('import "@tsed/platform-koa"'); expect(content).toContain('import "@tsed/ajv"'); expect(content).toMatchSnapshot();