Skip to content

Commit

Permalink
ts-node is registered only when it's actually needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Gmin2 committed Apr 1, 2024
1 parent 0b7dd90 commit 2924a25
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 4 deletions.
42 changes: 42 additions & 0 deletions lib/filtersRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 38 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const shouldIgnoreDir = dirPath =>
dirPath === '.git'
|| dirPath.startsWith(`.git${path.sep}`);

registerSourceMap();
// registerSourceMap();
registerTypeScript();

class Generator {
Expand Down Expand Up @@ -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<boolean>} 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<boolean>} 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.
*
Expand All @@ -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);
}
Expand Down
47 changes: 46 additions & 1 deletion 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, isJsFile } = require('./utils');

/**
* Registers all template hooks.
Expand All @@ -10,13 +10,58 @@ 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);

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
Expand Down
6 changes: 4 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
};

/**
Expand Down

0 comments on commit 2924a25

Please sign in to comment.