Skip to content

Commit

Permalink
fix: ts-node is registered only when it's actually needed (#1165)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukasz Gornicki <[email protected]>
  • Loading branch information
Gmin2 and derberg authored Jul 31, 2024
1 parent b093dbc commit 44fcc33
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/ts-node_register.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@asyncapi/generator": minor
---

ts-node is registered only when it's actually needed
9 changes: 7 additions & 2 deletions apps/generator/lib/filtersRegistry.js
Original file line number Diff line number Diff line change
@@ -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');

/**
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions apps/generator/lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const {
isReactTemplate,
isJsFile,
registerSourceMap,
registerTypeScript,
getTemplateDetails,
convertCollectionToObject,
} = require('./utils');
Expand Down Expand Up @@ -59,7 +58,6 @@ const shouldIgnoreDir = dirPath =>
|| dirPath.startsWith(`.git${path.sep}`);

registerSourceMap();
registerTypeScript();

class Generator {
/**
Expand Down
5 changes: 4 additions & 1 deletion apps/generator/lib/hooksRegistry.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 7 additions & 1 deletion apps/generator/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 44fcc33

Please sign in to comment.