From 9fab55a3ed3f11e8c1138bcfe01609365d1a6ddd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Tue, 3 Oct 2023 19:05:05 +0100 Subject: [PATCH] chore(npm): upgrade project for Node.js v20.x --- node | 22 --------------- package-lock.json | 31 ++++++++++++++------ package.json | 9 +++--- src/exceptions/NotFoundResolveException.ts | 20 ------------- src/helpers/Module.ts | 16 ++++------- tests/unit/ExecTest.ts | 6 +++- tests/unit/ModuleTest.ts | 10 ------- tsconfig.json | 33 ++-------------------- 8 files changed, 39 insertions(+), 108 deletions(-) delete mode 100755 node delete mode 100644 src/exceptions/NotFoundResolveException.ts diff --git a/node b/node deleted file mode 100755 index 5a47d02..0000000 --- a/node +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash - -# This file is useful for development purposes because -# it allows you to run TypeScript files without having -# to add the --loader option every time. - -# Usage: - -# ./node --version -# ./node bin/test.ts -# ./node --inspect bin/test.ts - -# Usage with "sh" or "bash": - -# sh node --version -# sh node bin/test.ts -# sh node --inspect bin/test.ts - -# Node.js executable with all arguments required to run the application. -node="node --loader=ts-node/esm --experimental-import-meta-resolve --no-warnings" - -$node "$@" diff --git a/package-lock.json b/package-lock.json index aa1505f..66d86fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/common", - "version": "4.15.5", + "version": "4.16.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@athenna/common", - "version": "4.15.5", + "version": "4.16.0", "license": "MIT", "dependencies": { "@fastify/formbody": "^7.4.0", @@ -34,7 +34,8 @@ "youch-terminal": "^2.2.2" }, "devDependencies": { - "@athenna/test": "^4.7.0", + "@athenna/test": "^4.9.1", + "@athenna/tsconfig": "^4.5.0", "@types/bytes": "^3.1.1", "@types/callsite": "^1.0.31", "@types/debug": "^4.1.7", @@ -106,9 +107,9 @@ "dev": true }, "node_modules/@athenna/test": { - "version": "4.7.0", - "resolved": "https://registry.npmjs.org/@athenna/test/-/test-4.7.0.tgz", - "integrity": "sha512-2g5M9SVY55IIJ/ucznwUPIJIdRJXt5a1r4eR2oLGLR/EEdvCyDEf4oRCjCIfsM5KBq3H0ubHRm5ec7dAISg4HQ==", + "version": "4.9.1", + "resolved": "https://registry.npmjs.org/@athenna/test/-/test-4.9.1.tgz", + "integrity": "sha512-oyhqLj6Y9turEF7G+HTtznX9f389j2fjpE+rLCtt1gb3D3IJqigRxKxhhbbalG0j2LAmwex4HtoWqDbkxH+PKg==", "dev": true, "dependencies": { "@japa/assert": "^1.4.1", @@ -119,6 +120,12 @@ "sinon": "^15.1.0" } }, + "node_modules/@athenna/tsconfig": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/@athenna/tsconfig/-/tsconfig-4.5.0.tgz", + "integrity": "sha512-jgLFpxCAzMiE6zavulwMrZl0/02gDQGWEU8Ysy4V00QgAXypUCEqv42XTYU7u1xQqG+xNPc/w0yX8Mdhu+2M+A==", + "dev": true + }, "node_modules/@babel/code-frame": { "version": "7.22.5", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.22.5.tgz", @@ -9286,9 +9293,9 @@ "dev": true }, "@athenna/test": { - "version": "4.7.0", - "resolved": "https://registry.npmjs.org/@athenna/test/-/test-4.7.0.tgz", - "integrity": "sha512-2g5M9SVY55IIJ/ucznwUPIJIdRJXt5a1r4eR2oLGLR/EEdvCyDEf4oRCjCIfsM5KBq3H0ubHRm5ec7dAISg4HQ==", + "version": "4.9.1", + "resolved": "https://registry.npmjs.org/@athenna/test/-/test-4.9.1.tgz", + "integrity": "sha512-oyhqLj6Y9turEF7G+HTtznX9f389j2fjpE+rLCtt1gb3D3IJqigRxKxhhbbalG0j2LAmwex4HtoWqDbkxH+PKg==", "dev": true, "requires": { "@japa/assert": "^1.4.1", @@ -9299,6 +9306,12 @@ "sinon": "^15.1.0" } }, + "@athenna/tsconfig": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/@athenna/tsconfig/-/tsconfig-4.5.0.tgz", + "integrity": "sha512-jgLFpxCAzMiE6zavulwMrZl0/02gDQGWEU8Ysy4V00QgAXypUCEqv42XTYU7u1xQqG+xNPc/w0yX8Mdhu+2M+A==", + "dev": true + }, "@babel/code-frame": { "version": "7.22.5", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.22.5.tgz", diff --git a/package.json b/package.json index e04f320..7ca3a31 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/common", - "version": "4.15.5", + "version": "4.16.0", "description": "The Athenna common helpers to use in any Node.js ESM project.", "license": "MIT", "author": "João Lenon ", @@ -19,8 +19,8 @@ "scripts": { "build": "rimraf build && tsc --project bin/tsconfig.build.json", "lint:fix": "eslint \"{bin,src,tests}/**/*.ts\" --fix", - "test": "npm run --silent lint:fix && sh node bin/test.ts", - "test:debug": "cross-env NODE_DEBUG=athenna:* sh node --inspect bin/test.ts", + "test": "npm run --silent lint:fix && node --import=@athenna/tsconfig bin/test.ts", + "test:debug": "cross-env NODE_DEBUG=athenna:* node --inspect bin/test.js", "test:coverage": "c8 npm run --silent test" }, "files": [ @@ -75,7 +75,8 @@ "youch-terminal": "^2.2.2" }, "devDependencies": { - "@athenna/test": "^4.7.0", + "@athenna/test": "^4.9.1", + "@athenna/tsconfig": "^4.5.0", "@types/bytes": "^3.1.1", "@types/callsite": "^1.0.31", "@types/debug": "^4.1.7", diff --git a/src/exceptions/NotFoundResolveException.ts b/src/exceptions/NotFoundResolveException.ts deleted file mode 100644 index 0e14d83..0000000 --- a/src/exceptions/NotFoundResolveException.ts +++ /dev/null @@ -1,20 +0,0 @@ -/** - * @athenna/common - * - * (c) João Lenon - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -import { Exception } from '#src/helpers/Exception' - -export class NotFoundResolveException extends Exception { - public constructor() { - super({ - code: 'E_NOT_FOUND_RESOLVE', - message: `The importa.meta.resolve function does not exist in import.meta object.`, - help: 'The resolve function is not defined because the --experimental-import-meta-resolve option in Node CLI is required when running your application. Try to set up this option when running your application.' - }) - } -} diff --git a/src/helpers/Module.ts b/src/helpers/Module.ts index 2d0dab9..df0806c 100644 --- a/src/helpers/Module.ts +++ b/src/helpers/Module.ts @@ -12,7 +12,6 @@ import { Path, File, Folder } from '#src' import { createRequire } from 'node:module' import { fileURLToPath, pathToFileURL } from 'node:url' import { extname, dirname, resolve, isAbsolute } from 'node:path' -import { NotFoundResolveException } from '#src/exceptions/NotFoundResolveException' export class Module { /** @@ -188,14 +187,10 @@ export class Module { * Resolve the module path by meta url and import it. */ public static async resolve(path: string, meta: string): Promise { - if (!import.meta.resolve || process.env.RESOLVE_TESTING) { - throw new NotFoundResolveException() - } - - const splited = path.split('?') - const queries = splited[1] || '' + const splitted = path.split('?') + const queries = splitted[1] || '' - path = splited[0] + path = splitted[0] if (!path.startsWith('#') && extname(path)) { path = resolve(path) @@ -212,9 +207,8 @@ export class Module { queries ) - return import.meta - .resolve(path, meta) - .then(resolved => Module.get(import(`${resolved}?${queries}`))) + // `await` is not needed for `import.meta.resolve` method, but TypeScript complains on it. + return Module.get(import(await import.meta.resolve(path, meta))) } /** diff --git a/tests/unit/ExecTest.ts b/tests/unit/ExecTest.ts index dcc441b..7c49986 100644 --- a/tests/unit/ExecTest.ts +++ b/tests/unit/ExecTest.ts @@ -109,7 +109,11 @@ export default class ExecTest { @Test() public async shouldBeAbleToExecuteANodeScriptInTheVMAndGetTheStdout({ assert }: Context) { - const { stdout, stderr, exitCode } = await Exec.node(Path.fixtures('node-script.ts')) + const { stdout, stderr, exitCode } = await Exec.node(Path.fixtures('node-script.ts'), [ + 'node', + '--loader=ts-node/esm', + Path.fixtures('node-script.ts') + ]) assert.equal(exitCode, 0) assert.equal(stdout, 'hello') diff --git a/tests/unit/ModuleTest.ts b/tests/unit/ModuleTest.ts index df2b546..debca0e 100644 --- a/tests/unit/ModuleTest.ts +++ b/tests/unit/ModuleTest.ts @@ -9,7 +9,6 @@ import { Module, Path } from '#src' import { Test, type Context } from '@athenna/test' -import { NotFoundResolveException } from '#src/exceptions/NotFoundResolveException' export default class ModuleTest { @Test() @@ -150,13 +149,4 @@ export default class ModuleTest { assert.deepEqual(chalk, (await import('chalk')).default) } - - @Test() - public async shouldThrownAnExceptionWhenTheImportMetaResolveFunctionIsNotDefined({ assert }: Context) { - process.env.RESOLVE_TESTING = 'true' - - await assert.rejects(() => Module.resolve('chalk', import.meta.url), NotFoundResolveException) - - delete process.env.RESOLVE_TESTING - } } diff --git a/tsconfig.json b/tsconfig.json index 5a20cac..14f70e0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,33 +1,4 @@ { - "ts-node": { - "esm": true, - "transpileOnly": true, - "ignoreDiagnostics": [5104] - }, - "compilerOptions": { - "strict": false, - "rootDir": ".", - "baseUrl": ".", - "outDir": "build", - "module": "NodeNext", - "target": "ESNext", - "moduleResolution": "NodeNext", - "declaration": true, - "skipLibCheck": true, - "esModuleInterop": true, - "removeComments": false, - "resolveJsonModule": true, - "experimentalDecorators": true, - "useDefineForClassFields": false, - "verbatimModuleSyntax": true, - "forceConsistentCasingInFileNames": true, - "paths": { - "#bin/*": ["./bin/*.ts"], - "#src/*": ["./src/*.ts"], - "#tests/*": ["./tests/*.ts"], - "#src/types": ["./src/types/index.ts"] - } - }, - "include": ["./**/*"], - "exclude": ["build", "node_modules"] + "extends": "@athenna/tsconfig/tsconfig.lib.json", + "compilerOptions": {} }