Skip to content

Commit

Permalink
chore: remove getPackageType deps, fix for Bun (#335)
Browse files Browse the repository at this point in the history
* chore: remove `getPackageType` deps, fix for Bun

* chore: support Node 14-, fix Bun

* chore: destruct req, use readFile for package.json
  • Loading branch information
ObscuritySRL authored Oct 19, 2023
1 parent e175642 commit 620cc91
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
38 changes: 25 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict'

const path = require('node:path')
const url = require('node:url')
const { readdir } = require('node:fs').promises
const pkgUp = require('pkg-up')
const { promises: { readdir, readFile } } = require('node:fs')
const { join, relative, sep } = require('node:path')
const { pathToFileURL } = require('node:url')

const isFastifyAutoloadTypescriptOverride = !!process.env.FASTIFY_AUTOLOAD_TYPESCRIPT
const isTsNode = (Symbol.for('ts-node.register.instance') in process) || !!process.env.TS_NODE_DEV
Expand Down Expand Up @@ -116,9 +115,22 @@ const fastifyAutoload = async function autoload (fastify, options) {
}

async function getPackageType (cwd) {
const nearestPackage = await pkgUp({ cwd })
if (nearestPackage) {
return require(nearestPackage).type
const directories = cwd.split(sep)

// required for paths that begin with the sep, such as linux root
directories[0] = directories[0] !== '' ? directories[0] : sep

while (directories.length > 0) {
const filePath = join(...directories, 'package.json')

const fileContents = await readFile(filePath, 'utf-8')
.catch(() => null)

if (fileContents) {
return JSON.parse(fileContents).type
}

directories.pop()
}
}

Expand Down Expand Up @@ -149,7 +161,7 @@ async function findPlugins (dir, options, hookedAccumulator = {}, prefix, depth
// Contains autohooks file?
const autoHooks = list.find((dirent) => autoHooksPattern.test(dirent.name))
if (autoHooks) {
const autoHooksFile = path.join(dir, autoHooks.name)
const autoHooksFile = join(dir, autoHooks.name)
const autoHooksType = getScriptType(autoHooksFile, options.packageType)

// Overwrite current hooks?
Expand All @@ -167,7 +179,7 @@ async function findPlugins (dir, options, hookedAccumulator = {}, prefix, depth
// Contains index file?
const indexDirent = list.find((dirent) => indexPattern.test(dirent.name))
if (indexDirent) {
const file = path.join(dir, indexDirent.name)
const file = join(dir, indexDirent.name)
const type = getScriptType(file, options.packageType)
if (type === 'typescript' && !typescriptSupport) {
throw new Error(`@fastify/autoload cannot import hooks plugin at '${file}'. To fix this error compile TypeScript to JavaScript or use 'ts-node' to run your app.`)
Expand Down Expand Up @@ -195,7 +207,7 @@ async function findPlugins (dir, options, hookedAccumulator = {}, prefix, depth
}

const atMaxDepth = Number.isFinite(maxDepth) && maxDepth <= depth
const file = path.join(dir, dirent.name)
const file = join(dir, dirent.name)
if (dirent.isDirectory() && !atMaxDepth) {
let prefixBreadCrumb = (prefix ? `${prefix}/` : '/')
if (dirNameRoutePrefix === true) {
Expand Down Expand Up @@ -238,7 +250,7 @@ async function findPlugins (dir, options, hookedAccumulator = {}, prefix, depth

function accumulatePlugin ({ file, type }) {
// Replace backward slash to forward slash for consistent behavior between windows and posix.
const filePath = '/' + path.relative(options.dir, file).replace(/\\/gu, '/')
const filePath = '/' + relative(options.dir, file).replace(/\\/gu, '/')

if (matchFilter && !filterPath(filePath, matchFilter)) {
return
Expand All @@ -256,7 +268,7 @@ async function loadPlugin ({ file, type, directoryPrefix, options, log }) {
const { options: overrideConfig, forceESM, encapsulate } = options
let content
if (forceESM || type === 'module' || forceESMEnvironment) {
content = await import(url.pathToFileURL(file).href)
content = await import(pathToFileURL(file).href)
} else {
content = require(file)
}
Expand Down Expand Up @@ -400,7 +412,7 @@ async function loadHook (hook, options) {
if (!hook) return null
let hookContent
if (options.forceESM || hook.type === 'module' || forceESMEnvironment) {
hookContent = await import(url.pathToFileURL(hook.file).href)
hookContent = await import(pathToFileURL(hook.file).href)
} else {
hookContent = require(hook.file)
}
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@
"vite": "^4.0.0",
"vitest": "^0.34.1"
},
"dependencies": {
"pkg-up": "^3.1.0"
},
"dependencies": {},
"standard": {
"ignore": [
"test/*/*-error/lib/a.js"
Expand Down

0 comments on commit 620cc91

Please sign in to comment.