@@ -8,7 +8,7 @@ async function findPlugins (dir, options) {
88 const { opts, prefix } = options
99
1010 const pluginTree = {
11- [ prefix || '/' ] : { hooks : [ ] , plugins : [ ] } ,
11+ [ prefix || '/' ] : { hooks : [ ] , plugins : [ ] }
1212 }
1313
1414 await buildTree ( pluginTree , dir , { prefix, opts, depth : 0 , hooks : [ ] } )
@@ -24,48 +24,24 @@ async function buildTree (pluginTree, dir, { prefix, opts, depth, hooks }) {
2424
2525 const dirEntries = await readdir ( dir , { withFileTypes : true } )
2626
27- const currentDirHooks = findCurrentDirHooks ( pluginTree , {
28- dir,
29- dirEntries,
30- hooks,
31- opts,
32- prefix,
33- } )
34-
35- const { indexDirEntry, hasNoDirectory } = processIndexDirEntryIfExists (
36- pluginTree ,
37- { dirEntries, opts, dir, prefix }
38- )
27+ const currentDirHooks = findCurrentDirHooks ( pluginTree , { dir, dirEntries, hooks, opts, prefix } )
28+
29+ const { indexDirEntry, hasNoDirectory } = processIndexDirEntryIfExists ( pluginTree , { dirEntries, opts, dir, prefix } )
3930 if ( hasNoDirectory ) {
4031 return
4132 }
4233
4334 // Contains package.json but no index.js file?
44- const packageDirEntry = dirEntries . find (
45- ( dirEntry ) => dirEntry . name === 'package.json'
46- )
35+ const packageDirEntry = dirEntries . find ( ( dirEntry ) => dirEntry . name === 'package.json' )
4736 if ( packageDirEntry && ! indexDirEntry ) {
48- throw new Error (
49- `@fastify/autoload cannot import plugin at '${ dir } '. To fix this error rename the main entry file to 'index.js' (or .cjs, .mjs, .ts).`
50- )
37+ throw new Error ( `@fastify/autoload cannot import plugin at '${ dir } '. To fix this error rename the main entry file to 'index.js' (or .cjs, .mjs, .ts).` )
5138 }
5239
5340 // Otherwise treat each script file as a plugin
54- await processDirContents ( pluginTree , {
55- dirEntries,
56- opts,
57- indexDirEntry,
58- prefix,
59- dir,
60- depth,
61- currentDirHooks,
62- } )
41+ await processDirContents ( pluginTree , { dirEntries, opts, indexDirEntry, prefix, dir, depth, currentDirHooks } )
6342}
6443
65- function findCurrentDirHooks (
66- pluginTree ,
67- { dir, dirEntries, hooks, opts, prefix }
68- ) {
44+ function findCurrentDirHooks ( pluginTree , { dir, dirEntries, hooks, opts, prefix } ) {
6945 if ( ! opts . autoHooks ) return [ ]
7046
7147 let currentDirHooks = [ ]
@@ -75,9 +51,7 @@ function findCurrentDirHooks (
7551 }
7652
7753 // Contains autohooks file?
78- const autoHooks = dirEntries . find ( ( dirEntry ) =>
79- opts . autoHooksPattern . test ( dirEntry . name )
80- )
54+ const autoHooks = dirEntries . find ( ( dirEntry ) => opts . autoHooksPattern . test ( dirEntry . name ) )
8155 if ( autoHooks ) {
8256 const file = join ( dir , autoHooks . name )
8357 const { type } = getScriptType ( file , opts . packageType )
@@ -96,32 +70,22 @@ function findCurrentDirHooks (
9670 return currentDirHooks
9771}
9872
99- function processIndexDirEntryIfExists (
100- pluginTree ,
101- { opts, dirEntries, dir, prefix }
102- ) {
73+ function processIndexDirEntryIfExists ( pluginTree , { opts, dirEntries, dir, prefix } ) {
10374 // Contains index file?
104- const indexDirEntry = dirEntries . find ( ( dirEntry ) =>
105- opts . indexPattern . test ( dirEntry . name )
106- )
75+ const indexDirEntry = dirEntries . find ( ( dirEntry ) => opts . indexPattern . test ( dirEntry . name ) )
10776 if ( ! indexDirEntry ) return { indexDirEntry }
10877
10978 const file = join ( dir , indexDirEntry . name )
11079 const { language, type } = getScriptType ( file , opts . packageType )
11180 handleTypeScriptSupport ( file , language , true )
11281 accumulatePlugin ( { file, type, opts, pluginTree, prefix } )
11382
114- const hasNoDirectory = dirEntries . every (
115- ( dirEntry ) => ! dirEntry . isDirectory ( )
116- )
83+ const hasNoDirectory = dirEntries . every ( ( dirEntry ) => ! dirEntry . isDirectory ( ) )
11784
11885 return { indexDirEntry, hasNoDirectory }
11986}
12087
121- async function processDirContents (
122- pluginTree ,
123- { dirEntries, opts, indexDirEntry, prefix, dir, depth, currentDirHooks }
124- ) {
88+ async function processDirContents ( pluginTree , { dirEntries, opts, indexDirEntry, prefix, dir, depth, currentDirHooks } ) {
12589 for ( const dirEntry of dirEntries ) {
12690 if ( opts . ignorePattern && RegExp ( opts . ignorePattern ) . test ( dirEntry . name ) ) {
12791 continue
@@ -130,15 +94,7 @@ async function processDirContents (
13094 const atMaxDepth = Number . isFinite ( opts . maxDepth ) && opts . maxDepth <= depth
13195 const file = join ( dir , dirEntry . name )
13296 if ( dirEntry . isDirectory ( ) && ! atMaxDepth ) {
133- await processDirectory ( pluginTree , {
134- prefix,
135- opts,
136- dirEntry,
137- dir,
138- file,
139- depth,
140- currentDirHooks,
141- } )
97+ await processDirectory ( pluginTree , { prefix, opts, dirEntry, dir, file, depth, currentDirHooks } )
14298 } else if ( indexDirEntry ) {
14399 // An index.js file is present in the directory so we ignore the others modules (but not the subdirectories)
144100 } else if ( dirEntry . isFile ( ) && opts . scriptPattern . test ( dirEntry . name ) ) {
@@ -147,11 +103,8 @@ async function processDirContents (
147103 }
148104}
149105
150- async function processDirectory (
151- pluginTree ,
152- { prefix, opts, dirEntry, dir, file, depth, currentDirHooks }
153- ) {
154- let prefixBreadCrumb = prefix ? `${ prefix } /` : '/'
106+ async function processDirectory ( pluginTree , { prefix, opts, dirEntry, dir, file, depth, currentDirHooks } ) {
107+ let prefixBreadCrumb = ( prefix ? `${ prefix } /` : '/' )
155108 if ( opts . dirNameRoutePrefix === true ) {
156109 prefixBreadCrumb += dirEntry . name
157110 } else if ( typeof opts . dirNameRoutePrefix === 'function' ) {
@@ -163,12 +116,7 @@ async function processDirectory (
163116
164117 // Pass hooks forward to next level
165118 const hooks = opts . autoHooks && opts . cascadeHooks ? currentDirHooks : [ ]
166- await buildTree ( pluginTree , file , {
167- opts,
168- prefix : prefixBreadCrumb ,
169- depth : depth + 1 ,
170- hooks,
171- } )
119+ await buildTree ( pluginTree , file , { opts, prefix : prefixBreadCrumb , depth : depth + 1 , hooks } )
172120}
173121
174122function processFile ( pluginTree , { file, opts, dirEntry, prefix } ) {
@@ -202,9 +150,7 @@ function handleTypeScriptSupport (file, language, isHook = false) {
202150 ! runtime . supportTypeScript
203151 ) {
204152 throw new Error (
205- `@fastify/autoload cannot import ${
206- isHook ? 'hooks ' : ''
207- } plugin at '${ file } '. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.`
153+ `@fastify/autoload cannot import ${ isHook ? 'hooks ' : '' } plugin at '${ file } '. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.`
208154 )
209155 }
210156 /* c8 ignore end */
@@ -226,7 +172,7 @@ const typescriptPattern = /\.(?:ts|mts|cts)$/iu
226172function getScriptType ( fname , packageType ) {
227173 return {
228174 language : typescriptPattern . test ( fname ) ? 'typescript' : 'javascript' ,
229- type : determineModuleType ( fname , packageType ) ,
175+ type : determineModuleType ( fname , packageType )
230176 }
231177}
232178
0 commit comments