All files / src/functions add.fastify.routes.ts

19.56% Statements 9/46
0% Branches 0/14
0% Functions 0/3
25.71% Lines 9/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63    1x 1x 1x   1x         1x 1x       1x   1x                                                                                     1x  
'use strict';
 
import * as fs from 'fs';
import * as path from 'path';
import {hrtime} from 'node:process';
 
import * as _ from 'lodash';
 
import {IncomingMessage, Server, ServerResponse} from 'http';
import {FastifyBaseLogger, FastifyInstance, FastifyTypeProviderDefault} from 'fastify';
 
import addRoute from './add.route';
import cleanRelativePath from './clean.relative.path';
 
let routesRootFolder: string;
 
const NS_PER_SEC: number = 1e9;
 
const addFastifyRoutes = (
	fastify: FastifyInstance<Server<typeof IncomingMessage, typeof ServerResponse>, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>,
	folder: string,
	verbose: boolean = false
): void => {
	Iif (verbose) console.log('\n[@owservable/fastify-auto-routes] -> addFastifyRoutes:', folder);
	Iif (!routesRootFolder) routesRootFolder = folder;
 
	const fileNames: string[] = fs.readdirSync(folder);
	const files: string[] = _.filter(fileNames, (name) => !fs.lstatSync(path.join(folder, name)).isDirectory());
	Iif (verbose) console.log('[@owservable/fastify-auto-routes] -> addFastifyRoutes:', folder, `${files.length} files`);
 
	for (const file of files) {
		Iif (verbose) console.log('[@owservable/fastify-auto-routes] -> addFastifyRoutes: processing...', `${folder}/${file}`);
		const ext: string = path.extname(file);
		Iif (ext !== '.ts' && ext !== '.js') return;
 
		const absoluteFilePath: string = path.join(folder, file);
		const relativeFilePath: string = cleanRelativePath(routesRootFolder, absoluteFilePath, ext);
 
		const start: number = Number(hrtime.bigint());
		Iif (verbose) console.log('[@owservable/fastify-auto-routes] -> addFastifyRoutes: loading file...', absoluteFilePath);
		const routes = require(absoluteFilePath);
 
		const time: number = Number(Number(hrtime.bigint()) - start) / NS_PER_SEC;
		console.log('[@owservable/fastify-auto-routes] -> addFastifyRoutes: loaded file', `[${time.toFixed(3)}s] ${folder}/${file}`);
		if (_.isArray(routes)) {
			Iif (verbose) console.log('[@owservable/fastify-auto-routes] -> addFastifyRoutes:', absoluteFilePath, `${routes.length} routes`);
			for (const route of routes) {
				addRoute(fastify, route, relativeFilePath, verbose);
			}
		} else {
			Iif (verbose) console.log('[@owservable/fastify-auto-routes] -> addFastifyRoutes:', absoluteFilePath, '1 route');
			addRoute(fastify, routes, relativeFilePath, verbose);
		}
	}
 
	const folders: string[] = _.filter(fileNames, (name: string) => fs.lstatSync(path.join(folder, name)).isDirectory());
	Iif (verbose) console.log('[@owservable/fastify-auto-routes] -> addFastifyRoutes:', folder, 'subfolders:', `${folders.length} subfolders`);
	for (const sub of folders) {
		addFastifyRoutes(fastify, path.join(folder, sub), verbose);
	}
};
export default addFastifyRoutes;