diff --git a/lib/filtersRegistry.js b/lib/filtersRegistry.js index 15b23f0bed..d1d17920e5 100644 --- a/lib/filtersRegistry.js +++ b/lib/filtersRegistry.js @@ -11,10 +11,52 @@ const { isAsyncFunction } = require('./utils'); * @param {String} filtersDir Directory where local filters are located. */ module.exports.registerFilters = async (nunjucks, templateConfig, templateDir, filtersDir) => { + const hasTypescriptFiles = await checkForTypescriptFiles(templateDir, filtersDir); + if(hasTypescriptFiles) { + utils.registerTypescript(true) + } + await registerLocalFilters(nunjucks, templateDir, filtersDir); registerConfigFilters(nunjucks, templateDir, templateConfig); }; +/** + * Check if there are Typescript files in the files directory. + * @param {String} templateDir Directory where template is located. + * @param {String} filtersDir Directory where local filters are located. + */ +async function checkForTypescriptFiles(templateDir, filtersDir) { + const localFilters = path.resolve(templateDir, filtersDir); + + if(!fs.existsSync(localFilters)) { + const walker = xfs.walk(localFilters, { + followLinks: false + }) + } + const hasTypescriptFiles = await new Promise((resolve, reject) => { + let foundTsFile = false; + walker.on('file', async (root, stats, next) => { + try { + const filePath = path.resolve(templateDir, path.resolve(root, stats.name)); + if (!isJsFile(filePath)) { + foundTsFile = true; + wlaker.stop(); + resolve(true); + } + next(); + } catch (error) { + reject(error); + } + }) + + walker.on('end', async () => { + resolve(foundTsFile); + }); + }); + + return hasTypescriptFiles; +} + /** * Registers the local template filters. * @private diff --git a/lib/generator.js b/lib/generator.js index c00c2e0bea..bc4c65e0c6 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -53,7 +53,7 @@ const shouldIgnoreDir = dirPath => dirPath === '.git' || dirPath.startsWith(`.git${path.sep}`); -registerSourceMap(); +// registerSourceMap(); registerTypeScript(); class Generator { @@ -290,6 +290,37 @@ class Generator { return { templatePkgName, templatePkgPath }; } + /** + * Checks if there are any TypeScript files in the template's filters and hooks directories. + * + * @private + * @returns {Promise} A promise that resolves to true if TypeScript files are found, false otherwise. + */ + async checkForTypescriptFiles() { + const filtersDir = path.resolve(this.templateDir, FILTERS_DIRNAME); + const hooksDir = path.resolve(this.templateDir, HOOKS_DIRNAME); + + const hasTypescriptFilters = await this.hasTypescriptFilesInDir(filtersDir); + const hasTypescriptHooks = await this.hasTypescriptFilesInDir(hooksDir); + + return hasTypescriptFilters || hasTypescriptHooks; + } + + /** + * Checks if a given directory contains any TypeScript files. + * + * @private + * @param {string} dirPath - The path to the directory to check. + * @returns {Promise} A promise that resolves to true if TypeScript files are found, false otherwise. + */ + + async hasTypescriptFilesInDir(dirPath) { + if (!await exists(dirPath)) return false; + + const files = await readDir(dirPath); + return files.some(file => !isJsFile(path.join(dirPath, file))); + } + /** * Configures the template workflow based on provided parsing options. * @@ -308,6 +339,12 @@ class Generator { await this.parseInput(this.asyncapi, parseOptions); validateTemplateConfig(this.templateConfig, this.templateParams, this.asyncapi); await this.configureTemplate(); + + const hasTypescriptFiles = await this.checkForTypescriptFiles(); + if(hasTypescriptFiles) { + utils.registerTypeScript(true); + } + if (!isReactTemplate(this.templateConfig)) { await registerFilters(this.nunjucks, this.templateConfig, this.templateDir, FILTERS_DIRNAME); } diff --git a/lib/hooksRegistry.js b/lib/hooksRegistry.js index bde5c919c7..00f85b8f23 100644 --- a/lib/hooksRegistry.js +++ b/lib/hooksRegistry.js @@ -1,6 +1,6 @@ const path = require('path'); const xfs = require('fs.extra'); -const { exists } = require('./utils'); +const { exists, isJsFile } = require('./utils'); /** * Registers all template hooks. @@ -10,6 +10,11 @@ const { exists } = require('./utils'); * @param {String} hooksDir Directory where local hooks are located. */ module.exports.registerHooks = async (hooks, templateConfig, templateDir, hooksDir) => { + const hasTypescriptFiles = await checkForTypescriptFiles(templateDir, hooksDir); + if(hasTypescriptFiles){ + utils.registerTypescript(true) + } + await registerLocalHooks(hooks, templateDir, hooksDir); if (templateConfig && Object.keys(templateConfig).length > 0) await registerConfigHooks(hooks, templateDir, templateConfig); @@ -17,6 +22,46 @@ module.exports.registerHooks = async (hooks, templateConfig, templateDir, hooksD return hooks; }; +/** + * Check if there are TypeScript files in the hooks directory. + * @param {String} templateDir Directory where the template is located. + * @param {String} hooksDir Directory where local hooks are located. + */ + +async function checkForTypescriptFiles(templateDir, hooksDir) { + const localHooks = path.resolve(templateDir, hooksDir); + + if (!fs.existsSync(localHooks)) return false; + + const walker = xfs.walk(localHooks, { + followLinks: false + }); + + const hasTypescriptFiles = await new Promise((resolve, reject) => { + let foundTsFile = false; + + walker.on('file', async (root, stats, next) => { + try { + const filePath = path.resolve(templateDir, path.resolve(root, stats.name)); + if (!isJsFile(filePath)) { + foundTsFile = true; + walker.stop(); + resolve(true); + } + next(); + } catch (e) { + reject(e); + } + }); + + walker.on('end', async () => { + resolve(foundTsFile); + }); + }); + + return hasTypescriptFiles; +} + /** * Loads the local template hooks from default location. * @private diff --git a/lib/utils.js b/lib/utils.js index f7026b1662..f004ce4d2b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -149,14 +149,16 @@ utils.registerSourceMap = () => { * * @private */ -utils.registerTypeScript = () => { +utils.registerTypeScript = (shouldRegister) => { 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 if (process[REGISTER_INSTANCE] || process.env.TS_NODE_DEV) { return; } - register(); + if (shouldRegister){ + register() + } }; /**