From 44fcc33ca6fb62a4759bb64f4b92bda7ea356bd1 Mon Sep 17 00:00:00 2001 From: Mintu Gogoi <127925465+Gmin2@users.noreply.github.com> Date: Thu, 1 Aug 2024 00:12:31 +0530 Subject: [PATCH] fix: `ts-node` is registered only when it's actually needed (#1165) Co-authored-by: Lukasz Gornicki --- .changeset/ts-node_register.md | 5 +++++ apps/generator/lib/filtersRegistry.js | 9 +++++++-- apps/generator/lib/generator.js | 2 -- apps/generator/lib/hooksRegistry.js | 5 ++++- apps/generator/lib/utils.js | 8 +++++++- 5 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 .changeset/ts-node_register.md diff --git a/.changeset/ts-node_register.md b/.changeset/ts-node_register.md new file mode 100644 index 000000000..6073b699d --- /dev/null +++ b/.changeset/ts-node_register.md @@ -0,0 +1,5 @@ +--- +"@asyncapi/generator": minor +--- + +ts-node is registered only when it's actually needed \ No newline at end of file diff --git a/apps/generator/lib/filtersRegistry.js b/apps/generator/lib/filtersRegistry.js index 4c90aed31..6659960ed 100644 --- a/apps/generator/lib/filtersRegistry.js +++ b/apps/generator/lib/filtersRegistry.js @@ -1,7 +1,7 @@ const path = require('path'); const fs = require('fs'); const xfs = require('fs.extra'); -const { isAsyncFunction } = require('./utils'); +const { isAsyncFunction, registerTypeScript } = require('./utils'); const nunjucksFilters = require('@asyncapi/nunjucks-filters'); /** @@ -38,7 +38,12 @@ function registerLocalFilters(nunjucks, templateDir, filtersDir) { walker.on('file', async (root, stats, next) => { try { - const filePath = path.resolve(templateDir, path.resolve(root, stats.name)); + const filePath = path.resolve( + templateDir, + path.resolve(root, stats.name) + ); + + registerTypeScript(filePath); // If it's a module constructor, inject dependencies to ensure consistent usage in remote templates in other projects or plain directories. delete require.cache[require.resolve(filePath)]; const mod = require(filePath); diff --git a/apps/generator/lib/generator.js b/apps/generator/lib/generator.js index a8fb6c102..5910859d7 100644 --- a/apps/generator/lib/generator.js +++ b/apps/generator/lib/generator.js @@ -28,7 +28,6 @@ const { isReactTemplate, isJsFile, registerSourceMap, - registerTypeScript, getTemplateDetails, convertCollectionToObject, } = require('./utils'); @@ -59,7 +58,6 @@ const shouldIgnoreDir = dirPath => || dirPath.startsWith(`.git${path.sep}`); registerSourceMap(); -registerTypeScript(); class Generator { /** diff --git a/apps/generator/lib/hooksRegistry.js b/apps/generator/lib/hooksRegistry.js index bde5c919c..2c21389fb 100644 --- a/apps/generator/lib/hooksRegistry.js +++ b/apps/generator/lib/hooksRegistry.js @@ -1,6 +1,6 @@ const path = require('path'); const xfs = require('fs.extra'); -const { exists } = require('./utils'); +const { exists, registerTypeScript } = require('./utils'); /** * Registers all template hooks. @@ -37,6 +37,9 @@ async function registerLocalHooks(hooks, templateDir, hooksDir) { walker.on('file', async (root, stats, next) => { try { const filePath = path.resolve(templateDir, path.resolve(root, stats.name)); + + registerTypeScript(filePath); + delete require.cache[require.resolve(filePath)]; const mod = require(filePath); diff --git a/apps/generator/lib/utils.js b/apps/generator/lib/utils.js index f7026b166..27e26de0f 100644 --- a/apps/generator/lib/utils.js +++ b/apps/generator/lib/utils.js @@ -149,7 +149,13 @@ utils.registerSourceMap = () => { * * @private */ -utils.registerTypeScript = () => { +utils.registerTypeScript = (filePath) => { + const isTypescriptFile = filePath.endsWith('.ts'); + + if (!isTypescriptFile) { + return; + } + const { REGISTER_INSTANCE, register } = require('ts-node'); // if the ts-node has already been registered before, do not register it again. // Check the env. TS_NODE_ENV if ts-node started via ts-node-dev package