Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove getPackageType deps, fix for Bun #335

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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