Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ts-node is registered only when it's actually needed #1165

Merged
merged 13 commits into from
Jul 31, 2024
Merged
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
Loading