From d9d5a0669241ed14541a1452aa0deaa12c1109b4 Mon Sep 17 00:00:00 2001 From: Richard Zampieri Date: Mon, 9 Oct 2023 00:27:34 -0700 Subject: [PATCH 1/3] fix: add global prefix on opinionated --- src/adapter-express/application-express.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/adapter-express/application-express.ts b/src/adapter-express/application-express.ts index feb0a9a..0271fd2 100644 --- a/src/adapter-express/application-express.ts +++ b/src/adapter-express/application-express.ts @@ -45,6 +45,7 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress private environment: ServerEnvironment; private container: Container; private middlewares: Array = []; + private globalPrefix: string | undefined; protected configureServices(): void | Promise {} protected postServerInitialization(): void | Promise {} @@ -80,7 +81,9 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress const allMiddlewareEntries: Array = [...this.middlewares]; - const expressServer = new InversifyExpressServer(container); + const expressServer = new InversifyExpressServer(container, null, { + rootPath: this.globalPrefix ? this.globalPrefix : "/", + }); expressServer.setConfig((app: express.Application) => { allMiddlewareEntries.forEach((entry) => { @@ -138,6 +141,19 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress await Promise.resolve(this.postServerInitialization()); } + /** + * Sets the global route prefix for the application. + * + * @public + * @method setGlobalRoutePrefix + * + * @param {string} prefix - The prefix to use for all routes. + * @use Use this method inside of `configureServices()` hook. + */ + public setGlobalRoutePrefix(prefix: string): void { + this.globalPrefix = prefix; + } + /** * Configures the application's view engine based on the provided configuration options. * From 046fd2c2e2c5beacf9602f929b223713d48e8b9c Mon Sep 17 00:00:00 2001 From: Richard Zampieri Date: Mon, 9 Oct 2023 22:11:39 -0700 Subject: [PATCH 2/3] fix: add global route prefix for both templates --- src/adapter-express/application-express.ts | 29 ++++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/adapter-express/application-express.ts b/src/adapter-express/application-express.ts index 0271fd2..4686bc1 100644 --- a/src/adapter-express/application-express.ts +++ b/src/adapter-express/application-express.ts @@ -65,12 +65,10 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress * @param middlewares - An array of Express middlewares to be applied. * @returns The configured Application instance. */ - public async create( + private async init( container: Container, middlewares: Array = [], ): Promise { - this.container = container; - await Promise.resolve(this.configureServices()); const middleware = container.get(Middleware); @@ -82,7 +80,7 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress const allMiddlewareEntries: Array = [...this.middlewares]; const expressServer = new InversifyExpressServer(container, null, { - rootPath: this.globalPrefix ? this.globalPrefix : "/", + rootPath: this.globalPrefix as string, }); expressServer.setConfig((app: express.Application) => { @@ -112,6 +110,23 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress return this; } + /** + * Create and configure the Express application. + * @param container - The InversifyJS container. + * @param middlewares - An array of Express middlewares to be applied. + * @returns The configured Application instance. + */ + public async create( + container: Container, + middlewares: Array = [], + ): Promise { + this.container = container; + this.middlewares = middlewares; + this.globalPrefix = this.globalPrefix || "/"; + + return this; + } + /** * Start listening on the given port and environment. * @param port - The port number to listen on. @@ -123,6 +138,10 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress environment: ServerEnvironment, consoleMessage?: IApplicationMessageToConsole, ): Promise { + /* Initializes the application and executes the middleware pipeline */ + await this.init(this.container, this.middlewares as Array); + + /* Sets the port and environment */ this.port = port; this.environment = environment; this.app.set("env", environment); @@ -148,7 +167,7 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress * @method setGlobalRoutePrefix * * @param {string} prefix - The prefix to use for all routes. - * @use Use this method inside of `configureServices()` hook. + * */ public setGlobalRoutePrefix(prefix: string): void { this.globalPrefix = prefix; From b35af7b9ea91935572920314eddc8f5258bca4bc Mon Sep 17 00:00:00 2001 From: Richard Zampieri Date: Mon, 9 Oct 2023 22:31:07 -0700 Subject: [PATCH 3/3] fix: adjust build script and remove esm build option --- package.json | 11 +++-------- scripts/copy.js | 35 +++++++++++++++++++++++++++++++++++ scripts/mv.js | 30 ++++++++++++++++++++++++++++++ scripts/rm.js | 19 +++++++++++++++++++ tsconfig.base.json | 21 ++++++--------------- tsconfig.esm.json | 11 ----------- 6 files changed, 93 insertions(+), 34 deletions(-) create mode 100644 scripts/copy.js create mode 100644 scripts/mv.js create mode 100644 scripts/rm.js delete mode 100644 tsconfig.esm.json diff --git a/package.json b/package.json index cecce0a..098ab17 100644 --- a/package.json +++ b/package.json @@ -51,15 +51,10 @@ }, "scripts": { "prepare": "husky install", - "win-clean": "if exist lib (del /q lib\\*)", - "linux-clean": "rm -rf lib/*", - "win-cpy": "xcopy package.json lib\\ && xcopy README.md lib\\ && xcopy CHANGELOG.md lib\\ /Y", - "linux-cpy": "cp package.json README.md CHANGELOG.md lib/", - "build:win": "npm run win-clean && npm run build:esm && npm run build:cjs && npm run win-cpy", - "build:linux": "npm run linux-clean && npm run build:esm && npm run build:cjs && npm run linux-cpy", - "build:esm": "tsc -p tsconfig.esm.json && mv lib/esm/index.js lib/esm/index.mjs ", + "clean": "node scripts/rm.js lib", + "copy": "node scripts/copy.js package.json README.md CHANGELOG.md lib", + "build": "npm run clean && npm run build:cjs && npm run copy", "build:cjs": "tsc -p tsconfig.cjs.json", - "git": "powershell -Command \"git add .; git commit -m \"%1\"; npm run release\"", "release": "release-it", "test": "jest", "format": "prettier --write \"src/**/*.ts\" --cache", diff --git a/scripts/copy.js b/scripts/copy.js new file mode 100644 index 0000000..48f1931 --- /dev/null +++ b/scripts/copy.js @@ -0,0 +1,35 @@ +const fs = require("fs"); +const path = require("path"); + +function copyRecursiveSync(src, dest) { + const exists = fs.existsSync(src); + const stats = exists && fs.statSync(src); + const isDirectory = exists && stats.isDirectory(); + + if (isDirectory) { + fs.mkdirSync(dest); + fs.readdirSync(src).forEach(function (childItemName) { + copyRecursiveSync( + path.join(src, childItemName), + path.join(dest, childItemName), + ); + }); + } else { + fs.copyFileSync(src, dest); + } +} + +if (process.argv.length < 4) { + process.stderr.write( + "Usage: node copy.js ... \n", + ); + process.exit(1); +} + +const destination = process.argv[process.argv.length - 1]; + +for (let i = 2; i < process.argv.length - 1; i++) { + const origin = process.argv[i]; + copyRecursiveSync(origin, path.join(destination, path.basename(origin))); + process.stdout.write(`Copied: ${origin} to ${destination}\n`); +} diff --git a/scripts/mv.js b/scripts/mv.js new file mode 100644 index 0000000..5187ed6 --- /dev/null +++ b/scripts/mv.js @@ -0,0 +1,30 @@ +const fs = require("fs").promises; + +const moveFile = async (origin, destination) => { + try { + await fs.access(origin); + } catch (error) { + process.stderr.write(`Error: Origin '${origin}' not found\n`); + process.exit(1); + } + + try { + await fs.rename(origin, destination); + process.stdout.write(`Move: ${origin} to ${destination}\n`); + } catch (error) { + process.stderr.write( + `Error: Unable to move '${origin}' to '${destination}'\n`, + ); + process.exit(1); + } +}; + +if (process.argv.length !== 4) { + process.stderr.write("Usage: node mv.js \n"); + process.exit(1); +} + +const origin = process.argv[2]; +const destination = process.argv[3]; + +moveFile(origin, destination); diff --git a/scripts/rm.js b/scripts/rm.js new file mode 100644 index 0000000..98053d6 --- /dev/null +++ b/scripts/rm.js @@ -0,0 +1,19 @@ +const fs = require("fs").promises; + +const removeTarget = async (target) => { + try { + await fs.rm(target, { recursive: true, force: true }); + process.stdout.write(`Removed: ${target}\n`); + } catch (error) { + process.stderr.write(`Error: Unable to remove '${target}'\n`); + process.exit(1); + } +}; + +if (process.argv.length !== 3) { + process.stderr.write("Usage: node rm.js \n"); + process.exit(1); +} + +const target = process.argv[2]; +removeTarget(target); diff --git a/tsconfig.base.json b/tsconfig.base.json index dd7560a..0a50e52 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -2,27 +2,18 @@ "compilerOptions": { "strict": true, "esModuleInterop": true, - "forceConsistentCasingInFileNames":true, + "forceConsistentCasingInFileNames": true, "skipLibCheck": true, "strictNullChecks": false, "checkJs": true, "allowJs": true, "declaration": true, - "declarationMap": true, "noImplicitAny": false, "allowSyntheticDefaultImports": true, "experimentalDecorators": true, "emitDecoratorMetadata": true, - "types": [ - "node", - "reflect-metadata", - "jest" - ], -}, - "include": [ - "src/**/*.ts" - ], - "exclude": [ - "node_modules", - ] -} \ No newline at end of file + "types": ["node", "reflect-metadata", "jest"] + }, + "include": ["src/**/*.ts"], + "exclude": ["node_modules"] +} diff --git a/tsconfig.esm.json b/tsconfig.esm.json deleted file mode 100644 index ca0f6d8..0000000 --- a/tsconfig.esm.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "extends": "./tsconfig.base.json", - "compilerOptions": { - "lib": ["ES2022"], - "target": "ES2022", - "module": "NodeNext", - "moduleResolution": "NodeNext", - "outDir": "lib/esm", - "declarationDir": "lib/esm/types" - } -}