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 | 1x 1x 1x 1x 1x 1x 1x | 'use strict'; import * as fs from 'fs'; import * as path from 'path'; import * as _ from 'lodash'; import {FastifyInstance} from 'fastify'; import {IncomingMessage, Server, ServerResponse} from 'http'; import {Http2Server, Http2ServerRequest, Http2ServerResponse} from 'http2'; import addRoute from './add.route'; import cleanRelativePath from './clean.relative.path'; let routesRootFolder: string; // TODO: convert to a fastify plugin! See: https://github.com/fastify/fastify-routes const addFastifyRoutes = ( fastify: FastifyInstance<Server, IncomingMessage, ServerResponse> | FastifyInstance<Http2Server, Http2ServerRequest, Http2ServerResponse>, folder: string, verbose: boolean = false ): void => { Iif (!routesRootFolder) routesRootFolder = folder; const fileNames: string[] = fs.readdirSync(folder); const files: string[] = _.filter(fileNames, (name) => !fs.lstatSync(path.join(folder, name)).isDirectory()); for (const file of files) { 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 routes = require(absoluteFilePath); if (_.isArray(routes)) { for (const route of routes) { addRoute(fastify, route, relativeFilePath, verbose); } } else { addRoute(fastify, routes, relativeFilePath, verbose); } } const folders: string[] = _.filter(fileNames, (name: string) => fs.lstatSync(path.join(folder, name)).isDirectory()); for (const sub of folders) { addFastifyRoutes(fastify, path.join(folder, sub)); } }; export default addFastifyRoutes; |