Skip to content

Commit eaedfe0

Browse files
committed
revert: style changes
1 parent d7d65a0 commit eaedfe0

File tree

3 files changed

+72
-126
lines changed

3 files changed

+72
-126
lines changed

lib/find-plugins.js

Lines changed: 19 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -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

174122
function 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
226172
function 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

lib/runtime.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let preloadModules
1616
function checkPreloadModules (moduleName) {
1717
/* c8 ignore start */
1818
// nullish needed for non Node.js runtime
19-
preloadModules ??= process._preload_modules ?? []
19+
preloadModules ??= (process._preload_modules ?? [])
2020
/* c8 ignore stop */
2121
return preloadModules.includes(moduleName)
2222
}
@@ -28,80 +28,85 @@ function checkPreloadModulesString (moduleName) {
2828
}
2929

3030
function checkEnvVariable (name, value) {
31-
return value ? process.env[name] === value : process.env[name] !== undefined
31+
return value
32+
? process.env[name] === value
33+
: process.env[name] !== undefined
3234
}
3335

3436
const runtime = {}
3537
// use Object.defineProperties to provide lazy load
3638
Object.defineProperties(runtime, {
3739
tsNode: {
3840
get () {
39-
cache.tsNode ??=
41+
cache.tsNode ??= (
4042
// --require tsnode/register
41-
Symbol.for('ts-node.register.instance') in process ||
43+
(Symbol.for('ts-node.register.instance') in process) ||
4244
// --loader ts-node/esm
4345
checkProcessArgv('ts-node/esm') ||
4446
// ts-node-dev
4547
!!process.env.TS_NODE_DEV
48+
)
4649
return cache.tsNode
47-
},
50+
}
4851
},
4952
babelNode: {
5053
get () {
5154
cache.babelNode ??= checkProcessArgv('babel-node')
5255
return cache.babelNode
53-
},
56+
}
5457
},
5558
vitest: {
5659
get () {
57-
cache.vitest ??=
60+
cache.vitest ??= (
5861
checkEnvVariable('VITEST', 'true') ||
5962
checkEnvVariable('VITEST_WORKER_ID')
63+
)
6064
return cache.vitest
61-
},
65+
}
6266
},
6367
jest: {
6468
get () {
6569
cache.jest ??= checkEnvVariable('JEST_WORKER_ID')
6670
return cache.jest
67-
},
71+
}
6872
},
6973
swc: {
7074
get () {
71-
cache.swc ??=
75+
cache.swc ??= (
7276
checkPreloadModules('@swc/register') ||
7377
checkPreloadModules('@swc-node/register') ||
7478
checkProcessArgv('.bin/swc-node')
79+
)
7580
return cache.swc
76-
},
81+
}
7782
},
7883
tsm: {
7984
get () {
8085
cache.tsm ??= checkPreloadModules('tsm')
8186
return cache.tsm
82-
},
87+
}
8388
},
8489
esbuild: {
8590
get () {
8691
cache.esbuild ??= checkPreloadModules('esbuild-register')
8792
return cache.esbuild
88-
},
93+
}
8994
},
9095
tsx: {
9196
get () {
9297
cache.tsx ??= checkPreloadModulesString('tsx')
9398
return cache.tsx
94-
},
99+
}
95100
},
96101
tsimp: {
97102
get () {
98103
cache.tsimp ??= checkProcessArgv('tsimp/import')
99104
return cache.tsimp
100-
},
105+
}
101106
},
102107
supportTypeScript: {
103108
get () {
104-
cache.supportTypeScript ??=
109+
cache.supportTypeScript ??= (
105110
checkEnvVariable('FASTIFY_AUTOLOAD_TYPESCRIPT') ||
106111
runtime.tsNode ||
107112
runtime.vitest ||
@@ -113,16 +118,19 @@ Object.defineProperties(runtime, {
113118
runtime.esbuild ||
114119
runtime.tsimp ||
115120
runtime.nodeVersion >= 23
116-
121+
)
117122
return cache.supportTypeScript
118-
},
123+
}
119124
},
120125
forceESM: {
121126
get () {
122-
cache.forceESM ??=
123-
checkProcessArgv('ts-node/esm') || runtime.vitest || false
127+
cache.forceESM ??= (
128+
checkProcessArgv('ts-node/esm') ||
129+
runtime.vitest ||
130+
false
131+
)
124132
return cache.forceESM
125-
},
133+
}
126134
},
127135
nodeVersion: {
128136
get () {

0 commit comments

Comments
 (0)